add a few more tests

This commit is contained in:
AJ ONeal 2019-03-15 13:53:49 -06:00
parent 5060c505b6
commit e6de23532b
1 changed files with 49 additions and 36 deletions

View File

@ -23,42 +23,55 @@ keyfetch.oidcJwks(testIss).then(function (hits) {
/*global Promise*/ /*global Promise*/
var keypairs = require('keypairs.js'); var keypairs = require('keypairs.js');
keypairs.generate().then(function (pair) { keypairs.generate().then(function (pair) {
return keypairs.signJwt({ return Promise.all([
jwk: pair.private, iss: 'https://example.com/', sub: 'mikey', exp: '1h' keypairs.signJwt({
}).then(function (jwt) { jwk: pair.private, iss: 'https://example.com/', sub: 'mikey', exp: '1h'
return Promise.all([ }).then(function (jwt) {
keyfetch.jwt.verify(jwt, { jwk: pair.public }).then(function (verified) { return Promise.all([
if (!(verified.claims && verified.claims.exp)) { keyfetch.jwt.verify(jwt, { jwk: pair.public }).then(function (verified) {
throw new Error("malformed decoded token"); if (!(verified.claims && verified.claims.exp)) {
} throw new Error("malformed decoded token");
}) }
, keyfetch.jwt.verify(keyfetch.jwt.decode(jwt), { jwk: pair.public }).then(function (verified) { })
if (!(verified.claims && verified.claims.exp)) { , keyfetch.jwt.verify(keyfetch.jwt.decode(jwt), { jwk: pair.public }).then(function (verified) {
throw new Error("malformed decoded token"); if (!(verified.claims && verified.claims.exp)) {
} throw new Error("malformed decoded token");
}) }
, keyfetch.jwt.verify(jwt, { jwks: [pair.public] }) })
, keyfetch.jwt.verify(jwt, { jwk: pair.public, issuers: ['https://example.com/'] }) , keyfetch.jwt.verify(jwt, { jwks: [pair.public] })
, keyfetch.jwt.verify(jwt, { jwk: pair.public, issuers: ['https://example.com'] }) , keyfetch.jwt.verify(jwt, { jwk: pair.public, issuers: ['https://example.com/'] })
, keyfetch.jwt.verify(jwt, { jwk: pair.public, issuers: ['*'] }) , keyfetch.jwt.verify(jwt, { jwk: pair.public, issuers: ['https://example.com'] })
, keyfetch.jwt.verify(jwt, { jwk: pair.public, issuers: ['http://example.com'] }) , keyfetch.jwt.verify(jwt, { jwk: pair.public, issuers: ['*'] })
.then(e("bad scheme")).catch(throwIfNotExpected) , keyfetch.jwt.verify(jwt, { jwk: pair.public, issuers: ['http://example.com'] })
, keyfetch.jwt.verify(jwt, { jwk: pair.public, issuers: ['https://www.example.com'] }) .then(e("bad scheme")).catch(throwIfNotExpected)
.then(e("bad prefix")).catch(throwIfNotExpected) , keyfetch.jwt.verify(jwt, { jwk: pair.public, issuers: ['https://www.example.com'] })
, keyfetch.jwt.verify(jwt, { jwk: pair.public, issuers: ['https://wexample.com'] }) .then(e("bad prefix")).catch(throwIfNotExpected)
.then(e("bad sld")).catch(throwIfNotExpected) , keyfetch.jwt.verify(jwt, { jwk: pair.public, issuers: ['https://wexample.com'] })
, keyfetch.jwt.verify(jwt, { jwk: pair.public, issuers: ['https://example.comm'] }) .then(e("bad sld")).catch(throwIfNotExpected)
.then(e("bad tld")).catch(throwIfNotExpected) , keyfetch.jwt.verify(jwt, { jwk: pair.public, issuers: ['https://example.comm'] })
, keyfetch.jwt.verify(jwt, { jwk: pair.public, claims: { iss: 'https://example.com/' } }) .then(e("bad tld")).catch(throwIfNotExpected)
, keyfetch.jwt.verify(jwt, { jwk: pair.public, claims: { iss: 'https://example.com' } }) , keyfetch.jwt.verify(jwt, { jwk: pair.public, claims: { iss: 'https://example.com/' } })
.then(e("inexact claim")).catch(throwIfNotExpected) , keyfetch.jwt.verify(jwt, { jwk: pair.public, claims: { iss: 'https://example.com' } })
]).then(function () { .then(e("inexact claim")).catch(throwIfNotExpected)
console.log("JWT PASSES"); ]);
}).catch(function (err) { })
console.error("NONE SHALL PASS!"); , keypairs.signJwt({
console.error(err); jwk: pair.private, iss: false, sub: 'mikey', exp: '1h'
process.exit(1); }).then(function (jwt) {
}); return Promise.all([
keyfetch.jwt.verify(jwt, { jwk: pair.public })
, keyfetch.jwt.verify(jwt)
.then(e("should have an issuer")).catch(throwIfNotExpected)
, keyfetch.jwt.verify(jwt, { jwk: pair.public, issuers: ['https://example.com/'] })
.then(e("fail when issuer specified and doesn't exist")).catch(throwIfNotExpected)
]);
})
]).then(function () {
console.log("JWT PASSES");
}).catch(function (err) {
console.error("NONE SHALL PASS!");
console.error(err);
process.exit(1);
}); });
}); });
/* /*