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 284a46e..077e2ae 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -23,6 +23,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/app.js b/server/app.js index 76053ba..88e8da4 100644 --- a/server/app.js +++ b/server/app.js @@ -12,6 +12,7 @@ const userRouter = require('./routers/users.js'); const folderRouter = require('./routers/folders.js'); const quizRouter = require('./routers/quiz.js'); const imagesRouter = require('./routers/images.js') +const authRouter = require('./routers/auth.js') // Setup environement dotenv.config(); @@ -48,6 +49,7 @@ app.use('/api/user', userRouter); app.use('/api/folder', folderRouter); app.use('/api/quiz', quizRouter); app.use('/api/image', imagesRouter); +app.use('/api/auth', authRouter); app.use(errorHandler) diff --git a/server/config/auth.js b/server/config/auth.js new file mode 100644 index 0000000..fffb426 --- /dev/null +++ b/server/config/auth.js @@ -0,0 +1,53 @@ +const fs = require('fs'); +const path = require('path'); +const pathAuthConfig = './auth_config.json'; + +class AuthConfig { + + 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, pathAuthConfig); +const instance = new AuthConfig(configPath); +module.exports = instance; + diff --git a/server/controllers/auth.js b/server/controllers/auth.js new file mode 100644 index 0000000..8d7fa53 --- /dev/null +++ b/server/controllers/auth.js @@ -0,0 +1,26 @@ +const authConfig = require('../config/auth.js'); + +class authController { + + async getActive(req, res, next) { + + try { + + const passportConfig = authConfig.getPassportJSConfig(); + const simpleLoginConfig = authConfig.getSimpleLoginConfig(); + + const response = { + passportConfig, + simpleLoginConfig + }; + + return res.json(response); + } + catch (error) { + return next(error); // Gérer l'erreur + } + } + +} + +module.exports = new authController; \ No newline at end of file diff --git a/server/routers/auth.js b/server/routers/auth.js new file mode 100644 index 0000000..8e8fb4a --- /dev/null +++ b/server/routers/auth.js @@ -0,0 +1,9 @@ +const express = require('express'); +const router = express.Router(); +const jwt = require('../middleware/jwtToken.js'); + +const authController = require('../controllers/auth.js') + +router.get("/getActiveAuth",jwt.authenticate, authController.getActive); + +module.exports = router; \ No newline at end of file