mirror of
https://github.com/zitadel/zitadel.git
synced 2025-01-07 15:57:41 +00:00
change context keys and fix tests
This commit is contained in:
parent
96b1817d62
commit
40e4f69ca3
@ -270,7 +270,7 @@ func Test_GetPermissionCtxIDs(t *testing.T) {
|
|||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
result := GetPermissionCtxIDs(tt.args.perms)
|
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)
|
t.Errorf("got wrong result, expecting: %v, actual: %v ", tt.result, result)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -6,8 +6,12 @@ import (
|
|||||||
"github.com/caos/logging"
|
"github.com/caos/logging"
|
||||||
)
|
)
|
||||||
|
|
||||||
type CtxKeyPermissions struct{}
|
type key int
|
||||||
type CtxKeyData struct{}
|
|
||||||
|
var (
|
||||||
|
permissionsKey key
|
||||||
|
dataKey key
|
||||||
|
)
|
||||||
|
|
||||||
type CtxData struct {
|
type CtxData struct {
|
||||||
UserID string
|
UserID string
|
||||||
@ -42,15 +46,15 @@ func VerifyTokenAndWriteCtxData(ctx context.Context, token, orgID string, t Toke
|
|||||||
projectID, err := t.GetProjectIDByClientID(ctx, clientID)
|
projectID, err := t.GetProjectIDByClientID(ctx, clientID)
|
||||||
logging.LogWithFields("AUTH-GfAoV", "clientID", clientID).OnError(err).Warn("could not read projectid by 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 {
|
func GetCtxData(ctx context.Context) CtxData {
|
||||||
ctxData, _ := ctx.Value(CtxKeyData{}).(CtxData)
|
ctxData, _ := ctx.Value(dataKey).(CtxData)
|
||||||
return ctxData
|
return ctxData
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetPermissionsFromCtx(ctx context.Context) []string {
|
func GetPermissionsFromCtx(ctx context.Context) []string {
|
||||||
ctxPermission, _ := ctx.Value(CtxKeyPermissions{}).([]string)
|
ctxPermission, _ := ctx.Value(permissionsKey).([]string)
|
||||||
return ctxPermission
|
return ctxPermission
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@ func getUserMethodPermissions(ctx context.Context, t TokenVerifier, requiredPerm
|
|||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
permissions := mapGrantsToPermissions(requiredPerm, grants, authConfig)
|
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 {
|
func mapGrantsToPermissions(requiredPerm string, grants []*Grant, authConfig *Config) []string {
|
||||||
|
@ -8,7 +8,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func getTestCtx(userID, orgID string) context.Context {
|
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 {
|
type testVerifier struct {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user