mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-06-19 19:45:47 +00:00
* feat: Mint CodeAPI auth tokens * style: Format CodeAPI download route * fix: Prune CodeAPI token cache * fix: Propagate CodeAPI managed auth * test: Mock CodeAPI auth in traversal suite * fix: Pass auth context to invoked skill cache * feat: Mint CodeAPI plan context * chore: Refresh CodeAPI auth guidance * fix: Guard OpenID JWT fallback * fix: Default CodeAPI JWT tenant in single-tenant mode * chore: Update @librechat/agents to version 3.1.84 in package-lock.json and package.json files * chore: Standardize references to Code API in comments and tests
50 lines
1.9 KiB
JavaScript
50 lines
1.9 KiB
JavaScript
const cookies = require('cookie');
|
|
const passport = require('passport');
|
|
const { isEnabled, tenantContextMiddleware } = require('@librechat/api');
|
|
|
|
const hasPassportStrategy = (strategy) =>
|
|
typeof passport._strategy === 'function' && passport._strategy(strategy) != null;
|
|
|
|
/**
|
|
* Custom Middleware to handle JWT authentication, with support for OpenID token reuse.
|
|
* Switches between JWT and OpenID authentication based on cookies and environment settings.
|
|
*
|
|
* After successful authentication (req.user populated), automatically chains into
|
|
* `tenantContextMiddleware` to propagate `req.user.tenantId` into AsyncLocalStorage
|
|
* for downstream Mongoose tenant isolation.
|
|
*/
|
|
const requireJwtAuth = (req, res, next) => {
|
|
const cookieHeader = req.headers.cookie;
|
|
const tokenProvider = cookieHeader ? cookies.parse(cookieHeader).token_provider : null;
|
|
const openidReuseEnabled = isEnabled(process.env.OPENID_REUSE_TOKENS);
|
|
const openidJwtAvailable = openidReuseEnabled && hasPassportStrategy('openidJwt');
|
|
const strategies =
|
|
tokenProvider === 'openid' && openidJwtAvailable
|
|
? ['openidJwt', 'jwt']
|
|
: ['jwt', ...(openidJwtAvailable ? ['openidJwt'] : [])];
|
|
|
|
const authenticateWithStrategy = (index) => {
|
|
const strategy = strategies[index];
|
|
passport.authenticate(strategy, { session: false }, (err, user, info, status) => {
|
|
if (err) {
|
|
return next(err);
|
|
}
|
|
if (!user) {
|
|
if (index + 1 < strategies.length) {
|
|
return authenticateWithStrategy(index + 1);
|
|
}
|
|
return res.status(status || 401).json({
|
|
message: info?.message || 'Unauthorized',
|
|
});
|
|
}
|
|
req.user = user;
|
|
req.authStrategy = strategy;
|
|
// req.user is now populated by passport — set up tenant ALS context
|
|
tenantContextMiddleware(req, res, next);
|
|
})(req, res, next);
|
|
};
|
|
|
|
authenticateWithStrategy(0);
|
|
};
|
|
|
|
module.exports = requireJwtAuth;
|