mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-03-18 02:21:47 -05:00
Added JWTTokenProvider to preserve functionality
no-issue This allows the magic-link module to be used with the current functionality with minimal changes
This commit is contained in:
parent
37c8c15dd6
commit
a208a6c957
2 changed files with 53 additions and 2 deletions
50
ghost/magic-link/JWTTokenProvider.js
Normal file
50
ghost/magic-link/JWTTokenProvider.js
Normal file
|
@ -0,0 +1,50 @@
|
|||
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') {
|
||||
throw new Error();
|
||||
}
|
||||
|
||||
return claims;
|
||||
}
|
||||
};
|
|
@ -1,6 +1,7 @@
|
|||
const should = require('should');
|
||||
const sinon = require('sinon');
|
||||
const MagicLink = require('../');
|
||||
const JWTTokenProvider = require('../JWTTokenProvider');
|
||||
const crypto = require('crypto');
|
||||
|
||||
const sandbox = sinon.createSandbox();
|
||||
|
@ -14,7 +15,7 @@ describe('MagicLink', function () {
|
|||
describe('#sendMagicLink', function () {
|
||||
it('Sends an email to the user with a link generated from getSigninURL(token, type)', async function () {
|
||||
const options = {
|
||||
secret,
|
||||
tokenProvider: new JWTTokenProvider(secret),
|
||||
getSigninURL: sandbox.stub().returns('FAKEURL'),
|
||||
getText: sandbox.stub().returns('SOMETEXT'),
|
||||
getHTML: sandbox.stub().returns('SOMEHTML'),
|
||||
|
@ -57,7 +58,7 @@ describe('MagicLink', function () {
|
|||
describe('#getDataFromToken', function () {
|
||||
it('Returns the user data which from the token that was encoded by #sendMagicLink', async function () {
|
||||
const options = {
|
||||
secret,
|
||||
tokenProvider: new JWTTokenProvider(secret),
|
||||
getSigninURL: sandbox.stub().returns('FAKEURL'),
|
||||
getText: sandbox.stub().returns('SOMETEXT'),
|
||||
getHTML: sandbox.stub().returns('SOMEHTML'),
|
||||
|
|
Loading…
Add table
Reference in a new issue