|
| 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 signWithIssuer(issuer, payload, callback) { |
| 9 | + const options = {algorithm: 'none'}; |
| 10 | + if (issuer !== undefined) { |
| 11 | + options.issuer = issuer; |
| 12 | + } |
| 13 | + testUtils.signJWTHelper(payload, 'secret', options, callback); |
| 14 | +} |
| 15 | + |
| 16 | +describe('issuer', function() { |
| 17 | + describe('`jwt.sign` "issuer" 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((issuer) => { |
| 35 | + it(`should error with with value ${util.inspect(issuer)}`, function (done) { |
| 36 | + signWithIssuer(issuer, {}, (err) => { |
| 37 | + testUtils.asyncCheck(done, () => { |
| 38 | + expect(err).to.be.instanceOf(Error); |
| 39 | + expect(err).to.have.property('message', '"issuer" must be a string'); |
| 40 | + }); |
| 41 | + }); |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + // undefined needs special treatment because {} is not the same as {issuer: undefined} |
| 46 | + it('should error with with value undefined', function (done) { |
| 47 | + testUtils.signJWTHelper({}, undefined, {issuer: undefined, algorithm: 'none'}, (err) => { |
| 48 | + testUtils.asyncCheck(done, () => { |
| 49 | + expect(err).to.be.instanceOf(Error); |
| 50 | + expect(err).to.have.property('message', '"issuer" must be a string'); |
| 51 | + }); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should error when "iss" is in payload', function (done) { |
| 56 | + signWithIssuer('foo', {iss: 'bar'}, (err) => { |
| 57 | + testUtils.asyncCheck(done, () => { |
| 58 | + expect(err).to.be.instanceOf(Error); |
| 59 | + expect(err).to.have.property( |
| 60 | + 'message', |
| 61 | + 'Bad "options.issuer" option. The payload already has an "iss" property.' |
| 62 | + ); |
| 63 | + }); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should error with a string payload', function (done) { |
| 68 | + signWithIssuer('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 issuer option for string payload' |
| 74 | + ); |
| 75 | + }); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should error with a Buffer payload', function (done) { |
| 80 | + signWithIssuer('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 issuer option for object payload' |
| 86 | + ); |
| 87 | + }); |
| 88 | + }); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + describe('when signing and verifying a token', function () { |
| 93 | + it('should not verify "iss" if verify "issuer" option not provided', function(done) { |
| 94 | + signWithIssuer(undefined, {iss: '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('iss', 'foo'); |
| 100 | + }); |
| 101 | + }) |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + describe('with string "issuer" option', function () { |
| 106 | + it('should verify with a string "issuer"', function (done) { |
| 107 | + signWithIssuer('foo', {}, (e1, token) => { |
| 108 | + testUtils.verifyJWTHelper(token, undefined, {issuer: '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('iss', 'foo'); |
| 113 | + }); |
| 114 | + }) |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should verify with a string "iss"', function (done) { |
| 119 | + signWithIssuer(undefined, {iss: 'foo'}, (e1, token) => { |
| 120 | + testUtils.verifyJWTHelper(token, undefined, {issuer: '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('iss', 'foo'); |
| 125 | + }); |
| 126 | + }) |
| 127 | + }); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should error if "iss" does not match verify "issuer" option', function(done) { |
| 131 | + signWithIssuer(undefined, {iss: 'foobar'}, (e1, token) => { |
| 132 | + testUtils.verifyJWTHelper(token, undefined, {issuer: '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 issuer invalid. expected: foo'); |
| 137 | + }); |
| 138 | + }) |
| 139 | + }); |
| 140 | + }); |
| 141 | + |
| 142 | + it('should error without "iss" and with verify "issuer" option', function(done) { |
| 143 | + signWithIssuer(undefined, {}, (e1, token) => { |
| 144 | + testUtils.verifyJWTHelper(token, undefined, {issuer: '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 issuer invalid. expected: foo'); |
| 149 | + }); |
| 150 | + }) |
| 151 | + }); |
| 152 | + }); |
| 153 | + }); |
| 154 | + |
| 155 | + describe('with array "issuer" option', function () { |
| 156 | + it('should verify with a string "issuer"', function (done) { |
| 157 | + signWithIssuer('bar', {}, (e1, token) => { |
| 158 | + testUtils.verifyJWTHelper(token, undefined, {issuer: ['foo', 'bar']}, (e2, decoded) => { |
| 159 | + testUtils.asyncCheck(done, () => { |
| 160 | + expect(e1).to.be.null; |
| 161 | + expect(e2).to.be.null; |
| 162 | + expect(decoded).to.have.property('iss', 'bar'); |
| 163 | + }); |
| 164 | + }) |
| 165 | + }); |
| 166 | + }); |
| 167 | + |
| 168 | + it('should verify with a string "iss"', function (done) { |
| 169 | + signWithIssuer(undefined, {iss: 'foo'}, (e1, token) => { |
| 170 | + testUtils.verifyJWTHelper(token, undefined, {issuer: ['foo', 'bar']}, (e2, decoded) => { |
| 171 | + testUtils.asyncCheck(done, () => { |
| 172 | + expect(e1).to.be.null; |
| 173 | + expect(e2).to.be.null; |
| 174 | + expect(decoded).to.have.property('iss', 'foo'); |
| 175 | + }); |
| 176 | + }) |
| 177 | + }); |
| 178 | + }); |
| 179 | + |
| 180 | + it('should error if "iss" does not match verify "issuer" option', function(done) { |
| 181 | + signWithIssuer(undefined, {iss: 'foobar'}, (e1, token) => { |
| 182 | + testUtils.verifyJWTHelper(token, undefined, {issuer: ['foo', 'bar']}, (e2) => { |
| 183 | + testUtils.asyncCheck(done, () => { |
| 184 | + expect(e1).to.be.null; |
| 185 | + expect(e2).to.be.instanceOf(jwt.JsonWebTokenError); |
| 186 | + expect(e2).to.have.property('message', 'jwt issuer invalid. expected: foo,bar'); |
| 187 | + }); |
| 188 | + }) |
| 189 | + }); |
| 190 | + }); |
| 191 | + |
| 192 | + it('should error without "iss" and with verify "issuer" option', function(done) { |
| 193 | + signWithIssuer(undefined, {}, (e1, token) => { |
| 194 | + testUtils.verifyJWTHelper(token, undefined, {issuer: ['foo', 'bar']}, (e2) => { |
| 195 | + testUtils.asyncCheck(done, () => { |
| 196 | + expect(e1).to.be.null; |
| 197 | + expect(e2).to.be.instanceOf(jwt.JsonWebTokenError); |
| 198 | + expect(e2).to.have.property('message', 'jwt issuer invalid. expected: foo,bar'); |
| 199 | + }); |
| 200 | + }) |
| 201 | + }); |
| 202 | + }); |
| 203 | + }); |
| 204 | + }); |
| 205 | +}); |
0 commit comments