83 lines
3.2 KiB
JavaScript
83 lines
3.2 KiB
JavaScript
|
|
//var jwt = require('express-jwt');
|
|
var { expressjwt: jwt } = require("express-jwt"),
|
|
secret = require('../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 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};
|
|
return 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;
|