Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't error out on undefined options #793

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions sign.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ function validate(schema, allowUnknown, object, parameterName) {
}
Object.keys(object)
.forEach(function(key) {
// Don't validate undefined attribute
if (object[key] === undefined) {
return;
}

const validator = schema[key];
if (!validator) {
if (!allowUnknown) {
Expand Down
12 changes: 1 addition & 11 deletions test/claim-aud.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('audience', function() {
{},
{foo: 'bar'},
].forEach((audience) => {
it(`should error with with value ${util.inspect(audience)}`, function (done) {
it(`should error with value ${util.inspect(audience)}`, function (done) {
signWithAudience(audience, {}, (err) => {
testUtils.asyncCheck(done, () => {
expect(err).to.be.instanceOf(Error);
Expand All @@ -45,16 +45,6 @@ describe('audience', function() {
});
});

// undefined needs special treatment because {} is not the same as {aud: undefined}
it('should error with with value undefined', function (done) {
testUtils.signJWTHelper({}, 'secret', {audience: undefined, algorithm: 'HS256'}, (err) => {
testUtils.asyncCheck(done, () => {
expect(err).to.be.instanceOf(Error);
expect(err).to.have.property('message', '"audience" must be a string or array');
});
});
});

it('should error when "aud" is in payload', function (done) {
signWithAudience('my_aud', {aud: ''}, (err) => {
testUtils.asyncCheck(done, () => {
Expand Down
20 changes: 3 additions & 17 deletions test/claim-exp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('expires', function() {
{},
{foo: 'bar'},
].forEach((expiresIn) => {
it(`should error with with value ${util.inspect(expiresIn)}`, function (done) {
it(`should error with value ${util.inspect(expiresIn)}`, function (done) {
signWithExpiresIn(expiresIn, {}, (err) => {
testUtils.asyncCheck(done, () => {
expect(err).to.be.instanceOf(Error);
Expand All @@ -45,19 +45,6 @@ describe('expires', function() {
});
});

// undefined needs special treatment because {} is not the same as {expiresIn: undefined}
it('should error with with value undefined', function (done) {
testUtils.signJWTHelper({}, 'secret', {expiresIn: undefined, algorithm: 'HS256'}, (err) => {
testUtils.asyncCheck(done, () => {
expect(err).to.be.instanceOf(Error);
expect(err).to.have.property(
'message',
'"expiresIn" should be a number of seconds or string representing a timespan'
);
});
});
});

it ('should error when "exp" is in payload', function(done) {
signWithExpiresIn(100, {exp: 100}, (err) => {
testUtils.asyncCheck(done, () => {
Expand Down Expand Up @@ -94,7 +81,6 @@ describe('expires', function() {
true,
false,
null,
undefined,
'',
' ',
'invalid',
Expand All @@ -103,7 +89,7 @@ describe('expires', function() {
{},
{foo: 'bar'},
].forEach((exp) => {
it(`should error with with value ${util.inspect(exp)}`, function (done) {
it(`should error with value ${util.inspect(exp)}`, function (done) {
signWithExpiresIn(undefined, {exp}, (err) => {
testUtils.asyncCheck(done, () => {
expect(err).to.be.instanceOf(Error);
Expand All @@ -130,7 +116,7 @@ describe('expires', function() {
{},
{foo: 'bar'},
].forEach((exp) => {
it(`should error with with value ${util.inspect(exp)}`, function (done) {
it(`should error with value ${util.inspect(exp)}`, function (done) {
const header = { alg: 'HS256' };
const payload = { exp };
const token = jws.sign({ header, payload, secret: 'secret', encoding: 'utf8' });
Expand Down
10 changes: 0 additions & 10 deletions test/claim-iat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,6 @@ describe('issue at', function() {
});
});
});

// undefined needs special treatment because {} is not the same as {iat: undefined}
it('should error with iat of undefined', function (done) {
testUtils.signJWTHelper({iat: undefined}, 'secret', {algorithm: 'HS256'}, (err) => {
testUtils.asyncCheck(done, () => {
expect(err).to.be.instanceOf(Error);
expect(err.message).to.equal('"iat" should be a number of seconds');
});
});
});
});

describe('"iat" in payload with "maxAge" option validation', function () {
Expand Down
12 changes: 1 addition & 11 deletions test/claim-iss.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('issuer', function() {
{},
{foo: 'bar'},
].forEach((issuer) => {
it(`should error with with value ${util.inspect(issuer)}`, function (done) {
it(`should error with value ${util.inspect(issuer)}`, function (done) {
signWithIssuer(issuer, {}, (err) => {
testUtils.asyncCheck(done, () => {
expect(err).to.be.instanceOf(Error);
Expand All @@ -42,16 +42,6 @@ describe('issuer', function() {
});
});

// undefined needs special treatment because {} is not the same as {issuer: undefined}
it('should error with with value undefined', function (done) {
testUtils.signJWTHelper({}, 'secret', {issuer: undefined, algorithm: 'HS256'}, (err) => {
testUtils.asyncCheck(done, () => {
expect(err).to.be.instanceOf(Error);
expect(err).to.have.property('message', '"issuer" must be a string');
});
});
});

it('should error when "iss" is in payload', function (done) {
signWithIssuer('foo', {iss: 'bar'}, (err) => {
testUtils.asyncCheck(done, () => {
Expand Down
12 changes: 1 addition & 11 deletions test/claim-jti.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('jwtid', function() {
{},
{foo: 'bar'},
].forEach((jwtid) => {
it(`should error with with value ${util.inspect(jwtid)}`, function (done) {
it(`should error with value ${util.inspect(jwtid)}`, function (done) {
signWithJWTId(jwtid, {}, (err) => {
testUtils.asyncCheck(done, () => {
expect(err).to.be.instanceOf(Error);
Expand All @@ -42,16 +42,6 @@ describe('jwtid', function() {
});
});

// undefined needs special treatment because {} is not the same as {jwtid: undefined}
it('should error with with value undefined', function (done) {
testUtils.signJWTHelper({}, 'secret', {jwtid: undefined, algorithm: 'HS256'}, (err) => {
testUtils.asyncCheck(done, () => {
expect(err).to.be.instanceOf(Error);
expect(err).to.have.property('message', '"jwtid" must be a string');
});
});
});

it('should error when "jti" is in payload', function (done) {
signWithJWTId('foo', {jti: 'bar'}, (err) => {
testUtils.asyncCheck(done, () => {
Expand Down
20 changes: 3 additions & 17 deletions test/claim-nbf.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('not before', function() {
{},
{foo: 'bar'},
].forEach((notBefore) => {
it(`should error with with value ${util.inspect(notBefore)}`, function (done) {
it(`should error with value ${util.inspect(notBefore)}`, function (done) {
signWithNotBefore(notBefore, {}, (err) => {
testUtils.asyncCheck(done, () => {
expect(err).to.be.instanceOf(Error);
Expand All @@ -45,19 +45,6 @@ describe('not before', function() {
});
});

// undefined needs special treatment because {} is not the same as {notBefore: undefined}
it('should error with with value undefined', function (done) {
testUtils.signJWTHelper({}, 'secret', {notBefore: undefined, algorithm: 'HS256'}, (err) => {
testUtils.asyncCheck(done, () => {
expect(err).to.be.instanceOf(Error);
expect(err).to.have.property(
'message',
'"notBefore" should be a number of seconds or string representing a timespan'
);
});
});
});

it('should error when "nbf" is in payload', function (done) {
signWithNotBefore(100, {nbf: 100}, (err) => {
testUtils.asyncCheck(done, () => {
Expand Down Expand Up @@ -94,7 +81,6 @@ describe('not before', function() {
true,
false,
null,
undefined,
'',
' ',
'invalid',
Expand All @@ -103,7 +89,7 @@ describe('not before', function() {
{},
{foo: 'bar'},
].forEach((nbf) => {
it(`should error with with value ${util.inspect(nbf)}`, function (done) {
it(`should error with value ${util.inspect(nbf)}`, function (done) {
signWithNotBefore(undefined, {nbf}, (err) => {
testUtils.asyncCheck(done, () => {
expect(err).to.be.instanceOf(Error);
Expand All @@ -130,7 +116,7 @@ describe('not before', function() {
{},
{foo: 'bar'},
].forEach((nbf) => {
it(`should error with with value ${util.inspect(nbf)}`, function (done) {
it(`should error with value ${util.inspect(nbf)}`, function (done) {
const header = { alg: 'HS256' };
const payload = { nbf };
const token = jws.sign({ header, payload, secret: 'secret', encoding: 'utf8' });
Expand Down
12 changes: 1 addition & 11 deletions test/claim-sub.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('subject', function() {
{},
{foo: 'bar'},
].forEach((subject) => {
it(`should error with with value ${util.inspect(subject)}`, function (done) {
it(`should error with value ${util.inspect(subject)}`, function (done) {
signWithSubject(subject, {}, (err) => {
testUtils.asyncCheck(done, () => {
expect(err).to.be.instanceOf(Error);
Expand All @@ -42,16 +42,6 @@ describe('subject', function() {
});
});

// undefined needs special treatment because {} is not the same as {subject: undefined}
it('should error with with value undefined', function (done) {
testUtils.signJWTHelper({}, 'secret', {subject: undefined, algorithm: 'HS256'}, (err) => {
testUtils.asyncCheck(done, () => {
expect(err).to.be.instanceOf(Error);
expect(err).to.have.property('message', '"subject" must be a string');
});
});
});

it('should error when "sub" is in payload', function (done) {
signWithSubject('foo', {sub: 'bar'}, (err) => {
testUtils.asyncCheck(done, () => {
Expand Down
12 changes: 1 addition & 11 deletions test/header-kid.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('keyid', function() {
{},
{foo: 'bar'},
].forEach((keyid) => {
it(`should error with with value ${util.inspect(keyid)}`, function (done) {
it(`should error with value ${util.inspect(keyid)}`, function (done) {
signWithKeyId(keyid, {}, (err) => {
testUtils.asyncCheck(done, () => {
expect(err).to.be.instanceOf(Error);
Expand All @@ -41,16 +41,6 @@ describe('keyid', function() {
});
});
});

// undefined needs special treatment because {} is not the same as {keyid: undefined}
it('should error with with value undefined', function (done) {
testUtils.signJWTHelper({}, 'secret', {keyid: undefined, algorithm: 'HS256'}, (err) => {
testUtils.asyncCheck(done, () => {
expect(err).to.be.instanceOf(Error);
expect(err).to.have.property('message', '"keyid" must be a string');
});
});
});
});

describe('when signing a token', function () {
Expand Down
Loading