Skip to content

fix(oidc): carry the user's groups in OIDC access tokens - #78

Open
alghanim wants to merge 3 commits into
bodaay:masterfrom
alghanim:fix/oidc-code-flow-groups
Open

alghanim wants to merge 3 commits into
bodaay:masterfrom
alghanim:fix/oidc-code-flow-groups

Conversation

@alghanim

@alghanim alghanim commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

A user who signs in through the OIDC authorization-code flow gets an access token with no groups claim. issueOIDCTokens and the refresh grant pass nil groups to buildOIDCAccessClaims. Both userinfo endpoints read groups from the token, so the groups appear nowhere: OIDC /userinfo omits them and /api/auth/userinfo answers null. A direct login's token, meanwhile, carries them.

This change makes the OIDC paths pass the user's stored groups, user.Groups. That is the set syncUserFromLDAP stores at each directory sign-in, and the set resolveTokenRoles already reads for per-app group assignments:

  • issueOIDCTokens: the authorization-code exchange and, sharing it, the password grant;
  • the refresh_token grant: the resolved user's current user.Groups, so a refreshed token carries the current set, and a user removed from every group gets none.

A user with no groups still gets no claim. Roles, permissions, audience, issuer, lifetimes and the id_token are unchanged.

Behaviour note (owner decision)

Groups are the last-known set, as stored at the user's last directory sign-in. So OIDC tokens also carry them in four cases where a direct login carries none:

  • a local-password sign-in;
  • a Kerberos sign-in while LDAP is down;
  • a session-cookie sign-in;
  • every refresh.

This was chosen deliberately: it is the set SimpleAuth already persists and resyncs at every directory login.

Tests

internal/handler/oidc_groups_test.go drives authorize → code → token, with PKCE, and the refresh grant. It checks the access token, OIDC /userinfo and /api/auth/userinfo, and covers a user with no groups.

  • The 4 new tests failed first, then passed.
  • 7 mutants are caught: nil groups at the exchange or the refresh, another user's groups, groups where a direct login would omit them, and others.
  • The full suite, -race, go vet, gofmt and the build pass.
  • The branch merges cleanly with feat/oidc-given-family-name, and the merged tree passes.

Verified against a consumer

A local image built from this branch (simpleauth:2.3.0-groupsfix-local) ran in CORTEX's dev stack. CORTEX's live test signed in a directory user (an, AD groups CORTEX-Analysts and Team-B) through the browser code flow. OIDC userinfo with the session's access token then returned exactly ["CORTEX-Analysts","Team-B"]. Before this change, it returned none.

Userinfo answers the stored groups (follow-up commits)

Review on the consumer side found that a person in no group got no groups member at all. So a consumer couldn't tell "no groups" from "groups unavailable", whether that came from an older SimpleAuth or a failure.

Two follow-up commits close this:

  • d2bf301: both userinfo endpoints always answer groups as a JSON list, [] when the user has none.
  • 223a2ce: both userinfo endpoints answer from the user's stored groups (user.Groups, synced at the last directory sign-in), whatever the presented token's claim holds. This avoids a false "no groups" for tokens whose minting path never set the claim: /api/auth/refresh, a session-cookie sign-in, impersonation, and a local-password or LDAP-down direct login. It is consistent with the last-known-groups behaviour above.

Token claims and the id_token are unchanged. Userinfo roles still come from the token, so for a token minted before a group change they can lag groups until the next refresh.

TestUserinfoAnswersStoredGroups failed first, then passed. It covers a local-password login, /api/auth/refresh, impersonation and a pre-change token. All 13 mutants are caught, and the suite, -race, vet, gofmt and the build pass.

Not included

There is no version bump, tag or release; that is left to the maintainer. CHANGELOG.md gains an Unreleased entry, and docs/API.md is corrected.

🤖 Generated with Claude Code

Your Name and others added 3 commits November 9, 2026 16:00
A user who signed in through the OIDC authorization-code flow got an
access token with no `groups` claim: issueOIDCTokens and the refresh
grant passed nil groups to buildOIDCAccessClaims. Both userinfo
endpoints read groups from the token, so the groups appeared nowhere
(OIDC /userinfo omitted them, /api/auth/userinfo answered null), while
a direct login's token carried them.

A direct login's groups are the directory's groups for the person, as
bare CNs (entryToResult), never filtered per app; syncUserFromLDAP
stores exactly that set on the user in the same login. The OIDC paths
now pass that stored set:

- issueOIDCTokens (authorization-code exchange and, sharing it, the
  password grant) passes user.Groups;
- the refresh_token grant passes the resolved user's current
  user.Groups, so a refreshed token carries the current set and a user
  removed from every group gets none.

It is the set resolveTokenRoles already reads for per-app group
assignments. A user with no groups still gets no claim. Roles,
permissions, audience, issuer, lifetimes and the id_token are
unchanged.

Tests drive authorize -> code -> token with PKCE and the refresh grant,
and check the access token, OIDC /userinfo and /api/auth/userinfo.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
For a user in no group, OIDC /userinfo omitted `groups` and
/api/auth/userinfo answered null. A client could not tell "no groups"
from "groups unavailable" (an older SimpleAuth, or a failure).

Both endpoints now answer `groups` as a JSON list through one helper,
userinfoGroups: the token's groups, or [] when it carries none. The
access token's claim stays omitempty and the id_token is unchanged.

The no-groups tests now expect [] from both endpoints for a direct
login, the code exchange and its refreshes, and for a user the
directory removed from every group.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Both userinfo endpoints echoed the presented token's `groups` claim.
Several token paths set no claim (a local-password direct login,
/api/auth/refresh, the session-cookie sign-in, impersonation), so for a
user with groups they answered [] since the previous commit, a false
"no groups".

Both endpoints now answer the resolved user's stored groups, the set
synced at their last LDAP or Kerberos sign-in, always as a list, []
when there are none, whatever the token's claim holds. A token minted
before the directory moved the user reads the current set. The token
claims and the id_token are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant