From 4849380b731cd6c5410583f48d4f46e9143b7b33 Mon Sep 17 00:00:00 2001 From: MathieuSevignyLavallee <89943988+MathieuSevignyLavallee@users.noreply.github.com> Date: Sun, 22 Sep 2024 21:00:15 -0400 Subject: [PATCH] Dynamic auth config --- .env.auth | 23 ------------ auth_config.json | 32 ++++++++++++++++ docker-compose.yaml | 2 + server/config/auth.js | 75 +++++++++++++++++++++++++------------- server/controllers/auth.js | 17 +++++---- 5 files changed, 93 insertions(+), 56 deletions(-) delete mode 100644 .env.auth create mode 100644 auth_config.json diff --git a/.env.auth b/.env.auth deleted file mode 100644 index 65fb680..0000000 --- a/.env.auth +++ /dev/null @@ -1,23 +0,0 @@ -# Type of Autorizarions -SIMPLE_LOGIN_ACTIVE=true -OAUTH_ACTIVE=false -OIDC_ACTIVE=false - -# Configuration Simple Login -SESSION_SECRET=your_session_secret - -# Configuration OAuth -OAUTH_AUTHORIZATION_URL=https://www.testurl.com/oauth2/authorize -OAUTH_TOKEN_URL=https://www.testurl.com/oauth2/token -OAUTH_CLIENT_ID=your_oauth_client_id -OAUTH_CLIENT_SECRET=your_oauth_client_secret -OAUTH_CALLBACK_URL=https://localhost:3000/auth/provider/callback -OAUTH_ADD_SCOPE=scopes -OAUTH_ROLE_TEACHER_VALUE=teacher-claim-value -OAUTH_ROLE_STUDENT_VALUE=student-claim-value - -# Configuration OIDC -OIDC_CLIENT_ID=your_oidc_client_id -OIDC_CLIENT_SECRET=your_oidc_client_secret -OIDC_ISSUER_URL=https://your-issuer.com -OIDC_CALLBACK_URL=http://localhost:3000/auth/oidc/callback diff --git a/auth_config.json b/auth_config.json new file mode 100644 index 0000000..d5569e5 --- /dev/null +++ b/auth_config.json @@ -0,0 +1,32 @@ +{ + "auth": { + "passportjs": [ + { + "provider1": { + "OAUTH_AUTHORIZATION_URL": "https://www.testurl.com/oauth2/authorize", + "OAUTH_TOKEN_URL": "https://www.testurl.com/oauth2/token", + "OAUTH_CLIENT_ID": "your_oauth_client_id", + "OAUTH_CLIENT_SECRET": "your_oauth_client_secret", + "OAUTH_CALLBACK_URL": "https://localhost:3000/auth/provider/callback", + "OAUTH_ADD_SCOPE": "scopes", + "OAUTH_ROLE_TEACHER_VALUE": "teacher-claim-value", + "OAUTH_ROLE_STUDENT_VALUE": "student-claim-value" + } + }, + { + "provider2": { + "type": "oidc", + "OIDC_CLIENT_ID": "your_oidc_client_id", + "OIDC_CLIENT_SECRET": "your_oidc_client_secret", + "OIDC_ISSUER_URL": "https://your-issuer.com", + "OIDC_CALLBACK_URL": "http://localhost:3000/auth/oidc/callback" + } + } + ], + "simple-login": { + "enabled": true, + "name": "provider3", + "SESSION_SECRET": "your_session_secret" + } + } +} \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml index 93e29f2..9f91bd6 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -25,6 +25,8 @@ services: EMAIL_PSW: 'vvml wmfr dkzb vjzb' JWT_SECRET: haQdgd2jp09qb897GeBZyJetC8ECSpbFJe FRONTEND_URL: "http://localhost:5173" + volumes: + - ./auth_config.json:/usr/src/app/serveur/config/auth_config.json depends_on: - mongo restart: always diff --git a/server/config/auth.js b/server/config/auth.js index dbcdca8..493dd0c 100644 --- a/server/config/auth.js +++ b/server/config/auth.js @@ -1,29 +1,52 @@ -module.exports = { - // Enable or disable the types of authentications - simpleLoginActive: process.env.SIMPLE_LOGIN_ACTIVE || 'true', - oauthActive: process.env.OAUTH_ACTIVE || 'false', - oidcActive: process.env.OIDC_ACTIVE || 'false', +const fs = require('fs'); +const path = require('path'); - // Simple Login Configuration - sessionSecret: process.env.SESSION_SECRET || 'default_session_secret', +class AuthConfig { - // OAuth Configuration - oauth: { - authorizationURL: process.env.OAUTH_AUTHORIZATION_URL || '', - tokenURL: process.env.OAUTH_TOKEN_URL || '', - clientID: process.env.OAUTH_CLIENT_ID || '', - clientSecret: process.env.OAUTH_CLIENT_SECRET || '', - callbackURL: process.env.OAUTH_CALLBACK_URL || '', - scope: process.env.OAUTH_ADD_SCOPE || '', - teacherRoleClaim: process.env.OAUTH_ROLE_TEACHER_VALUE || '', - studentRoleClaim: process.env.OAUTH_ROLE_STUDENT_VALUE || '', - }, - - // OIDC Configuration - oidc: { - clientID: process.env.OIDC_CLIENT_ID || '', - clientSecret: process.env.OIDC_CLIENT_SECRET || '', - issuerURL: process.env.OIDC_ISSUER_URL || '', - callbackURL: process.env.OIDC_CALLBACK_URL || '', + constructor(configPath) { + this.configPath = configPath; + this.config = this.loadConfig(); } -}; + + // Méthode pour lire le fichier de configuration JSON + loadConfig() { + try { + const configData = fs.readFileSync(this.configPath, 'utf-8'); + return JSON.parse(configData); + } catch (error) { + console.error("Erreur lors de la lecture du fichier de configuration :", error); + return null; + } + } + + // 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["simple-login"]) { + return this.config.auth["simple-login"]; + } else { + return { error: "Aucune configuration Simple Login disponible." }; + } + } +} + +// Utilisation de la classe ConfigManager +const configPath = path.join(__dirname, './auth_config.json'); +const instance = new AuthConfig(configPath); +module.exports = instance; + diff --git a/server/controllers/auth.js b/server/controllers/auth.js index f468216..8d7fa53 100644 --- a/server/controllers/auth.js +++ b/server/controllers/auth.js @@ -3,18 +3,21 @@ const authConfig = require('../config/auth.js'); class authController { async getActive(req, res, next) { + try { - console.log(authConfig); - const authServices = { - simpleLoginActive: authConfig.simpleLoginActive, - oauthActive: authConfig.oauthActive, - oidcActive: authConfig.oidcActive + + const passportConfig = authConfig.getPassportJSConfig(); + const simpleLoginConfig = authConfig.getSimpleLoginConfig(); + + const response = { + passportConfig, + simpleLoginConfig }; - res.json(authServices); + return res.json(response); } catch (error) { - return next(error); + return next(error); // Gérer l'erreur } }