mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-18 07:51:55 +03:00
7ec3f61e71
no-issue This is to better fit the index.js, lib model
45 lines
1.0 KiB
JavaScript
45 lines
1.0 KiB
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,
|
|
plans,
|
|
kid: jwk.kid
|
|
}, privateKey, {
|
|
algorithm: 'RS512',
|
|
audience: aud,
|
|
expiresIn: exp,
|
|
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
|
|
};
|
|
};
|