Repository navigation
fix: reuse bearer session on interactive authorization_code login - #3191
Conversation
An interactive client-web login created two `auth_sessions` rows: the SSR `/authorize` page password-grants a client-less bearer session to authenticate `POST /authorize`, then `/login/callback` exchanges the auth code, whose grant created a second session. The bearer session was abandoned but lingered until expiry and surfaced in the sessions list. Thread the bearer's session id (server-derived, stashed by the authorization middleware) through the auth-code blob and reuse it in the authorization_code grant when it still matches the code's sub/sub_kind/realm_id, stamping the authorizing client onto the row. Session-less flows (external-IdP callback, non-interactive clients) and any mismatch fall back to creating a fresh session. The auth-code blob is Redis-backed, so no migration is required.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (10)
📝 WalkthroughWalkthroughAdds session_id propagation for interactive OAuth2 authorization flows: the HTTP authorizer captures the current session id and threads it through ChangesSession Reuse for Interactive authorization_code Grants
Estimated code review effort: 3 (Moderate) | ~25 minutes Sequence Diagram(s)sequenceDiagram
participant Client
participant HttpAuthorizer
participant OAuth2Authorization
participant OAuth2AuthorizeGrant
participant SessionManager
Client->>HttpAuthorizer: GET /authorize (bearer token)
HttpAuthorizer->>HttpAuthorizer: useRequestSessionId(event)
HttpAuthorizer->>OAuth2Authorization: authorize(data, identity, { sessionId })
OAuth2Authorization->>OAuth2Authorization: codeIssuer.issue({ sessionId })
OAuth2Authorization-->>Client: authorization code (with session_id)
Client->>OAuth2AuthorizeGrant: exchange code for token
OAuth2AuthorizeGrant->>OAuth2AuthorizeGrant: resolveSession(authorizationCode)
alt session_id matches sub/sub_kind/realm_id
OAuth2AuthorizeGrant->>SessionManager: reuse & refresh session
else no match
OAuth2AuthorizeGrant->>SessionManager: create new session
end
OAuth2AuthorizeGrant-->>Client: access/refresh tokens (with session_id)
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ 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.
Pull request overview
This PR fixes duplicate auth_sessions creation during interactive authorization_code logins by threading the initial bearer session id through the issued authorization code and reusing that session during the token exchange when it still matches the same subject/realm.
Changes:
- Extend the authorization-code blob to optionally carry a
session_idand thread it through the/authorizepipeline into the code issuer. - Update the
authorization_codegrant to reuse an existing matching session (and refresh/stampclient_id) instead of creating a second session, with safe fallback to the prior “create new session” behavior. - Add unit + end-to-end tests covering reuse and fallback branches, including mismatch safeguards.
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| packages/core-kit/src/domains/authorization-code/entity.ts | Adds optional session_id to the authorization-code entity contract. |
| apps/server-core/src/core/oauth2/grant-types/authorize.ts | Reuses an existing session when session_id is present and matches subject/realm; otherwise creates a new session. |
| apps/server-core/src/core/oauth2/authorization/types.ts | Introduces OAuth2AuthorizationOptions to carry sessionId through authorization. |
| apps/server-core/src/core/oauth2/authorization/module.ts | Threads sessionId into authorization-code issuance. |
| apps/server-core/src/core/oauth2/authorization/code/issuer/types.ts | Adds sessionId to code issuer options. |
| apps/server-core/src/core/oauth2/authorization/code/issuer/module.ts | Persists session_id onto the issued authorization-code blob when provided. |
| apps/server-core/src/adapters/http/adapters/oauth2/authorization/module.ts | Passes request-scoped session id (useRequestSessionId) into authorize(...). |
| apps/server-core/test/unit/core/oauth2/grant-types/authorize.spec.ts | Unit tests for session reuse vs create fallback, including mismatch cases. |
| apps/server-core/test/unit/http/controllers/workflows/token/grant-authorize-session.spec.ts | End-to-end test asserting only one session survives interactive login → authorize → exchange. |
| .agents/architecture.md | Documents the “one session per interactive login” session continuity behavior and mechanism. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
authup
@authup/client-web
@authup/server-core
@authup/access
@authup/client-web-kit
@authup/client-web-kit-theme
@authup/client-web-nuxt
@authup/client-web-theme
@authup/core-http-kit
@authup/core-kit
@authup/core-realtime-kit
@authup/errors
@authup/i18n
@authup/kit
@authup/server-adapter-kit
@authup/server-adapter-node
@authup/server-adapter-socket-io
@authup/server-adapter-web
@authup/server-kit
@authup/server-test-kit
@authup/specs
commit: |
…gout (#3201) * feat: retain the id_token in the kit store & round-trip client-web logout (plan 042) The kit store discarded the grant response's id_token, so no kit RP could pass an id_token_hint to the authup end_session_endpoint — every logout degraded to the click-gated confirm page. - add an `idToken` ref to the store (ID_TOKEN_UPDATED dispatcher event, CookieName.ID_TOKEN persistence, cleared in cleanup); applyTokenGrantResponse retains the existing value when a response carries none (refresh grants) - client-web pages/logout.vue now performs the RP-initiated round-trip: it drops REQUIRED_LOGGED_OUT (so the interceptor no longer logs out before the page runs), captures idToken/realmId, runs the local-only store.logout(), then redirects to buildEndSessionURL(...) with the id_token hint and a post_logout_redirect_uri back to /login store.logout() stays local-only — the round-trip ends the shared authup session, never a DELETE /sessions/@me (which would collide with #3191 session reuse). * fix: omit client_id in logout round-trip so the id_token_hint aud resolves the client logout.vue passed client_id=web (the name) but the id_token aud is the web client UUID. The end-session service aud/client_id cross-check then failed, clearing hintVerified, so the session was never revoked and the browser never bounced back to /login — every client-web logout silently degraded to the confirm page, defeating the round-trip. Omitting client_id lets the service resolve the client from the hint sole aud.
#3200) * feat: drop implicit & hybrid oauth2 response types (plan 042) response_type=code is now the only supported response type (OAuth 2.1 / RFC 9700 posture): - the code-request validator requires response_type to be exactly 'code'; token / id_token / none / hybrid combos are rejected - OAuth2Authorization.authorize() is code-only (defense in depth) — the TOKEN/ID_TOKEN issuance branches, at_hash computation and the authorize-response accessToken/idToken outputs are gone; an openid-scoped code still carries its id_token on the code blob for the /token exchange - AuthorizeController.confirm() no longer writes access_token / id_token into the redirect URL (tokens leaked via history, proxy logs and Referer - the fix at source is to not produce them at all) - discovery response_types_supported now advertises only 'code' - the code-request verifier requires PKCE + state for public clients unconditionally (the willIssueCode gate is dead with code-only) - client-web's client URL generator emits response_type=code BREAKING: RPs using the implicit or hybrid flow must migrate to the authorization code flow with PKCE. * feat: mint the id_token at the token exchange for an authoritative sid (plan 042) The id_token was minted at authorize time from options.sessionId, so its `sid` could reference a stale session (the #3191 session-reuse-vs-fallback path) and the session-less federated IdP flow produced no id_token at all. Move minting into the authorization_code grant, after resolveSession: - OAuth2AuthorizeGrant.runWith mints the openid-scoped id_token with sid = the real backing session (reuse, fallback, and federated flows alike); at_hash (over the fresh access token) and c_hash are recomputed, nonce and auth_time ride from the code - the authentication instant is carried on the auth-code blob as OAuth2AuthorizationCode.auth_time (replacing the removed id_token field — cache blob, no migration); OAuth2Authorization stamps it and drops its openIdTokenIssuer / identityResolver dependencies and id_token minting - openIdTokenIssuer is wired into the TokenController authorize grant; codeIssuer.updateIdToken is removed Fixes the plan-041 stale-sid residual; the federated openid flow now returns an id_token whose sid matches the access token's session. * docs: fix stale id_token-on-code-blob claim in architecture The response-types section still said an openid-scoped code stores its id_token on the code blob and the /token exchange returns it, contradicting the minting-site section in the same file: authorize() no longer mints or stores the id_token — it stamps auth_time on the blob (the id_token field was removed) and the /token exchange mints and returns it. Reported by CodeRabbit on PR #3200.
Problem
An interactive client-web login created two
auth_sessionsrows:/authorizepage password-grants a client-less bearer session purely to authenticatePOST /authorize./login/callbackthen exchanges the authorization code, whoseauthorization_codegrantcreate()d a second session.The bearer session (#1) was abandoned but lingered until expiry — and showed up in the user's sessions list, producing two near-simultaneous sessions per login.
Fix
Thread the bearer's session id through the authorization-code blob and reuse it in the
authorization_codegrant instead of minting a second session:OAuth2AuthorizationCodegains an optionalsession_id(the auth-code blob is Redis-backed → no migration).HTTPOAuth2Authorizer.authorizeWithRequestreadsuseRequestSessionId(event)— the id the authorization middleware stashed from the authenticated bearer (server-derived, never client input) — and threads it throughOAuth2Authorization.authorize→OAuth2AuthorizationCodeIssuer.issue→entity.session_id.OAuth2AuthorizeGrant.resolveSessionreuses the referenced session iff it still exists and matches the code'ssub/sub_kind/realm_id(defense in depth), stamps the authorizingclient_idonto the row, andrefresh()es it.sessionManager.create()— prior behavior is unchanged.Tests
test/unit/core/oauth2/grant-types/authorize.spec.ts— reuse vs. create-fallback branches, including the sub/realm-mismatch fail-safes.test/unit/http/controllers/workflows/token/grant-authorize-session.spec.ts— end-to-end login → authorize → exchange asserting a single session survives (verified to fail without the fix).Full server-core suite (1094 tests) +
check:typesgreen.Summary by CodeRabbit
New Features
Bug Fixes