link oauths

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 00:14:55 -04:00
parent b1e26d7895
commit f7f03ebeaa
7 changed files with 109 additions and 20 deletions

View file

@ -1,6 +1,19 @@
var OAuth2Strategy = require('passport-oauth2')
var authUserAssoc = require('../../../models/authUserAssociation')
var users = require('../../../models/users')
var {hasNestedValue} = require('../../../utils')
class PassportOAuth {
constructor(passportjs,auth_id){
this.passportjs = passportjs
this.auth_id = auth_id
}
updateUser(userinfos){
}
register(app, passport,endpoint, name, provider) {
const cb_url =`${process.env['BACKEND_URL']}${endpoint}/${name}/callback`
passport.use(name, new OAuth2Strategy({
@ -18,15 +31,31 @@ class PassportOAuth {
});
const userInfo = await userInfoResponse.json();
const user = {
id: userInfo.sub,
let received_user = {
email: userInfo.email,
name: userInfo.name,
groups: userInfo.groups ?? [],
accessToken: accessToken,
refreshToken: refreshToken,
expiresIn: params.expires_in
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(user_linked){
let user = await users.getById(user_association.user_id)
user.name = received_user.name
user.email = received_user.email
user.roles = received_user.roles
users.editUser(user)
this.passportjs.authenticate(user)
}
else {
let user_id = await users.getId(userInfo.email)
if(!user_id){
await users.register(received_user.email,"");
users.editUser
}
}
// Store the tokens in the session
req.session.oauth2Tokens = {
@ -56,6 +85,7 @@ class PassportOAuth {
(req, res) => {
if (req.user) {
res.json(req.user)
//const redirectUrl = `http://your-frontend-url.com/oauth/callback?user=${encodeURIComponent(req.user)}`;
//res.redirect(redirectUrl);
console.info(`L'utilisateur '${req.user.name}' vient de se connecter`)

View file

@ -1,6 +1,10 @@
var OpenIDConnectStrategy = require('passport-openidconnect')
class PassportOpenIDConnect {
constructor(passportjs,auth_id){
this.passportjs = passportjs
this.auth_id = auth_id
}
async getConfigFromConfigURL(name,provider){
try{

View file

@ -16,13 +16,13 @@ class PassportJs{
for(const p of this.providers){
for(const [name,provider] of Object.entries(p)){
const auth_id = `passportjs_${provider.type}_${name}`
if(!(provider.type in this.registeredProviders)){
this.registerProvider(provider.type)
this.registerProvider(provider.typename,auth_id)
}
try{
this.registeredProviders[provider.type].register(expressapp,passport,this.endpoint,name,provider)
const auth_id = `passportjs_${provider.type}_${name}`
authprovider.create(auth_id)
} catch(error){
console.error(`La connexion ${name} de type ${provider.type} n'as pu être chargé.`)
@ -39,11 +39,11 @@ class PassportJs{
});
}
async registerProvider(providerType){
async registerProvider(providerType,auth_id){
try{
const providerPath = `${process.cwd()}/auth/modules/passport-providers/${providerType}.js`
const Provider = require(providerPath);
this.registeredProviders[providerType]= new Provider()
this.registeredProviders[providerType]= new Provider(this,auth_id)
console.info(`Le type de connexion '${providerType}' a été ajouté dans passportjs.`)
} catch(error){
console.error(`Le type de connexion '${providerType}' n'as pas pu être chargé dans passportjs.`)
@ -51,12 +51,12 @@ class PassportJs{
}
register(){
register(userinfos){
this.authmanager.register(userinfos)
}
authenticate(){
authenticate(userinfos){
this.authenticate(userinfos)
}
}

View file

@ -15,14 +15,14 @@ class AuthProvider {
const existingauth = await collection.findOne({ name:name });
if(foldersCollection){
if(existingauth){
return existingauth._id;
}
const newProvider = {
name:name
}
const result = await foldersCollection.insertOne(newProvider);
const result = await collection.insertOne(newProvider);
return result.insertedId;
}
}

View file

@ -8,7 +8,17 @@ class AuthUserAssociation {
this.authProvider_id = authProviderId;
this.auth_id = authId;
this.user_id = userId;
this.connected = false;
}
async find_user_association(authId){
await db.connect()
const conn = db.getConnection();
const collection = conn.collection('authUserAssociation');
const userAssociation = await collection.findOne({ authId: authId });
return userAssociation
}
}
module.exports = new AuthUserAssociation;

View file

@ -6,7 +6,6 @@ const { USER_ALREADY_EXISTS } = require('../constants/errorCodes');
const Folders = require('../models/folders.js');
class Users {
async hashPassword(password) {
return await bcrypt.hash(password, 10)
}
@ -34,7 +33,7 @@ class Users {
const newUser = {
email: email,
password: await this.hashPassword(password),
created_at: new Date()
created_at: new Date(),
};
await userCollection.insertOne(newUser);
@ -116,6 +115,37 @@ class Users {
return user._id;
}
async getById(id){
await db.connect()
const conn = db.getConnection();
const userCollection = conn.collection('users');
const user = await userCollection.findOne({ _id: id });
if (!user) {
return false;
}
return user;
}
async editUser(userInfo){
await db.connect()
const conn = db.getConnection();
const userCollection = conn.collection('users');
const user = await userCollection.findOne({ _id: userInfo.id });
if (!user) {
return false;
}
const updatedFields = { ...userInfo };
return user;
}
}
module.exports = new Users;

15
server/utils.js Normal file
View file

@ -0,0 +1,15 @@
function hasNestedValue(obj, path, delimiter="_") {
const keys = path.split(delimiter);
let current = obj;
for (const key of keys) {
if (current && typeof current === 'object' && key in current) {
current = current[key];
} else {
return false;
}
}
return true;
}