Skip to content
This repository was archived by the owner on Aug 28, 2025. It is now read-only.

Commit 1630e65

Browse files
committed
feat: add support for group claims in token
1 parent 1b809b8 commit 1630e65

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

cmd/start.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ var startCmd = &cobra.Command{
7171
Pretty: true,
7272
Playground: true,
7373
},
74-
UserClaim: "mail",
74+
UserClaim: "mail",
75+
GroupsClaim: "groups",
7576
}))
7677

7778
return http.ListenAndServe(":3000", nil)

gateway/gateway.go

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -517,11 +517,15 @@ func New(ctx context.Context, conf Config) (graphql.Schema, error) {
517517
})
518518
}
519519

520-
type userContextKey struct{}
520+
type (
521+
userContextKey struct{}
522+
groupsContextKey struct{}
523+
)
521524

522525
type HandlerConfig struct {
523526
*handler.Config
524-
UserClaim string
527+
UserClaim string
528+
GroupsClaim string
525529
}
526530

527531
func Handler(conf HandlerConfig) http.Handler {
@@ -551,6 +555,21 @@ func Handler(conf HandlerConfig) http.Handler {
551555

552556
ctx := AddUserToContext(r.Context(), userIdentifier)
553557

558+
if conf.GroupsClaim != "" {
559+
groups, ok := claims[conf.GroupsClaim].([]any)
560+
561+
var parsedGroups []string
562+
for _, group := range groups {
563+
if group, ok := group.(string); ok {
564+
parsedGroups = append(parsedGroups, group)
565+
}
566+
}
567+
568+
if ok && len(groups) >= 0 {
569+
ctx = AddGroupsToContext(ctx, parsedGroups)
570+
}
571+
}
572+
554573
if r.Header.Get("Accept") == "text/event-stream" {
555574
opts := handler.NewRequestOptions(r)
556575

@@ -588,11 +607,20 @@ func AddUserToContext(ctx context.Context, user string) context.Context {
588607
return context.WithValue(ctx, userContextKey{}, user)
589608
}
590609

610+
func AddGroupsToContext(ctx context.Context, groups []string) context.Context {
611+
return context.WithValue(ctx, groupsContextKey{}, groups)
612+
}
613+
591614
func GetUserFromContext(ctx context.Context) (string, bool) {
592615
user, ok := ctx.Value(userContextKey{}).(string)
593616
return user, ok
594617
}
595618

619+
func GetGroupsFromContext(ctx context.Context) ([]string, bool) {
620+
groups, ok := ctx.Value(groupsContextKey{}).([]string)
621+
return groups, ok
622+
}
623+
596624
type impersonation struct {
597625
delegate http.RoundTripper
598626
}
@@ -618,6 +646,13 @@ func (i *impersonation) RoundTrip(req *http.Request) (*http.Response, error) {
618646
req = utilnet.CloneRequest(req)
619647
req.Header.Set(transport.ImpersonateUserHeader, user)
620648

649+
groups, ok := GetGroupsFromContext(req.Context())
650+
if ok && len(groups) > 0 {
651+
for _, group := range groups {
652+
req.Header.Set(transport.ImpersonateGroupHeader, group)
653+
}
654+
}
655+
621656
return i.delegate.RoundTrip(req)
622657
}
623658

gateway/resolver.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ func isAuthorized(ctx context.Context, c client.Client, resourceAttributes authz
406406
ctx, span := otel.Tracer("").Start(ctx, "AuthorizationCheck")
407407
defer span.End()
408408

409-
user, ok := ctx.Value(userContextKey{}).(string)
409+
user, ok := GetUserFromContext(ctx)
410410
if !ok || user == "" {
411411
return errors.New("no user found in context")
412412
}
@@ -418,6 +418,11 @@ func isAuthorized(ctx context.Context, c client.Client, resourceAttributes authz
418418
},
419419
}
420420

421+
groups, ok := GetGroupsFromContext(ctx)
422+
if ok {
423+
sar.Spec.Groups = groups
424+
}
425+
421426
err := c.Create(ctx, &sar)
422427
if err != nil {
423428
return err

0 commit comments

Comments
 (0)