Skip to content

Commit

Permalink
Merge pull request #74 from mozilla/better-error-messages
Browse files Browse the repository at this point in the history
for expired certificates or assertions, specify which in error messages ...
  • Loading branch information
callahad committed Nov 20, 2013
2 parents 558712b + f314d2f commit 8e3037a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 22 deletions.
7 changes: 4 additions & 3 deletions lib/assertion.js
Expand Up @@ -77,13 +77,14 @@ exports.verify = function(signedObject, publicKey, now, cb) {
// check iat
if (assertionParams.issuedAt) {
if (assertionParams.issuedAt.valueOf() > now.valueOf())
return cb("assertion issued later than verification date");
return cb("issued later than verification date");
}

// check exp expiration
if (assertionParams.expiresAt) {
if (assertionParams.expiresAt.valueOf() < now.valueOf())
return cb("assertion has expired");
if (assertionParams.expiresAt.valueOf() < now.valueOf()) {
return cb("expired");
}
}

cb(null, payload, assertionParams);
Expand Down
31 changes: 18 additions & 13 deletions lib/cert.js
Expand Up @@ -29,6 +29,7 @@ var serializeCertParamsInto = function(certParams, params) {
SERIALIZER._LEGACY_extractCertParamsFrom = function(params) {
var certParams = {};


certParams.publicKey = jwcrypto.loadPublicKey(JSON.stringify(params['public-key']));
delete params['public-key'];
certParams.principal = params.principal;
Expand Down Expand Up @@ -138,13 +139,24 @@ var verifyChain = function(certs, now, getRoot, cb) {
// we're done
cb(null, certParamsArray);
});

});

};

exports.verifyChain = verifyChain;

// msg is an error message returned by .verify, entity is either 'assertion' or
// 'certificate'
function improveVerifyErrorMessage(err, entity) {
// allow through the malformed signature
if (err === "issued later than verification date" ||
err === "expired") {
err = entity + " " + err;
} else if (err !== 'malformed signature') {
err = "bad signature in chain";
}
return err;
}

exports.verifyBundle = function(bundle, now, getRoot, cb) {
// unbundle
if (typeof(bundle) !== 'string' && !(bundle instanceof String)) {
Expand All @@ -162,23 +174,16 @@ exports.verifyBundle = function(bundle, now, getRoot, cb) {

// verify the chain
verifyChain(certs, now, getRoot, function(err, certParamsArray) {
// simplify error message
if (err) {
// allow through the malformed signature
if (err === 'malformed signature' ||
err === "assertion issued later than verification date" ||
err === "assertion has expired")
return cb(err);
else
return cb("bad signature in chain");
}
// ergonomic error messages
if (err) return cb(improveVerifyErrorMessage(err, 'certificate'));

// what was the last PK in the successful chain?
var lastPK = certParamsArray[certParamsArray.length - 1].certParams.publicKey;

// now verify the assertion
assertion.verify(signedAssertion, lastPK, now, function(err, payload, assertionParams) {
if (err) return cb(err);
// ergonomic error messages
if (err) return cb(improveVerifyErrorMessage(err, 'assertion'));

// we're good!
cb(null, certParamsArray, payload, assertionParams);
Expand Down
8 changes: 4 additions & 4 deletions test/assertion-test.js
Expand Up @@ -78,7 +78,7 @@ testUtils.addBatches(suite, function(alg, keysize) {
assert.isNotNull(assertionParams.expiresAt);
assert.isNotNull(assertionParams.issuer);
assert.isNotNull(assertionParams.audience);
assert.equal(assertionParams.audience, "https://example.com");
assert.equal(assertionParams.audience, "https://example.com");
assert.equal(assertionParams.expiresAt.valueOf(), in_a_minute.valueOf());
}
}
Expand Down Expand Up @@ -124,7 +124,7 @@ testUtils.addBatches(suite, function(alg, keysize) {
assert.isUndefined(payload);
},
"returns the right error message": function(err, payload, assertionParams) {
assert.equal(err, "assertion has expired");
assert.equal(err, "expired");
}
}
},
Expand Down Expand Up @@ -156,7 +156,7 @@ testUtils.addBatches(suite, function(alg, keysize) {
assert.isNotNull(payload.iss);
assert.isUndefined(payload.aud);
assert.equal(payload.exp, in_a_minute.valueOf());
assert.equal(payload.iat, in_a_minute.valueOf());
assert.equal(payload.iat, in_a_minute.valueOf());
}
},
"when verified with assertion": {
Expand All @@ -169,7 +169,7 @@ testUtils.addBatches(suite, function(alg, keysize) {
assert.isUndefined(payload);
},
"returns the right error message": function(err, payload, assertionParams) {
assert.equal(err, "assertion issued later than verification date");
assert.equal(err, "issued later than verification date");
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/vectors-test.js
Expand Up @@ -142,9 +142,9 @@ suite.addBatch(
this.callback);
},
"fails appropriately": function(err, certParamsArray, payload, assertionParams) {
assert.equal(err, "assertion has expired");
assert.equal(err, "certificate expired");
}
}
}
})

suite.addBatch(
Expand Down

0 comments on commit 8e3037a

Please sign in to comment.