🔄 feat: Body-Based OpenID Refresh for Cross-Origin Admin Panels#46
Merged
Conversation
The cookie-based `/api/auth/refresh` controller can't be reached cross-origin because the refresh-token cookie doesn't ship on a fetch from a different host. Cross-origin admin panel sessions silently die at JWT expiry with no recovery. This routes `openid` sessions through the new `POST /api/admin/oauth/refresh` endpoint (danny-avila/LibreChat#13007) which accepts the refresh token in the request body and returns the same shape as `/api/admin/oauth/exchange`: `{ token, refreshToken, user, expiresAt }`. Non-openid sessions still use the legacy cookie-based path. The `expiresAt` field (ms epoch) is now threaded through `SessionData` and `OAuthExchangeResponse` so the admin panel can drive proactive refresh before the bearer expires. `refreshAdminToken` takes a new `userId` argument that's forwarded as `user_id` in the refresh request body for disambiguation when multiple user docs share the same OpenID `sub`.
Contributor
|
One follow-up needed now that
Suggested tests: tenant header forwarded/omitted correctly, |
Two follow-ups for the BFF refresh path now that the LibreChat backend scopes /api/admin/oauth/refresh by tenant. apiFetch was sending the session bearer as-is. If a query landed past JWT expiry it would fail before the 60s revalidation interval kicked in. apiFetch now reads expiresAt, refreshes proactively when the bearer is within 30s of expiry, persists any rotated refresh token, and retries the original request exactly once on a 401. The OpenID refresh request now forwards the deployment's X-Tenant-Id header so the backend's preAuthTenantMiddleware can scope the user lookup. Without this the backend would fall back to single-tenant behavior and the multi-tenant duplicate (sub, iss) protection added in danny-avila/LibreChat#13007 wouldn't activate. Refresh logic moves to a shared src/server/utils/refresh.ts so the verify path and apiFetch share one implementation. Concurrent callers are deduped on the refresh token so two React Query subscribers can't both consume a rotating token in the same BFF process. Adds 19 tests covering tenant header forwarded/omitted, dedupe, proactive refresh inside/outside skew, rotation persistence, and single-retry 401 behavior.
Contributor
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d6aa8680e1
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Keying the in-flight refresh map on refreshToken alone meant two concurrent calls that happen to share a token string but differ by userId, tokenProvider, or tenant would coalesce, and the second caller would receive the first caller's bearer and persist it into the wrong session. Build the dedupe key from tokenProvider, userId, the request's X-Tenant-Id header, and the refresh token (joined by NUL). Adds three regression tests covering each discriminator.
danny-avila
approved these changes
May 8, 2026
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
The cookie-based
/api/auth/refreshcontroller can't be reached cross-origin because the refresh-token cookie doesn't ship on a fetch from a different host than the LibreChat backend. The result: cross-origin admin panel sessions silently die at JWT expiry with no recovery.This routes
openidsessions through the newPOST /api/admin/oauth/refreshendpoint (danny-avila/LibreChat#13007) which accepts the refresh token in the request body and returns the same shape as/api/admin/oauth/exchange:{ token, refreshToken, user, expiresAt }. Non-openid (librechat) sessions still use the legacy cookie-based path.Changes
src/server/auth.ts—refreshAdminTokenposts to/api/admin/oauth/refreshfor openid sessions, taking a newuserIdargument that's forwarded asuser_idin the body for disambiguation when multiple user docs share the same OpenIDsub. ThreadsexpiresAtfrom the response into the session.src/types/server.ts— adds optionalexpiresAt?: number(ms epoch) toSessionDataandOAuthExchangeResponseso the admin panel can drive proactive refresh before the bearer expires.Compatibility
librechat(local) sessions: unchanged, still use/api/auth/refreshwith the cookie-based path.openidsessions on backends without the new endpoint: refresh returns 404, the admin panel clears the session and redirects to login. Same end-state as before, just one cycle later.Testing
/api/admin/oauth/refreshendpoint (danny-avila/LibreChat#13007) was validated via direct POST against a Microsoft Entra (Azure AD) personal-account tenant: a stored Entra refresh token returned a200with a valid{ token, refreshToken, user, expiresAt }payload (rotated refresh_token, fresh HS256 LC JWT, ~24hexpiresAt).