Files
jose/test/cookbook/4_3.ecdsa_signature.test.js
Filip Skokan 0b834e7e6e refactor: removed payload parsing from JWS.verify
BREAKING CHANGE: JWS.verify returned payloads are now always buffers
BREAKING CHANGE: JWS.verify options `encoding` and `parse` were removed
2020-09-08 14:12:04 +02:00

96 lines
3.7 KiB
JavaScript

const test = require('ava')
const recipe = require('./recipes').get('4.3')
const { sig: verifiers } = require('./verifiers')
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.crv, { alg: key.alg, use: key.use }), key)
const keystoreMatchMore = new KeyStore(generateSync(key.kty, key.crv, { 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} - compact sign (random)`, t => {
const res = JWS.sign(payload, key, header)
verifiers.compact(t, res, recipe.output.compact)
t.deepEqual(JWS.verify(res, key), payload)
})
test(`${recipe.title} - flattened sign (random)`, t => {
const res = JWS.sign.flattened(payload, key, header)
verifiers.flattened(t, res, recipe.output.json_flat)
t.deepEqual(JWS.verify(res, key), payload)
})
test(`${recipe.title} - general sign (random)`, t => {
const res = JWS.sign.general(payload, key, header)
verifiers.general(t, res, recipe.output.json)
t.deepEqual(JWS.verify(res, key), payload)
})
test(`${recipe.title} - compact verify`, t => {
t.deepEqual(JWS.verify(recipe.output.compact, key), payload)
})
test(`${recipe.title} - flattened verify`, t => {
t.deepEqual(JWS.verify(recipe.output.json_flat, key), payload)
})
test(`${recipe.title} - general verify`, t => {
t.deepEqual(JWS.verify(recipe.output.json, key), payload)
})
;[keystoreMatchOne, keystoreMatchMore].forEach((keystore, i) => {
test(`${recipe.title} - compact verify (using keystore ${i + 1}/2)`, t => {
t.deepEqual(JWS.verify(recipe.output.compact, keystore), payload)
})
test(`${recipe.title} - flattened verify (using keystore ${i + 1}/2)`, t => {
t.deepEqual(JWS.verify(recipe.output.json_flat, keystore), payload)
})
test(`${recipe.title} - general verify (using keystore ${i + 1}/2)`, t => {
t.deepEqual(JWS.verify(recipe.output.json, keystore), payload)
})
})
test(`${recipe.title} - compact verify (failing)`, t => {
t.throws(() => {
JWS.verify(recipe.output.compact, keystoreMatchNone)
}, { instanceOf: errors.JWKSNoMatchingKey, code: 'ERR_JWKS_NO_MATCHING_KEY', message: 'no matching key found in the KeyStore' })
})
test(`${recipe.title} - flattened verify (failing)`, t => {
t.throws(() => {
JWS.verify(recipe.output.json_flat, keystoreMatchNone)
}, { instanceOf: errors.JWKSNoMatchingKey, code: 'ERR_JWKS_NO_MATCHING_KEY', message: 'no matching key found in the KeyStore' })
})
test(`${recipe.title} - general verify (failing)`, t => {
t.throws(() => {
JWS.verify(recipe.output.json, keystoreMatchNone)
}, { instanceOf: errors.JWKSNoMatchingKey, code: 'ERR_JWKS_NO_MATCHING_KEY', message: 'no matching key found in the KeyStore' })
})
test(`${recipe.title} - compact verify (using empty keystore)`, t => {
t.throws(() => {
JWS.verify(recipe.output.compact, keystoreEmpty)
}, { instanceOf: errors.JWKSNoMatchingKey, code: 'ERR_JWKS_NO_MATCHING_KEY', message: 'no matching key found in the KeyStore' })
})
test(`${recipe.title} - flattened verify (using empty keystore)`, t => {
t.throws(() => {
JWS.verify(recipe.output.json_flat, keystoreEmpty)
}, { 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)
}, { instanceOf: errors.JWKSNoMatchingKey, code: 'ERR_JWKS_NO_MATCHING_KEY', message: 'no matching key found in the KeyStore' })
})