Dynamic auth config

This commit is contained in:
MathieuSevignyLavallee 2024-09-22 21:00:15 -04:00
parent 97e7a4888f
commit 4849380b73
5 changed files with 93 additions and 56 deletions

View file

@ -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

32
auth_config.json Normal file
View file

@ -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"
}
}
}

View file

@ -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

View file

@ -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;

View file

@ -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
}
}