@oari/jose 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +150 -0
  3. package/dist/types/index.d.ts +55 -0
  4. package/dist/types/jwe/compact/decrypt.d.ts +43 -0
  5. package/dist/types/jwe/compact/encrypt.d.ts +76 -0
  6. package/dist/types/jwe/flattened/decrypt.d.ts +53 -0
  7. package/dist/types/jwe/flattened/encrypt.d.ts +95 -0
  8. package/dist/types/jwe/general/decrypt.d.ts +64 -0
  9. package/dist/types/jwe/general/encrypt.d.ts +89 -0
  10. package/dist/types/jwk/embedded.d.ts +31 -0
  11. package/dist/types/jwk/thumbprint.d.ts +60 -0
  12. package/dist/types/jwks/local.d.ts +90 -0
  13. package/dist/types/jwks/remote.d.ts +306 -0
  14. package/dist/types/jws/compact/sign.d.ts +47 -0
  15. package/dist/types/jws/compact/verify.d.ts +45 -0
  16. package/dist/types/jws/flattened/sign.d.ts +53 -0
  17. package/dist/types/jws/flattened/verify.d.ts +50 -0
  18. package/dist/types/jws/general/sign.d.ts +67 -0
  19. package/dist/types/jws/general/verify.d.ts +61 -0
  20. package/dist/types/jwt/decrypt.d.ts +51 -0
  21. package/dist/types/jwt/encrypt.d.ts +105 -0
  22. package/dist/types/jwt/sign.d.ts +140 -0
  23. package/dist/types/jwt/unsecured.d.ts +70 -0
  24. package/dist/types/jwt/verify.d.ts +124 -0
  25. package/dist/types/key/export.d.ts +59 -0
  26. package/dist/types/key/generate_key_pair.d.ts +64 -0
  27. package/dist/types/key/generate_secret.d.ts +42 -0
  28. package/dist/types/key/import.d.ts +146 -0
  29. package/dist/types/types.d.ts +869 -0
  30. package/dist/types/util/base64url.d.ts +9 -0
  31. package/dist/types/util/decode_jwt.d.ts +25 -0
  32. package/dist/types/util/decode_protected_header.d.ts +24 -0
  33. package/dist/types/util/errors.d.ts +488 -0
  34. package/dist/webapi/index.js +32 -0
  35. package/dist/webapi/jwe/compact/decrypt.js +27 -0
  36. package/dist/webapi/jwe/compact/encrypt.js +27 -0
  37. package/dist/webapi/jwe/flattened/decrypt.js +159 -0
  38. package/dist/webapi/jwe/flattened/encrypt.js +167 -0
  39. package/dist/webapi/jwe/general/decrypt.js +31 -0
  40. package/dist/webapi/jwe/general/encrypt.js +182 -0
  41. package/dist/webapi/jwk/embedded.js +17 -0
  42. package/dist/webapi/jwk/thumbprint.js +68 -0
  43. package/dist/webapi/jwks/local.js +119 -0
  44. package/dist/webapi/jwks/remote.js +179 -0
  45. package/dist/webapi/jws/compact/sign.js +18 -0
  46. package/dist/webapi/jws/compact/verify.js +21 -0
  47. package/dist/webapi/jws/flattened/sign.js +87 -0
  48. package/dist/webapi/jws/flattened/verify.js +110 -0
  49. package/dist/webapi/jws/general/sign.js +70 -0
  50. package/dist/webapi/jws/general/verify.js +24 -0
  51. package/dist/webapi/jwt/decrypt.js +23 -0
  52. package/dist/webapi/jwt/encrypt.js +101 -0
  53. package/dist/webapi/jwt/sign.js +52 -0
  54. package/dist/webapi/jwt/unsecured.js +63 -0
  55. package/dist/webapi/jwt/verify.js +15 -0
  56. package/dist/webapi/key/export.js +11 -0
  57. package/dist/webapi/key/generate_key_pair.js +97 -0
  58. package/dist/webapi/key/generate_secret.js +40 -0
  59. package/dist/webapi/key/import.js +57 -0
  60. package/dist/webapi/lib/aesgcmkw.js +15 -0
  61. package/dist/webapi/lib/aeskw.js +25 -0
  62. package/dist/webapi/lib/asn1.js +243 -0
  63. package/dist/webapi/lib/base64.js +22 -0
  64. package/dist/webapi/lib/buffer_utils.js +43 -0
  65. package/dist/webapi/lib/check_key_type.js +127 -0
  66. package/dist/webapi/lib/content_encryption.js +217 -0
  67. package/dist/webapi/lib/crypto_key.js +136 -0
  68. package/dist/webapi/lib/deflate.js +44 -0
  69. package/dist/webapi/lib/ecdhes.js +52 -0
  70. package/dist/webapi/lib/helpers.js +19 -0
  71. package/dist/webapi/lib/invalid_key_input.js +27 -0
  72. package/dist/webapi/lib/is_key_like.js +17 -0
  73. package/dist/webapi/lib/jwk_to_key.js +107 -0
  74. package/dist/webapi/lib/jwt_claims_set.js +238 -0
  75. package/dist/webapi/lib/key_management.js +186 -0
  76. package/dist/webapi/lib/key_to_jwk.js +31 -0
  77. package/dist/webapi/lib/normalize_key.js +166 -0
  78. package/dist/webapi/lib/pbes2kw.js +42 -0
  79. package/dist/webapi/lib/rsaes.js +24 -0
  80. package/dist/webapi/lib/signing.js +74 -0
  81. package/dist/webapi/lib/type_checks.js +41 -0
  82. package/dist/webapi/lib/validate_algorithms.js +10 -0
  83. package/dist/webapi/lib/validate_crit.js +33 -0
  84. package/dist/webapi/util/base64url.js +30 -0
  85. package/dist/webapi/util/decode_jwt.js +32 -0
  86. package/dist/webapi/util/decode_protected_header.js +34 -0
  87. package/dist/webapi/util/errors.js +99 -0
  88. package/package.json +195 -0
@@ -0,0 +1,119 @@
1
+ import { importJWK } from '../key/import.js';
2
+ import { JWKSInvalid, JOSENotSupported, JWKSNoMatchingKey, JWKSMultipleMatchingKeys, } from '../util/errors.js';
3
+ import { isObject } from '../lib/type_checks.js';
4
+ function getKtyFromAlg(alg) {
5
+ switch (typeof alg === 'string' && alg.slice(0, 2)) {
6
+ case 'RS':
7
+ case 'PS':
8
+ return 'RSA';
9
+ case 'ES':
10
+ return 'EC';
11
+ case 'Ed':
12
+ return 'OKP';
13
+ case 'ML':
14
+ return 'AKP';
15
+ default:
16
+ throw new JOSENotSupported('Unsupported "alg" value for a JSON Web Key Set');
17
+ }
18
+ }
19
+ function isJWKSLike(jwks) {
20
+ return (jwks &&
21
+ typeof jwks === 'object' &&
22
+ Array.isArray(jwks.keys) &&
23
+ jwks.keys.every(isJWKLike));
24
+ }
25
+ function isJWKLike(key) {
26
+ return isObject(key);
27
+ }
28
+ class LocalJWKSet {
29
+ #jwks;
30
+ #cached = new WeakMap();
31
+ constructor(jwks) {
32
+ if (!isJWKSLike(jwks)) {
33
+ throw new JWKSInvalid('JSON Web Key Set malformed');
34
+ }
35
+ this.#jwks = structuredClone(jwks);
36
+ }
37
+ jwks() {
38
+ return this.#jwks;
39
+ }
40
+ async getKey(protectedHeader, token) {
41
+ const { alg, kid } = { ...protectedHeader, ...token?.header };
42
+ const kty = getKtyFromAlg(alg);
43
+ const candidates = this.#jwks.keys.filter((jwk) => {
44
+ let candidate = kty === jwk.kty;
45
+ if (candidate && typeof kid === 'string') {
46
+ candidate = kid === jwk.kid;
47
+ }
48
+ if (candidate && (typeof jwk.alg === 'string' || kty === 'AKP')) {
49
+ candidate = alg === jwk.alg;
50
+ }
51
+ if (candidate && typeof jwk.use === 'string') {
52
+ candidate = jwk.use === 'sig';
53
+ }
54
+ if (candidate && Array.isArray(jwk.key_ops)) {
55
+ candidate = jwk.key_ops.includes('verify');
56
+ }
57
+ if (candidate) {
58
+ switch (alg) {
59
+ case 'ES256':
60
+ candidate = jwk.crv === 'P-256';
61
+ break;
62
+ case 'ES384':
63
+ candidate = jwk.crv === 'P-384';
64
+ break;
65
+ case 'ES512':
66
+ candidate = jwk.crv === 'P-521';
67
+ break;
68
+ case 'Ed25519':
69
+ case 'EdDSA':
70
+ candidate = jwk.crv === 'Ed25519';
71
+ break;
72
+ }
73
+ }
74
+ return candidate;
75
+ });
76
+ const { 0: jwk, length } = candidates;
77
+ if (length === 0) {
78
+ throw new JWKSNoMatchingKey();
79
+ }
80
+ if (length !== 1) {
81
+ const error = new JWKSMultipleMatchingKeys();
82
+ const _cached = this.#cached;
83
+ error[Symbol.asyncIterator] = async function* () {
84
+ for (const jwk of candidates) {
85
+ try {
86
+ yield await importWithAlgCache(_cached, jwk, alg);
87
+ }
88
+ catch { }
89
+ }
90
+ };
91
+ throw error;
92
+ }
93
+ return importWithAlgCache(this.#cached, jwk, alg);
94
+ }
95
+ }
96
+ async function importWithAlgCache(cache, jwk, alg) {
97
+ const cached = cache.get(jwk) || cache.set(jwk, {}).get(jwk);
98
+ if (cached[alg] === undefined) {
99
+ const key = await importJWK({ ...jwk, ext: true }, alg);
100
+ if (key instanceof Uint8Array || key.type !== 'public') {
101
+ throw new JWKSInvalid('JSON Web Key Set members must be public keys');
102
+ }
103
+ cached[alg] = key;
104
+ }
105
+ return cached[alg];
106
+ }
107
+ export function createLocalJWKSet(jwks) {
108
+ const set = new LocalJWKSet(jwks);
109
+ const localJWKSet = async (protectedHeader, token) => set.getKey(protectedHeader, token);
110
+ Object.defineProperties(localJWKSet, {
111
+ jwks: {
112
+ value: () => structuredClone(set.jwks()),
113
+ enumerable: false,
114
+ configurable: false,
115
+ writable: false,
116
+ },
117
+ });
118
+ return localJWKSet;
119
+ }
@@ -0,0 +1,179 @@
1
+ import { JOSEError, JWKSNoMatchingKey, JWKSTimeout } from '../util/errors.js';
2
+ import { createLocalJWKSet } from './local.js';
3
+ import { isObject } from '../lib/type_checks.js';
4
+ function isCloudflareWorkers() {
5
+ return (typeof WebSocketPair !== 'undefined' ||
6
+ (typeof navigator !== 'undefined' && navigator.userAgent === 'Cloudflare-Workers') ||
7
+ (typeof EdgeRuntime !== 'undefined' && EdgeRuntime === 'vercel'));
8
+ }
9
+ let USER_AGENT;
10
+ if (typeof navigator === 'undefined' || !navigator.userAgent?.startsWith?.('Mozilla/5.0 ')) {
11
+ const NAME = 'jose';
12
+ const VERSION = 'v6.2.3';
13
+ USER_AGENT = `${NAME}/${VERSION}`;
14
+ }
15
+ export const customFetch = Symbol();
16
+ async function fetchJwks(url, headers, signal, fetchImpl = fetch) {
17
+ const response = await fetchImpl(url, {
18
+ method: 'GET',
19
+ signal,
20
+ redirect: 'manual',
21
+ headers,
22
+ }).catch((err) => {
23
+ if (err.name === 'TimeoutError') {
24
+ throw new JWKSTimeout();
25
+ }
26
+ throw err;
27
+ });
28
+ if (response.status !== 200) {
29
+ throw new JOSEError('Expected 200 OK from the JSON Web Key Set HTTP response');
30
+ }
31
+ try {
32
+ return await response.json();
33
+ }
34
+ catch {
35
+ throw new JOSEError('Failed to parse the JSON Web Key Set HTTP response as JSON');
36
+ }
37
+ }
38
+ export const jwksCache = Symbol();
39
+ function isFreshJwksCache(input, cacheMaxAge) {
40
+ if (typeof input !== 'object' || input === null) {
41
+ return false;
42
+ }
43
+ if (!('uat' in input) || typeof input.uat !== 'number' || Date.now() - input.uat >= cacheMaxAge) {
44
+ return false;
45
+ }
46
+ if (!('jwks' in input) ||
47
+ !isObject(input.jwks) ||
48
+ !Array.isArray(input.jwks.keys) ||
49
+ !Array.prototype.every.call(input.jwks.keys, isObject)) {
50
+ return false;
51
+ }
52
+ return true;
53
+ }
54
+ class RemoteJWKSet {
55
+ #url;
56
+ #timeoutDuration;
57
+ #cooldownDuration;
58
+ #cacheMaxAge;
59
+ #jwksTimestamp;
60
+ #pendingFetch;
61
+ #headers;
62
+ #customFetch;
63
+ #local;
64
+ #cache;
65
+ constructor(url, options) {
66
+ if (!(url instanceof URL)) {
67
+ throw new TypeError('url must be an instance of URL');
68
+ }
69
+ this.#url = new URL(url.href);
70
+ this.#timeoutDuration =
71
+ typeof options?.timeoutDuration === 'number' ? options?.timeoutDuration : 5000;
72
+ this.#cooldownDuration =
73
+ typeof options?.cooldownDuration === 'number' ? options?.cooldownDuration : 30000;
74
+ this.#cacheMaxAge = typeof options?.cacheMaxAge === 'number' ? options?.cacheMaxAge : 600000;
75
+ this.#headers = new Headers(options?.headers);
76
+ if (USER_AGENT && !this.#headers.has('User-Agent')) {
77
+ this.#headers.set('User-Agent', USER_AGENT);
78
+ }
79
+ if (!this.#headers.has('accept')) {
80
+ this.#headers.set('accept', 'application/json');
81
+ this.#headers.append('accept', 'application/jwk-set+json');
82
+ }
83
+ this.#customFetch = options?.[customFetch];
84
+ if (options?.[jwksCache] !== undefined) {
85
+ this.#cache = options?.[jwksCache];
86
+ if (isFreshJwksCache(options?.[jwksCache], this.#cacheMaxAge)) {
87
+ this.#jwksTimestamp = this.#cache.uat;
88
+ this.#local = createLocalJWKSet(this.#cache.jwks);
89
+ }
90
+ }
91
+ }
92
+ pendingFetch() {
93
+ return !!this.#pendingFetch;
94
+ }
95
+ coolingDown() {
96
+ return typeof this.#jwksTimestamp === 'number'
97
+ ? Date.now() < this.#jwksTimestamp + this.#cooldownDuration
98
+ : false;
99
+ }
100
+ fresh() {
101
+ return typeof this.#jwksTimestamp === 'number'
102
+ ? Date.now() < this.#jwksTimestamp + this.#cacheMaxAge
103
+ : false;
104
+ }
105
+ jwks() {
106
+ return this.#local?.jwks();
107
+ }
108
+ async getKey(protectedHeader, token) {
109
+ if (!this.#local || !this.fresh()) {
110
+ await this.reload();
111
+ }
112
+ try {
113
+ return await this.#local(protectedHeader, token);
114
+ }
115
+ catch (err) {
116
+ if (err instanceof JWKSNoMatchingKey) {
117
+ if (this.coolingDown() === false) {
118
+ await this.reload();
119
+ return this.#local(protectedHeader, token);
120
+ }
121
+ }
122
+ throw err;
123
+ }
124
+ }
125
+ async reload() {
126
+ if (this.#pendingFetch && isCloudflareWorkers()) {
127
+ this.#pendingFetch = undefined;
128
+ }
129
+ this.#pendingFetch ||= fetchJwks(this.#url.href, this.#headers, AbortSignal.timeout(this.#timeoutDuration), this.#customFetch)
130
+ .then((json) => {
131
+ this.#local = createLocalJWKSet(json);
132
+ if (this.#cache) {
133
+ this.#cache.uat = Date.now();
134
+ this.#cache.jwks = json;
135
+ }
136
+ this.#jwksTimestamp = Date.now();
137
+ this.#pendingFetch = undefined;
138
+ })
139
+ .catch((err) => {
140
+ this.#pendingFetch = undefined;
141
+ throw err;
142
+ });
143
+ await this.#pendingFetch;
144
+ }
145
+ }
146
+ export function createRemoteJWKSet(url, options) {
147
+ const set = new RemoteJWKSet(url, options);
148
+ const remoteJWKSet = async (protectedHeader, token) => set.getKey(protectedHeader, token);
149
+ Object.defineProperties(remoteJWKSet, {
150
+ coolingDown: {
151
+ get: () => set.coolingDown(),
152
+ enumerable: true,
153
+ configurable: false,
154
+ },
155
+ fresh: {
156
+ get: () => set.fresh(),
157
+ enumerable: true,
158
+ configurable: false,
159
+ },
160
+ reload: {
161
+ value: () => set.reload(),
162
+ enumerable: true,
163
+ configurable: false,
164
+ writable: false,
165
+ },
166
+ reloading: {
167
+ get: () => set.pendingFetch(),
168
+ enumerable: true,
169
+ configurable: false,
170
+ },
171
+ jwks: {
172
+ value: () => set.jwks(),
173
+ enumerable: true,
174
+ configurable: false,
175
+ writable: false,
176
+ },
177
+ });
178
+ return remoteJWKSet;
179
+ }
@@ -0,0 +1,18 @@
1
+ import { FlattenedSign } from '../flattened/sign.js';
2
+ export class CompactSign {
3
+ #flattened;
4
+ constructor(payload) {
5
+ this.#flattened = new FlattenedSign(payload);
6
+ }
7
+ setProtectedHeader(protectedHeader) {
8
+ this.#flattened.setProtectedHeader(protectedHeader);
9
+ return this;
10
+ }
11
+ async sign(key, options) {
12
+ const jws = await this.#flattened.sign(key, options);
13
+ if (jws.payload === undefined) {
14
+ throw new TypeError('use the flattened module for creating JWS with b64: false');
15
+ }
16
+ return `${jws.protected}.${jws.payload}.${jws.signature}`;
17
+ }
18
+ }
@@ -0,0 +1,21 @@
1
+ import { flattenedVerify } from '../flattened/verify.js';
2
+ import { JWSInvalid } from '../../util/errors.js';
3
+ import { decoder } from '../../lib/buffer_utils.js';
4
+ export async function compactVerify(jws, key, options) {
5
+ if (jws instanceof Uint8Array) {
6
+ jws = decoder.decode(jws);
7
+ }
8
+ if (typeof jws !== 'string') {
9
+ throw new JWSInvalid('Compact JWS must be a string or Uint8Array');
10
+ }
11
+ const { 0: protectedHeader, 1: payload, 2: signature, length } = jws.split('.');
12
+ if (length !== 3) {
13
+ throw new JWSInvalid('Invalid Compact JWS');
14
+ }
15
+ const verified = await flattenedVerify({ payload, protected: protectedHeader, signature }, key, options);
16
+ const result = { payload: verified.payload, protectedHeader: verified.protectedHeader };
17
+ if (typeof key === 'function') {
18
+ return { ...result, key: verified.key };
19
+ }
20
+ return result;
21
+ }
@@ -0,0 +1,87 @@
1
+ import { encode as b64u } from '../../util/base64url.js';
2
+ import { sign } from '../../lib/signing.js';
3
+ import { isDisjoint } from '../../lib/type_checks.js';
4
+ import { JWSInvalid } from '../../util/errors.js';
5
+ import { concat, encode } from '../../lib/buffer_utils.js';
6
+ import { checkKeyType } from '../../lib/check_key_type.js';
7
+ import { validateCrit } from '../../lib/validate_crit.js';
8
+ import { assertNotSet } from '../../lib/helpers.js';
9
+ export class FlattenedSign {
10
+ #payload;
11
+ #protectedHeader;
12
+ #unprotectedHeader;
13
+ constructor(payload) {
14
+ if (!(payload instanceof Uint8Array)) {
15
+ throw new TypeError('payload must be an instance of Uint8Array');
16
+ }
17
+ this.#payload = payload;
18
+ }
19
+ setProtectedHeader(protectedHeader) {
20
+ assertNotSet(this.#protectedHeader, 'setProtectedHeader');
21
+ this.#protectedHeader = protectedHeader;
22
+ return this;
23
+ }
24
+ setUnprotectedHeader(unprotectedHeader) {
25
+ assertNotSet(this.#unprotectedHeader, 'setUnprotectedHeader');
26
+ this.#unprotectedHeader = unprotectedHeader;
27
+ return this;
28
+ }
29
+ async sign(key, options) {
30
+ if (!this.#protectedHeader && !this.#unprotectedHeader) {
31
+ throw new JWSInvalid('either setProtectedHeader or setUnprotectedHeader must be called before #sign()');
32
+ }
33
+ if (!isDisjoint(this.#protectedHeader, this.#unprotectedHeader)) {
34
+ throw new JWSInvalid('JWS Protected and JWS Unprotected Header Parameter names must be disjoint');
35
+ }
36
+ const joseHeader = {
37
+ ...this.#protectedHeader,
38
+ ...this.#unprotectedHeader,
39
+ };
40
+ const extensions = validateCrit(JWSInvalid, new Map([['b64', true]]), options?.crit, this.#protectedHeader, joseHeader);
41
+ let b64 = true;
42
+ if (extensions.has('b64')) {
43
+ b64 = this.#protectedHeader.b64;
44
+ if (typeof b64 !== 'boolean') {
45
+ throw new JWSInvalid('The "b64" (base64url-encode payload) Header Parameter must be a boolean');
46
+ }
47
+ }
48
+ const { alg } = joseHeader;
49
+ if (typeof alg !== 'string' || !alg) {
50
+ throw new JWSInvalid('JWS "alg" (Algorithm) Header Parameter missing or invalid');
51
+ }
52
+ checkKeyType(alg, key, 'sign');
53
+ let payloadS;
54
+ let payloadB;
55
+ if (b64) {
56
+ payloadS = b64u(this.#payload);
57
+ payloadB = encode(payloadS);
58
+ }
59
+ else {
60
+ payloadB = this.#payload;
61
+ payloadS = '';
62
+ }
63
+ let protectedHeaderString;
64
+ let protectedHeaderBytes;
65
+ if (this.#protectedHeader) {
66
+ protectedHeaderString = b64u(JSON.stringify(this.#protectedHeader));
67
+ protectedHeaderBytes = encode(protectedHeaderString);
68
+ }
69
+ else {
70
+ protectedHeaderString = '';
71
+ protectedHeaderBytes = new Uint8Array();
72
+ }
73
+ const data = concat(protectedHeaderBytes, encode('.'), payloadB);
74
+ const signature = await sign(alg, key, data);
75
+ const jws = {
76
+ signature: b64u(signature),
77
+ payload: payloadS,
78
+ };
79
+ if (this.#unprotectedHeader) {
80
+ jws.header = this.#unprotectedHeader;
81
+ }
82
+ if (this.#protectedHeader) {
83
+ jws.protected = protectedHeaderString;
84
+ }
85
+ return jws;
86
+ }
87
+ }
@@ -0,0 +1,110 @@
1
+ import { decode as b64u } from '../../util/base64url.js';
2
+ import { verify } from '../../lib/signing.js';
3
+ import { JOSEAlgNotAllowed, JWSInvalid, JWSSignatureVerificationFailed } from '../../util/errors.js';
4
+ import { concat, encoder, decoder, encode } from '../../lib/buffer_utils.js';
5
+ import { decodeBase64url } from '../../lib/helpers.js';
6
+ import { isDisjoint } from '../../lib/type_checks.js';
7
+ import { isObject } from '../../lib/type_checks.js';
8
+ import { checkKeyType } from '../../lib/check_key_type.js';
9
+ import { validateCrit } from '../../lib/validate_crit.js';
10
+ import { validateAlgorithms } from '../../lib/validate_algorithms.js';
11
+ import { normalizeKey } from '../../lib/normalize_key.js';
12
+ export async function flattenedVerify(jws, key, options) {
13
+ if (!isObject(jws)) {
14
+ throw new JWSInvalid('Flattened JWS must be an object');
15
+ }
16
+ if (jws.protected === undefined && jws.header === undefined) {
17
+ throw new JWSInvalid('Flattened JWS must have either of the "protected" or "header" members');
18
+ }
19
+ if (jws.protected !== undefined && typeof jws.protected !== 'string') {
20
+ throw new JWSInvalid('JWS Protected Header incorrect type');
21
+ }
22
+ if (jws.payload === undefined) {
23
+ throw new JWSInvalid('JWS Payload missing');
24
+ }
25
+ if (typeof jws.signature !== 'string') {
26
+ throw new JWSInvalid('JWS Signature missing or incorrect type');
27
+ }
28
+ if (jws.header !== undefined && !isObject(jws.header)) {
29
+ throw new JWSInvalid('JWS Unprotected Header incorrect type');
30
+ }
31
+ let parsedProt = {};
32
+ if (jws.protected) {
33
+ try {
34
+ const protectedHeader = b64u(jws.protected);
35
+ parsedProt = JSON.parse(decoder.decode(protectedHeader));
36
+ }
37
+ catch {
38
+ throw new JWSInvalid('JWS Protected Header is invalid');
39
+ }
40
+ }
41
+ if (!isDisjoint(parsedProt, jws.header)) {
42
+ throw new JWSInvalid('JWS Protected and JWS Unprotected Header Parameter names must be disjoint');
43
+ }
44
+ const joseHeader = {
45
+ ...parsedProt,
46
+ ...jws.header,
47
+ };
48
+ const extensions = validateCrit(JWSInvalid, new Map([['b64', true]]), options?.crit, parsedProt, joseHeader);
49
+ let b64 = true;
50
+ if (extensions.has('b64')) {
51
+ b64 = parsedProt.b64;
52
+ if (typeof b64 !== 'boolean') {
53
+ throw new JWSInvalid('The "b64" (base64url-encode payload) Header Parameter must be a boolean');
54
+ }
55
+ }
56
+ const { alg } = joseHeader;
57
+ if (typeof alg !== 'string' || !alg) {
58
+ throw new JWSInvalid('JWS "alg" (Algorithm) Header Parameter missing or invalid');
59
+ }
60
+ const algorithms = options && validateAlgorithms('algorithms', options.algorithms);
61
+ if (algorithms && !algorithms.has(alg)) {
62
+ throw new JOSEAlgNotAllowed('"alg" (Algorithm) Header Parameter value not allowed');
63
+ }
64
+ if (b64) {
65
+ if (typeof jws.payload !== 'string') {
66
+ throw new JWSInvalid('JWS Payload must be a string');
67
+ }
68
+ }
69
+ else if (typeof jws.payload !== 'string' && !(jws.payload instanceof Uint8Array)) {
70
+ throw new JWSInvalid('JWS Payload must be a string or an Uint8Array instance');
71
+ }
72
+ let resolvedKey = false;
73
+ if (typeof key === 'function') {
74
+ key = await key(parsedProt, jws);
75
+ resolvedKey = true;
76
+ }
77
+ checkKeyType(alg, key, 'verify');
78
+ const data = concat(jws.protected !== undefined ? encode(jws.protected) : new Uint8Array(), encode('.'), typeof jws.payload === 'string'
79
+ ? b64
80
+ ? encode(jws.payload)
81
+ : encoder.encode(jws.payload)
82
+ : jws.payload);
83
+ const signature = decodeBase64url(jws.signature, 'signature', JWSInvalid);
84
+ const k = await normalizeKey(key, alg);
85
+ const verified = await verify(alg, k, signature, data);
86
+ if (!verified) {
87
+ throw new JWSSignatureVerificationFailed();
88
+ }
89
+ let payload;
90
+ if (b64) {
91
+ payload = decodeBase64url(jws.payload, 'payload', JWSInvalid);
92
+ }
93
+ else if (typeof jws.payload === 'string') {
94
+ payload = encoder.encode(jws.payload);
95
+ }
96
+ else {
97
+ payload = jws.payload;
98
+ }
99
+ const result = { payload };
100
+ if (jws.protected !== undefined) {
101
+ result.protectedHeader = parsedProt;
102
+ }
103
+ if (jws.header !== undefined) {
104
+ result.unprotectedHeader = jws.header;
105
+ }
106
+ if (resolvedKey) {
107
+ return { ...result, key: k };
108
+ }
109
+ return result;
110
+ }
@@ -0,0 +1,70 @@
1
+ import { FlattenedSign } from '../flattened/sign.js';
2
+ import { JWSInvalid } from '../../util/errors.js';
3
+ import { assertNotSet } from '../../lib/helpers.js';
4
+ class IndividualSignature {
5
+ #parent;
6
+ protectedHeader;
7
+ unprotectedHeader;
8
+ options;
9
+ key;
10
+ constructor(sig, key, options) {
11
+ this.#parent = sig;
12
+ this.key = key;
13
+ this.options = options;
14
+ }
15
+ setProtectedHeader(protectedHeader) {
16
+ assertNotSet(this.protectedHeader, 'setProtectedHeader');
17
+ this.protectedHeader = protectedHeader;
18
+ return this;
19
+ }
20
+ setUnprotectedHeader(unprotectedHeader) {
21
+ assertNotSet(this.unprotectedHeader, 'setUnprotectedHeader');
22
+ this.unprotectedHeader = unprotectedHeader;
23
+ return this;
24
+ }
25
+ addSignature(...args) {
26
+ return this.#parent.addSignature(...args);
27
+ }
28
+ sign(...args) {
29
+ return this.#parent.sign(...args);
30
+ }
31
+ done() {
32
+ return this.#parent;
33
+ }
34
+ }
35
+ export class GeneralSign {
36
+ #payload;
37
+ #signatures = [];
38
+ constructor(payload) {
39
+ this.#payload = payload;
40
+ }
41
+ addSignature(key, options) {
42
+ const signature = new IndividualSignature(this, key, options);
43
+ this.#signatures.push(signature);
44
+ return signature;
45
+ }
46
+ async sign() {
47
+ if (!this.#signatures.length) {
48
+ throw new JWSInvalid('at least one signature must be added');
49
+ }
50
+ const jws = {
51
+ signatures: [],
52
+ payload: '',
53
+ };
54
+ for (let i = 0; i < this.#signatures.length; i++) {
55
+ const signature = this.#signatures[i];
56
+ const flattened = new FlattenedSign(this.#payload);
57
+ flattened.setProtectedHeader(signature.protectedHeader);
58
+ flattened.setUnprotectedHeader(signature.unprotectedHeader);
59
+ const { payload, ...rest } = await flattened.sign(signature.key, signature.options);
60
+ if (i === 0) {
61
+ jws.payload = payload;
62
+ }
63
+ else if (jws.payload !== payload) {
64
+ throw new JWSInvalid('inconsistent use of JWS Unencoded Payload (RFC7797)');
65
+ }
66
+ jws.signatures.push(rest);
67
+ }
68
+ return jws;
69
+ }
70
+ }
@@ -0,0 +1,24 @@
1
+ import { flattenedVerify } from '../flattened/verify.js';
2
+ import { JWSInvalid, JWSSignatureVerificationFailed } from '../../util/errors.js';
3
+ import { isObject } from '../../lib/type_checks.js';
4
+ export async function generalVerify(jws, key, options) {
5
+ if (!isObject(jws)) {
6
+ throw new JWSInvalid('General JWS must be an object');
7
+ }
8
+ if (!Array.isArray(jws.signatures) || !jws.signatures.every(isObject)) {
9
+ throw new JWSInvalid('JWS Signatures missing or incorrect type');
10
+ }
11
+ for (const signature of jws.signatures) {
12
+ try {
13
+ return await flattenedVerify({
14
+ header: signature.header,
15
+ payload: jws.payload,
16
+ protected: signature.protected,
17
+ signature: signature.signature,
18
+ }, key, options);
19
+ }
20
+ catch {
21
+ }
22
+ }
23
+ throw new JWSSignatureVerificationFailed();
24
+ }
@@ -0,0 +1,23 @@
1
+ import { compactDecrypt } from '../jwe/compact/decrypt.js';
2
+ import { validateClaimsSet } from '../lib/jwt_claims_set.js';
3
+ import { JWTClaimValidationFailed } from '../util/errors.js';
4
+ export async function jwtDecrypt(jwt, key, options) {
5
+ const decrypted = await compactDecrypt(jwt, key, options);
6
+ const payload = validateClaimsSet(decrypted.protectedHeader, decrypted.plaintext, options);
7
+ const { protectedHeader } = decrypted;
8
+ if (protectedHeader.iss !== undefined && protectedHeader.iss !== payload.iss) {
9
+ throw new JWTClaimValidationFailed('replicated "iss" claim header parameter mismatch', payload, 'iss', 'mismatch');
10
+ }
11
+ if (protectedHeader.sub !== undefined && protectedHeader.sub !== payload.sub) {
12
+ throw new JWTClaimValidationFailed('replicated "sub" claim header parameter mismatch', payload, 'sub', 'mismatch');
13
+ }
14
+ if (protectedHeader.aud !== undefined &&
15
+ JSON.stringify(protectedHeader.aud) !== JSON.stringify(payload.aud)) {
16
+ throw new JWTClaimValidationFailed('replicated "aud" claim header parameter mismatch', payload, 'aud', 'mismatch');
17
+ }
18
+ const result = { payload, protectedHeader };
19
+ if (typeof key === 'function') {
20
+ return { ...result, key: decrypted.key };
21
+ }
22
+ return result;
23
+ }