2025-02-21 14:48:21 -05:00
|
|
|
const fs = require('fs');
|
|
|
|
|
const AuthConfig = require('../config/auth.js');
|
|
|
|
|
const jwt = require('../middleware/jwtToken.js');
|
|
|
|
|
const emailer = require('../config/email.js');
|
|
|
|
|
const { MISSING_REQUIRED_PARAMETER } = require('../constants/errorCodes.js');
|
|
|
|
|
const AppError = require('../middleware/AppError.js');
|
|
|
|
|
|
|
|
|
|
class AuthManager{
|
|
|
|
|
constructor(expressapp,configs=null,userModel){
|
2025-03-06 21:19:57 -05:00
|
|
|
console.log(`AuthManager: constructor: configs: ${JSON.stringify(configs)}`);
|
|
|
|
|
console.log(`AuthManager: constructor: userModel: ${JSON.stringify(userModel)}`);
|
2025-02-21 14:48:21 -05:00
|
|
|
this.modules = []
|
|
|
|
|
this.app = expressapp
|
2025-03-06 21:19:57 -05:00
|
|
|
|
2025-02-21 14:48:21 -05:00
|
|
|
this.configs = configs ?? (new AuthConfig()).loadConfig()
|
|
|
|
|
this.addModules()
|
2025-03-06 21:19:57 -05:00
|
|
|
this.simpleregister = userModel;
|
2025-03-04 14:47:44 -05:00
|
|
|
this.registerAuths()
|
2025-03-06 21:19:57 -05:00
|
|
|
console.log(`AuthManager: constructor: this.configs: ${JSON.stringify(this.configs)}`);
|
2025-02-21 14:48:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getUserModel(){
|
|
|
|
|
return this.simpleregister;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async addModules(){
|
|
|
|
|
for(const module in this.configs.auth){
|
|
|
|
|
this.addModule(module)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async addModule(name){
|
|
|
|
|
const modulePath = `${process.cwd()}/auth/modules/${name}.js`
|
|
|
|
|
|
|
|
|
|
if(fs.existsSync(modulePath)){
|
|
|
|
|
const Module = require(modulePath);
|
|
|
|
|
this.modules.push(new Module(this,this.configs.auth[name]));
|
|
|
|
|
console.info(`Module d'authentification '${name}' ajouté`)
|
|
|
|
|
} else{
|
|
|
|
|
console.error(`Le module d'authentification ${name} n'as pas été chargé car il est introuvable`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async registerAuths(){
|
2025-03-04 14:47:44 -05:00
|
|
|
console.log(``);
|
2025-02-21 14:48:21 -05:00
|
|
|
for(const module of this.modules){
|
|
|
|
|
try{
|
2025-03-02 17:57:49 -05:00
|
|
|
module.registerAuth(this.app, this.simpleregister);
|
2025-02-21 14:48:21 -05:00
|
|
|
} catch(error){
|
|
|
|
|
console.error(`L'enregistrement du module ${module} a échoué.`);
|
|
|
|
|
console.error(`Error: ${error} `);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
2025-03-02 17:57:49 -05:00
|
|
|
async login(userInfo,req,res,next){ //passport and simpleauth use next
|
2025-03-06 00:37:22 -05:00
|
|
|
const tokenToSave = jwt.create(userInfo.email, userInfo._id, userInfo.roles);
|
2025-03-02 17:57:49 -05:00
|
|
|
res.redirect(`/auth/callback?user=${tokenToSave}&username=${userInfo.name}`);
|
|
|
|
|
console.info(`L'utilisateur '${userInfo.name}' vient de se connecter`)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
|
async loginSimple(email,pswd,req,res,next){ //passport and simpleauth use next
|
2025-03-06 00:37:22 -05:00
|
|
|
console.log(`auth-manager: loginSimple: email: ${email}, pswd: ${pswd}`);
|
2025-03-01 23:01:31 -05:00
|
|
|
const userInfo = await this.simpleregister.login(email, pswd);
|
2025-03-06 00:37:22 -05:00
|
|
|
console.log(`auth-manager: loginSimple: userInfo: ${JSON.stringify(userInfo)}`);
|
|
|
|
|
userInfo.roles = ['teacher']; // hard coded role
|
|
|
|
|
const tokenToSave = jwt.create(userInfo.email, userInfo._id, userInfo.roles);
|
|
|
|
|
console.log(`auth-manager: loginSimple: tokenToSave: ${tokenToSave}`);
|
|
|
|
|
//res.redirect(`/auth/callback?user=${tokenToSave}&username=${userInfo.email}`);
|
|
|
|
|
res.status(200).json({token: tokenToSave});
|
|
|
|
|
console.info(`L'utilisateur '${userInfo.email}' vient de se connecter`)
|
2025-02-21 14:48:21 -05:00
|
|
|
}
|
|
|
|
|
|
2025-03-07 14:04:30 -05:00
|
|
|
async register(userInfos, sendEmail=false){
|
2025-03-01 23:01:31 -05:00
|
|
|
console.log(userInfos);
|
2025-02-21 14:48:21 -05:00
|
|
|
if (!userInfos.email || !userInfos.password) {
|
|
|
|
|
throw new AppError(MISSING_REQUIRED_PARAMETER);
|
|
|
|
|
}
|
|
|
|
|
const user = await this.simpleregister.register(userInfos);
|
2025-03-07 14:04:30 -05:00
|
|
|
if(sendEmail){
|
|
|
|
|
emailer.registerConfirmation(user.email);
|
|
|
|
|
}
|
2025-02-21 14:48:21 -05:00
|
|
|
return user
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-04 14:24:36 -05:00
|
|
|
module.exports = AuthManager;
|