Skip to content

Commit

Permalink
Add ProjectStore support (#65)
Browse files Browse the repository at this point in the history
* Add ProjectStore support

* version bump
  • Loading branch information
klaidliadon authored Nov 6, 2024
1 parent 41f221e commit 79529fe
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 16 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.23.2
// replace github.com/0xsequence/authcontrol => ../authcontrol

require (
github.com/0xsequence/authcontrol v0.1.0
github.com/0xsequence/authcontrol v0.2.0
github.com/0xsequence/go-sequence v0.43.0
github.com/alicebob/miniredis/v2 v2.33.0
github.com/go-chi/chi/v5 v5.1.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/0xsequence/authcontrol v0.1.0 h1:FB8vsYuhn0Z5tXhl9n/S17GOY+5pJ+iWnCYEgwR/nHY=
github.com/0xsequence/authcontrol v0.1.0/go.mod h1:wicKJcmJYJU6jn07JBMqwxcAzyFm2BtfAzWpnL/40g8=
github.com/0xsequence/authcontrol v0.2.0 h1:7enDdLZSP3ngtMD7P6R6/yN1avgUFeHia0J8J8RKV5A=
github.com/0xsequence/authcontrol v0.2.0/go.mod h1:wicKJcmJYJU6jn07JBMqwxcAzyFm2BtfAzWpnL/40g8=
github.com/0xsequence/ethkit v1.28.0 h1:11p4UXXvYnixQk01+qmAcOF71N9DlSeMcEMbaCPtjaY=
github.com/0xsequence/ethkit v1.28.0/go.mod h1:rv0FAIyEyN0hhwGefbduAz4ujmyjyJXhCd6a0/yF3tk=
github.com/0xsequence/go-sequence v0.43.0 h1:PErMuTg4PeaamJutEJ6tAjrFBA8z0t6lvT9LOVC5RMs=
Expand Down
4 changes: 2 additions & 2 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ cloud.google.com/go/compute/metadata v0.5.1/go.mod h1:C66sj2AluDcIqakBq/M8lw8/yb
cloud.google.com/go/iam v1.2.1/go.mod h1:3VUIJDPpwT6p/amXRC5GY8fCCh70lxPygguVtI0Z4/g=
cloud.google.com/go/monitoring v1.21.0/go.mod h1:tuJ+KNDdJbetSsbSGTqnaBvbauS5kr3Q/koy3Up6r+4=
cloud.google.com/go/storage v1.44.0/go.mod h1:wpPblkIuMP5jCB/E48Pz9zIo2S/zD8g+ITmxKkPCITE=
github.com/0xsequence/authcontrol v0.1.0 h1:FB8vsYuhn0Z5tXhl9n/S17GOY+5pJ+iWnCYEgwR/nHY=
github.com/0xsequence/authcontrol v0.1.0/go.mod h1:wicKJcmJYJU6jn07JBMqwxcAzyFm2BtfAzWpnL/40g8=
github.com/0xsequence/authcontrol v0.2.0 h1:7enDdLZSP3ngtMD7P6R6/yN1avgUFeHia0J8J8RKV5A=
github.com/0xsequence/authcontrol v0.2.0/go.mod h1:wicKJcmJYJU6jn07JBMqwxcAzyFm2BtfAzWpnL/40g8=
github.com/0xsequence/go-ethauth v0.13.0/go.mod h1:f3kx39S9F+W+qvZEB6bkKKbpUstmyB7goUntO3wvlhg=
github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.24.1/go.mod h1:itPGVDKf9cC/ov4MdvJ2QZ0khw4bfoo9jzwTJlaxy2k=
Expand Down
22 changes: 13 additions & 9 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestMiddlewareUseAccessKey(t *testing.T) {
}
}

authOptions := authcontrol.Options[any]{
authOptions := authcontrol.Options{
JWTSecret: Secret,
}
quotaOptions := middleware.Options{}
Expand Down Expand Up @@ -380,7 +380,7 @@ func TestJWT(t *testing.T) {
logger := logger.NewLogger(logger.LogLevel_INFO)
client := quotacontrol.NewClient(logger, Service, cfg, nil)

authOptions := authcontrol.Options[any]{
authOptions := authcontrol.Options{
JWTSecret: Secret,
}
quotaOptions := middleware.Options{
Expand Down Expand Up @@ -461,7 +461,7 @@ func TestJWTAccess(t *testing.T) {

limitCounter := quotacontrol.NewLimitCounter(cfg.Redis, logger)

authOptions := authcontrol.Options[any]{
authOptions := authcontrol.Options{
JWTSecret: Secret,
}
quotaOptions := middleware.Options{}
Expand Down Expand Up @@ -563,9 +563,10 @@ func TestSession(t *testing.T) {
logger := logger.NewLogger(logger.LogLevel_INFO)
client := quotacontrol.NewClient(logger, Service, cfg, nil)

authOptions := authcontrol.Options[struct{}]{
JWTSecret: Secret,
UserStore: server.Store,
authOptions := authcontrol.Options{
JWTSecret: Secret,
UserStore: server.Store,
ProjectStore: server.Store,
}
quotaOptions := middleware.Options{}

Expand All @@ -582,6 +583,7 @@ func TestSession(t *testing.T) {
ctx := context.Background()
limit := proto.Limit{RateLimit: 100, FreeWarn: 5, FreeMax: 5, OverWarn: 7, OverMax: 10}
server.Store.AddUser(ctx, UserAddress, false)
server.Store.AddProject(ctx, ProjectID)
server.Store.SetAccessLimit(ctx, ProjectID, &limit)
server.Store.SetUserPermission(ctx, ProjectID, WalletAddress, proto.UserPermission_READ, proto.ResourceAccess{ProjectID: ProjectID})
server.Store.InsertAccessKey(ctx, &proto.AccessKey{Active: true, AccessKey: AccessKey, ProjectID: ProjectID})
Expand Down Expand Up @@ -688,9 +690,10 @@ func TestSessionDisabled(t *testing.T) {
logger := logger.NewLogger(logger.LogLevel_INFO)
client := quotacontrol.NewClient(logger, Service, cfg, nil)

authOptions := authcontrol.Options[struct{}]{
JWTSecret: Secret,
UserStore: server.Store,
authOptions := authcontrol.Options{
JWTSecret: Secret,
UserStore: server.Store,
ProjectStore: server.Store,
}
quotaOptions := middleware.Options{}

Expand All @@ -707,6 +710,7 @@ func TestSessionDisabled(t *testing.T) {
ctx := context.Background()
limit := proto.Limit{RateLimit: 100, FreeWarn: 5, FreeMax: 5, OverWarn: 7, OverMax: 10}
server.Store.AddUser(ctx, UserAddress, false)
server.Store.AddProject(ctx, ProjectID)
server.Store.SetAccessLimit(ctx, ProjectID, &limit)
server.Store.SetUserPermission(ctx, ProjectID, WalletAddress, proto.UserPermission_READ, proto.ResourceAccess{ProjectID: ProjectID})
server.Store.InsertAccessKey(ctx, &proto.AccessKey{Active: true, AccessKey: AccessKey, ProjectID: ProjectID})
Expand Down
23 changes: 21 additions & 2 deletions test/mem.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func NewMemoryStore() *MemoryStore {
accessKeys: map[string]proto.AccessKey{},
usage: usage.NewRecord(),
users: map[string]bool{},
projects: map[uint64]struct{}{},
permissions: map[uint64]map[string]userPermission{},
}
}
Expand All @@ -34,6 +35,7 @@ type MemoryStore struct {
accessKeys map[string]proto.AccessKey
usage usage.Record
users map[string]bool
projects map[uint64]struct{}
permissions map[uint64]map[string]userPermission
}

Expand Down Expand Up @@ -175,14 +177,31 @@ func (m *MemoryStore) AddUser(ctx context.Context, userID string, admin bool) er
return nil
}

func (m *MemoryStore) GetUser(ctx context.Context, userID string) (*struct{}, bool, error) {
func (m *MemoryStore) GetUser(ctx context.Context, userID string) (any, bool, error) {
m.Lock()
v, ok := m.users[userID]
m.Unlock()
if !ok {
return nil, false, nil
}
return new(struct{}), v, nil
return struct{}{}, v, nil
}

func (m *MemoryStore) AddProject(ctx context.Context, projectID uint64) error {
m.Lock()
m.projects[projectID] = struct{}{}
m.Unlock()
return nil
}

func (m *MemoryStore) GetProject(ctx context.Context, projectID uint64) (any, error) {
m.Lock()
_, ok := m.projects[projectID]
m.Unlock()
if !ok {
return nil, nil
}
return struct{}{}, nil
}

func (m *MemoryStore) GetUserPermission(ctx context.Context, projectID uint64, userID string) (proto.UserPermission, *proto.ResourceAccess, error) {
Expand Down

0 comments on commit 79529fe

Please sign in to comment.