fix(oidc): stop a flat claim shadowing a dotted groupClaim

The group lookup tried the raw groupClaim as one top-level key before the
dotted path. A token that carried a flat key such as "realm_access.roles"
next to the nested claim got the flat value. Only a groupClaim with "://"
is one key now. parseClaimPath already keeps such a claim as one segment,
so the separate whole-key lookup is gone. A dotted groupClaim walks the
path, as it did before this branch.
This commit is contained in:
2026-12-02 02:24:52 +00:00
parent 004da308f8
commit b3fffdbeed
5 changed files with 32 additions and 21 deletions
+1 -1
View File
@@ -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. |
+2 -5
View File
@@ -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)
}
+5 -5
View File
@@ -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 {
+4 -8
View File
@@ -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")
}
+20 -2
View File
@@ -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)
}
}