|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const jwt = require('../'); |
| 4 | +const expect = require('chai').expect; |
| 5 | +const util = require('util'); |
| 6 | + |
| 7 | +function signWithSubject(payload, subject) { |
| 8 | + const options = {algorithm: 'none'}; |
| 9 | + if (subject !== undefined) { |
| 10 | + options.subject = subject; |
| 11 | + } |
| 12 | + return jwt.sign(payload, undefined, options); |
| 13 | +} |
| 14 | + |
| 15 | +describe('subject', function() { |
| 16 | + describe('`jwt.sign` "subject" option validation', function () { |
| 17 | + [ |
| 18 | + true, |
| 19 | + false, |
| 20 | + null, |
| 21 | + -1, |
| 22 | + 0, |
| 23 | + 1, |
| 24 | + -1.1, |
| 25 | + 1.1, |
| 26 | + -Infinity, |
| 27 | + Infinity, |
| 28 | + NaN, |
| 29 | + [], |
| 30 | + ['foo'], |
| 31 | + {}, |
| 32 | + {foo: 'bar'}, |
| 33 | + ].forEach((subject) => { |
| 34 | + it(`should error with with value ${util.inspect(subject)}`, function () { |
| 35 | + expect(() => signWithSubject({}, subject)).to.throw('"subject" must be a string'); |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + // undefined needs special treatment because {} is not the same as {subject: undefined} |
| 40 | + it('should error with with value undefined', function () { |
| 41 | + expect(() => jwt.sign({}, undefined, {subject: undefined, algorithm: 'none'})).to.throw( |
| 42 | + '"subject" must be a string' |
| 43 | + ); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should error when "sub" is in payload', function () { |
| 47 | + expect(() => signWithSubject({sub: 'bar'}, 'foo')).to.throw( |
| 48 | + 'Bad "options.subject" option. The payload already has an "sub" property.' |
| 49 | + ); |
| 50 | + }); |
| 51 | + |
| 52 | + |
| 53 | + it('should error with a string payload', function () { |
| 54 | + expect(() => signWithSubject('a string payload', 'foo')).to.throw( |
| 55 | + 'invalid subject option for string payload' |
| 56 | + ); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should error with a Buffer payload', function () { |
| 60 | + expect(() => signWithSubject(new Buffer('a Buffer payload'), 'foo')).to.throw( |
| 61 | + 'invalid subject option for object payload' |
| 62 | + ); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + describe('when signing and verifying a token with "subject" option', function () { |
| 67 | + it('should verify with a string "subject"', function () { |
| 68 | + const token = signWithSubject({}, 'foo'); |
| 69 | + const decoded = jwt.decode(token); |
| 70 | + const verified = jwt.verify(token, undefined, {subject: 'foo'}); |
| 71 | + expect(decoded).to.deep.equal(verified); |
| 72 | + expect(decoded.sub).to.equal('foo'); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should verify with a string "sub"', function () { |
| 76 | + const token = signWithSubject({sub: 'foo'}); |
| 77 | + const decoded = jwt.decode(token); |
| 78 | + const verified = jwt.verify(token, undefined, {subject: 'foo'}); |
| 79 | + expect(decoded).to.deep.equal(verified); |
| 80 | + expect(decoded.sub).to.equal('foo'); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should not verify "sub" if "verify.subject" option not provided', function() { |
| 84 | + const token = signWithSubject({sub: 'foo'}); |
| 85 | + const decoded = jwt.decode(token); |
| 86 | + const verified = jwt.verify(token, undefined); |
| 87 | + expect(decoded).to.deep.equal(verified); |
| 88 | + expect(decoded.sub).to.equal('foo'); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should error if "sub" does not match "verify.subject" option', function() { |
| 92 | + const token = signWithSubject({sub: 'foo'}); |
| 93 | + expect(() => jwt.verify(token, undefined, {subject: 'bar'})).to.throw( |
| 94 | + jwt.JsonWebTokenError, |
| 95 | + 'jwt subject invalid. expected: bar' |
| 96 | + ); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should error without "sub" and with "verify.subject" option', function() { |
| 100 | + const token = signWithSubject({}); |
| 101 | + expect(() => jwt.verify(token, undefined, {subject: 'foo'})).to.throw( |
| 102 | + jwt.JsonWebTokenError, |
| 103 | + 'jwt subject invalid. expected: foo' |
| 104 | + ); |
| 105 | + }); |
| 106 | + }); |
| 107 | +}); |
0 commit comments