Adds Oauths parsing

Co-authored-by: roesnerb <roesnerb@users.noreply.github.com>
Co-authored-by: MathieuSevignyLavallee <MathieuSevignyLavallee@users.noreply.github.com>
This commit is contained in:
Gabriel Matte 2024-10-01 10:55:48 -04:00
parent f7f03ebeaa
commit 8f7c0a3ac9
6 changed files with 244 additions and 149 deletions

View file

@ -1,21 +1,24 @@
var OAuth2Strategy = require('passport-oauth2') var OAuth2Strategy = require('passport-oauth2')
var authProvider = require('../../../models/authProvider')
var authUserAssoc = require('../../../models/authUserAssociation') var authUserAssoc = require('../../../models/authUserAssociation')
var users = require('../../../models/users') var users = require('../../../models/users')
var { hasNestedValue } = require('../../../utils') var { hasNestedValue } = require('../../../utils')
class PassportOAuth { class PassportOAuth {
constructor(passportjs,auth_id){ constructor(passportjs,auth_name){
this.passportjs = passportjs this.passportjs = passportjs
this.auth_id = auth_id this.auth_name = auth_name
} }
updateUser(userinfos){ async getProviderInfo(auth_name){
return await authProvider.find(auth_name)
} }
register(app, passport,endpoint, name, provider) { register(app, passport,endpoint, name, provider) {
const cb_url =`${process.env['BACKEND_URL']}${endpoint}/${name}/callback` const cb_url =`${process.env['BACKEND_URL']}${endpoint}/${name}/callback`
const self = this
passport.use(name, new OAuth2Strategy({ passport.use(name, new OAuth2Strategy({
authorizationURL: provider.OAUTH_AUTHORIZATION_URL, authorizationURL: provider.OAUTH_AUTHORIZATION_URL,
tokenURL: provider.OAUTH_TOKEN_URL, tokenURL: provider.OAUTH_TOKEN_URL,
@ -32,31 +35,32 @@ class PassportOAuth {
const userInfo = await userInfoResponse.json(); const userInfo = await userInfoResponse.json();
let received_user = { let received_user = {
auth_id: userInfo.sub,
email: userInfo.email, email: userInfo.email,
name: userInfo.name, name: userInfo.name,
roles: [] roles: []
}; };
if(hasNestedValue(userInfo,provider.OIDC_ROLE_TEACHER_VALUE)) received_user.roles.push('teacher')
if(hasNestedValue(userInfo,provider.OIDC_ROLE_STUDENT_VALUE)) received_user.roles.push('student')
const user_association = await authUserAssoc.find_user_association(userInfo.sub) if(hasNestedValue(userInfo,provider.OAUTH_ROLE_TEACHER_VALUE)) received_user.roles.push('teacher')
if(hasNestedValue(userInfo,provider.OAUTH_ROLE_STUDENT_VALUE)) received_user.roles.push('student')
if(user_linked){ const user_association = await authUserAssoc.find_user_association(self.auth_name._id,userInfo.sub)
let user = await users.getById(user_association.user_id)
user.name = received_user.name let user_account = null
user.email = received_user.email if(user_association){
user.roles = received_user.roles user_account = await users.getById(user_association.user_id)
users.editUser(user)
this.passportjs.authenticate(user)
} }
else { else {
let user_id = await users.getId(userInfo.email) let user_id = await users.getId(userInfo.email)
if(!user_id){ user_account = user_id ? await users.getById(user_id) : await users.register(received_user.email,"")
await users.register(received_user.email,""); await authUserAssoc.link(self.auth_name,received_user.auth_id,user_account._id)
users.editUser
}
} }
user_account.name = received_user.name
user_account.roles = received_user.roles
await users.editUser(user_account)
self.passportjs.authenticate(user_account)
// Store the tokens in the session // Store the tokens in the session
req.session.oauth2Tokens = { req.session.oauth2Tokens = {
accessToken: accessToken, accessToken: accessToken,
@ -64,7 +68,7 @@ class PassportOAuth {
expiresIn: params.expires_in expiresIn: params.expires_in
}; };
return done(null, user); return done(null, user_account);
} catch (error) { } catch (error) {
console.error(`Erreur dans la strategie OAuth2 '${name}' : ${error}`); console.error(`Erreur dans la strategie OAuth2 '${name}' : ${error}`);
return done(error); return done(error);

View file

@ -19,7 +19,7 @@ class PassportJs{
const auth_id = `passportjs_${provider.type}_${name}` const auth_id = `passportjs_${provider.type}_${name}`
if(!(provider.type in this.registeredProviders)){ if(!(provider.type in this.registeredProviders)){
this.registerProvider(provider.typename,auth_id) this.registerProvider(provider.type,auth_id)
} }
try{ try{
this.registeredProviders[provider.type].register(expressapp,passport,this.endpoint,name,provider) this.registeredProviders[provider.type].register(expressapp,passport,this.endpoint,name,provider)
@ -56,7 +56,7 @@ class PassportJs{
} }
authenticate(userinfos){ authenticate(userinfos){
this.authenticate(userinfos) this.authmanager.login(userinfos)
} }
} }

View file

@ -7,6 +7,20 @@ class AuthProvider {
this.name = name; this.name = name;
} }
async getId(name){
await db.connect()
const conn = db.getConnection();
const collection = conn.collection('authprovider');
const existingauth = await collection.findOne({ name:name });
if(existingauth){
return existingauth._id
}
return null
}
async create(name) { async create(name) {
await db.connect() await db.connect()
const conn = db.getConnection(); const conn = db.getConnection();

View file

@ -1,3 +1,4 @@
const authProvider = require('./authProvider.js')
const db = require('../config/db.js') const db = require('../config/db.js')
const { ObjectId } = require('mongodb'); const { ObjectId } = require('mongodb');
@ -11,14 +12,48 @@ class AuthUserAssociation {
this.connected = false; this.connected = false;
} }
async find_user_association(authId){ async find_user_association(provider_name,auth_id){
await db.connect() await db.connect()
const conn = db.getConnection(); const conn = db.getConnection();
const collection = conn.collection('authUserAssociation'); const collection = conn.collection('authUserAssociation');
const provider_id = await authProvider.getId(provider_name)
const userAssociation = await collection.findOne({ authId: authId }); const userAssociation = await collection.findOne({ authProvider_id: provider_id,auth_id,auth_id });
return userAssociation return userAssociation
} }
async link(provider_name,auth_id,user_id){
await db.connect()
const conn = db.getConnection();
const collection = conn.collection('authUserAssociation');
const provider_id = await authProvider.getId(provider_name)
const userAssociation = await collection.findOne({ authProvider_id: provider_id, user_id: user_id });
if(!userAssociation){
return await collection.insertOne({
_id:ObjectId,
authProvider_id:provider_id,
auth_id:auth_id,
user_id:user_id,
})
}
}
async unlink(provider_name,user_id){
await db.connect()
const conn = db.getConnection();
const collection = conn.collection('authUserAssociation');
const provider_id = await authProvider.getId(provider_name)
const userAssociation = await collection.findOne({ authProvider_id: provider_id, user_id: user_id });
if(userAssociation){
return await collection.deleteOne(userAssociation)
} else return null
}
} }
module.exports = new AuthUserAssociation; module.exports = new AuthUserAssociation;

View file

@ -1,13 +1,13 @@
//user //user
const db = require('../config/db.js'); const db = require("../config/db.js");
const bcrypt = require('bcrypt'); const bcrypt = require("bcrypt");
const AppError = require('../middleware/AppError.js'); const AppError = require("../middleware/AppError.js");
const { USER_ALREADY_EXISTS } = require('../constants/errorCodes'); const { USER_ALREADY_EXISTS } = require("../constants/errorCodes");
const Folders = require('../models/folders.js'); const Folders = require("../models/folders.js");
class Users { class Users {
async hashPassword(password) { async hashPassword(password) {
return await bcrypt.hash(password, 10) return await bcrypt.hash(password, 10);
} }
generatePassword() { generatePassword() {
@ -15,14 +15,14 @@ class Users {
} }
async verify(password, hash) { async verify(password, hash) {
return await bcrypt.compare(password, hash) return await bcrypt.compare(password, hash);
} }
async register(email, password) { async register(email, password) {
await db.connect() await db.connect();
const conn = db.getConnection(); const conn = db.getConnection();
const userCollection = conn.collection('users'); const userCollection = conn.collection("users");
const existingUser = await userCollection.findOne({ email: email }); const existingUser = await userCollection.findOne({ email: email });
@ -36,20 +36,36 @@ class Users {
created_at: new Date(), created_at: new Date(),
}; };
await userCollection.insertOne(newUser); let created_user = await userCollection.insertOne(newUser);
let user = await this.getById(created_user.insertedId)
const folderTitle = 'Dossier par Défaut'; const folderTitle = "Dossier par Défaut";
const userId = newUser._id.toString(); const userId = newUser._id.toString();
await Folders.create(folderTitle, userId); await Folders.create(folderTitle, userId);
// TODO: verif if inserted properly... // TODO: verif if inserted properly...
return user;
}
async login(userid) {
await db.connect();
const conn = db.getConnection();
const userCollection = conn.collection("users");
const user = await userCollection.findOne({ _id: userid });
if (!user) {
return false;
}
return user;
} }
async login(email, password) { async login(email, password) {
await db.connect() await db.connect();
const conn = db.getConnection(); const conn = db.getConnection();
const userCollection = conn.collection('users'); const userCollection = conn.collection("users");
const user = await userCollection.findOne({ email: email }); const user = await userCollection.findOne({ email: email });
@ -73,25 +89,28 @@ class Users {
} }
async changePassword(email, newPassword) { async changePassword(email, newPassword) {
await db.connect() await db.connect();
const conn = db.getConnection(); const conn = db.getConnection();
const userCollection = conn.collection('users'); const userCollection = conn.collection("users");
const hashedPassword = await this.hashPassword(newPassword); const hashedPassword = await this.hashPassword(newPassword);
const result = await userCollection.updateOne({ email }, { $set: { password: hashedPassword } }); const result = await userCollection.updateOne(
{ email },
{ $set: { password: hashedPassword } }
);
if (result.modifiedCount != 1) return null; if (result.modifiedCount != 1) return null;
return newPassword return newPassword;
} }
async delete(email) { async delete(email) {
await db.connect() await db.connect();
const conn = db.getConnection(); const conn = db.getConnection();
const userCollection = conn.collection('users'); const userCollection = conn.collection("users");
const result = await userCollection.deleteOne({ email }); const result = await userCollection.deleteOne({ email });
@ -101,10 +120,10 @@ class Users {
} }
async getId(email) { async getId(email) {
await db.connect() await db.connect();
const conn = db.getConnection(); const conn = db.getConnection();
const userCollection = conn.collection('users'); const userCollection = conn.collection("users");
const user = await userCollection.findOne({ email: email }); const user = await userCollection.findOne({ email: email });
@ -116,10 +135,10 @@ class Users {
} }
async getById(id) { async getById(id) {
await db.connect() await db.connect();
const conn = db.getConnection(); const conn = db.getConnection();
const userCollection = conn.collection('users'); const userCollection = conn.collection("users");
const user = await userCollection.findOne({ _id: id }); const user = await userCollection.findOne({ _id: id });
@ -131,10 +150,10 @@ class Users {
} }
async editUser(userInfo) { async editUser(userInfo) {
await db.connect() await db.connect();
const conn = db.getConnection(); const conn = db.getConnection();
const userCollection = conn.collection('users'); const userCollection = conn.collection("users");
const user = await userCollection.findOne({ _id: userInfo.id }); const user = await userCollection.findOne({ _id: userInfo.id });
@ -143,9 +162,19 @@ class Users {
} }
const updatedFields = { ...userInfo }; const updatedFields = { ...userInfo };
delete updatedFields.id;
return user; const result = await userCollection.updateOne(
{ _id: userInfo.id },
{ $set: updatedFields }
);
if (result.modifiedCount === 1) {
return true;
}
return false;
} }
} }
module.exports = new Users; module.exports = new Users();

View file

@ -3,13 +3,26 @@ function hasNestedValue(obj, path, delimiter="_") {
let current = obj; let current = obj;
for (const key of keys) { for (const key of keys) {
if (current && typeof current === 'object' && key in current) { if (current && typeof current === "object") {
if (Array.isArray(current)) {
const index = current.findIndex(x => x == key)
if (index != -1) {
current = current[index];
} else {
return false;
}
} else if (key in current) {
current = current[key]; current = current[key];
} else { } else {
return false; return false;
} }
} else {
return false;
}
} }
return true; return true;
} }
module.exports = { hasNestedValue};