This commit is contained in:
Erik 2022-11-09 20:33:51 +02:00
parent 664127cdbc
commit 743619e4cf
Signed by: Navy.gif
GPG Key ID: 811EC0CD80E7E5FB
2 changed files with 17 additions and 9 deletions

View File

@ -52,7 +52,7 @@ class Server extends EventEmitter {
this.mariadb = new MariaDB(this, { options: databases.mariadb, MARIA_HOST, MARIA_USER, MARIA_PORT, MARIA_PASS, MARIA_DB }); this.mariadb = new MariaDB(this, { options: databases.mariadb, MARIA_HOST, MARIA_USER, MARIA_PORT, MARIA_PASS, MARIA_DB });
this.mongodb = new MongoDB(this, { options: databases.mongodb, MONGO_HOST, MONGO_USER, MONGO_PORT, MONGO_PASS, MONGO_DB }); this.mongodb = new MongoDB(this, { options: databases.mongodb, MONGO_HOST, MONGO_USER, MONGO_PORT, MONGO_PASS, MONGO_DB });
this.userDatabase = new UserDatabase(this, this.mongodb, { validUserTypes }); this.userDatabase = new UserDatabase(this, this.mongodb, { validUserTypes });
this.authenticator = new Authenticator(this, this.app, this.userDatabase, { this.authenticator = new Authenticator(this, this.userDatabase, {
mongo: this.mongodb, mongo: this.mongodb,
secret: SECRET, secret: SECRET,
discordID: DISCORD_ID, discordID: DISCORD_ID,
@ -92,7 +92,7 @@ class Server extends EventEmitter {
this.userDatabase.init(); this.userDatabase.init();
this.logger.info('Loading endpoints'); this.logger.info('Loading endpoints');
this.registry.loadEndpoints(); await this.registry.loadEndpoints();
this.logger.debug(this.registry.print); this.logger.debug(this.registry.print);
this.logger.info('Creating http server'); this.logger.info('Creating http server');

View File

@ -27,7 +27,7 @@ class Authenticator {
* } * }
* @memberof Authenticator * @memberof Authenticator
*/ */
constructor (server, express, users, { constructor (server, users, {
mongo, secret, discordID, discordSecret, callbackURL, discordScope, discordVersion, mongo, secret, discordID, discordSecret, callbackURL, discordScope, discordVersion,
cookie = { } cookie = { }
}) { }) {
@ -41,7 +41,7 @@ class Authenticator {
cookie = { maxAge: 0.5 * 24 * 60 * 60 * 1000, secure: false, ...cookie }; cookie = { maxAge: 0.5 * 24 * 60 * 60 * 1000, secure: false, ...cookie };
cookie.secure = cookie.secure && process.env.NODE_ENV !== 'development'; cookie.secure = cookie.secure && process.env.NODE_ENV !== 'development';
express.use(session({ server.app.use(session({
cookie, cookie,
store: MongoStore.create({ client: mongo.client, dbName: mongo.database, touchAfter: 600 }), store: MongoStore.create({ client: mongo.client, dbName: mongo.database, touchAfter: 600 }),
secret, secret,
@ -49,8 +49,8 @@ class Authenticator {
saveUninitialized: true saveUninitialized: true
})); }));
express.use(Passport.initialize()); server.app.use(Passport.initialize());
express.use(Passport.session()); server.app.use(Passport.session());
Passport.serializeUser((user, callback) => { Passport.serializeUser((user, callback) => {
callback(null, user.id); callback(null, user.id);
@ -61,7 +61,8 @@ class Authenticator {
callback(null, user); callback(null, user);
}); });
Passport.use(new Strategy({ // TODO: Should probably allow injection of strategies instead of hardcoding this this through a function
Passport.use('discord', new Strategy({
clientID: discordID, clientSecret: discordSecret, callbackURL, scope: discordScope, version: discordVersion clientID: discordID, clientSecret: discordSecret, callbackURL, scope: discordScope, version: discordVersion
}, async (accessToken, refreshToken, profile, callback) => { }, async (accessToken, refreshToken, profile, callback) => {
this.logger.info(`${profile.username} (${profile.id}) is logging in.`); this.logger.info(`${profile.username} (${profile.id}) is logging in.`);
@ -71,12 +72,19 @@ class Authenticator {
} }
// For API requests, does not redirect to a login page
async authenticate (req, res, next) { async authenticate (req, res, next) {
if (this._authenticate(req, res)) return next(); if (await this._authenticate(req, res)) return next();
} }
// Meant for non-api paths
authenticateRedirect (req, res, next) {
if (!req.isAuthenticated()) return res.redirect('/login');
next();
}
async _authenticate (req, res) { async _authenticate (req, res) {
if (req.isAuthenticated()) return true; if (req.isAuthenticated()) return true;
@ -102,7 +110,7 @@ class Authenticator {
* Authorisation implicitly checks for authentication * Authorisation implicitly checks for authentication
* *
* @param {*} permission * @param {*} permission
* @return {*} * @return {Function}
* @memberof Authenticator * @memberof Authenticator
*/ */
createAuthoriser (permission) { createAuthoriser (permission) {