Commit graph

94 commits

Author SHA1 Message Date
Jón Levy
ef3bf0a932
🆔 feat: Add OpenID Connect Federated Provider Token Support (#9931)
* feat: Add OpenID Connect federated provider token support

Implements support for passing federated provider tokens (Cognito, Azure AD, Auth0)
as variables in LibreChat's librechat.yaml configuration for both custom endpoints
and MCP servers.

Features:
- New LIBRECHAT_OPENID_* template variables for federated provider tokens
- JWT claims parsing from ID tokens without verification (for claim extraction)
- Token validation with expiration checking
- Support for multiple token storage locations (federatedTokens, openidTokens)
- Integration with existing template variable system
- Comprehensive test suite with Cognito-specific scenarios
- Provider-agnostic design supporting Cognito, Azure AD, Auth0, etc.

Security:
- Server-side only token processing
- Automatic token expiration validation
- Graceful fallbacks for missing/invalid tokens
- No client-side token exposure

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* fix: Add federated token propagation to OIDC authentication strategies

Adds federatedTokens object to user during authentication to enable
federated provider token template variables in LibreChat configuration.

Changes:
- OpenID JWT Strategy: Extract raw JWT from Authorization header and
  attach as federatedTokens.access_token to enable {{LIBRECHAT_OPENID_TOKEN}}
  placeholder resolution
- OpenID Strategy: Attach tokenset tokens as federatedTokens object to
  standardize token access across both authentication strategies

This enables proper token propagation for custom endpoints and MCP
servers that require federated provider tokens for authorization.

Resolves missing token issue reported by @ramden in PR #9931

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Denis Ramic <denis.ramic@nfon.com>
Co-Authored-By: Claude <noreply@anthropic.com>

* test: Add federatedTokens validation tests for OIDC strategies

Adds comprehensive test coverage for the federated token propagation
feature implemented in the authentication strategies.

Tests added:
- Verify federatedTokens object is attached to user with correct structure
  (access_token, refresh_token, expires_at)
- Verify both tokenset and federatedTokens are present in user object
- Ensure tokens from OIDC provider are correctly propagated

Also fixes existing test suite by adding missing mocks:
- isEmailDomainAllowed function mock
- findOpenIDUser function mock

These tests validate the fix from commit 5874ba29f that enables
{{LIBRECHAT_OPENID_TOKEN}} template variable functionality.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* docs: Remove implementation documentation file

The PR description already contains all necessary implementation details.
This documentation file is redundant and was requested to be removed.

* fix: skip s256 check

* fix(openid): handle missing refresh token in Cognito token refresh response

When OPENID_REUSE_TOKENS=true, the token refresh flow was failing because
Cognito (and most OAuth providers) don't return a new refresh token in the
refresh grant response - they only return new access and ID tokens.

Changes:
- Modified setOpenIDAuthTokens() to accept optional existingRefreshToken parameter
- Updated validation to only require access_token (refresh_token now optional)
- Added logic to reuse existing refresh token when not provided in tokenset
- Updated refreshController to pass original refresh token as fallback
- Added comments explaining standard OAuth 2.0 refresh token behavior

This fixes the "Token is not present. User is not authenticated." error that
occurred during silent token refresh with Cognito as the OpenID provider.

Fixes: Authentication loop with OPENID_REUSE_TOKENS=true and AWS Cognito

* fix(openid): extract refresh token from cookies for template variable replacement

When OPENID_REUSE_TOKENS=true, the openIdJwtStrategy populates user.federatedTokens
to enable template variable replacement (e.g., {{LIBRECHAT_OPENID_ACCESS_TOKEN}}).

However, the refresh_token field was incorrectly sourced from payload.refresh_token,
which is always undefined because:
1. JWTs don't contain refresh tokens in their payload
2. The JWT itself IS the access token
3. Refresh tokens are separate opaque tokens stored in HTTP-only cookies

This caused extractOpenIDTokenInfo() to receive incomplete federatedTokens,
resulting in template variables remaining unreplaced in headers.

**Root Cause:**
- Line 90: `refresh_token: payload.refresh_token` (always undefined)
- JWTs only contain access token data in their claims
- Refresh tokens are separate, stored securely in cookies

**Solution:**
- Import `cookie` module to parse cookies from request
- Extract refresh token from `refreshToken` cookie
- Populate federatedTokens with both access token (JWT) and refresh token (from cookie)

**Impact:**
- Template variables like {{LIBRECHAT_OPENID_ACCESS_TOKEN}} now work correctly
- Headers in librechat.yaml are properly replaced with actual tokens
- MCP server authentication with federated tokens now functional

**Technical Details:**
- passReqToCallback=true in JWT strategy provides req object access
- Refresh token extracted via cookies.parse(req.headers.cookie).refreshToken
- Falls back gracefully if cookie header or refreshToken is missing

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* fix: re-resolve headers on each request to pick up fresh federatedTokens

- OpenAIClient now re-resolves headers in chatCompletion() before each API call
- This ensures template variables like {{LIBRECHAT_OPENID_TOKEN}} are replaced
  with actual token values from req.user.federatedTokens
- initialize.js now stores original template headers instead of pre-resolved ones
- Fixes template variable replacement when OPENID_REUSE_TOKENS=true

The issue was that headers were only resolved once during client initialization,
before openIdJwtStrategy had populated user.federatedTokens. Now headers are
re-resolved on every request with the current user's fresh tokens.

* debug: add logging to track header resolution in OpenAIClient

* debug: log tokenset structure after refresh to diagnose missing access_token

* fix: set federatedTokens on user object after OAuth refresh

- After successful OAuth token refresh, the user object was not being
  updated with federatedTokens
- This caused template variable resolution to fail on subsequent requests
- Now sets user.federatedTokens with access_token, id_token, refresh_token
  and expires_at from the refreshed tokenset
- Fixes template variables like {{LIBRECHAT_OPENID_TOKEN}} not being
  replaced after token refresh
- Related to PR #9931 (OpenID federated token support)

* fix(openid): pass user object through agent chain for template variable resolution

Root cause: buildAgentContext in agents/run.ts called resolveHeaders without
the user parameter, preventing OpenID federated token template variables from
being resolved in agent runtime parameters.

Changes:
- packages/api/src/agents/run.ts: Add user parameter to createRun signature
- packages/api/src/agents/run.ts: Pass user to resolveHeaders in buildAgentContext
- api/server/controllers/agents/client.js: Pass user when calling createRun
- api/server/services/Endpoints/bedrock/options.js: Add resolveHeaders call with debug logging
- api/server/services/Endpoints/custom/initialize.js: Add debug logging
- packages/api/src/utils/env.ts: Add comprehensive debug logging and stack traces
- packages/api/src/utils/oidc.ts: Fix eslint errors (unused type, explicit any)

This ensures template variables like {{LIBRECHAT_OPENID_TOKEN}} and
{{LIBRECHAT_USER_OPENIDID}} are properly resolved in both custom endpoint
headers and Bedrock AgentCore runtime parameters.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* refactor: remove debug logging from OpenID token template feature

Removed excessive debug logging that was added during development to make
the PR more suitable for upstream review:

- Removed 7 debug statements from OpenAIClient.js
- Removed all console.log statements from packages/api/src/utils/env.ts
- Removed debug logging from bedrock/options.js
- Removed debug logging from custom/initialize.js
- Removed debug statement from AuthController.js

This reduces the changeset by ~50 lines while maintaining full functionality
of the OpenID federated token template variable feature.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* test(openid): add comprehensive unit tests for template variable substitution

- Add 34 unit tests for OIDC token utilities (oidc.spec.ts)
- Test coverage for token extraction, validation, and placeholder processing
- Integration tests for full OpenID token flow
- All tests pass with comprehensive edge case coverage

🤖 Generated with Claude Code
Co-Authored-By: Claude <noreply@anthropic.com>

* test: fix OpenID federated tokens test failures

- Add serverMetadata() mock to openid-client mock configuration
  * Fixes TypeError in openIdJwtStrategy.js where serverMetadata() was being called
  * Mock now returns jwks_uri and end_session_endpoint as expected by the code

- Update outdated initialize.spec.js test
  * Remove test expecting resolveHeaders call during initialization
  * Header resolution was refactored to be deferred until LLM request time
  * Update test to verify options are returned correctly with useLegacyContent flag

Fixes #9931 CI failures for backend unit tests

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* chore: fix package-lock.json conflict

* chore: sync package-log with upstream

* chore: cleanup

* fix: use createSafeUser

* fix: fix createSafeUser signature

* chore: remove comments

* chore: purge comments

* fix: update Jest testPathPattern to testPathPatterns for Jest 30+ compatibility

---------

Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Denis Ramic <denis.ramic@nfon.com>
Co-authored-by: kristjanaapro <kristjana@apro.is>

chore: import order and add back JSDoc for OpenID JWT callback
2025-11-21 09:51:11 -05:00
Danny Avila
f5e079742a chore: reorganize client files for docker 2023-03-06 10:47:06 -05:00
Danny Avila
fca546af63 reorganize dirs for dockerize 2023-03-06 10:15:07 -05:00
Danny Avila
6ae154cc42 optimize convo paginations 2023-03-06 08:58:52 -05:00
Danny Avila
c617b15bcd disable prompts until complete 2023-03-06 08:50:36 -05:00
Daniel Avila
8d60cbdc7a feat: dynamic custom model saving/updating in dialog 2023-03-05 17:02:00 -05:00
Daniel Avila
15f3972b9f remove models caching 2023-03-05 15:30:40 -05:00
Daniel Avila
a98367d27f convo fetching in increments of 12 2023-03-05 14:41:50 -05:00
Daniel Avila
dae7f8d822 match more official stylings 2023-03-05 13:31:12 -05:00
Daniel Avila
fc899da944 convo change also changes model selection 2023-03-04 21:10:45 -05:00
Daniel Avila
d64f914552 complete customGpt 2023-03-04 20:48:59 -05:00
Daniel Avila
b4b0c123ba prompt prefix and label state changes when customGpt selected 2023-03-04 18:00:12 -05:00
Daniel Avila
9c3a78f96b customGpts persist through localStorage 2023-03-04 17:39:06 -05:00
Daniel Avila
62bb6ea8f8 refactor model menu items into component 2023-03-03 21:33:09 -05:00
Daniel Avila
42c74f254c reduce stylesheet clutter 2023-03-03 21:31:05 -05:00
Danny Avila
9a56130750 save custom settings in progress 2023-03-03 16:33:02 -05:00
Danny Avila
214228542a complete customChatGpt model selection 2023-03-03 15:52:06 -05:00
Danny Avila
92648554ad complete copyCode for code blocks 2023-03-03 09:45:20 -05:00
Danny Avila
ec7aaf01a4 add hoverbuttons 2023-03-03 08:51:33 -05:00
Danny Avila
50ff96cbc5 copy code in progress 2023-03-02 16:31:00 -05:00
Danny Avila
d235bb7b1b complete language detection for code blocks 2023-03-02 16:07:36 -05:00
Daniel Avila
897c384ac9 fix code parsing bug 2023-03-01 21:59:13 -05:00
Daniel Avila
1f270d349a add official api, remove davinci, organize root dir 2023-03-01 21:39:57 -05:00
Daniel Avila
1bba86f2e2 minor changes 2023-02-26 14:10:44 -05:00
Daniel Avila
8e5148b390 fix embed wrapper not invoking immediately 2023-02-25 10:33:26 -05:00
Daniel Avila
0ee31f0443 fix codeWrapper issue, will produce funky text with ` commenting 2023-02-25 10:16:21 -05:00
Daniel Avila
ed699d2c06 wrap code in progress, revert old message handling 2023-02-25 09:06:26 -05:00
Daniel Avila
78c9f3dc74 trying different markdown library for proper list styling 2023-02-24 23:16:19 -05:00
Danny Avila
61d898c387 fix bing chat not persisting after 1st invocation 2023-02-24 16:20:23 -05:00
Daniel Avila
fd01fd3334 adds markdown support, fix code wrapping 2023-02-24 11:24:09 -05:00
Daniel Avila
c58a9bbe93 finish highlight.js styling (for chatgpt) 2023-02-23 23:56:55 -05:00
Danny Avila
e95e22de15 code highlighting in progress 2023-02-23 16:32:08 -05:00
Daniel Avila
187f7b5b03 handles in-line code in model response text (not multi-line yet) 2023-02-22 22:42:22 -05:00
Daniel Avila
1a6cddb8bb persist models in localStorage, minor changes 2023-02-22 21:30:48 -05:00
Daniel Avila
a516b38e27 adds linting, fix linter errors, abort automatic scrolling 2023-02-21 21:31:36 -05:00
Daniel Avila
16932b37c0 fix bingai bugs and add chatgptbrowser client 2023-02-21 21:30:56 -05:00
Daniel Avila
168d5e8075 fix linter errors 2023-02-21 19:40:28 -05:00
Daniel Avila
7b7548c905 persist bing ai conversation 2023-02-20 21:52:08 -05:00
Daniel Avila
81e52fed73 properly records bing conversations 2023-02-20 21:16:40 -05:00
Daniel Avila
b4e22936ba update schema for bing options 2023-02-19 21:06:21 -05:00
Danny Avila
ac2e897709 adds og bing icon 2023-02-16 14:06:18 -05:00
Danny Avila
5c7000c273 adds bing ai functionality, still need to code out message/convo storing since it returns a lot of useful parameters 2023-02-15 12:29:56 -05:00
Daniel Avila
f18317f7b7 useDocumentTitle changes title on first title change, scrollBar renders when expected 2023-02-14 21:23:14 -05:00
Daniel Avila
0833d795bd readme update and footer link update 2023-02-14 18:40:00 -05:00
Danny Avila
742073fb89 refactor convos fetching to nav components, results in faster loading 2023-02-14 16:15:45 -05:00
Daniel Avila
e4d1ff2523 rearrange convo fetching for performance in progress 2023-02-13 23:14:35 -05:00
Daniel Avila
c00a2c902b front and backend logic for model switching 2023-02-13 21:15:28 -05:00
Daniel Avila
a5afd5c48f animates scroll to bottom and debounces bottom 2023-02-13 20:13:59 -05:00
Daniel Avila
779f142058 fix static flag bug 2023-02-13 18:02:29 -05:00
Danny Avila
92b2109dc3 adds scrolltobottom button, still handling initial state when scrollbar is present 2023-02-13 16:31:18 -05:00