Repository navigation
feat: oAuth2/OIDC spec compliance fixes - #2922
Conversation
📝 WalkthroughWalkthroughAdded nonce propagation and ID token hashing, made issuer (iss) realm-scoped across token types, introduced atomic cache pop for single-use authorization codes, updated verifier to consume codes, and adjusted grant flow to rely on atomic consumption. Changes
Sequence DiagramssequenceDiagram
actor Client
participant AuthServer as Authorization Server
participant CodeIssuer as Code Issuer
participant Cache
Client->>AuthServer: Authorization Request (with nonce)
AuthServer->>CodeIssuer: issue(input with nonce)
CodeIssuer->>Cache: save authorization code (contains nonce)
CodeIssuer-->>AuthServer: OAuth2AuthorizationCode
AuthServer-->>Client: Redirect with code
sequenceDiagram
actor Client
participant AuthServer as Authorization Server
participant Cache
participant TokenIssuer as Token Issuer
Client->>AuthServer: Token Request (code)
AuthServer->>Cache: popOneById(code)
alt code found (atomic)
Cache-->>AuthServer: OAuth2AuthorizationCode (removed)
AuthServer->>TokenIssuer: issue tokens (include nonce)
TokenIssuer->>TokenIssuer: compute at_hash / c_hash
TokenIssuer-->>AuthServer: Access Token + ID Token
AuthServer-->>Client: Token Response
else code missing
Cache-->>AuthServer: null
AuthServer-->>Client: Error (invalid_grant)
end
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (2)
apps/server-core/src/core/oauth2/authorization/code/issuer/module.ts (1)
53-62: Verify TTL behavior onupdateIdTokenis intentional.Using
this.options.maxAgeresets the authorization code's TTL to a fresh duration. If time has elapsed since code issuance, this could extend its validity beyond the original window. Consider whether the remaining TTL should be preserved instead.💡 Alternative: preserve remaining TTL
If preserving the original expiry is desired, track the original expiration timestamp on the entity and calculate remaining TTL:
async updateIdToken( entity: OAuth2AuthorizationCode, idToken: string, ) : Promise<void> { entity.id_token = idToken; - await this.repository.save(entity, { - maxAge: this.options.maxAge, - }); + // Pass entity with existing TTL metadata, or omit maxAge to use repository default + await this.repository.save(entity); }This would require the repository to handle TTL preservation logic.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@apps/server-core/src/core/oauth2/authorization/code/issuer/module.ts` around lines 53 - 62, The updateIdToken method currently resets the code TTL by passing this.options.maxAge into repository.save; change it to preserve the original remaining TTL instead of extending it. Inspect OAuth2AuthorizationCode for an expiration timestamp (e.g., expires_at or created_at + original TTL) and compute remainingTtl = expiresAt - now (min 0), then call this.repository.save(entity, { maxAge: remainingTtl }) or omit maxAge if remainingTtl <= 0 so the repository enforces expiry; update/updateIdToken to use that remainingTtl calculation (and if the entity lacks an expiry field, add one to OAuth2AuthorizationCode and ensure issuance sets it).apps/server-core/src/core/oauth2/authorization/helpers.ts (1)
41-41: Consider: Hash algorithm must match the ID token's signing algorithm per OIDC Core §3.3.2.11.The
buildOAuth2TokenHash()function uses a hardcoded SHA-256 algorithm for generatingat_hashandc_hashclaims. However, per OIDC Core specification, the hash algorithm should match the signing algorithm: SHA-256 for RS256/ES256/HS256, SHA-384 for RS384/ES384/HS384, and SHA-512 for RS512/ES512/HS512.While the codebase supports multiple signing algorithms (RS256, RS384, RS512, ES256, ES384, ES512, HS256, HS384, HS512), the hash calculation does not adapt to the configured algorithm. To ensure spec compliance, consider refactoring
buildOAuth2TokenHash()to accept and use the appropriate hash algorithm based on the realm's signing algorithm configuration.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@apps/server-core/src/core/oauth2/authorization/helpers.ts` at line 41, The buildOAuth2TokenHash function currently always uses SHA-256; change it to accept the token/signing algorithm (e.g., a parameter like signingAlg or realm.signingAlgorithm) and map signing algorithms (RS256/ES256/HS256 -> SHA-256, RS384/ES384/HS384 -> SHA-384, RS512/ES512/HS512 -> SHA-512), then call subtle.digest with the mapped hash when computing at_hash and c_hash; update any callers to pass the realm's configured signing algorithm (or derive it from the realm/JWK config) so the hash algorithm matches the ID token signing algorithm per OIDC Core §3.3.2.11.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@apps/server-core/src/core/oauth2/authorization/module.ts`:
- Around line 89-91: The null/undefined guard for identity is placed after
identity is already dereferenced, making it dead code; move the check for
identity (the OAuth2Error.identityInvalid() throw) to before any use of
identity, i.e., before accesses like identity.data.id and identity.type (or
remove the redundant guard if callers guarantee non-null). Ensure the
early-return/throw happens immediately at the start of the function or before
the first use of the identity parameter so no dereference can occur on a
null/undefined value.
In `@packages/server-kit/src/services/cache/adapters/memory.ts`:
- Around line 23-31: The pop method (and likewise get) uses a truthiness check
`if (output)` which treats legitimate falsy cached values (0, '', false) as
miss; change the logic to detect presence by using a Map/Store-specific has
method or by checking for undefined (e.g., compare against undefined) so that
falsy values are returned and only undefined means missing; update the pop<T =
unknown>(key: string) and get<T = unknown>(key: string) implementations to check
for undefined (or use this.instance.has(key) then this.instance.get(key)) and
ensure pop still deletes the key after retrieving a present value.
---
Nitpick comments:
In `@apps/server-core/src/core/oauth2/authorization/code/issuer/module.ts`:
- Around line 53-62: The updateIdToken method currently resets the code TTL by
passing this.options.maxAge into repository.save; change it to preserve the
original remaining TTL instead of extending it. Inspect OAuth2AuthorizationCode
for an expiration timestamp (e.g., expires_at or created_at + original TTL) and
compute remainingTtl = expiresAt - now (min 0), then call
this.repository.save(entity, { maxAge: remainingTtl }) or omit maxAge if
remainingTtl <= 0 so the repository enforces expiry; update/updateIdToken to use
that remainingTtl calculation (and if the entity lacks an expiry field, add one
to OAuth2AuthorizationCode and ensure issuance sets it).
In `@apps/server-core/src/core/oauth2/authorization/helpers.ts`:
- Line 41: The buildOAuth2TokenHash function currently always uses SHA-256;
change it to accept the token/signing algorithm (e.g., a parameter like
signingAlg or realm.signingAlgorithm) and map signing algorithms
(RS256/ES256/HS256 -> SHA-256, RS384/ES384/HS384 -> SHA-384, RS512/ES512/HS512
-> SHA-512), then call subtle.digest with the mapped hash when computing at_hash
and c_hash; update any callers to pass the realm's configured signing algorithm
(or derive it from the realm/JWK config) so the hash algorithm matches the ID
token signing algorithm per OIDC Core §3.3.2.11.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: b150f5a0-def6-4cec-b153-f32b028cec0f
📒 Files selected for processing (21)
apps/server-core/src/adapters/http/adapters/oauth2/grant-types/authorize.tsapps/server-core/src/app/modules/oauth2/repositories/authorize/code/repository.tsapps/server-core/src/core/oauth2/authorization/code-request/validator.tsapps/server-core/src/core/oauth2/authorization/code/issuer/module.tsapps/server-core/src/core/oauth2/authorization/code/issuer/types.tsapps/server-core/src/core/oauth2/authorization/code/repository/types.tsapps/server-core/src/core/oauth2/authorization/code/verifier/module.tsapps/server-core/src/core/oauth2/authorization/code/verifier/types.tsapps/server-core/src/core/oauth2/authorization/helpers.tsapps/server-core/src/core/oauth2/authorization/module.tsapps/server-core/src/core/oauth2/token/issuer/access/module.tsapps/server-core/src/core/oauth2/token/issuer/base.tsapps/server-core/src/core/oauth2/token/issuer/open-id/module.tsapps/server-core/src/core/oauth2/token/issuer/refresh/module.tsapps/server-core/test/unit/core/oauth2/authorization/code/verifier/module.spec.tsapps/server-core/test/unit/core/oauth2/authorization/helpers.spec.tsapps/server-core/test/unit/core/oauth2/token/issuer/open-id/module.spec.tspackages/core-kit/src/domains/authorization-code/entity.tspackages/server-kit/src/services/cache/adapters/memory.tspackages/server-kit/src/services/cache/adapters/redis.tspackages/server-kit/src/services/cache/types.ts
💤 Files with no reviewable changes (1)
- apps/server-core/src/core/oauth2/authorization/code/verifier/types.ts
There was a problem hiding this comment.
🧹 Nitpick comments (1)
apps/server-core/src/core/oauth2/authorization/module.ts (1)
110-111: Consider tighteningneedsIdTokencondition to avoid unused token issuance.When
response_type=tokenwithscope=openid, an ID token is issued but neither returned (sinceid_tokenisn't in response types) nor stored with a code (since no code exists). The token is persisted to the database byissueWithIdentitybut never used.Consider refining the condition to only issue when the token will actually be used:
♻️ Proposed refinement
- const needsIdToken = enabledResponseTypes[OAuth2AuthorizationResponseType.ID_TOKEN] || - (data.scope && hasOAuth2Scopes(data.scope, ScopeName.OPEN_ID)); + const needsIdToken = enabledResponseTypes[OAuth2AuthorizationResponseType.ID_TOKEN] || + (codeEntity && data.scope && hasOAuth2Scopes(data.scope, ScopeName.OPEN_ID));🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@apps/server-core/src/core/oauth2/authorization/module.ts` around lines 110 - 111, The needsIdToken flag is too broad and causes an ID token to be issued for response_type=token with scope=openid even when it will neither be returned nor stored; narrow the condition so an ID token is only generated when it will actually be used by checking response types: modify the needsIdToken calculation (the variable named needsIdToken that uses enabledResponseTypes and hasOAuth2Scopes) to require either OAuth2AuthorizationResponseType.ID_TOKEN or that scope includes openid AND a response type that produces/stores a code (e.g., OAuth2AuthorizationResponseType.CODE) so issueWithIdentity only persists an ID token when ID_TOKEN is requested or when a CODE flow will store/consume it.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@apps/server-core/src/core/oauth2/authorization/module.ts`:
- Around line 110-111: The needsIdToken flag is too broad and causes an ID token
to be issued for response_type=token with scope=openid even when it will neither
be returned nor stored; narrow the condition so an ID token is only generated
when it will actually be used by checking response types: modify the
needsIdToken calculation (the variable named needsIdToken that uses
enabledResponseTypes and hasOAuth2Scopes) to require either
OAuth2AuthorizationResponseType.ID_TOKEN or that scope includes openid AND a
response type that produces/stores a code (e.g.,
OAuth2AuthorizationResponseType.CODE) so issueWithIdentity only persists an ID
token when ID_TOKEN is requested or when a CODE flow will store/consume it.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 1bcab889-569c-4e44-8ea3-cd738597d514
📒 Files selected for processing (2)
apps/server-core/src/core/oauth2/authorization/module.tspackages/server-kit/src/services/cache/adapters/memory.ts
closes #2919
Summary by CodeRabbit
New Features
Refactor
Tests