Repository navigation
🪙 refactor: Distinguish ID Tokens from Access Tokens in OIDC Federated Auth #11711
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
danny-avila
merged 5 commits into
LibreChat-AI:dev
from
aproorg:fix/openid-token-distinction
Feb 13, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8355bf4
fix(openid): distinguish ID tokens from access tokens in federated auth
busla 7a50d76
Merge branch 'main' into fix/openid-token-distinction
busla 64daabd
Merge branch 'main' into fix/openid-token-distinction
busla de7710e
fix(openid): add separate openid_id_token cookie for ID token storage
busla 3a3fa67
test(openid): add JWT strategy cookie fallback tests
busla File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| const { SystemRoles } = require('librechat-data-provider'); | ||
|
|
||
| // --- Capture the verify callback from JwtStrategy --- | ||
| let capturedVerifyCallback; | ||
| jest.mock('passport-jwt', () => ({ | ||
| Strategy: jest.fn((_opts, verifyCallback) => { | ||
| capturedVerifyCallback = verifyCallback; | ||
| return { name: 'jwt' }; | ||
| }), | ||
| ExtractJwt: { | ||
| fromAuthHeaderAsBearerToken: jest.fn(() => 'mock-extractor'), | ||
| }, | ||
| })); | ||
| jest.mock('jwks-rsa', () => ({ | ||
| passportJwtSecret: jest.fn(() => 'mock-secret-provider'), | ||
| })); | ||
| jest.mock('https-proxy-agent', () => ({ | ||
| HttpsProxyAgent: jest.fn(), | ||
| })); | ||
| jest.mock('@librechat/data-schemas', () => ({ | ||
| logger: { info: jest.fn(), warn: jest.fn(), debug: jest.fn(), error: jest.fn() }, | ||
| })); | ||
| jest.mock('@librechat/api', () => ({ | ||
| isEnabled: jest.fn(() => false), | ||
| findOpenIDUser: jest.fn(), | ||
| math: jest.fn((val, fallback) => fallback), | ||
| })); | ||
| jest.mock('~/models', () => ({ | ||
| findUser: jest.fn(), | ||
| updateUser: jest.fn(), | ||
| })); | ||
|
|
||
| const { findOpenIDUser } = require('@librechat/api'); | ||
| const { updateUser } = require('~/models'); | ||
| const openIdJwtLogin = require('./openIdJwtStrategy'); | ||
|
|
||
| // Helper: build a mock openIdConfig | ||
| const mockOpenIdConfig = { | ||
| serverMetadata: () => ({ jwks_uri: 'https://example.com/.well-known/jwks.json' }), | ||
| }; | ||
|
|
||
| // Helper: invoke the captured verify callback | ||
| async function invokeVerify(req, payload) { | ||
| return new Promise((resolve, reject) => { | ||
| capturedVerifyCallback(req, payload, (err, user, info) => { | ||
| if (err) { | ||
| return reject(err); | ||
| } | ||
| resolve({ user, info }); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| describe('openIdJwtStrategy – token source handling', () => { | ||
| const baseUser = { | ||
| _id: { toString: () => 'user-abc' }, | ||
| role: SystemRoles.USER, | ||
| provider: 'openid', | ||
| }; | ||
|
|
||
| const payload = { sub: 'oidc-123', email: 'test@example.com', exp: 9999999999 }; | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| findOpenIDUser.mockResolvedValue({ user: { ...baseUser }, error: null, migration: false }); | ||
| updateUser.mockResolvedValue({}); | ||
|
|
||
| // Initialize the strategy so capturedVerifyCallback is set | ||
| openIdJwtLogin(mockOpenIdConfig); | ||
| }); | ||
|
|
||
| it('should read all tokens from session when available', async () => { | ||
| const req = { | ||
| headers: { authorization: 'Bearer raw-bearer-token' }, | ||
| session: { | ||
| openidTokens: { | ||
| accessToken: 'session-access', | ||
| idToken: 'session-id', | ||
| refreshToken: 'session-refresh', | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| const { user } = await invokeVerify(req, payload); | ||
|
|
||
| expect(user.federatedTokens).toEqual({ | ||
| access_token: 'session-access', | ||
| id_token: 'session-id', | ||
| refresh_token: 'session-refresh', | ||
| expires_at: payload.exp, | ||
| }); | ||
| }); | ||
|
|
||
| it('should fall back to cookies when session is absent', async () => { | ||
| const req = { | ||
| headers: { | ||
| authorization: 'Bearer raw-bearer-token', | ||
| cookie: | ||
| 'openid_access_token=cookie-access; openid_id_token=cookie-id; refreshToken=cookie-refresh', | ||
| }, | ||
| }; | ||
|
|
||
| const { user } = await invokeVerify(req, payload); | ||
|
|
||
| expect(user.federatedTokens).toEqual({ | ||
| access_token: 'cookie-access', | ||
| id_token: 'cookie-id', | ||
| refresh_token: 'cookie-refresh', | ||
| expires_at: payload.exp, | ||
| }); | ||
| }); | ||
|
|
||
| it('should fall back to cookie for idToken only when session lacks it', async () => { | ||
| const req = { | ||
| headers: { | ||
| authorization: 'Bearer raw-bearer-token', | ||
| cookie: 'openid_id_token=cookie-id', | ||
| }, | ||
| session: { | ||
| openidTokens: { | ||
| accessToken: 'session-access', | ||
| // idToken intentionally missing | ||
| refreshToken: 'session-refresh', | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| const { user } = await invokeVerify(req, payload); | ||
|
|
||
| expect(user.federatedTokens).toEqual({ | ||
| access_token: 'session-access', | ||
| id_token: 'cookie-id', | ||
| refresh_token: 'session-refresh', | ||
| expires_at: payload.exp, | ||
| }); | ||
| }); | ||
|
|
||
| it('should use raw Bearer token as access_token fallback when neither session nor cookie has one', async () => { | ||
| const req = { | ||
| headers: { | ||
| authorization: 'Bearer raw-bearer-token', | ||
| cookie: 'openid_id_token=cookie-id; refreshToken=cookie-refresh', | ||
| }, | ||
| }; | ||
|
|
||
| const { user } = await invokeVerify(req, payload); | ||
|
|
||
| expect(user.federatedTokens.access_token).toBe('raw-bearer-token'); | ||
| expect(user.federatedTokens.id_token).toBe('cookie-id'); | ||
| expect(user.federatedTokens.refresh_token).toBe('cookie-refresh'); | ||
| }); | ||
|
|
||
| it('should set id_token to undefined when not available in session or cookies', async () => { | ||
| const req = { | ||
| headers: { | ||
| authorization: 'Bearer raw-bearer-token', | ||
| cookie: 'openid_access_token=cookie-access; refreshToken=cookie-refresh', | ||
| }, | ||
| }; | ||
|
|
||
| const { user } = await invokeVerify(req, payload); | ||
|
|
||
| expect(user.federatedTokens.access_token).toBe('cookie-access'); | ||
| expect(user.federatedTokens.id_token).toBeUndefined(); | ||
| expect(user.federatedTokens.refresh_token).toBe('cookie-refresh'); | ||
| }); | ||
|
|
||
| it('should keep id_token and access_token as distinct values from cookies', async () => { | ||
| const req = { | ||
| headers: { | ||
| authorization: 'Bearer raw-bearer-token', | ||
| cookie: | ||
| 'openid_access_token=the-access-token; openid_id_token=the-id-token; refreshToken=the-refresh', | ||
| }, | ||
| }; | ||
|
|
||
| const { user } = await invokeVerify(req, payload); | ||
|
|
||
| expect(user.federatedTokens.access_token).toBe('the-access-token'); | ||
| expect(user.federatedTokens.id_token).toBe('the-id-token'); | ||
| expect(user.federatedTokens.access_token).not.toBe(user.federatedTokens.id_token); | ||
| }); | ||
| }); |
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.