2018-12-11 11:18:07 +03:00
|
|
|
const jwt = require('express-jwt');
|
|
|
|
const membersService = require('../../members');
|
|
|
|
const labs = require('../../labs');
|
|
|
|
const config = require('../../../config');
|
2018-11-07 13:10:07 +03:00
|
|
|
|
2018-12-11 11:18:07 +03:00
|
|
|
let UNO_MEMBERINO;
|
2018-11-07 13:10:07 +03:00
|
|
|
|
2018-12-11 11:18:07 +03:00
|
|
|
module.exports = {
|
|
|
|
get authenticateMembersToken() {
|
|
|
|
if (!labs.isSet('members')) {
|
|
|
|
return function (req, res, next) {
|
|
|
|
return next();
|
|
|
|
};
|
|
|
|
}
|
2018-12-17 14:53:45 +03:00
|
|
|
|
2018-12-11 11:18:07 +03:00
|
|
|
if (!UNO_MEMBERINO) {
|
2018-12-17 14:53:45 +03:00
|
|
|
const url = require('url');
|
|
|
|
const {protocol, host} = url.parse(config.get('url'));
|
|
|
|
const siteOrigin = `${protocol}//${host}`;
|
|
|
|
|
2018-12-11 11:18:07 +03:00
|
|
|
UNO_MEMBERINO = jwt({
|
|
|
|
credentialsRequired: false,
|
|
|
|
requestProperty: 'member',
|
2018-12-11 15:45:03 +03:00
|
|
|
audience: siteOrigin,
|
|
|
|
issuer: siteOrigin,
|
2018-12-11 11:18:07 +03:00
|
|
|
algorithm: 'RS512',
|
|
|
|
secret: membersService.api.publicKey,
|
|
|
|
getToken(req) {
|
|
|
|
if (!req.get('authorization')) {
|
|
|
|
return null;
|
|
|
|
}
|
2018-11-07 13:10:07 +03:00
|
|
|
|
2018-12-11 11:18:07 +03:00
|
|
|
const [scheme, credentials] = req.get('authorization').split(/\s+/);
|
2018-11-07 13:10:07 +03:00
|
|
|
|
2018-12-11 11:18:07 +03:00
|
|
|
if (scheme !== 'GhostMembers') {
|
|
|
|
return null;
|
|
|
|
}
|
2018-11-07 13:10:07 +03:00
|
|
|
|
2018-12-11 11:18:07 +03:00
|
|
|
return credentials;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return UNO_MEMBERINO;
|
|
|
|
}
|
2018-11-07 13:10:07 +03:00
|
|
|
};
|