0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-11 02:12:21 -05:00

Added test coverage for JWKS token verification

refs https://github.com/TryGhost/Team/issues/1640

- Adds coverage for token verification based on public key exposed through the `/.well-known/jwks.json` endpoint
This commit is contained in:
Naz 2022-05-23 17:59:33 +08:00
parent 9e96916a6d
commit 900f7951b7
2 changed files with 21 additions and 0 deletions

View file

@ -20,6 +20,7 @@
"devDependencies": {
"@types/node-jose": "1.1.8",
"c8": "7.11.3",
"jwk-to-pem": "2.0.5",
"mocha": "10.0.0",
"nock": "13.2.4",
"should": "13.2.3",

View file

@ -1,4 +1,6 @@
const assert = require('assert');
const jwt = require('jsonwebtoken');
const jwkToPem = require('jwk-to-pem');
const TokenService = require('../../../../lib/services/token');
describe('TokenService', function () {
@ -27,4 +29,22 @@ describe('TokenService', function () {
assert.equal(decodedToken.sub, 'member@example.com');
});
});
describe('getPublicKeys', function () {
it('can verify the token using public keys', async function () {
const token = await tokenService.encodeIdentityToken({sub: 'member@example.com'});
const jwks = await tokenService.getPublicKeys();
const publicKey = jwkToPem(jwks.keys[0]);
const decodedToken = jwt.verify(token, publicKey, {
algorithms: ['RS512'],
issuer: this._issuer
});
assert.deepEqual(Object.keys(decodedToken), ['sub', 'kid', 'iat', 'exp', 'aud', 'iss']);
assert.equal(decodedToken.aud, 'http://127.0.0.1:2369/members/api');
assert.equal(decodedToken.iss, 'http://127.0.0.1:2369/members/api');
assert.equal(decodedToken.sub, 'member@example.com');
});
});
});