option for not allowing creation of new accounts from oauth

This commit is contained in:
Erik 2022-11-25 15:43:26 +02:00
parent ea7f8351f2
commit 11b5ed396d
Signed by: Navy.gif
GPG Key ID: 811EC0CD80E7E5FB
5 changed files with 38 additions and 8 deletions

View File

@ -83,6 +83,8 @@ class Controller extends EventEmitter {
shard.on('ready', () => this.logger.info(`Shard ${shard.id} is ready`));
shard.on('disconnect', () => this.logger.warn(`Shard ${shard.id} has disconnected`));
shard.on('spawn', () => this.logger.info(`Shard ${shard.id} spawned`));
shard.on('error', (err) => this.logger.error(`Shard ${shard.id} ran into an error:\n${err.stack}`));
shard.on('warn', (msg) => this.logger.warn(`Warning from shard ${shard.id}: ${msg}`));
shard.on('message', (msg) => this._handleMessage(shard, msg));
}

View File

@ -24,6 +24,11 @@ class Shard extends EventEmitter {
this.process = null;
this.fatal = false;
// Keep track of crashes for preventing crash loops
this.crashes = [];
// Set in the spawn method
this.spawnedAt = null;
}
async spawn (waitForReady = false) {
@ -33,12 +38,16 @@ class Shard extends EventEmitter {
this.process = ChildProcess.fork(this.filePath, this.args, { env: { ...this.env, SHARD_ID: this.id }, execArgv: this.execArgv })
.on('message', this._handleMessage.bind(this))
.on('exit', () => this._handleExit())
.on('exit', () => {
this.crashes.push(Date.now() - this.spawnedAt);
this._handleExit();
})
.on('disconnect', this._handleDisconnect.bind(this)); // Don't know if this is going to help, but monitoring whether this gets called whenever a process on its own closes the IPC channel
this.process.once('spawn', () => {
this.emit('spawn');
this.process.send({ _start: this.serverOptions });
this.spawnedAt = Date.now();
});
if (!waitForReady) return;
@ -136,7 +145,20 @@ class Shard extends EventEmitter {
this.ready = false;
this.process = null;
if (respawn) this.spawn().catch(err => this.emit('error', err));
if (respawn) {
const len = this.crashes.length;
if (len > 2) {
const last3 = this.crashes.slice(len - 3);
const sum = last3.reduce((s, val) => {
s += val;
return s;
}, 0);
const avg = sum / 3;
// If average run duration is below 5 mins send a notification about potential crash loop
if (avg < 5 * 60 * 1000) this.emit('warn', `Potentially in a crash loop, average run time for the last 3 spawns: ${avg}`);
}
this.spawn().catch(err => this.emit('error', err));
}
}
}

View File

@ -199,8 +199,12 @@ class Server extends EventEmitter {
clientID: DISCORD_ID, clientSecret: DISCORD_SECRET, callbackURL: this.baseURL + callbackURL + '/discord', scope: discord.scope, version: discord.version
}, async (accessToken, refreshToken, profile, callback) => {
this.logger.info(`${profile.username} (${profile.id}) is logging in.`);
const user = await this.userDatabase.userFromDiscord(profile);
callback(null, user);
// We don't need this here
delete profile.accessToken;
const user = await this.userDatabase.userFromDiscord(profile, !this.registrationEnabled);
let err = null;
if (!user) err = new Error('No such user');
callback(err, user);
}), { successRedirect: '/home' });
this.authenticator.addStrategy('local', new LocalStrategy(async (username, password, callback) => {

View File

@ -137,7 +137,7 @@ class UserDatabase extends AbstractUserDatabase {
* @return {*}
* @memberof UserDatabase
*/
async userFromDiscord (profile) {
async userFromDiscord (profile, noNew = false) {
let user = this.cache.find((u) => u.externalProfiles.discord?.id === profile.id);
if (user) return Promise.resolve(user);
@ -149,6 +149,8 @@ class UserDatabase extends AbstractUserDatabase {
return Promise.resolve(user);
}
if (noNew) return null;
// If a user with the same username already exists, force the holder of the discord account to create a new account or log in to the other account and link them
const existing = await this.db.findOne(this._userColllection, { username: profile.username }, { collation: { locale: 'en', strength: 2 } });
if (existing) {

View File

@ -758,9 +758,9 @@
tar "^6.1.11"
"@navy.gif/logger@^1.0.0":
version "1.1.8"
resolved "https://registry.corgi.wtf/@navy.gif%2flogger/-/logger-1.1.8.tgz#ecc991c9d01c14bf052cc4c4509867a92e94661e"
integrity sha512-czTaGqSqCKza1uR8EB3/Qnly8v+E80y8TEDIMNFotoE5cNVdexnSBTTgWrsqwXbbVXfFzwW/2BXzHnXPrm9MCQ==
version "1.1.9"
resolved "https://registry.corgi.wtf/@navy.gif%2flogger/-/logger-1.1.9.tgz#3a2c605a80a751a0f4a859e680a38999572ec911"
integrity sha512-1qeuMu1NIKoMDliTKTTY4JMegtG5ERK0w0VLOZO2jySpIp/ZZ06bgRW/mQGvoyVNqIKbV8Tj5gaFGx+L97SuvA==
dependencies:
chalk "^4.1.2"
moment "^2.29.4"