Files
adastra_api/routes/auth.js
T
2025-08-15 17:29:23 +02:00

95 lines
3.8 KiB
JavaScript

//var jwt = require('express-jwt');
var { expressjwt: jwt } = require("express-jwt"),
secret = require('../src/config').secret,
bcrypt = require('bcrypt');
const { v4 } = require('uuid'),
passport = require('passport'),
auths = ['Api-Key', 'Basic', 'Bearer', 'Token'];
function getTokenFromHeader(req) {
if(req.headers.authorization && auths.indexOf(req.headers.authorization.split(' ')[0]) !== -1) {
return req.headers.authorization.split(' ')[1];
}
return null;
}
const auth = {
requiredJwt: jwt({
secret: secret,
algorithms: ['HS256'],
requestProperty: 'payload',
getToken: getTokenFromHeader
}),
optional: jwt({
secret: secret,
algorithms: ['HS256'],
requestProperty: 'payload',
credentialsRequired: false,
getToken: getTokenFromHeader
}),
required: async (req, res, next) => {
if(req.headers.authorization && req.headers.authorization.split(' ')[0] === process.env.AUTHORIZATION_PREFIX) {
passport.authenticate('headerapikey', { session: false }, function (err, application, info) {
if (err) {
//return res.json({message: "An authentication error occured (required).", err: err.message});
//return res.status(err.status || 500).json({message: "An authentication error occured (required).", err: err.message});
return next({message: "An authentication error occured.", err: err.message});
}
if (!application) {
return res.status(401).json({message: "Unauthorized - You are not allowed to access this resource.", err: err, info: info});
}
req.application = application;
req.payload = {id: application.author._id};
//console.log(application);
//console.log(application.author._id);
return next();
/*
return jwt({
secret: secret,
algorithms: ['HS256'],
requestProperty: 'payload',
getToken: application.author.toAuthJSON().token
})(req, res, next);
*/
})(req, res, next);
} else {
return jwt({
secret: secret,
algorithms: ['HS256'],
requestProperty: 'payload',
getToken: getTokenFromHeader
})(req, res, next);
}
},
getAuthType: (req) => {
if(req.headers.authorization && auths.indexOf(req.headers.authorization.split(' ')[0]) !== -1) {
let value = req.headers.authorization.split(' ')[0];
return value;
}
return null;
},
generateAPIKey: async () => {
const key = v4().replace(/-/g, '');
const salt = secret;
// Génération d'un salt aléatoire : const salt = await Promise.resolve(bcrypt.genSalt(10));
const maskedKey = `${key.slice(0, 4)}...${key.slice(-4)}`;
const encryptedKey = await Promise.resolve(bcrypt.hash(key, salt));
return { key: key, masked: maskedKey, encrypted: encryptedKey};
},
authenticateKey: async (req, res, next) => {
passport.authenticate('headerapikey', { session: false }, function (err, application, info) {
if (err) {
return res.status(err.status || 500).json({message: "An authentication error occured (authenticateKey).", err: err.message});
}
if (!application) {
return res.status(401).json({message: "Unauthorized - You are not allowed to access this resource.", err: err, info: info});
}
req.application = application;
return next();
})(req, res, next);
}
};
module.exports = auth;