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*/
var keypairs = require('keypairs.js');
keypairs.generate().then(function (pair) {
return keypairs.signJwt({
jwk: pair.private, iss: 'https://example.com/', sub: 'mikey', exp: '1h'
}).then(function (jwt) {
return Promise.all([
keyfetch.jwt.verify(jwt, { jwk: pair.public }).then(function (verified) {
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)) {
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, { jwk: pair.public, issuers: ['https://example.com'] })
, keyfetch.jwt.verify(jwt, { jwk: pair.public, issuers: ['*'] })
, keyfetch.jwt.verify(jwt, { jwk: pair.public, issuers: ['http://example.com'] })
.then(e("bad scheme")).catch(throwIfNotExpected)
, keyfetch.jwt.verify(jwt, { jwk: pair.public, issuers: ['https://www.example.com'] })
.then(e("bad prefix")).catch(throwIfNotExpected)
, keyfetch.jwt.verify(jwt, { jwk: pair.public, issuers: ['https://wexample.com'] })
.then(e("bad sld")).catch(throwIfNotExpected)
, keyfetch.jwt.verify(jwt, { jwk: pair.public, issuers: ['https://example.comm'] })
.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)
]).then(function () {
console.log("JWT PASSES");
}).catch(function (err) {
console.error("NONE SHALL PASS!");
console.error(err);
process.exit(1);
});
return Promise.all([
keypairs.signJwt({
jwk: pair.private, iss: 'https://example.com/', sub: 'mikey', exp: '1h'
}).then(function (jwt) {
return Promise.all([
keyfetch.jwt.verify(jwt, { jwk: pair.public }).then(function (verified) {
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)) {
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, { jwk: pair.public, issuers: ['https://example.com'] })
, keyfetch.jwt.verify(jwt, { jwk: pair.public, issuers: ['*'] })
, keyfetch.jwt.verify(jwt, { jwk: pair.public, issuers: ['http://example.com'] })
.then(e("bad scheme")).catch(throwIfNotExpected)
, keyfetch.jwt.verify(jwt, { jwk: pair.public, issuers: ['https://www.example.com'] })
.then(e("bad prefix")).catch(throwIfNotExpected)
, keyfetch.jwt.verify(jwt, { jwk: pair.public, issuers: ['https://wexample.com'] })
.then(e("bad sld")).catch(throwIfNotExpected)
, keyfetch.jwt.verify(jwt, { jwk: pair.public, issuers: ['https://example.comm'] })
.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)
]);
})
, keypairs.signJwt({
jwk: pair.private, iss: false, sub: 'mikey', exp: '1h'
}).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);
});
});
/*