mirror of
https://github.com/hanzoai/jose.git
synced 2026-10-30 14:46:24 +00:00
BREAKING CHANGE: JWS.verify returned payloads are now always buffers BREAKING CHANGE: JWS.verify options `encoding` and `parse` were removed
65 lines
2.7 KiB
JavaScript
65 lines
2.7 KiB
JavaScript
const test = require('ava')
|
|
|
|
const recipe = require('./recipes').get('4.2 rfc7797')
|
|
|
|
const { JWS, JWK: { asKey, generateSync }, JWKS: { KeyStore }, errors } = require('../..')
|
|
|
|
const { input: { payload, key: jwk }, signing: { protected: header } } = recipe
|
|
|
|
const key = asKey(jwk)
|
|
|
|
const keystoreEmpty = new KeyStore()
|
|
const keystoreMatchOne = new KeyStore(generateSync(key.kty, key.length, { alg: key.alg, use: key.use }), key)
|
|
const keystoreMatchMore = new KeyStore(generateSync(key.kty, key.length, { alg: key.alg, use: key.use, kid: key.kid }), key, asKey(key))
|
|
const keystoreMatchNone = new KeyStore(generateSync(key.kty), generateSync(key.kty))
|
|
|
|
test(`${recipe.title} - flattened sign`, t => {
|
|
t.deepEqual(JWS.sign.flattened(payload, key, header), recipe.output.json_flat)
|
|
})
|
|
|
|
test(`${recipe.title} - general sign`, t => {
|
|
t.deepEqual(JWS.sign.general(payload, key, header), recipe.output.json)
|
|
})
|
|
|
|
test(`${recipe.title} - flattened verify`, t => {
|
|
t.deepEqual(JWS.verify(recipe.output.json_flat, key, { crit: ['b64'] }), Buffer.from(payload))
|
|
})
|
|
|
|
test(`${recipe.title} - general verify`, t => {
|
|
t.deepEqual(JWS.verify(recipe.output.json, key, { crit: ['b64'] }), Buffer.from(payload))
|
|
})
|
|
|
|
;[keystoreMatchOne, keystoreMatchMore].forEach((keystore, i) => {
|
|
test(`${recipe.title} - flattened verify (using keystore ${i + 1}/2)`, t => {
|
|
t.deepEqual(JWS.verify(recipe.output.json_flat, keystore, { crit: ['b64'] }), Buffer.from(payload))
|
|
})
|
|
|
|
test(`${recipe.title} - general verify (using keystore ${i + 1}/2)`, t => {
|
|
t.deepEqual(JWS.verify(recipe.output.json, keystore, { crit: ['b64'] }), Buffer.from(payload))
|
|
})
|
|
})
|
|
|
|
test(`${recipe.title} - flattened verify (failing)`, t => {
|
|
t.throws(() => {
|
|
JWS.verify(recipe.output.json_flat, keystoreMatchNone, { crit: ['b64'] })
|
|
}, { instanceOf: errors.JWSVerificationFailed, code: 'ERR_JWS_VERIFICATION_FAILED' })
|
|
})
|
|
|
|
test(`${recipe.title} - general verify (failing)`, t => {
|
|
t.throws(() => {
|
|
JWS.verify(recipe.output.json, keystoreMatchNone, { crit: ['b64'] })
|
|
}, { instanceOf: errors.JWSVerificationFailed, code: 'ERR_JWS_VERIFICATION_FAILED' })
|
|
})
|
|
|
|
test(`${recipe.title} - flattened verify (using empty keystore)`, t => {
|
|
t.throws(() => {
|
|
JWS.verify(recipe.output.json_flat, keystoreEmpty, { crit: ['b64'] })
|
|
}, { instanceOf: errors.JWKSNoMatchingKey, code: 'ERR_JWKS_NO_MATCHING_KEY', message: 'no matching key found in the KeyStore' })
|
|
})
|
|
|
|
test(`${recipe.title} - general verify (using empty keystore)`, t => {
|
|
t.throws(() => {
|
|
JWS.verify(recipe.output.json, keystoreEmpty, { crit: ['b64'] })
|
|
}, { instanceOf: errors.JWKSNoMatchingKey, code: 'ERR_JWKS_NO_MATCHING_KEY', message: 'no matching key found in the KeyStore' })
|
|
})
|