UNPKG

3.12 kBJavaScriptView Raw
1const CODES = {
2 JOSEAlgNotWhitelisted: 'ERR_JOSE_ALG_NOT_WHITELISTED',
3 JOSECritNotUnderstood: 'ERR_JOSE_CRIT_NOT_UNDERSTOOD',
4 JOSEInvalidEncoding: 'ERR_JOSE_INVALID_ENCODING',
5 JOSEMultiError: 'ERR_JOSE_MULTIPLE_ERRORS',
6 JOSENotSupported: 'ERR_JOSE_NOT_SUPPORTED',
7 JWEDecryptionFailed: 'ERR_JWE_DECRYPTION_FAILED',
8 JWEInvalid: 'ERR_JWE_INVALID',
9 JWKImportFailed: 'ERR_JWK_IMPORT_FAILED',
10 JWKInvalid: 'ERR_JWK_INVALID',
11 JWKKeySupport: 'ERR_JWK_KEY_SUPPORT',
12 JWKSNoMatchingKey: 'ERR_JWKS_NO_MATCHING_KEY',
13 JWSInvalid: 'ERR_JWS_INVALID',
14 JWSVerificationFailed: 'ERR_JWS_VERIFICATION_FAILED',
15 JWTClaimInvalid: 'ERR_JWT_CLAIM_INVALID',
16 JWTExpired: 'ERR_JWT_EXPIRED',
17 JWTMalformed: 'ERR_JWT_MALFORMED'
18}
19
20const DEFAULT_MESSAGES = {
21 JWEDecryptionFailed: 'decryption operation failed',
22 JWEInvalid: 'JWE invalid',
23 JWKSNoMatchingKey: 'no matching key found in the KeyStore',
24 JWSInvalid: 'JWS invalid',
25 JWSVerificationFailed: 'signature verification failed'
26}
27
28class JOSEError extends Error {
29 constructor (message) {
30 super(message)
31 if (message === undefined) {
32 this.message = DEFAULT_MESSAGES[this.constructor.name]
33 }
34 this.name = this.constructor.name
35 this.code = CODES[this.constructor.name]
36 Error.captureStackTrace(this, this.constructor)
37 }
38}
39
40const isMulti = e => e instanceof JOSEMultiError
41class JOSEMultiError extends JOSEError {
42 constructor (errors) {
43 super()
44 let i
45 while ((i = errors.findIndex(isMulti)) && i !== -1) {
46 errors.splice(i, 1, ...errors[i])
47 }
48 Object.defineProperty(this, 'errors', { value: errors })
49 }
50
51 * [Symbol.iterator] () {
52 for (const error of this.errors) {
53 yield error
54 }
55 }
56}
57module.exports.JOSEError = JOSEError
58
59module.exports.JOSEAlgNotWhitelisted = class JOSEAlgNotWhitelisted extends JOSEError {}
60module.exports.JOSECritNotUnderstood = class JOSECritNotUnderstood extends JOSEError {}
61module.exports.JOSEInvalidEncoding = class JOSEInvalidEncoding extends JOSEError {}
62module.exports.JOSEMultiError = JOSEMultiError
63module.exports.JOSENotSupported = class JOSENotSupported extends JOSEError {}
64
65module.exports.JWEDecryptionFailed = class JWEDecryptionFailed extends JOSEError {}
66module.exports.JWEInvalid = class JWEInvalid extends JOSEError {}
67
68module.exports.JWKImportFailed = class JWKImportFailed extends JOSEError {}
69module.exports.JWKInvalid = class JWKInvalid extends JOSEError {}
70module.exports.JWKKeySupport = class JWKKeySupport extends JOSEError {}
71
72module.exports.JWKSNoMatchingKey = class JWKSNoMatchingKey extends JOSEError {}
73
74module.exports.JWSInvalid = class JWSInvalid extends JOSEError {}
75module.exports.JWSVerificationFailed = class JWSVerificationFailed extends JOSEError {}
76
77class JWTClaimInvalid extends JOSEError {
78 constructor (message, claim = 'unspecified', reason = 'unspecified') {
79 super(message)
80 this.claim = claim
81 this.reason = reason
82 }
83}
84module.exports.JWTClaimInvalid = JWTClaimInvalid
85module.exports.JWTExpired = class JWTExpired extends JWTClaimInvalid {}
86module.exports.JWTMalformed = class JWTMalformed extends JOSEError {}