EvalueTonSavoir/server/config/auth.js

193 lines
6.1 KiB
JavaScript
Raw Normal View History

2024-09-22 21:00:15 -04:00
const fs = require('fs');
const path = require('path');
2024-09-23 16:24:07 -04:00
const pathAuthConfig = './auth_config.json';
2024-09-22 21:00:15 -04:00
2024-09-28 17:08:11 -04:00
const configPath = path.join(process.cwd(), pathAuthConfig);
2024-09-27 14:16:11 -04:00
2024-09-22 21:00:15 -04:00
class AuthConfig {
2024-09-27 14:16:11 -04:00
config = null;
2024-09-22 21:00:15 -04:00
// Méthode pour lire le fichier de configuration JSON
loadConfig() {
try {
2024-09-27 14:16:11 -04:00
const configData = fs.readFileSync(configPath, 'utf-8');
this.config = JSON.parse(configData);
2024-09-22 21:00:15 -04:00
} catch (error) {
2024-10-28 23:19:25 -04:00
console.error("Erreur lors de la lecture du fichier de configuration. Ne pas se fier si vous n'avez pas mit de fichier de configuration.");
this.config = {}
2024-09-22 21:00:15 -04:00
}
2024-10-28 23:19:25 -04:00
return this.config
2024-09-22 21:00:15 -04:00
}
2024-09-27 14:16:11 -04:00
// Méthode pour load le fichier de test
loadConfigTest(mockConfig) {
this.config = mockConfig;
}
2024-09-22 21:00:15 -04:00
// Méthode pour retourner la configuration des fournisseurs PassportJS
getPassportJSConfig() {
if (this.config && this.config.auth && this.config.auth.passportjs) {
const passportConfig = {};
this.config.auth.passportjs.forEach(provider => {
const providerName = Object.keys(provider)[0];
passportConfig[providerName] = provider[providerName];
});
return passportConfig;
} else {
return { error: "Aucune configuration PassportJS disponible." };
}
}
// Méthode pour retourner la configuration de Simple Login
getSimpleLoginConfig() {
if (this.config && this.config.auth && this.config.auth["simpleauth"]) {
return this.config.auth["simpleauth"];
2024-09-22 21:00:15 -04:00
} else {
return { error: "Aucune configuration Simple Login disponible." };
}
}
2024-09-24 22:00:28 -04:00
// Méthode pour retourner tous les providers de type OAuth
getOAuthProviders() {
if (this.config && this.config.auth && this.config.auth.passportjs) {
const oauthProviders = this.config.auth.passportjs.filter(provider => {
const providerName = Object.keys(provider)[0];
return provider[providerName].type === 'oauth';
});
if (oauthProviders.length > 0) {
return oauthProviders;
} else {
return { error: "Aucun fournisseur OAuth disponible." };
}
} else {
return { error: "Aucune configuration PassportJS disponible." };
}
}
// Méthode pour retourner tous les providers de type OIDC
getOIDCProviders() {
if (this.config && this.config.auth && this.config.auth.passportjs) {
const oidcProviders = this.config.auth.passportjs.filter(provider => {
const providerName = Object.keys(provider)[0];
return provider[providerName].type === 'oidc';
});
if (oidcProviders.length > 0) {
return oidcProviders;
} else {
return { error: "Aucun fournisseur OIDC disponible." };
}
} else {
return { error: "Aucune configuration PassportJS disponible." };
}
}
// Méthode pour vérifier si tous les providers ont les variables nécessaires
validateProvidersConfig() {
const requiredOAuthFields = [
2024-09-28 17:08:11 -04:00
'OAUTH_AUTHORIZATION_URL', 'OAUTH_TOKEN_URL','OAUTH_USERINFO_URL', 'OAUTH_CLIENT_ID', 'OAUTH_CLIENT_SECRET', 'OAUTH_ROLE_TEACHER_VALUE', 'OAUTH_ROLE_STUDENT_VALUE'
2024-09-24 22:00:28 -04:00
];
const requiredOIDCFields = [
2024-09-30 20:40:33 -04:00
'OIDC_CLIENT_ID', 'OIDC_CLIENT_SECRET', 'OIDC_CONFIG_URL', 'OIDC_ROLE_TEACHER_VALUE', 'OIDC_ROLE_STUDENT_VALUE','OIDC_ADD_SCOPE'
2024-09-24 22:00:28 -04:00
];
const missingFieldsReport = [];
if (this.config && this.config.auth && this.config.auth.passportjs) {
this.config.auth.passportjs.forEach(provider => {
const providerName = Object.keys(provider)[0];
const providerConfig = provider[providerName];
let missingFields = [];
// Vérification des providers de type OAuth
if (providerConfig.type === 'oauth') {
missingFields = requiredOAuthFields.filter(field => !(field in providerConfig));
}
// Vérification des providers de type OIDC
else if (providerConfig.type === 'oidc') {
missingFields = requiredOIDCFields.filter(field => !(field in providerConfig));
}
// Si des champs manquent, on les ajoute au rapport
if (missingFields.length > 0) {
missingFieldsReport.push({
provider: providerName,
missingFields: missingFields
});
}
});
// Si des champs manquent, lever une exception
if (missingFieldsReport.length > 0) {
throw new Error(`Configuration invalide pour les providers suivants : ${JSON.stringify(missingFieldsReport, null, 2)}`);
} else {
console.log("Configuration auth_config.json: Tous les providers ont les variables nécessaires.")
return { success: "Tous les providers ont les variables nécessaires." };
}
} else {
throw new Error("Aucune configuration PassportJS disponible.");
}
}
// Méthode pour retourner la configuration des fournisseurs PassportJS pour le frontend
getActiveAuth() {
2024-09-24 22:08:48 -04:00
if (this.config && this.config.auth) {
2024-09-24 22:00:28 -04:00
const passportConfig = {};
2024-09-24 22:08:48 -04:00
// Gestion des providers PassportJS
if (this.config.auth.passportjs) {
this.config.auth.passportjs.forEach(provider => {
const providerName = Object.keys(provider)[0];
const providerConfig = provider[providerName];
passportConfig[providerName] = {};
if (providerConfig.type === 'oauth') {
passportConfig[providerName] = {
2024-09-30 21:15:09 -04:00
type: providerConfig.type
2024-09-24 22:08:48 -04:00
};
} else if (providerConfig.type === 'oidc') {
passportConfig[providerName] = {
type: providerConfig.type,
};
}
});
}
2024-09-24 22:00:28 -04:00
2024-09-24 22:08:48 -04:00
// Gestion du Simple Login
if (this.config.auth["simpleauth"] && this.config.auth["simpleauth"].enabled) {
passportConfig['simpleauth'] = {
type: "simpleauth",
name: this.config.auth["simpleauth"].name
2024-09-24 22:08:48 -04:00
};
}
2024-09-24 22:00:28 -04:00
return passportConfig;
} else {
2024-09-24 22:08:48 -04:00
return { error: "Aucune configuration d'authentification disponible." };
2024-09-24 22:00:28 -04:00
}
}
// Check if students must be authenticated to join a room
getRoomsRequireAuth() {
const roomRequireAuth = process.env.AUTHENTICATED_ROOMS;
if (!roomRequireAuth || roomRequireAuth !== "true") {
return false;
}
return true;
}
2024-09-24 22:08:48 -04:00
2024-09-22 21:00:15 -04:00
}
2024-09-27 14:16:11 -04:00
module.exports = AuthConfig;