diff --git a/internal/api/auth/authorization_test.go b/internal/api/auth/authorization_test.go index ac1193385e..d140758321 100644 --- a/internal/api/auth/authorization_test.go +++ b/internal/api/auth/authorization_test.go @@ -270,7 +270,7 @@ func Test_GetPermissionCtxIDs(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { result := GetPermissionCtxIDs(tt.args.perms) - if !EqualStringArray(result, tt.result) { + if !equalStringArray(result, tt.result) { t.Errorf("got wrong result, expecting: %v, actual: %v ", tt.result, result) } }) diff --git a/internal/api/auth/context.go b/internal/api/auth/context.go index 78ef0bc252..e0c87929e5 100644 --- a/internal/api/auth/context.go +++ b/internal/api/auth/context.go @@ -6,8 +6,12 @@ import ( "github.com/caos/logging" ) -type CtxKeyPermissions struct{} -type CtxKeyData struct{} +type key int + +var ( + permissionsKey key + dataKey key +) type CtxData struct { UserID string @@ -42,15 +46,15 @@ func VerifyTokenAndWriteCtxData(ctx context.Context, token, orgID string, t Toke projectID, err := t.GetProjectIDByClientID(ctx, clientID) logging.LogWithFields("AUTH-GfAoV", "clientID", clientID).OnError(err).Warn("could not read projectid by clientid") - return context.WithValue(ctx, CtxKeyData{}, CtxData{UserID: userID, OrgID: orgID, ProjectID: projectID, AgentID: agentID}), nil + return context.WithValue(ctx, dataKey, CtxData{UserID: userID, OrgID: orgID, ProjectID: projectID, AgentID: agentID}), nil } func GetCtxData(ctx context.Context) CtxData { - ctxData, _ := ctx.Value(CtxKeyData{}).(CtxData) + ctxData, _ := ctx.Value(dataKey).(CtxData) return ctxData } func GetPermissionsFromCtx(ctx context.Context) []string { - ctxPermission, _ := ctx.Value(CtxKeyPermissions{}).([]string) + ctxPermission, _ := ctx.Value(permissionsKey).([]string) return ctxPermission } diff --git a/internal/api/auth/permissions.go b/internal/api/auth/permissions.go index d7ca516a9d..04c6713915 100644 --- a/internal/api/auth/permissions.go +++ b/internal/api/auth/permissions.go @@ -16,7 +16,7 @@ func getUserMethodPermissions(ctx context.Context, t TokenVerifier, requiredPerm return nil, nil, err } permissions := mapGrantsToPermissions(requiredPerm, grants, authConfig) - return context.WithValue(ctx, CtxKeyPermissions{}, permissions), permissions, nil + return context.WithValue(ctx, permissionsKey, permissions), permissions, nil } func mapGrantsToPermissions(requiredPerm string, grants []*Grant, authConfig *Config) []string { diff --git a/internal/api/auth/permissions_test.go b/internal/api/auth/permissions_test.go index d2afc36dfc..ba67f0d25f 100644 --- a/internal/api/auth/permissions_test.go +++ b/internal/api/auth/permissions_test.go @@ -8,7 +8,7 @@ import ( ) func getTestCtx(userID, orgID string) context.Context { - return context.WithValue(context.Background(), CtxKeyData{}, CtxData{UserID: userID, OrgID: orgID}) + return context.WithValue(context.Background(), dataKey, CtxData{UserID: userID, OrgID: orgID}) } type testVerifier struct {