-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add complete option in jwt.verify (#522)
* Add complete option in verify * Remove comment * Update README.md Co-Authored-By: javespi <[email protected]> * Move tests in a specific file
- Loading branch information
Showing
3 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
'use strict'; | ||
|
||
const jws = require('jws'); | ||
const expect = require('chai').expect; | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
const testUtils = require('./test-utils') | ||
|
||
describe('complete option', function () { | ||
const secret = fs.readFileSync(path.join(__dirname, 'priv.pem')); | ||
const pub = fs.readFileSync(path.join(__dirname, 'pub.pem')); | ||
|
||
const header = { alg: 'RS256' }; | ||
const payload = { iat: Math.floor(Date.now() / 1000 ) }; | ||
const signed = jws.sign({ header, payload, secret, encoding: 'utf8' }); | ||
const signature = jws.decode(signed).signature; | ||
|
||
[ | ||
{ | ||
description: 'should return header, payload and signature', | ||
complete: true, | ||
}, | ||
].forEach((testCase) => { | ||
it(testCase.description, function (done) { | ||
testUtils.verifyJWTHelper(signed, pub, { typ: 'JWT', complete: testCase.complete }, (err, decoded) => { | ||
testUtils.asyncCheck(done, () => { | ||
expect(err).to.be.null; | ||
expect(decoded.header).to.have.property('alg', header.alg); | ||
expect(decoded.payload).to.have.property('iat', payload.iat); | ||
expect(decoded).to.have.property('signature', signature); | ||
}); | ||
}); | ||
}); | ||
}); | ||
[ | ||
{ | ||
description: 'should return payload', | ||
complete: false, | ||
}, | ||
].forEach((testCase) => { | ||
it(testCase.description, function (done) { | ||
testUtils.verifyJWTHelper(signed, pub, { typ: 'JWT', complete: testCase.complete }, (err, decoded) => { | ||
testUtils.asyncCheck(done, () => { | ||
expect(err).to.be.null; | ||
expect(decoded.header).to.be.undefined; | ||
expect(decoded.payload).to.be.undefined; | ||
expect(decoded.signature).to.be.undefined; | ||
expect(decoded).to.have.property('iat', payload.iat); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters