0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-27 22:49:56 -05:00
ghost/core/server/services/auth/members/index.js
Renovate Bot b58e7af65e Updated dependency express-jwt to v7
- also requires a small change to the imports to reflect an API change
2022-04-28 11:15:04 +01:00

57 lines
1.7 KiB
JavaScript

const {expressjwt: jwt} = require('express-jwt');
const {UnauthorizedError} = require('@tryghost/errors');
const membersService = require('../../members');
const config = require('../../../../shared/config');
let UNO_MEMBERINO;
async function createMiddleware() {
const url = require('url');
const {protocol, host} = url.parse(config.get('url'));
const siteOrigin = `${protocol}//${host}`;
const membersConfig = await membersService.api.getPublicConfig();
return jwt({
credentialsRequired: false,
requestProperty: 'member',
audience: siteOrigin,
issuer: membersConfig.issuer,
algorithms: ['RS512'],
secret: membersConfig.publicKey,
getToken(req) {
if (!req.get('authorization')) {
return null;
}
const [scheme, credentials] = req.get('authorization').split(/\s+/);
if (scheme !== 'GhostMembers') {
return null;
}
return credentials;
}
});
}
module.exports = {
get authenticateMembersToken() {
return async function (req, res, next) {
if (!UNO_MEMBERINO) {
UNO_MEMBERINO = await createMiddleware();
}
try {
const middleware = UNO_MEMBERINO;
middleware(req, res, function (err, ...rest) {
if (err && err.name === 'UnauthorizedError') {
return next(new UnauthorizedError({err}), ...rest);
}
return next(err, ...rest);
});
} catch (err) {
next(err);
}
};
}
};