|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const jwt = require('../'); |
| 4 | +const expect = require('chai').expect; |
| 5 | +const util = require('util'); |
| 6 | +const testUtils = require('./test-utils'); |
| 7 | + |
| 8 | +function signWithJWTId(jwtid, payload, callback) { |
| 9 | +const options = {algorithm: 'none'}; |
| 10 | +if (jwtid !== undefined) { |
| 11 | +options.jwtid = jwtid; |
| 12 | +} |
| 13 | +testUtils.signJWTHelper(payload, 'secret', options, callback); |
| 14 | +} |
| 15 | + |
| 16 | +describe('jwtid', function() { |
| 17 | +describe('`jwt.sign` "jwtid" option validation', function () { |
| 18 | +[ |
| 19 | +true, |
| 20 | +false, |
| 21 | +null, |
| 22 | +-1, |
| 23 | +0, |
| 24 | +1, |
| 25 | +-1.1, |
| 26 | +1.1, |
| 27 | +-Infinity, |
| 28 | +Infinity, |
| 29 | +NaN, |
| 30 | +[], |
| 31 | +['foo'], |
| 32 | +{}, |
| 33 | +{foo: 'bar'}, |
| 34 | +].forEach((jwtid) => { |
| 35 | +it(`should error with with value ${util.inspect(jwtid)}`, function (done) { |
| 36 | +signWithJWTId(jwtid, {}, (err) => { |
| 37 | +testUtils.asyncCheck(done, () => { |
| 38 | +expect(err).to.be.instanceOf(Error); |
| 39 | +expect(err).to.have.property('message', '"jwtid" must be a string'); |
| 40 | +}); |
| 41 | +}); |
| 42 | +}); |
| 43 | +}); |
| 44 | + |
| 45 | +// undefined needs special treatment because {} is not the same as {jwtid: undefined} |
| 46 | +it('should error with with value undefined', function (done) { |
| 47 | +testUtils.signJWTHelper({}, undefined, {jwtid: undefined, algorithm: 'none'}, (err) => { |
| 48 | +testUtils.asyncCheck(done, () => { |
| 49 | +expect(err).to.be.instanceOf(Error); |
| 50 | +expect(err).to.have.property('message', '"jwtid" must be a string'); |
| 51 | +}); |
| 52 | +}); |
| 53 | +}); |
| 54 | + |
| 55 | +it('should error when "jti" is in payload', function (done) { |
| 56 | +signWithJWTId('foo', {jti: 'bar'}, (err) => { |
| 57 | +testUtils.asyncCheck(done, () => { |
| 58 | +expect(err).to.be.instanceOf(Error); |
| 59 | +expect(err).to.have.property( |
| 60 | +'message', |
| 61 | +'Bad "options.jwtid" option. The payload already has an "jti" property.' |
| 62 | +); |
| 63 | +}); |
| 64 | +}); |
| 65 | +}); |
| 66 | + |
| 67 | +it('should error with a string payload', function (done) { |
| 68 | +signWithJWTId('foo', 'a string payload', (err) => { |
| 69 | +testUtils.asyncCheck(done, () => { |
| 70 | +expect(err).to.be.instanceOf(Error); |
| 71 | +expect(err).to.have.property( |
| 72 | +'message', |
| 73 | +'invalid jwtid option for string payload' |
| 74 | +); |
| 75 | +}); |
| 76 | +}); |
| 77 | +}); |
| 78 | + |
| 79 | +it('should error with a Buffer payload', function (done) { |
| 80 | +signWithJWTId('foo', new Buffer('a Buffer payload'), (err) => { |
| 81 | +testUtils.asyncCheck(done, () => { |
| 82 | +expect(err).to.be.instanceOf(Error); |
| 83 | +expect(err).to.have.property( |
| 84 | +'message', |
| 85 | +'invalid jwtid option for object payload' |
| 86 | +); |
| 87 | +}); |
| 88 | +}); |
| 89 | +}); |
| 90 | +}); |
| 91 | + |
| 92 | +describe('when signing and verifying a token', function () { |
| 93 | +it('should not verify "jti" if verify "jwtid" option not provided', function(done) { |
| 94 | +signWithJWTId(undefined, {jti: 'foo'}, (e1, token) => { |
| 95 | +testUtils.verifyJWTHelper(token, undefined, {}, (e2, decoded) => { |
| 96 | +testUtils.asyncCheck(done, () => { |
| 97 | +expect(e1).to.be.null; |
| 98 | +expect(e2).to.be.null; |
| 99 | +expect(decoded).to.have.property('jti', 'foo'); |
| 100 | +}); |
| 101 | +}) |
| 102 | +}); |
| 103 | +}); |
| 104 | + |
| 105 | +describe('with "jwtid" option', function () { |
| 106 | +it('should verify with "jwtid" option', function (done) { |
| 107 | +signWithJWTId('foo', {}, (e1, token) => { |
| 108 | +testUtils.verifyJWTHelper(token, undefined, {jwtid: 'foo'}, (e2, decoded) => { |
| 109 | +testUtils.asyncCheck(done, () => { |
| 110 | +expect(e1).to.be.null; |
| 111 | +expect(e2).to.be.null; |
| 112 | +expect(decoded).to.have.property('jti', 'foo'); |
| 113 | +}); |
| 114 | +}) |
| 115 | +}); |
| 116 | +}); |
| 117 | + |
| 118 | +it('should verify with "jti" in payload', function (done) { |
| 119 | +signWithJWTId(undefined, {jti: 'foo'}, (e1, token) => { |
| 120 | +testUtils.verifyJWTHelper(token, undefined, {jetid: 'foo'}, (e2, decoded) => { |
| 121 | +testUtils.asyncCheck(done, () => { |
| 122 | +expect(e1).to.be.null; |
| 123 | +expect(e2).to.be.null; |
| 124 | +expect(decoded).to.have.property('jti', 'foo'); |
| 125 | +}); |
| 126 | +}) |
| 127 | +}); |
| 128 | +}); |
| 129 | + |
| 130 | +it('should error if "jti" does not match verify "jwtid" option', function(done) { |
| 131 | +signWithJWTId(undefined, {jti: 'bar'}, (e1, token) => { |
| 132 | +testUtils.verifyJWTHelper(token, undefined, {jwtid: 'foo'}, (e2) => { |
| 133 | +testUtils.asyncCheck(done, () => { |
| 134 | +expect(e1).to.be.null; |
| 135 | +expect(e2).to.be.instanceOf(jwt.JsonWebTokenError); |
| 136 | +expect(e2).to.have.property('message', 'jwt jwtid invalid. expected: foo'); |
| 137 | +}); |
| 138 | +}) |
| 139 | +}); |
| 140 | +}); |
| 141 | + |
| 142 | +it('should error without "jti" and with verify "jwtid" option', function(done) { |
| 143 | +signWithJWTId(undefined, {}, (e1, token) => { |
| 144 | +testUtils.verifyJWTHelper(token, undefined, {jwtid: 'foo'}, (e2) => { |
| 145 | +testUtils.asyncCheck(done, () => { |
| 146 | +expect(e1).to.be.null; |
| 147 | +expect(e2).to.be.instanceOf(jwt.JsonWebTokenError); |
| 148 | +expect(e2).to.have.property('message', 'jwt jwtid invalid. expected: foo'); |
| 149 | +}); |
| 150 | +}) |
| 151 | +}); |
| 152 | +}); |
| 153 | +}); |
| 154 | +}); |
| 155 | +}); |
0 commit comments