2021-07-14 16:11:33 +03:00
|
|
|
const {UnauthorizedError} = require('@tryghost/errors');
|
2020-09-17 17:59:02 +03:00
|
|
|
const jwt = require('jsonwebtoken');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {import('jsonwebtoken').Secret} Secret
|
|
|
|
* @typedef {string} JSONWebToken
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Object<string, any>} Data
|
|
|
|
*/
|
|
|
|
|
|
|
|
module.exports = class JWTTokenProvider {
|
|
|
|
/**
|
|
|
|
* @param {Secret} secret
|
|
|
|
*/
|
|
|
|
constructor(secret) {
|
|
|
|
this.secret = secret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Data} data
|
|
|
|
* @returns {Promise<JSONWebToken>}
|
|
|
|
*/
|
|
|
|
async create(data) {
|
|
|
|
const token = jwt.sign(data, this.secret, {
|
|
|
|
algorithm: 'HS256',
|
|
|
|
expiresIn: '10m'
|
|
|
|
});
|
|
|
|
|
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {JSONWebToken} token
|
|
|
|
* @returns {Promise<Data>}
|
|
|
|
*/
|
|
|
|
async validate(token) {
|
|
|
|
/** @type any */
|
|
|
|
const claims = jwt.verify(token, this.secret, {
|
|
|
|
algorithms: ['HS256'],
|
|
|
|
maxAge: '10m'
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!claims || typeof claims === 'string') {
|
2022-02-15 15:27:22 +03:00
|
|
|
// @TODO: throw a detailed error message here
|
2021-07-14 16:11:33 +03:00
|
|
|
throw new UnauthorizedError();
|
2020-09-17 17:59:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return claims;
|
|
|
|
}
|
|
|
|
};
|