@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,31 @@
1
+ /**
2
+ * Verification using a JWK Embedded in a JWS Header
3
+ *
4
+ * @module
5
+ */
6
+ import type * as types from '../types.d.ts';
7
+ /**
8
+ * EmbeddedJWK is an implementation of a GetKeyFunction intended to be used with the JWS/JWT verify
9
+ * operations whenever you need to opt-in to verify signatures with a public key embedded in the
10
+ * token's "jwk" (JSON Web Key) Header Parameter. It is recommended to combine this with the verify
11
+ * function's `algorithms` option to define accepted JWS "alg" (Algorithm) Header Parameter values.
12
+ *
13
+ * This function is exported (as a named export) from the main `'jose'` module entry point as well
14
+ * as from its subpath export `'jose/jwk/embedded'`.
15
+ *
16
+ * @example
17
+ *
18
+ * ```js
19
+ * const jwt =
20
+ * 'eyJqd2siOnsiY3J2IjoiUC0yNTYiLCJ4IjoiVU05ZzVuS25aWFlvdldBbE03NmNMejl2VG96UmpfX0NIVV9kT2wtZ09vRSIsInkiOiJkczhhZVF3MWwyY0RDQTdiQ2tPTnZ3REtwWEFidFhqdnFDbGVZSDhXc19VIiwia3R5IjoiRUMifSwiYWxnIjoiRVMyNTYifQ.eyJpc3MiOiJ1cm46ZXhhbXBsZTppc3N1ZXIiLCJhdWQiOiJ1cm46ZXhhbXBsZTphdWRpZW5jZSIsImlhdCI6MTYwNDU4MDc5NH0.60boak3_dErnW47ZPty1C0nrjeVq86EN_eK0GOq6K8w2OA0thKoBxFK4j-NuU9yZ_A9UKGxPT_G87DladBaV9g'
21
+ *
22
+ * const { payload, protectedHeader } = await jose.jwtVerify(jwt, jose.EmbeddedJWK, {
23
+ * issuer: 'urn:example:issuer',
24
+ * audience: 'urn:example:audience',
25
+ * })
26
+ *
27
+ * console.log(protectedHeader)
28
+ * console.log(payload)
29
+ * ```
30
+ */
31
+ export declare function EmbeddedJWK(protectedHeader?: types.JWSHeaderParameters, token?: types.FlattenedJWSInput): Promise<types.CryptoKey>;
@@ -0,0 +1,60 @@
1
+ /**
2
+ * JSON Web Key Thumbprint and JSON Web Key Thumbprint URI
3
+ *
4
+ * @module
5
+ */
6
+ import type * as types from '../types.d.ts';
7
+ /**
8
+ * Calculates a base64url-encoded JSON Web Key (JWK) Thumbprint
9
+ *
10
+ * This function is exported (as a named export) from the main `'jose'` module entry point as well
11
+ * as from its subpath export `'jose/jwk/thumbprint'`.
12
+ *
13
+ * @example
14
+ *
15
+ * ```js
16
+ * const thumbprint = await jose.calculateJwkThumbprint({
17
+ * kty: 'EC',
18
+ * crv: 'P-256',
19
+ * x: 'jJ6Flys3zK9jUhnOHf6G49Dyp5hah6CNP84-gY-n9eo',
20
+ * y: 'nhI6iD5eFXgBTLt_1p3aip-5VbZeMhxeFSpjfEAf7Ww',
21
+ * })
22
+ *
23
+ * console.log(thumbprint)
24
+ * // 'w9eYdC6_s_tLQ8lH6PUpc0mddazaqtPgeC2IgWDiqY8'
25
+ * ```
26
+ *
27
+ * @param key Key to calculate the thumbprint for.
28
+ * @param digestAlgorithm Digest Algorithm to use for calculating the thumbprint. Default is
29
+ * "sha256".
30
+ *
31
+ * @see {@link https://www.rfc-editor.org/rfc/rfc7638 RFC7638}
32
+ */
33
+ export declare function calculateJwkThumbprint(key: types.JWK | types.CryptoKey | types.KeyObject, digestAlgorithm?: 'sha256' | 'sha384' | 'sha512'): Promise<string>;
34
+ /**
35
+ * Calculates a JSON Web Key (JWK) Thumbprint URI
36
+ *
37
+ * This function is exported (as a named export) from the main `'jose'` module entry point as well
38
+ * as from its subpath export `'jose/jwk/thumbprint'`.
39
+ *
40
+ * @example
41
+ *
42
+ * ```js
43
+ * const thumbprintUri = await jose.calculateJwkThumbprintUri({
44
+ * kty: 'EC',
45
+ * crv: 'P-256',
46
+ * x: 'jJ6Flys3zK9jUhnOHf6G49Dyp5hah6CNP84-gY-n9eo',
47
+ * y: 'nhI6iD5eFXgBTLt_1p3aip-5VbZeMhxeFSpjfEAf7Ww',
48
+ * })
49
+ *
50
+ * console.log(thumbprintUri)
51
+ * // 'urn:ietf:params:oauth:jwk-thumbprint:sha-256:w9eYdC6_s_tLQ8lH6PUpc0mddazaqtPgeC2IgWDiqY8'
52
+ * ```
53
+ *
54
+ * @param key Key to calculate the thumbprint for.
55
+ * @param digestAlgorithm Digest Algorithm to use for calculating the thumbprint. Default is
56
+ * "sha256".
57
+ *
58
+ * @see {@link https://www.rfc-editor.org/rfc/rfc9278 RFC9278}
59
+ */
60
+ export declare function calculateJwkThumbprintUri(key: types.CryptoKey | types.KeyObject | types.JWK, digestAlgorithm?: 'sha256' | 'sha384' | 'sha512'): Promise<string>;
@@ -0,0 +1,90 @@
1
+ /**
2
+ * Verification using a JSON Web Key Set (JWKS) available locally
3
+ *
4
+ * @module
5
+ */
6
+ import type * as types from '../types.d.ts';
7
+ /**
8
+ * Returns a function that resolves a JWS JOSE Header to a public key object from a locally stored,
9
+ * or otherwise available, JSON Web Key Set.
10
+ *
11
+ * It uses the "alg" (JWS Algorithm) Header Parameter to determine the right JWK "kty" (Key Type),
12
+ * then proceeds to match the JWK "kid" (Key ID) with one found in the JWS Header Parameters (if
13
+ * there is one) while also respecting the JWK "use" (Public Key Use) and JWK "key_ops" (Key
14
+ * Operations) Parameters (if they are present on the JWK).
15
+ *
16
+ * Only a single public key must match the selection process. As shown in the example below when
17
+ * multiple keys get matched it is possible to opt-in to iterate over the matched keys and attempt
18
+ * verification in an iterative manner.
19
+ *
20
+ * > [!NOTE]\
21
+ * > The function's purpose is to resolve public keys used for verifying signatures and will not work
22
+ * > for public encryption keys.
23
+ *
24
+ * This function is exported (as a named export) from the main `'jose'` module entry point as well
25
+ * as from its subpath export `'jose/jwks/local'`.
26
+ *
27
+ * @example
28
+ *
29
+ * ```js
30
+ * const JWKS = jose.createLocalJWKSet({
31
+ * keys: [
32
+ * {
33
+ * kty: 'RSA',
34
+ * e: 'AQAB',
35
+ * n: '12oBZRhCiZFJLcPg59LkZZ9mdhSMTKAQZYq32k_ti5SBB6jerkh-WzOMAO664r_qyLkqHUSp3u5SbXtseZEpN3XPWGKSxjsy-1JyEFTdLSYe6f9gfrmxkUF_7DTpq0gn6rntP05g2-wFW50YO7mosfdslfrTJYWHFhJALabAeYirYD7-9kqq9ebfFMF4sRRELbv9oi36As6Q9B3Qb5_C1rAzqfao_PCsf9EPsTZsVVVkA5qoIAr47lo1ipfiBPxUCCNSdvkmDTYgvvRm6ZoMjFbvOtgyts55fXKdMWv7I9HMD5HwE9uW839PWA514qhbcIsXEYSFMPMV6fnlsiZvQQ',
36
+ * alg: 'PS256',
37
+ * },
38
+ * {
39
+ * crv: 'P-256',
40
+ * kty: 'EC',
41
+ * x: 'ySK38C1jBdLwDsNWKzzBHqKYEE5Cgv-qjWvorUXk9fw',
42
+ * y: '_LeQBw07cf5t57Iavn4j-BqJsAD1dpoz8gokd3sBsOo',
43
+ * alg: 'ES256',
44
+ * },
45
+ * ],
46
+ * })
47
+ *
48
+ * const { payload, protectedHeader } = await jose.jwtVerify(jwt, JWKS, {
49
+ * issuer: 'urn:example:issuer',
50
+ * audience: 'urn:example:audience',
51
+ * })
52
+ * console.log(protectedHeader)
53
+ * console.log(payload)
54
+ * ```
55
+ *
56
+ * @example
57
+ *
58
+ * Opting-in to multiple JWKS matches using `createLocalJWKSet`
59
+ *
60
+ * ```js
61
+ * const options = {
62
+ * issuer: 'urn:example:issuer',
63
+ * audience: 'urn:example:audience',
64
+ * }
65
+ * const { payload, protectedHeader } = await jose
66
+ * .jwtVerify(jwt, JWKS, options)
67
+ * .catch(async (error) => {
68
+ * if (error?.code === 'ERR_JWKS_MULTIPLE_MATCHING_KEYS') {
69
+ * for await (const publicKey of error) {
70
+ * try {
71
+ * return await jose.jwtVerify(jwt, publicKey, options)
72
+ * } catch (innerError) {
73
+ * if (innerError?.code === 'ERR_JWS_SIGNATURE_VERIFICATION_FAILED') {
74
+ * continue
75
+ * }
76
+ * throw innerError
77
+ * }
78
+ * }
79
+ * throw new jose.errors.JWSSignatureVerificationFailed()
80
+ * }
81
+ *
82
+ * throw error
83
+ * })
84
+ * console.log(protectedHeader)
85
+ * console.log(payload)
86
+ * ```
87
+ *
88
+ * @param jwks JSON Web Key Set formatted object.
89
+ */
90
+ export declare function createLocalJWKSet(jwks: types.JSONWebKeySet): (protectedHeader?: types.JWSHeaderParameters, token?: types.FlattenedJWSInput) => Promise<types.CryptoKey>;
@@ -0,0 +1,306 @@
1
+ /**
2
+ * Verification using a JSON Web Key Set (JWKS) available on an HTTP(S) URL
3
+ *
4
+ * @module
5
+ */
6
+ import type * as types from '../types.d.ts';
7
+ /**
8
+ * When passed to {@link jwks/remote.createRemoteJWKSet createRemoteJWKSet} this allows the resolver
9
+ * to make use of advanced fetch configurations, HTTP Proxies, retry on network errors, etc.
10
+ *
11
+ * > [!NOTE]\
12
+ * > Known caveat: Expect Type-related issues when passing the inputs through to fetch-like modules,
13
+ * > they hardly ever get their typings inline with actual fetch, you should `@ts-expect-error` them.
14
+ *
15
+ * @example
16
+ *
17
+ * Using [sindresorhus/ky](https://github.com/sindresorhus/ky) for retries and its hooks feature for
18
+ * logging outgoing requests and their responses.
19
+ *
20
+ * ```ts
21
+ * import ky from 'ky'
22
+ *
23
+ * let logRequest!: (request: Request) => void
24
+ * let logResponse!: (request: Request, response: Response) => void
25
+ * let logRetry!: (request: Request, error: Error, retryCount: number) => void
26
+ *
27
+ * const JWKS = jose.createRemoteJWKSet(url, {
28
+ * [jose.customFetch]: (...args) =>
29
+ * ky(args[0], {
30
+ * ...args[1],
31
+ * hooks: {
32
+ * beforeRequest: [
33
+ * (request) => {
34
+ * logRequest(request)
35
+ * },
36
+ * ],
37
+ * beforeRetry: [
38
+ * ({ request, error, retryCount }) => {
39
+ * logRetry(request, error, retryCount)
40
+ * },
41
+ * ],
42
+ * afterResponse: [
43
+ * (request, _, response) => {
44
+ * logResponse(request, response)
45
+ * },
46
+ * ],
47
+ * },
48
+ * }),
49
+ * })
50
+ * ```
51
+ *
52
+ * @example
53
+ *
54
+ * Using [nodejs/undici](https://github.com/nodejs/undici) to detect and use HTTP proxies.
55
+ *
56
+ * ```ts
57
+ * import * as undici from 'undici'
58
+ *
59
+ * // see https://undici.nodejs.org/#/docs/api/EnvHttpProxyAgent
60
+ * let envHttpProxyAgent = new undici.EnvHttpProxyAgent()
61
+ *
62
+ * // @ts-ignore
63
+ * const JWKS = jose.createRemoteJWKSet(url, {
64
+ * [jose.customFetch]: (...args) => {
65
+ * // @ts-ignore
66
+ * return undici.fetch(args[0], { ...args[1], dispatcher: envHttpProxyAgent }) // prettier-ignore
67
+ * },
68
+ * })
69
+ * ```
70
+ *
71
+ * @example
72
+ *
73
+ * Using [nodejs/undici](https://github.com/nodejs/undici) to automatically retry network errors.
74
+ *
75
+ * ```ts
76
+ * import * as undici from 'undici'
77
+ *
78
+ * // see https://undici.nodejs.org/#/docs/api/RetryAgent
79
+ * let retryAgent = new undici.RetryAgent(new undici.Agent(), {
80
+ * statusCodes: [],
81
+ * errorCodes: [
82
+ * 'ECONNRESET',
83
+ * 'ECONNREFUSED',
84
+ * 'ENOTFOUND',
85
+ * 'ENETDOWN',
86
+ * 'ENETUNREACH',
87
+ * 'EHOSTDOWN',
88
+ * 'UND_ERR_SOCKET',
89
+ * ],
90
+ * })
91
+ *
92
+ * // @ts-ignore
93
+ * const JWKS = jose.createRemoteJWKSet(url, {
94
+ * [jose.customFetch]: (...args) => {
95
+ * // @ts-ignore
96
+ * return undici.fetch(args[0], { ...args[1], dispatcher: retryAgent }) // prettier-ignore
97
+ * },
98
+ * })
99
+ * ```
100
+ *
101
+ * @example
102
+ *
103
+ * Using [nodejs/undici](https://github.com/nodejs/undici) to mock responses in tests.
104
+ *
105
+ * ```ts
106
+ * import * as undici from 'undici'
107
+ *
108
+ * // see https://undici.nodejs.org/#/docs/api/MockAgent
109
+ * let mockAgent = new undici.MockAgent()
110
+ * mockAgent.disableNetConnect()
111
+ *
112
+ * // @ts-ignore
113
+ * const JWKS = jose.createRemoteJWKSet(url, {
114
+ * [jose.customFetch]: (...args) => {
115
+ * // @ts-ignore
116
+ * return undici.fetch(args[0], { ...args[1], dispatcher: mockAgent }) // prettier-ignore
117
+ * },
118
+ * })
119
+ * ```
120
+ */
121
+ export declare const customFetch: unique symbol;
122
+ /** See {@link customFetch}. */
123
+ export type FetchImplementation = (
124
+ /** URL the request is being made sent to {@link !fetch} as the `resource` argument */
125
+ url: string,
126
+ /** Options otherwise sent to {@link !fetch} as the `options` argument */
127
+ options: {
128
+ /** HTTP Headers */
129
+ headers: Headers;
130
+ /** The {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods request method} */
131
+ method: 'GET';
132
+ /** See {@link !Request.redirect} */
133
+ redirect: 'manual';
134
+ signal: AbortSignal;
135
+ }) => Promise<Response>;
136
+ /**
137
+ * > [!WARNING]\
138
+ * > This option has security implications that must be understood, assessed for applicability, and
139
+ * > accepted before use. It is critical that the JSON Web Key Set cache only be writable by your own
140
+ * > code.
141
+ *
142
+ * This option is intended for cloud computing runtimes that cannot keep an in memory cache between
143
+ * their code's invocations. Use in runtimes where an in memory cache between requests is available
144
+ * is not desirable.
145
+ *
146
+ * When passed to {@link jwks/remote.createRemoteJWKSet createRemoteJWKSet} this allows the passed in
147
+ * object to:
148
+ *
149
+ * - Serve as an initial value for the JSON Web Key Set that the module would otherwise need to
150
+ * trigger an HTTP request for
151
+ * - Have the JSON Web Key Set the function optionally ended up triggering an HTTP request for
152
+ * assigned to it as properties
153
+ *
154
+ * The intended use pattern is:
155
+ *
156
+ * - Before verifying with {@link jwks/remote.createRemoteJWKSet createRemoteJWKSet} you pull the
157
+ * previously cached object from a low-latency key-value store offered by the cloud computing
158
+ * runtime it is executed on;
159
+ * - Default to an empty object `{}` instead when there's no previously cached value;
160
+ * - Pass it in as {@link RemoteJWKSetOptions[jwksCache]};
161
+ * - Afterwards, update the key-value storage if the {@link ExportedJWKSCache.uat `uat`} property of
162
+ * the object has changed.
163
+ *
164
+ * @example
165
+ *
166
+ * ```ts
167
+ * // Prerequisites
168
+ * let url!: URL
169
+ * let jwt!: string
170
+ * let getPreviouslyCachedJWKS!: () => Promise<jose.ExportedJWKSCache>
171
+ * let storeNewJWKScache!: (cache: jose.ExportedJWKSCache) => Promise<void>
172
+ *
173
+ * // Load JSON Web Key Set cache
174
+ * const jwksCache: jose.JWKSCacheInput = (await getPreviouslyCachedJWKS()) || {}
175
+ * const { uat } = jwksCache
176
+ *
177
+ * const JWKS = jose.createRemoteJWKSet(url, {
178
+ * [jose.jwksCache]: jwksCache,
179
+ * })
180
+ *
181
+ * // Use JSON Web Key Set cache
182
+ * await jose.jwtVerify(jwt, JWKS)
183
+ *
184
+ * if (uat !== jwksCache.uat) {
185
+ * // Update JSON Web Key Set cache
186
+ * await storeNewJWKScache(jwksCache)
187
+ * }
188
+ * ```
189
+ */
190
+ export declare const jwksCache: unique symbol;
191
+ /** Options for the remote JSON Web Key Set. */
192
+ export interface RemoteJWKSetOptions {
193
+ /**
194
+ * Timeout (in milliseconds) for the HTTP request. When reached the request will be aborted and
195
+ * the verification will fail. Default is 5000 (5 seconds).
196
+ */
197
+ timeoutDuration?: number;
198
+ /**
199
+ * Duration (in milliseconds) for which no more HTTP requests will be triggered after a previous
200
+ * successful fetch. Default is 30000 (30 seconds).
201
+ */
202
+ cooldownDuration?: number;
203
+ /**
204
+ * Maximum time (in milliseconds) between successful HTTP requests. Default is 600000 (10
205
+ * minutes).
206
+ */
207
+ cacheMaxAge?: number | typeof Infinity;
208
+ /** Headers to be sent with the HTTP request. */
209
+ headers?: Record<string, string>;
210
+ /** See {@link jwksCache}. */
211
+ [jwksCache]?: JWKSCacheInput;
212
+ /** See {@link customFetch}. */
213
+ [customFetch]?: FetchImplementation;
214
+ }
215
+ /** See {@link jwksCache}. */
216
+ export interface ExportedJWKSCache {
217
+ /** Current cached JSON Web Key Set */
218
+ jwks: types.JSONWebKeySet;
219
+ /** Last updated at timestamp (seconds since epoch) */
220
+ uat: number;
221
+ }
222
+ /** See {@link jwksCache}. */
223
+ export type JWKSCacheInput = ExportedJWKSCache | Record<string, never>;
224
+ /**
225
+ * Returns a function that resolves a JWS JOSE Header to a public key object downloaded from a
226
+ * remote endpoint returning a JSON Web Key Set, that is, for example, an OAuth 2.0 or OIDC
227
+ * jwks_uri. The JSON Web Key Set is fetched when no key matches the selection process but only as
228
+ * frequently as the `cooldownDuration` option allows to prevent abuse.
229
+ *
230
+ * It uses the "alg" (JWS Algorithm) Header Parameter to determine the right JWK "kty" (Key Type),
231
+ * then proceeds to match the JWK "kid" (Key ID) with one found in the JWS Header Parameters (if
232
+ * there is one) while also respecting the JWK "use" (Public Key Use) and JWK "key_ops" (Key
233
+ * Operations) Parameters (if they are present on the JWK).
234
+ *
235
+ * Only a single public key must match the selection process. As shown in the example below when
236
+ * multiple keys get matched it is possible to opt-in to iterate over the matched keys and attempt
237
+ * verification in an iterative manner.
238
+ *
239
+ * > [!NOTE]\
240
+ * > The function's purpose is to resolve public keys used for verifying signatures and will not work
241
+ * > for public encryption keys.
242
+ *
243
+ * This function is exported (as a named export) from the main `'jose'` module entry point as well
244
+ * as from its subpath export `'jose/jwks/remote'`.
245
+ *
246
+ * @example
247
+ *
248
+ * ```js
249
+ * const JWKS = jose.createRemoteJWKSet(new URL('https://www.googleapis.com/oauth2/v3/certs'))
250
+ *
251
+ * const { payload, protectedHeader } = await jose.jwtVerify(jwt, JWKS, {
252
+ * issuer: 'urn:example:issuer',
253
+ * audience: 'urn:example:audience',
254
+ * })
255
+ * console.log(protectedHeader)
256
+ * console.log(payload)
257
+ * ```
258
+ *
259
+ * @example
260
+ *
261
+ * Opting-in to multiple JWKS matches using `createRemoteJWKSet`
262
+ *
263
+ * ```js
264
+ * const options = {
265
+ * issuer: 'urn:example:issuer',
266
+ * audience: 'urn:example:audience',
267
+ * }
268
+ * const { payload, protectedHeader } = await jose
269
+ * .jwtVerify(jwt, JWKS, options)
270
+ * .catch(async (error) => {
271
+ * if (error?.code === 'ERR_JWKS_MULTIPLE_MATCHING_KEYS') {
272
+ * for await (const publicKey of error) {
273
+ * try {
274
+ * return await jose.jwtVerify(jwt, publicKey, options)
275
+ * } catch (innerError) {
276
+ * if (innerError?.code === 'ERR_JWS_SIGNATURE_VERIFICATION_FAILED') {
277
+ * continue
278
+ * }
279
+ * throw innerError
280
+ * }
281
+ * }
282
+ * throw new jose.errors.JWSSignatureVerificationFailed()
283
+ * }
284
+ *
285
+ * throw error
286
+ * })
287
+ * console.log(protectedHeader)
288
+ * console.log(payload)
289
+ * ```
290
+ *
291
+ * @param url URL to fetch the JSON Web Key Set from.
292
+ * @param options Options for the remote JSON Web Key Set.
293
+ */
294
+ export declare function createRemoteJWKSet(url: URL, options?: RemoteJWKSetOptions): {
295
+ (protectedHeader?: types.JWSHeaderParameters, token?: types.FlattenedJWSInput): Promise<types.CryptoKey>;
296
+ /** @ignore */
297
+ coolingDown: boolean;
298
+ /** @ignore */
299
+ fresh: boolean;
300
+ /** @ignore */
301
+ reloading: boolean;
302
+ /** @ignore */
303
+ reload: () => Promise<void>;
304
+ /** @ignore */
305
+ jwks: () => types.JSONWebKeySet | undefined;
306
+ };
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Signing JSON Web Signature (JWS) in Compact Serialization
3
+ *
4
+ * @module
5
+ */
6
+ import type * as types from '../../types.d.ts';
7
+ /**
8
+ * The CompactSign class is used to build and sign Compact JWS strings.
9
+ *
10
+ * This class is exported (as a named export) from the main `'jose'` module entry point as well as
11
+ * from its subpath export `'jose/jws/compact/sign'`.
12
+ *
13
+ * @example
14
+ *
15
+ * ```js
16
+ * const jws = await new jose.CompactSign(
17
+ * new TextEncoder().encode('It’s a dangerous business, Frodo, going out your door.'),
18
+ * )
19
+ * .setProtectedHeader({ alg: 'ES256' })
20
+ * .sign(privateKey)
21
+ *
22
+ * console.log(jws)
23
+ * ```
24
+ */
25
+ export declare class CompactSign {
26
+ #private;
27
+ /**
28
+ * {@link CompactSign} constructor
29
+ *
30
+ * @param payload Binary representation of the payload to sign.
31
+ */
32
+ constructor(payload: Uint8Array);
33
+ /**
34
+ * Sets the JWS Protected Header on the CompactSign object.
35
+ *
36
+ * @param protectedHeader JWS Protected Header.
37
+ */
38
+ setProtectedHeader(protectedHeader: types.CompactJWSHeaderParameters): this;
39
+ /**
40
+ * Signs and resolves the value of the Compact JWS string.
41
+ *
42
+ * @param key Private Key or Secret to sign the JWS with. See
43
+ * {@link https://github.com/panva/jose/issues/210#jws-alg Algorithm Key Requirements}.
44
+ * @param options JWS Sign options.
45
+ */
46
+ sign(key: types.CryptoKey | types.KeyObject | types.JWK | Uint8Array, options?: types.SignOptions): Promise<string>;
47
+ }
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Verifying JSON Web Signature (JWS) in Compact Serialization
3
+ *
4
+ * @module
5
+ */
6
+ import type * as types from '../../types.d.ts';
7
+ /**
8
+ * Interface for Compact JWS Verification dynamic key resolution. No token components have been
9
+ * verified at the time of this function call.
10
+ *
11
+ * @see {@link jwks/remote.createRemoteJWKSet createRemoteJWKSet} to verify using a remote JSON Web Key Set.
12
+ */
13
+ export interface CompactVerifyGetKey extends types.GenericGetKeyFunction<types.CompactJWSHeaderParameters, types.FlattenedJWSInput, types.CryptoKey | types.KeyObject | types.JWK | Uint8Array> {
14
+ }
15
+ /**
16
+ * Verifies the signature and format of and afterwards decodes the Compact JWS.
17
+ *
18
+ * This function is exported (as a named export) from the main `'jose'` module entry point as well
19
+ * as from its subpath export `'jose/jws/compact/verify'`.
20
+ *
21
+ * @example
22
+ *
23
+ * ```js
24
+ * const jws =
25
+ * 'eyJhbGciOiJFUzI1NiJ9.SXTigJlzIGEgZGFuZ2Vyb3VzIGJ1c2luZXNzLCBGcm9kbywgZ29pbmcgb3V0IHlvdXIgZG9vci4.kkAs_gPPxWMI3rHuVlxHaTPfDWDoqdI8jSvuSmqV-8IHIWXg9mcAeC9ggV-45ZHRbiRJ3obUIFo1rHphPA5URg'
26
+ *
27
+ * const { payload, protectedHeader } = await jose.compactVerify(jws, publicKey)
28
+ *
29
+ * console.log(protectedHeader)
30
+ * console.log(new TextDecoder().decode(payload))
31
+ * ```
32
+ *
33
+ * @param jws Compact JWS.
34
+ * @param key Key to verify the JWS with. See
35
+ * {@link https://github.com/panva/jose/issues/210#jws-alg Algorithm Key Requirements}.
36
+ * @param options JWS Verify options.
37
+ */
38
+ export declare function compactVerify(jws: string | Uint8Array, key: types.CryptoKey | types.KeyObject | types.JWK | Uint8Array, options?: types.VerifyOptions): Promise<types.CompactVerifyResult>;
39
+ /**
40
+ * @param jws Compact JWS.
41
+ * @param getKey Function resolving a key to verify the JWS with. See
42
+ * {@link https://github.com/panva/jose/issues/210#jws-alg Algorithm Key Requirements}.
43
+ * @param options JWS Verify options.
44
+ */
45
+ export declare function compactVerify(jws: string | Uint8Array, getKey: CompactVerifyGetKey, options?: types.VerifyOptions): Promise<types.CompactVerifyResult & types.ResolvedKey>;
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Signing JSON Web Signature (JWS) in Flattened JSON Serialization
3
+ *
4
+ * @module
5
+ */
6
+ import type * as types from '../../types.d.ts';
7
+ /**
8
+ * The FlattenedSign class is used to build and sign Flattened JWS objects.
9
+ *
10
+ * This class is exported (as a named export) from the main `'jose'` module entry point as well as
11
+ * from its subpath export `'jose/jws/flattened/sign'`.
12
+ *
13
+ * @example
14
+ *
15
+ * ```js
16
+ * const jws = await new jose.FlattenedSign(
17
+ * new TextEncoder().encode('It’s a dangerous business, Frodo, going out your door.'),
18
+ * )
19
+ * .setProtectedHeader({ alg: 'ES256' })
20
+ * .sign(privateKey)
21
+ *
22
+ * console.log(jws)
23
+ * ```
24
+ */
25
+ export declare class FlattenedSign {
26
+ #private;
27
+ /**
28
+ * {@link FlattenedSign} constructor
29
+ *
30
+ * @param payload Binary representation of the payload to sign.
31
+ */
32
+ constructor(payload: Uint8Array);
33
+ /**
34
+ * Sets the JWS Protected Header on the FlattenedSign object.
35
+ *
36
+ * @param protectedHeader JWS Protected Header.
37
+ */
38
+ setProtectedHeader(protectedHeader: types.JWSHeaderParameters): this;
39
+ /**
40
+ * Sets the JWS Unprotected Header on the FlattenedSign object.
41
+ *
42
+ * @param unprotectedHeader JWS Unprotected Header.
43
+ */
44
+ setUnprotectedHeader(unprotectedHeader: types.JWSHeaderParameters): this;
45
+ /**
46
+ * Signs and resolves the value of the Flattened JWS object.
47
+ *
48
+ * @param key Private Key or Secret to sign the JWS with. See
49
+ * {@link https://github.com/panva/jose/issues/210#jws-alg Algorithm Key Requirements}.
50
+ * @param options JWS Sign options.
51
+ */
52
+ sign(key: types.CryptoKey | types.KeyObject | types.JWK | Uint8Array, options?: types.SignOptions): Promise<types.FlattenedJWS>;
53
+ }
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Verifying JSON Web Signature (JWS) in Flattened JSON Serialization
3
+ *
4
+ * @module
5
+ */
6
+ import type * as types from '../../types.d.ts';
7
+ /**
8
+ * Interface for Flattened JWS Verification dynamic key resolution. No token components have been
9
+ * verified at the time of this function call.
10
+ *
11
+ * @see {@link jwks/remote.createRemoteJWKSet createRemoteJWKSet} to verify using a remote JSON Web Key Set.
12
+ */
13
+ export interface FlattenedVerifyGetKey extends types.GenericGetKeyFunction<types.JWSHeaderParameters | undefined, types.FlattenedJWSInput, types.CryptoKey | types.KeyObject | types.JWK | Uint8Array> {
14
+ }
15
+ /**
16
+ * Verifies the signature and format of and afterwards decodes the Flattened JWS.
17
+ *
18
+ * This function is exported (as a named export) from the main `'jose'` module entry point as well
19
+ * as from its subpath export `'jose/jws/flattened/verify'`.
20
+ *
21
+ * @example
22
+ *
23
+ * ```js
24
+ * const decoder = new TextDecoder()
25
+ * const jws = {
26
+ * signature:
27
+ * 'FVVOXwj6kD3DqdfD9yYqfT2W9jv-Nop4kOehp_DeDGNB5dQNSPRvntBY6xH3uxlCxE8na9d_kyhYOcanpDJ0EA',
28
+ * payload: 'SXTigJlzIGEgZGFuZ2Vyb3VzIGJ1c2luZXNzLCBGcm9kbywgZ29pbmcgb3V0IHlvdXIgZG9vci4',
29
+ * protected: 'eyJhbGciOiJFUzI1NiJ9',
30
+ * }
31
+ *
32
+ * const { payload, protectedHeader } = await jose.flattenedVerify(jws, publicKey)
33
+ *
34
+ * console.log(protectedHeader)
35
+ * console.log(decoder.decode(payload))
36
+ * ```
37
+ *
38
+ * @param jws Flattened JWS.
39
+ * @param key Key to verify the JWS with. See
40
+ * {@link https://github.com/panva/jose/issues/210#jws-alg Algorithm Key Requirements}.
41
+ * @param options JWS Verify options.
42
+ */
43
+ export declare function flattenedVerify(jws: types.FlattenedJWSInput, key: types.CryptoKey | types.KeyObject | types.JWK | Uint8Array, options?: types.VerifyOptions): Promise<types.FlattenedVerifyResult>;
44
+ /**
45
+ * @param jws Flattened JWS.
46
+ * @param getKey Function resolving a key to verify the JWS with. See
47
+ * {@link https://github.com/panva/jose/issues/210#jws-alg Algorithm Key Requirements}.
48
+ * @param options JWS Verify options.
49
+ */
50
+ export declare function flattenedVerify(jws: types.FlattenedJWSInput, getKey: FlattenedVerifyGetKey, options?: types.VerifyOptions): Promise<types.FlattenedVerifyResult & types.ResolvedKey>;