mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 17:00:15 +01:00
refactor: improve passport strategy handling in async/await manner to prevent race conditions upon importing modules (#682)
This commit is contained in:
parent
e38483a8b9
commit
6943f1c2c7
12 changed files with 273 additions and 246 deletions
|
|
@ -51,7 +51,9 @@
|
|||
"openai": "^3.2.1",
|
||||
"openid-client": "^5.4.2",
|
||||
"passport": "^0.6.0",
|
||||
"passport-discord": "^0.1.4",
|
||||
"passport-facebook": "^3.0.0",
|
||||
"passport-github2": "^0.1.12",
|
||||
"passport-google-oauth20": "^2.0.0",
|
||||
"passport-jwt": "^4.0.1",
|
||||
"passport-local": "^1.0.0",
|
||||
|
|
|
|||
|
|
@ -11,6 +11,15 @@ const passport = require('passport');
|
|||
const port = process.env.PORT || 3080;
|
||||
const host = process.env.HOST || 'localhost';
|
||||
const projectPath = path.join(__dirname, '..', '..', 'client');
|
||||
const {
|
||||
jwtLogin,
|
||||
passportLogin,
|
||||
googleLogin,
|
||||
githubLogin,
|
||||
discordLogin,
|
||||
facebookLogin,
|
||||
setupOpenId,
|
||||
} = require('../strategies');
|
||||
|
||||
// Init the config and validate it
|
||||
const config = require('../../config/loader');
|
||||
|
|
@ -40,19 +49,19 @@ config.validate(); // Validate the config
|
|||
|
||||
// OAUTH
|
||||
app.use(passport.initialize());
|
||||
require('../strategies/jwtStrategy');
|
||||
require('../strategies/localStrategy');
|
||||
passport.use(await jwtLogin());
|
||||
passport.use(await passportLogin());
|
||||
if (process.env.GOOGLE_CLIENT_ID && process.env.GOOGLE_CLIENT_SECRET) {
|
||||
require('../strategies/googleStrategy');
|
||||
passport.use(await googleLogin());
|
||||
}
|
||||
if (process.env.FACEBOOK_CLIENT_ID && process.env.FACEBOOK_CLIENT_SECRET) {
|
||||
require('../strategies/facebookStrategy');
|
||||
passport.use(await facebookLogin());
|
||||
}
|
||||
if (process.env.GITHUB_CLIENT_ID && process.env.GITHUB_CLIENT_SECRET) {
|
||||
require('../strategies/githubStrategy');
|
||||
passport.use(await githubLogin());
|
||||
}
|
||||
if (process.env.DISCORD_CLIENT_ID && process.env.DISCORD_CLIENT_SECRET) {
|
||||
require('../strategies/discordStrategy');
|
||||
passport.use(await discordLogin());
|
||||
}
|
||||
if (
|
||||
process.env.OPENID_CLIENT_ID &&
|
||||
|
|
@ -69,7 +78,7 @@ config.validate(); // Validate the config
|
|||
}),
|
||||
);
|
||||
app.use(passport.session());
|
||||
require('../strategies/openidStrategy');
|
||||
await setupOpenId();
|
||||
}
|
||||
app.use('/oauth', routes.oauth);
|
||||
// api endpoint
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
const passport = require('passport');
|
||||
const { Strategy: DiscordStrategy } = require('passport-discord');
|
||||
const User = require('../models/User');
|
||||
const config = require('../../config/loader');
|
||||
const domains = config.domains;
|
||||
|
||||
const discordLogin = new DiscordStrategy(
|
||||
const discordLogin = async () =>
|
||||
new DiscordStrategy(
|
||||
{
|
||||
clientID: process.env.DISCORD_CLIENT_ID,
|
||||
clientSecret: process.env.DISCORD_CLIENT_SECRET,
|
||||
|
|
@ -46,6 +46,6 @@ const discordLogin = new DiscordStrategy(
|
|||
cb(err);
|
||||
}
|
||||
},
|
||||
);
|
||||
);
|
||||
|
||||
passport.use(discordLogin);
|
||||
module.exports = discordLogin;
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
const passport = require('passport');
|
||||
const FacebookStrategy = require('passport-facebook').Strategy;
|
||||
const User = require('../models/User');
|
||||
const config = require('../../config/loader');
|
||||
const domains = config.domains;
|
||||
|
||||
// facebook strategy
|
||||
const facebookLogin = new FacebookStrategy(
|
||||
const facebookLogin = async () =>
|
||||
new FacebookStrategy(
|
||||
{
|
||||
clientID: process.env.FACEBOOK_APP_ID,
|
||||
clientSecret: process.env.FACEBOOK_SECRET,
|
||||
|
|
@ -54,6 +54,6 @@ const facebookLogin = new FacebookStrategy(
|
|||
console.log(err);
|
||||
}
|
||||
},
|
||||
);
|
||||
);
|
||||
|
||||
passport.use(facebookLogin);
|
||||
module.exports = facebookLogin;
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
const passport = require('passport');
|
||||
const { Strategy: GitHubStrategy } = require('passport-github2');
|
||||
const config = require('../../config/loader');
|
||||
const domains = config.domains;
|
||||
|
|
@ -6,7 +5,8 @@ const domains = config.domains;
|
|||
const User = require('../models/User');
|
||||
|
||||
// GitHub strategy
|
||||
const githubLogin = new GitHubStrategy(
|
||||
const githubLogin = async () =>
|
||||
new GitHubStrategy(
|
||||
{
|
||||
clientID: process.env.GITHUB_CLIENT_ID,
|
||||
clientSecret: process.env.GITHUB_CLIENT_SECRET,
|
||||
|
|
@ -42,6 +42,6 @@ const githubLogin = new GitHubStrategy(
|
|||
cb(err);
|
||||
}
|
||||
},
|
||||
);
|
||||
);
|
||||
|
||||
passport.use(githubLogin);
|
||||
module.exports = githubLogin;
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
const passport = require('passport');
|
||||
const { Strategy: GoogleStrategy } = require('passport-google-oauth20');
|
||||
const config = require('../../config/loader');
|
||||
const domains = config.domains;
|
||||
|
|
@ -6,7 +5,8 @@ const domains = config.domains;
|
|||
const User = require('../models/User');
|
||||
|
||||
// google strategy
|
||||
const googleLogin = new GoogleStrategy(
|
||||
const googleLogin = async () =>
|
||||
new GoogleStrategy(
|
||||
{
|
||||
clientID: process.env.GOOGLE_CLIENT_ID,
|
||||
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
|
||||
|
|
@ -38,6 +38,6 @@ const googleLogin = new GoogleStrategy(
|
|||
console.log(err);
|
||||
}
|
||||
},
|
||||
);
|
||||
);
|
||||
|
||||
passport.use(googleLogin);
|
||||
module.exports = googleLogin;
|
||||
|
|
|
|||
17
api/strategies/index.js
Normal file
17
api/strategies/index.js
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
const passportLogin = require('./localStrategy');
|
||||
const googleLogin = require('./googleStrategy');
|
||||
const githubLogin = require('./githubStrategy');
|
||||
const discordLogin = require('./discordStrategy');
|
||||
const jwtLogin = require('./jwtStrategy');
|
||||
const facebookLogin = require('./facebookStrategy');
|
||||
const setupOpenId = require('./openidStrategy');
|
||||
|
||||
module.exports = {
|
||||
passportLogin,
|
||||
googleLogin,
|
||||
githubLogin,
|
||||
discordLogin,
|
||||
jwtLogin,
|
||||
facebookLogin,
|
||||
setupOpenId,
|
||||
};
|
||||
|
|
@ -1,9 +1,9 @@
|
|||
const passport = require('passport');
|
||||
const { Strategy: JwtStrategy, ExtractJwt } = require('passport-jwt');
|
||||
const User = require('../models/User');
|
||||
|
||||
// JWT strategy
|
||||
const jwtLogin = new JwtStrategy(
|
||||
const jwtLogin = async () =>
|
||||
new JwtStrategy(
|
||||
{
|
||||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
||||
secretOrKey: process.env.JWT_SECRET,
|
||||
|
|
@ -21,6 +21,6 @@ const jwtLogin = new JwtStrategy(
|
|||
done(err, false);
|
||||
}
|
||||
},
|
||||
);
|
||||
);
|
||||
|
||||
passport.use(jwtLogin);
|
||||
module.exports = jwtLogin;
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
const passport = require('passport');
|
||||
const PassportLocalStrategy = require('passport-local').Strategy;
|
||||
|
||||
const User = require('../models/User');
|
||||
const { loginSchema } = require('./validators');
|
||||
const DebugControl = require('../utils/debug.js');
|
||||
|
||||
const passportLogin = new PassportLocalStrategy(
|
||||
const passportLogin = async () =>
|
||||
new PassportLocalStrategy(
|
||||
{
|
||||
usernameField: 'email',
|
||||
passwordField: 'password',
|
||||
|
|
@ -54,9 +54,7 @@ const passportLogin = new PassportLocalStrategy(
|
|||
return done(err);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
passport.use(passportLogin);
|
||||
);
|
||||
|
||||
function log({ title, parameters }) {
|
||||
DebugControl.log.functionName(title);
|
||||
|
|
@ -64,3 +62,5 @@ function log({ title, parameters }) {
|
|||
DebugControl.log.parameters(parameters);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = passportLogin;
|
||||
|
|
|
|||
|
|
@ -36,8 +36,9 @@ const downloadImage = async (url, imagePath, accessToken) => {
|
|||
}
|
||||
};
|
||||
|
||||
Issuer.discover(process.env.OPENID_ISSUER)
|
||||
.then((issuer) => {
|
||||
async function setupOpenId() {
|
||||
try {
|
||||
const issuer = await Issuer.discover(process.env.OPENID_ISSUER);
|
||||
const client = new issuer.Client({
|
||||
client_id: process.env.OPENID_CLIENT_ID,
|
||||
client_secret: process.env.OPENID_CLIENT_SECRET,
|
||||
|
|
@ -128,7 +129,9 @@ Issuer.discover(process.env.OPENID_ISSUER)
|
|||
);
|
||||
|
||||
passport.use('openid', openidLogin);
|
||||
})
|
||||
.catch((err) => {
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = setupOpenId;
|
||||
|
|
|
|||
7
package-lock.json
generated
7
package-lock.json
generated
|
|
@ -15,10 +15,7 @@
|
|||
"packages/*"
|
||||
],
|
||||
"dependencies": {
|
||||
"axios": "^1.4.0",
|
||||
"passport": "^0.6.0",
|
||||
"passport-discord": "^0.1.4",
|
||||
"passport-github2": "^0.1.12"
|
||||
"axios": "^1.4.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@playwright/test": "^1.32.1",
|
||||
|
|
@ -78,7 +75,9 @@
|
|||
"openai": "^3.2.1",
|
||||
"openid-client": "^5.4.2",
|
||||
"passport": "^0.6.0",
|
||||
"passport-discord": "^0.1.4",
|
||||
"passport-facebook": "^3.0.0",
|
||||
"passport-github2": "^0.1.12",
|
||||
"passport-google-oauth20": "^2.0.0",
|
||||
"passport-jwt": "^4.0.1",
|
||||
"passport-local": "^1.0.0",
|
||||
|
|
|
|||
|
|
@ -45,10 +45,7 @@
|
|||
},
|
||||
"homepage": "https://github.com/danny-avila/LibreChat#readme",
|
||||
"dependencies": {
|
||||
"axios": "^1.4.0",
|
||||
"passport": "^0.6.0",
|
||||
"passport-discord": "^0.1.4",
|
||||
"passport-github2": "^0.1.12"
|
||||
"axios": "^1.4.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@playwright/test": "^1.32.1",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue