Add identity extractor for OAuth2 token responses#5200
Open
Conversation
Some OAuth2 upstream providers do not expose a usable userinfo
endpoint and instead place user identity directly in the token
endpoint response. Two response shapes appear in practice:
- Identity as side-attributes alongside the tokens, e.g.
Snowflake's `username`, Slack's `authed_user.id`, Shopify's
`associated_user.{id,email,first_name}`.
- Identity claims embedded inside a JWT-shaped access token, e.g.
Auth0, Azure AD, Keycloak, Cognito.
Introduce a pure helper that reads operator-supplied gjson
dot-notation paths from the raw token-response body to extract
subject, name, and email. Register a custom gjson modifier
`@upstreamjwt` so paths can pipe through a JWT payload decode step
(e.g. "access_token|@upstreamjwt|sub"). The modifier base64url-decodes
the JWT payload without verifying the signature; trust comes from the
TLS-authenticated channel to the AS, the same trust model as the
existing userinfo path. Signed-token flows remain handled by the
existing OIDC provider type. Modifier registration is exported and
explicit (RegisterModifiers) so callers control when the
process-global gjson state mutates.
The helper is consumed by the embedded auth server's OAuth2 upstream
provider in a later commit; nothing in this commit calls it yet.
Type guard restricts the subject to scalar string or number values to
avoid silently returning a JSON blob as the user's identity. Numeric
subjects are returned via the raw JSON token rather than gjson's
float64 formatting to preserve integer precision beyond 2^53. Error
messages never include any portion of the body.
Closes #5152
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #5200 +/- ##
==========================================
+ Coverage 67.65% 67.70% +0.04%
==========================================
Files 607 608 +1
Lines 61982 62043 +61
==========================================
+ Hits 41937 42006 +69
+ Misses 16883 16876 -7
+ Partials 3162 3161 -1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Some OAuth2 upstream providers do not expose a usable userinfo endpoint and instead place user identity directly in the token endpoint response. Two shapes appear in practice:
username, Slack'sauthed_user.id, Shopify'sassociated_user.{id,email,first_name}.This PR adds a pure helper
extractIdentityFromTokenResponse(body, cfg)that reads operator-supplied gjson dot-notation paths from the raw token-response body to extract subject, name, and email. It also registers a gjson modifier@upstreamjwtso paths can pipe through a JWT payload decode step (e.g.access_token|@upstreamjwt|sub). The modifier does not verify the JWT signature — trust comes from the TLS-authenticated channel to the AS, the same trust model as the existing userinfo path. Signed-token flows remain handled by the existing OIDC provider type.The helper is consumed by the embedded auth server's OAuth2 upstream provider in a later commit; nothing in this PR calls it yet.
Closes #5152
Type of change
Test plan
9007199254740993, beyond 2^53)task lint-fixreports 0 issuesImplementation plan
Approved plan from the design session
extractIdentityFromTokenResponse(body, cfg)with type-guard subject validation (string or number only; objects, arrays, null, booleans, missing paths rejected withErrIdentityResolutionFailed).slog.Warnthat names the path but never the value or any body content.result.Rawrather thangjson.Result.String()(which formats via float64 and truncates >2^53).@upstreamjwtfor JWT-embedded identity claims. No signature verification (trust via TLS-to-AS, same as userinfo path). Returns""on any failure mode (non-string input, wrong dot-count, base64 error), letting downstreamvalidateIdentityFieldproduce a uniformErrIdentityResolutionFailed.RegisterModifiers()rather thaninit()— caller-controlled, testable, nogochecknoinitssuppression. Idempotent (gjson.AddModifier overwrites).Does this introduce a user-facing change?
No. This is an internal helper with no consumer in this PR. The OAuth2 upstream provider integration that exposes the feature to operators — and the corresponding CRD type — lands in follow-up PRs.
Special notes for reviewers
@upstreamjwtmodifier is an additive extension for the JWT-embedded shape (Auth0/Azure/Keycloak/Cognito). All eight original acceptance criteria from the issue are met; the JWT modifier is incremental on top.RegisterModifiers()must be called once during application or test wire-up before any consumer issues a path containing@upstreamjwt. The integrating commit will add this call to the OAuth2 upstream provider's constructor.scalarToStringusesresult.Raw). Any downstream JWT issuer must serialize them as JSON strings per RFC 7519 §4.1.2 (StringOrURI). A test for that contract belongs in the integrating PR.upstreamJWTModifier's godoc. The corresponding CRD type (out of scope here) should also carry operator-facing warnings about which fields are appropriate forsubjectPath(don't useaccess_token,token_type, etc.).🤖 Generated with Claude Code