diff --git a/README.md b/README.md index c949783..1680b8c 100644 --- a/README.md +++ b/README.md @@ -975,7 +975,7 @@ byte-identically to before multi-group support existed. | `issuer` | `string` | — | Required. The IdP issuer URL. Must be `https`, or `http` on a loopback host only. | | `audiences` | `[]string` | — | Required, at least one. A JWT's `aud` must name one of them. | | `acceptAzp` | `bool` | `false` | Also accept a JWT whose `azp` names one of `audiences` when `aud` does not. A token the client obtained for another service then passes too. | -| `groupClaim` | `string` | — | Required. The claim that holds the person's groups or roles, as a dotted path, for example `realm_access.roles`. The gateway first tries the whole value as one top-level claim name, so a namespaced claim such as `https://example.com/groups` works. A string or an array of strings. | +| `groupClaim` | `string` | — | Required. The claim that holds the person's groups or roles, as a dotted path, for example `realm_access.roles`. A value that contains `://`, such as the namespaced claim `https://example.com/groups`, is one top-level claim name and is not split on dots. A string or an array of strings. | | `groupMap` | `map[string]string` | — | Required, at least one entry. Claim value to gateway group. Every target must be a configured group. | | `identityClaim` | `string` | `email` | The claim that names the person, for example `sub`. Use a claim the user cannot change. The value is case-folded only for `email`. | | `requireEmailVerified` | `*bool` | `true` when `identityClaim` is `email`, else `false` | Require `email_verified` to be true. | diff --git a/oidc_auth.go b/oidc_auth.go index 7cfc18f..d5245e1 100644 --- a/oidc_auth.go +++ b/oidc_auth.go @@ -407,12 +407,9 @@ func claimString(claims map[string]any, path []string) (string, bool) { return s, ok } -// groupValues reads the group claim: the whole groupClaim as one top-level -// key when present, else the dotted path. +// groupValues reads the group claim at its parsed path. A namespaced +// groupClaim is one top-level key (parseClaimPath). func (s *oidcSettings) groupValues(claims map[string]any) []string { - if _, ok := claims[s.groupClaimKey]; s.groupClaimKey != "" && ok { - return claimStrings(claims, []string{s.groupClaimKey}) - } return claimStrings(claims, s.groupClaim) } diff --git a/oidc_auth_test.go b/oidc_auth_test.go index ce47102..41bbac5 100644 --- a/oidc_auth_test.go +++ b/oidc_auth_test.go @@ -268,7 +268,7 @@ func TestAuthenticate_LRUBound(t *testing.T) { } } -func TestClaims_WholeKeyBeforeDottedPath(t *testing.T) { +func TestClaims_NamespacedKeyAndDottedPath(t *testing.T) { const ns = "https://example.com/groups" tests := []struct { mutate func(c map[string]any) @@ -279,10 +279,10 @@ func TestClaims_WholeKeyBeforeDottedPath(t *testing.T) { delete(c, "groups") c[ns] = []string{"llm-users"} }}, - {name: "whole key wins over nested walk", key: "a.b", mutate: func(c map[string]any) { + {name: "flat key does not shadow the nested path", key: "realm_access.roles", mutate: func(c map[string]any) { delete(c, "groups") - c["a.b"] = []string{"llm-users"} - c["a"] = map[string]any{"b": []string{"other"}} + c["realm_access.roles"] = []string{"unmapped"} + c["realm_access"] = map[string]any{"roles": []string{"llm-users"}} }}, {name: "absent key walks the dotted path", key: "realm_access.roles", mutate: func(c map[string]any) { delete(c, "groups") @@ -296,7 +296,7 @@ func TestClaims_WholeKeyBeforeDottedPath(t *testing.T) { if err != nil { t.Fatal(err) } - s.groupClaim, s.groupClaimKey = path, tc.key + s.groupClaim = path }) entry, st, reason := fx.run(t, fx.token(t, tc.mutate), nil) if st != authOK { diff --git a/oidc_config.go b/oidc_config.go index 9a5f825..ca7bb90 100644 --- a/oidc_config.go +++ b/oidc_config.go @@ -58,13 +58,10 @@ const ( // oidcSettings is OIDCConfig validated and defaulted. type oidcSettings struct { - audiences map[string]bool - groupMap map[string]string - issuer string - identityClaim string - // groupClaimKey is the raw groupClaim, tried as one top-level key before - // the dotted groupClaim path. - groupClaimKey string + audiences map[string]bool + groupMap map[string]string + issuer string + identityClaim string groupClaim []string cacheTTL time.Duration jwksRefresh time.Duration @@ -122,7 +119,6 @@ func parseOIDCConfig(c *OIDCConfig, groups map[string]*group) (*oidcSettings, er if s.groupClaim, err = parseClaimPath(c.GroupClaim); err != nil { return nil, err } - s.groupClaimKey = c.GroupClaim if len(c.GroupMap) == 0 { return nil, fmt.Errorf("llmgateway: oidc.groupMap must map at least one claim value to a group") } diff --git a/oidc_config_test.go b/oidc_config_test.go index 29e8c90..afe32b5 100644 --- a/oidc_config_test.go +++ b/oidc_config_test.go @@ -255,8 +255,26 @@ func TestParseOIDCConfig_NamespacedGroupClaim(t *testing.T) { if err != nil { t.Fatalf("%q: %v", key, err) } - if s.groupClaimKey != key { - t.Fatalf("groupClaimKey = %q, want %q", s.groupClaimKey, key) + if len(s.groupClaim) != 1 || s.groupClaim[0] != key { + t.Fatalf("groupClaim = %q, want one segment %q", s.groupClaim, key) } } } + +// A flat top-level claim spelled like a dotted groupClaim must not shadow the +// nested value the IdP asserts. +func TestGroupValues_FlatKeyDoesNotShadowDottedPath(t *testing.T) { + c := validOIDCConfig() + c.GroupClaim = "realm_access.roles" + s, err := parseOIDCConfig(c, oidcTestGroups()) + if err != nil { + t.Fatal(err) + } + claims := map[string]any{ + "realm_access.roles": []any{"unmapped"}, + "realm_access": map[string]any{"roles": []any{"llm-users"}}, + } + if got := s.groupValues(claims); !reflect.DeepEqual(got, []string{"llm-users"}) { + t.Fatalf("groupValues = %v, want the nested [llm-users]", got) + } +}