mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-11-27 18:52:14 +03:00
25aac1359d
no-issue - Added member auth middleware to siteApp - Passed member as context in routing service - set Cache-Control: private for member requests - fucked up some tests - Added member as global template variable - Updated tokens to have expiry of subscription_period_end
45 lines
1013 B
JavaScript
45 lines
1013 B
JavaScript
const jose = require('node-jose');
|
|
const jwt = require('jsonwebtoken');
|
|
|
|
module.exports = function ({
|
|
privateKey,
|
|
publicKey,
|
|
issuer
|
|
}) {
|
|
const keyStore = jose.JWK.createKeyStore();
|
|
const keyStoreReady = keyStore.add(privateKey, 'pem');
|
|
|
|
function encodeToken({sub, aud = issuer, plans, exp}) {
|
|
return keyStoreReady.then(jwk => jwt.sign({
|
|
sub,
|
|
exp,
|
|
plans,
|
|
kid: jwk.kid
|
|
}, privateKey, {
|
|
algorithm: 'RS512',
|
|
audience: aud,
|
|
issuer
|
|
}));
|
|
}
|
|
|
|
function decodeToken(token) {
|
|
return keyStoreReady.then(jwk => jwt.verify(token, publicKey, {
|
|
algorithm: 'RS512',
|
|
kid: jwk.kid,
|
|
issuer
|
|
})).then(() => jwt.decode(token));
|
|
}
|
|
|
|
function getPublicKeys() {
|
|
return keyStoreReady.then(() => {
|
|
keyStore.toJSON();
|
|
});
|
|
}
|
|
|
|
return {
|
|
encodeToken,
|
|
decodeToken,
|
|
getPublicKeys
|
|
};
|
|
};
|