mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-08-28 12:44:28 +00:00
* feat: make OpenID token reuse window configurable via OPENID_REUSE_MAX_SESSION_AGE_MS
The OpenID session-token reuse window in AuthController was a hardcoded 15-minute
constant, forcing /api/auth/refresh to perform a real refreshTokenGrant against the
IdP every 15 minutes even when the current access token is still valid. IdPs that
rotate and revoke the previous access token on refresh then invalidate a token that
is still in use by downstream consumers of the reused OpenID token (e.g. MCP servers
that receive {{LIBRECHAT_OPENID_TOKEN}} and introspect the bearer), producing
~15-minute 401 cycles regardless of the access token's actual lifetime.
Read the window from process.env.OPENID_REUSE_MAX_SESSION_AGE_MS via the existing
math() helper, so it accepts an arithmetic expression like SESSION_EXPIRY (e.g.
60 * 60 * 24 * 1000), defaulting to the existing 15 minutes so behavior is unchanged
unless explicitly configured. The existing 30s-before-expiry guard still forces a
refresh before genuine expiry, so a larger window remains safe.
* fix: extend OpenID reuse session lifetime
---------
Co-authored-by: Danny Avila <danny@librechat.ai>
136 lines
4 KiB
JavaScript
136 lines
4 KiB
JavaScript
const passport = require('passport');
|
|
const session = require('express-session');
|
|
const { CacheKeys } = require('librechat-data-provider');
|
|
const { math, isEnabled, shouldUseSecureCookie } = require('@librechat/api');
|
|
const { logger, DEFAULT_SESSION_EXPIRY } = require('@librechat/data-schemas');
|
|
const {
|
|
openIdJwtLogin,
|
|
facebookLogin,
|
|
facebookAdminLogin,
|
|
discordLogin,
|
|
discordAdminLogin,
|
|
setupOpenId,
|
|
googleLogin,
|
|
googleAdminLogin,
|
|
githubLogin,
|
|
githubAdminLogin,
|
|
appleLogin,
|
|
appleAdminLogin,
|
|
setupSaml,
|
|
} = require('~/strategies');
|
|
const { getLogStores } = require('~/cache');
|
|
|
|
const DEFAULT_OPENID_REUSE_MAX_SESSION_AGE_MS = 15 * 60 * 1000;
|
|
|
|
const getSessionExpiry = () => math(process.env.SESSION_EXPIRY, DEFAULT_SESSION_EXPIRY);
|
|
|
|
const getOpenIdSessionExpiry = () => {
|
|
const sessionExpiry = getSessionExpiry();
|
|
if (!isEnabled(process.env.OPENID_REUSE_TOKENS)) {
|
|
return sessionExpiry;
|
|
}
|
|
|
|
const reuseMaxSessionAge = math(
|
|
process.env.OPENID_REUSE_MAX_SESSION_AGE_MS,
|
|
DEFAULT_OPENID_REUSE_MAX_SESSION_AGE_MS,
|
|
);
|
|
return Math.max(sessionExpiry, reuseMaxSessionAge);
|
|
};
|
|
|
|
/**
|
|
* Configures OpenID Connect for the application.
|
|
* @param {Express.Application} app - The Express application instance.
|
|
* @returns {Promise<void>}
|
|
*/
|
|
async function configureOpenId(app) {
|
|
logger.info('Configuring OpenID Connect...');
|
|
const sessionExpiry = getOpenIdSessionExpiry();
|
|
const sessionOptions = {
|
|
secret: process.env.OPENID_SESSION_SECRET,
|
|
resave: false,
|
|
saveUninitialized: false,
|
|
store: getLogStores(CacheKeys.OPENID_SESSION),
|
|
cookie: {
|
|
maxAge: sessionExpiry,
|
|
secure: shouldUseSecureCookie(),
|
|
},
|
|
};
|
|
app.use(session(sessionOptions));
|
|
app.use(passport.session());
|
|
|
|
const config = await setupOpenId();
|
|
if (!config) {
|
|
logger.error('OpenID Connect configuration failed - strategy not registered.');
|
|
return;
|
|
}
|
|
|
|
if (isEnabled(process.env.OPENID_REUSE_TOKENS)) {
|
|
logger.info('OpenID token reuse is enabled.');
|
|
passport.use('openidJwt', openIdJwtLogin(config));
|
|
}
|
|
logger.info('OpenID Connect configured successfully.');
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param {Express.Application} app
|
|
*/
|
|
const configureSocialLogins = async (app) => {
|
|
logger.info('Configuring social logins...');
|
|
|
|
if (process.env.GOOGLE_CLIENT_ID && process.env.GOOGLE_CLIENT_SECRET) {
|
|
passport.use(googleLogin());
|
|
passport.use('googleAdmin', googleAdminLogin());
|
|
}
|
|
if (process.env.FACEBOOK_CLIENT_ID && process.env.FACEBOOK_CLIENT_SECRET) {
|
|
passport.use(facebookLogin());
|
|
passport.use('facebookAdmin', facebookAdminLogin());
|
|
}
|
|
if (process.env.GITHUB_CLIENT_ID && process.env.GITHUB_CLIENT_SECRET) {
|
|
passport.use(githubLogin());
|
|
passport.use('githubAdmin', githubAdminLogin());
|
|
}
|
|
if (process.env.DISCORD_CLIENT_ID && process.env.DISCORD_CLIENT_SECRET) {
|
|
passport.use(discordLogin());
|
|
passport.use('discordAdmin', discordAdminLogin());
|
|
}
|
|
if (process.env.APPLE_CLIENT_ID && process.env.APPLE_PRIVATE_KEY_PATH) {
|
|
passport.use(appleLogin());
|
|
passport.use('appleAdmin', appleAdminLogin());
|
|
}
|
|
if (
|
|
process.env.OPENID_CLIENT_ID &&
|
|
(isEnabled(process.env.OPENID_USE_PKCE) || process.env.OPENID_CLIENT_SECRET?.trim()) &&
|
|
process.env.OPENID_ISSUER &&
|
|
process.env.OPENID_SCOPE &&
|
|
process.env.OPENID_SESSION_SECRET
|
|
) {
|
|
await configureOpenId(app);
|
|
}
|
|
if (
|
|
process.env.SAML_ENTRY_POINT &&
|
|
process.env.SAML_ISSUER &&
|
|
process.env.SAML_CERT &&
|
|
process.env.SAML_SESSION_SECRET
|
|
) {
|
|
logger.info('Configuring SAML Connect...');
|
|
const sessionExpiry = getSessionExpiry();
|
|
const sessionOptions = {
|
|
secret: process.env.SAML_SESSION_SECRET,
|
|
resave: false,
|
|
saveUninitialized: false,
|
|
store: getLogStores(CacheKeys.SAML_SESSION),
|
|
cookie: {
|
|
maxAge: sessionExpiry,
|
|
secure: shouldUseSecureCookie(),
|
|
},
|
|
};
|
|
app.use(session(sessionOptions));
|
|
app.use(passport.session());
|
|
setupSaml();
|
|
|
|
logger.info('SAML Connect configured.');
|
|
}
|
|
};
|
|
|
|
module.exports = configureSocialLogins;
|