mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-20 15:02:02 +00:00
Some checks are pending
Backend Unit Tests / Build packages (push) Waiting to run
Backend Unit Tests / Codegraph select (push) Waiting to run
Backend Unit Tests / TypeScript type checks (push) Blocked by required conditions
Backend Unit Tests / Circular dependency checks (push) Waiting to run
Backend Unit Tests / Tests: api (shard 1/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: api (shard 2/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: api (shard 3/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: data-provider (push) Blocked by required conditions
Backend Unit Tests / Tests: data-schemas (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 1/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 2/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 3/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 4/4) (push) Blocked by required conditions
Codegraph E2E Vote Gate / Gate (push) Waiting to run
Codegraph E2E Votes / vote (shard 1/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 10/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 2/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 3/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 4/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 5/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 6/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 7/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 8/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 9/10) (push) Waiting to run
Docker Dev Branch Images Build / publish (push) Waiting to run
Frontend Unit Tests / Client build recovery regression (push) Waiting to run
Frontend Unit Tests / Codegraph select (push) Waiting to run
Frontend Unit Tests / Build packages (push) Waiting to run
Frontend Unit Tests / TypeScript type checks (client) (push) Blocked by required conditions
Frontend Unit Tests / Tests: @librechat/chat (push) Blocked by required conditions
Frontend Unit Tests / Tests: @librechat/client (push) Blocked by required conditions
Frontend Unit Tests / Tests: Ubuntu (shard 1/2) (push) Blocked by required conditions
Frontend Unit Tests / Tests: Ubuntu (shard 2/2) (push) Blocked by required conditions
Frontend Unit Tests / Vite build verification (push) Blocked by required conditions
Sync Locize Translations & Create Translation PR / Sync Translation Keys with Locize (push) Waiting to run
Sync Locize Translations & Create Translation PR / Create Translation PR on Version Published (push) Blocked by required conditions
* fix: restore strict tenant startup and sign-in * test: preserve real middleware in auth route fixtures --------- Co-authored-by: Lia <lia@librechat.ai>
241 lines
5.1 KiB
JavaScript
241 lines
5.1 KiB
JavaScript
// file deepcode ignore NoRateLimitingForLogin: Rate limiting is handled by the `loginLimiter` middleware
|
|
const express = require('express');
|
|
const passport = require('passport');
|
|
const { randomState } = require('openid-client');
|
|
const { logger } = require('@librechat/data-schemas');
|
|
const { ErrorTypes } = require('librechat-data-provider');
|
|
const {
|
|
buildOAuthFailureLog,
|
|
TENANT_CONTEXT_REQUIRED,
|
|
createPreAuthTenantGuard,
|
|
requirePreAuthTenantContext,
|
|
createOpenIDCallbackAuthenticator,
|
|
createSetBalanceConfig,
|
|
getOAuthFailureMessage,
|
|
redirectToAuthFailure,
|
|
} = require('@librechat/api');
|
|
const {
|
|
checkDomainAllowed,
|
|
loginLimiter,
|
|
logHeaders,
|
|
markOAuthNavigation,
|
|
} = require('~/server/middleware');
|
|
const { createOAuthHandler } = require('~/server/controllers/auth/oauth');
|
|
const { findBalanceByUser, upsertBalanceFields } = require('~/models');
|
|
const { getAppConfig } = require('~/server/services/Config');
|
|
|
|
const setBalanceConfig = createSetBalanceConfig({
|
|
getAppConfig,
|
|
findBalanceByUser,
|
|
upsertBalanceFields,
|
|
});
|
|
|
|
const router = express.Router();
|
|
|
|
const domains = {
|
|
client: process.env.DOMAIN_CLIENT,
|
|
server: process.env.DOMAIN_SERVER,
|
|
};
|
|
|
|
const authFailureRedirectOptions = {
|
|
clientDomain: domains.client,
|
|
authFailedError: ErrorTypes.AUTH_FAILED,
|
|
};
|
|
|
|
router.use(logHeaders);
|
|
router.use(markOAuthNavigation);
|
|
router.use(loginLimiter);
|
|
|
|
const requireCallbackTenant = createPreAuthTenantGuard({
|
|
failureRedirect: `${domains.client}/login?redirect=false&error=${ErrorTypes.AUTH_FAILED}&code=${TENANT_CONTEXT_REQUIRED}`,
|
|
});
|
|
const oauthHandler = createOAuthHandler();
|
|
const authenticateOpenIDCallback = createOpenIDCallbackAuthenticator({
|
|
passport,
|
|
logger,
|
|
...authFailureRedirectOptions,
|
|
});
|
|
|
|
router.get('/error', (req, res) => {
|
|
/** A single error message is pushed by passport when authentication fails. */
|
|
const errorMessage = getOAuthFailureMessage(req);
|
|
logger.warn(
|
|
'[OAuth] Authentication failed',
|
|
buildOAuthFailureLog({
|
|
provider: 'unknown',
|
|
req,
|
|
info: { message: errorMessage },
|
|
defaultMessage: errorMessage,
|
|
}),
|
|
);
|
|
|
|
redirectToAuthFailure(res, authFailureRedirectOptions);
|
|
});
|
|
|
|
/**
|
|
* Google Routes
|
|
*/
|
|
router.get(
|
|
'/google',
|
|
requirePreAuthTenantContext,
|
|
passport.authenticate('google', {
|
|
scope: ['openid', 'profile', 'email'],
|
|
session: false,
|
|
}),
|
|
);
|
|
|
|
router.get(
|
|
'/google/callback',
|
|
requireCallbackTenant,
|
|
passport.authenticate('google', {
|
|
failureRedirect: `${domains.client}/oauth/error`,
|
|
session: false,
|
|
scope: ['openid', 'profile', 'email'],
|
|
}),
|
|
setBalanceConfig,
|
|
checkDomainAllowed,
|
|
oauthHandler,
|
|
);
|
|
|
|
/**
|
|
* Facebook Routes
|
|
*/
|
|
router.get(
|
|
'/facebook',
|
|
requirePreAuthTenantContext,
|
|
passport.authenticate('facebook', {
|
|
scope: ['public_profile'],
|
|
profileFields: ['id', 'email', 'name'],
|
|
session: false,
|
|
}),
|
|
);
|
|
|
|
router.get(
|
|
'/facebook/callback',
|
|
requireCallbackTenant,
|
|
passport.authenticate('facebook', {
|
|
failureRedirect: `${domains.client}/oauth/error`,
|
|
session: false,
|
|
scope: ['public_profile'],
|
|
profileFields: ['id', 'email', 'name'],
|
|
}),
|
|
setBalanceConfig,
|
|
checkDomainAllowed,
|
|
oauthHandler,
|
|
);
|
|
|
|
/**
|
|
* OpenID Routes
|
|
*/
|
|
router.get('/openid', requirePreAuthTenantContext, (req, res, next) => {
|
|
return passport.authenticate('openid', {
|
|
session: false,
|
|
state: randomState(),
|
|
})(req, res, next);
|
|
});
|
|
|
|
router.get(
|
|
'/openid/callback',
|
|
requireCallbackTenant,
|
|
authenticateOpenIDCallback,
|
|
setBalanceConfig,
|
|
checkDomainAllowed,
|
|
oauthHandler,
|
|
);
|
|
|
|
/**
|
|
* GitHub Routes
|
|
*/
|
|
router.get(
|
|
'/github',
|
|
requirePreAuthTenantContext,
|
|
passport.authenticate('github', {
|
|
scope: ['user:email', 'read:user'],
|
|
session: false,
|
|
}),
|
|
);
|
|
|
|
router.get(
|
|
'/github/callback',
|
|
requireCallbackTenant,
|
|
passport.authenticate('github', {
|
|
failureRedirect: `${domains.client}/oauth/error`,
|
|
session: false,
|
|
scope: ['user:email', 'read:user'],
|
|
}),
|
|
setBalanceConfig,
|
|
checkDomainAllowed,
|
|
oauthHandler,
|
|
);
|
|
|
|
/**
|
|
* Discord Routes
|
|
*/
|
|
router.get(
|
|
'/discord',
|
|
requirePreAuthTenantContext,
|
|
passport.authenticate('discord', {
|
|
scope: ['identify', 'email'],
|
|
session: false,
|
|
}),
|
|
);
|
|
|
|
router.get(
|
|
'/discord/callback',
|
|
requireCallbackTenant,
|
|
passport.authenticate('discord', {
|
|
failureRedirect: `${domains.client}/oauth/error`,
|
|
session: false,
|
|
scope: ['identify', 'email'],
|
|
}),
|
|
setBalanceConfig,
|
|
checkDomainAllowed,
|
|
oauthHandler,
|
|
);
|
|
|
|
/**
|
|
* Apple Routes
|
|
*/
|
|
router.get(
|
|
'/apple',
|
|
requirePreAuthTenantContext,
|
|
passport.authenticate('apple', {
|
|
session: false,
|
|
}),
|
|
);
|
|
|
|
router.post(
|
|
'/apple/callback',
|
|
requireCallbackTenant,
|
|
passport.authenticate('apple', {
|
|
failureRedirect: `${domains.client}/oauth/error`,
|
|
session: false,
|
|
}),
|
|
setBalanceConfig,
|
|
checkDomainAllowed,
|
|
oauthHandler,
|
|
);
|
|
|
|
/**
|
|
* SAML Routes
|
|
*/
|
|
router.get(
|
|
'/saml',
|
|
requirePreAuthTenantContext,
|
|
passport.authenticate('saml', {
|
|
session: false,
|
|
}),
|
|
);
|
|
|
|
router.post(
|
|
'/saml/callback',
|
|
requireCallbackTenant,
|
|
passport.authenticate('saml', {
|
|
failureRedirect: `${domains.client}/oauth/error`,
|
|
failureMessage: true,
|
|
session: false,
|
|
}),
|
|
oauthHandler,
|
|
);
|
|
|
|
module.exports = router;
|