1 | const 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 |
|
20 | const 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 |
|
28 | class 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 |
|
40 | const isMulti = e => e instanceof JOSEMultiError
|
41 | class 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 | }
|
57 | module.exports.JOSEError = JOSEError
|
58 |
|
59 | module.exports.JOSEAlgNotWhitelisted = class JOSEAlgNotWhitelisted extends JOSEError {}
|
60 | module.exports.JOSECritNotUnderstood = class JOSECritNotUnderstood extends JOSEError {}
|
61 | module.exports.JOSEInvalidEncoding = class JOSEInvalidEncoding extends JOSEError {}
|
62 | module.exports.JOSEMultiError = JOSEMultiError
|
63 | module.exports.JOSENotSupported = class JOSENotSupported extends JOSEError {}
|
64 |
|
65 | module.exports.JWEDecryptionFailed = class JWEDecryptionFailed extends JOSEError {}
|
66 | module.exports.JWEInvalid = class JWEInvalid extends JOSEError {}
|
67 |
|
68 | module.exports.JWKImportFailed = class JWKImportFailed extends JOSEError {}
|
69 | module.exports.JWKInvalid = class JWKInvalid extends JOSEError {}
|
70 | module.exports.JWKKeySupport = class JWKKeySupport extends JOSEError {}
|
71 |
|
72 | module.exports.JWKSNoMatchingKey = class JWKSNoMatchingKey extends JOSEError {}
|
73 |
|
74 | module.exports.JWSInvalid = class JWSInvalid extends JOSEError {}
|
75 | module.exports.JWSVerificationFailed = class JWSVerificationFailed extends JOSEError {}
|
76 |
|
77 | class JWTClaimInvalid extends JOSEError {
|
78 | constructor (message, claim = 'unspecified', reason = 'unspecified') {
|
79 | super(message)
|
80 | this.claim = claim
|
81 | this.reason = reason
|
82 | }
|
83 | }
|
84 | module.exports.JWTClaimInvalid = JWTClaimInvalid
|
85 | module.exports.JWTExpired = class JWTExpired extends JWTClaimInvalid {}
|
86 | module.exports.JWTMalformed = class JWTMalformed extends JOSEError {}
|