mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-11-24 06:35:49 +03:00
5ffdfe9875
no-issue
43 lines
965 B
JavaScript
43 lines
965 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}) {
|
|
return keyStoreReady.then(jwk => jwt.sign({
|
|
sub,
|
|
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
|
|
};
|
|
};
|