jose 5.0.1 → 5.1.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.
@@ -6,6 +6,12 @@ function isCloudflareWorkers() {
6
6
  (typeof navigator !== 'undefined' && navigator.userAgent === 'Cloudflare-Workers') ||
7
7
  (typeof EdgeRuntime !== 'undefined' && EdgeRuntime === 'vercel'));
8
8
  }
9
+ let USER_AGENT;
10
+ if (typeof navigator === 'undefined' || !navigator.userAgent?.startsWith?.('Mozilla/5.0 ')) {
11
+ const NAME = 'jose';
12
+ const VERSION = 'v5.1.0';
13
+ USER_AGENT = `${NAME}/${VERSION}`;
14
+ }
9
15
  class RemoteJWKSet extends LocalJWKSet {
10
16
  constructor(url, options) {
11
17
  super({ keys: [] });
@@ -52,6 +58,11 @@ class RemoteJWKSet extends LocalJWKSet {
52
58
  if (this._pendingFetch && isCloudflareWorkers()) {
53
59
  this._pendingFetch = undefined;
54
60
  }
61
+ const headers = new Headers(this._options.headers);
62
+ if (USER_AGENT && !headers.has('User-Agent')) {
63
+ headers.set('User-Agent', USER_AGENT);
64
+ this._options.headers = Object.fromEntries(headers.entries());
65
+ }
55
66
  this._pendingFetch || (this._pendingFetch = fetchJwks(this._url, this._timeoutDuration, this._options)
56
67
  .then((json) => {
57
68
  if (!isJWKSLike(json)) {
@@ -9,6 +9,12 @@ function isCloudflareWorkers() {
9
9
  (typeof navigator !== 'undefined' && navigator.userAgent === 'Cloudflare-Workers') ||
10
10
  (typeof EdgeRuntime !== 'undefined' && EdgeRuntime === 'vercel'));
11
11
  }
12
+ let USER_AGENT;
13
+ if (typeof navigator === 'undefined' || !navigator.userAgent?.startsWith?.('Mozilla/5.0 ')) {
14
+ const NAME = 'jose';
15
+ const VERSION = 'v5.1.0';
16
+ USER_AGENT = `${NAME}/${VERSION}`;
17
+ }
12
18
  class RemoteJWKSet extends local_js_1.LocalJWKSet {
13
19
  _url;
14
20
  _timeoutDuration;
@@ -62,6 +68,11 @@ class RemoteJWKSet extends local_js_1.LocalJWKSet {
62
68
  if (this._pendingFetch && isCloudflareWorkers()) {
63
69
  this._pendingFetch = undefined;
64
70
  }
71
+ const headers = new Headers(this._options.headers);
72
+ if (USER_AGENT && !headers.has('User-Agent')) {
73
+ headers.set('User-Agent', USER_AGENT);
74
+ this._options.headers = Object.fromEntries(headers.entries());
75
+ }
65
76
  this._pendingFetch ||= (0, fetch_jwks_js_1.default)(this._url, this._timeoutDuration, this._options)
66
77
  .then((json) => {
67
78
  if (!(0, local_js_1.isJWKSLike)(json)) {
@@ -6,6 +6,12 @@ function isCloudflareWorkers() {
6
6
  (typeof navigator !== 'undefined' && navigator.userAgent === 'Cloudflare-Workers') ||
7
7
  (typeof EdgeRuntime !== 'undefined' && EdgeRuntime === 'vercel'));
8
8
  }
9
+ let USER_AGENT;
10
+ if (typeof navigator === 'undefined' || !navigator.userAgent?.startsWith?.('Mozilla/5.0 ')) {
11
+ const NAME = 'jose';
12
+ const VERSION = 'v5.1.0';
13
+ USER_AGENT = `${NAME}/${VERSION}`;
14
+ }
9
15
  class RemoteJWKSet extends LocalJWKSet {
10
16
  _url;
11
17
  _timeoutDuration;
@@ -59,6 +65,11 @@ class RemoteJWKSet extends LocalJWKSet {
59
65
  if (this._pendingFetch && isCloudflareWorkers()) {
60
66
  this._pendingFetch = undefined;
61
67
  }
68
+ const headers = new Headers(this._options.headers);
69
+ if (USER_AGENT && !headers.has('User-Agent')) {
70
+ headers.set('User-Agent', USER_AGENT);
71
+ this._options.headers = Object.fromEntries(headers.entries());
72
+ }
62
73
  this._pendingFetch ||= fetchJwks(this._url, this._timeoutDuration, this._options)
63
74
  .then((json) => {
64
75
  if (!isJWKSLike(json)) {
@@ -9,8 +9,8 @@ export declare class LocalJWKSet<KeyLikeType extends KeyLike = KeyLike> {
9
9
  getKey(protectedHeader?: JWSHeaderParameters, token?: FlattenedJWSInput): Promise<KeyLikeType>;
10
10
  }
11
11
  /**
12
- * Returns a function that resolves to a key object from a locally stored, or otherwise available,
13
- * JSON Web Key Set.
12
+ * Returns a function that resolves a JWS JOSE Header to a public key object from a locally stored,
13
+ * or otherwise available, JSON Web Key Set.
14
14
  *
15
15
  * It uses the "alg" (JWS Algorithm) Header Parameter to determine the right JWK "kty" (Key Type),
16
16
  * then proceeds to match the JWK "kid" (Key ID) with one found in the JWS Header Parameters (if
@@ -21,6 +21,9 @@ export declare class LocalJWKSet<KeyLikeType extends KeyLike = KeyLike> {
21
21
  * multiple keys get matched it is possible to opt-in to iterate over the matched keys and attempt
22
22
  * verification in an iterative manner.
23
23
  *
24
+ * Note: The function's purpose is to resolve public keys used for verifying signatures and will not
25
+ * work for public encryption keys.
26
+ *
24
27
  * @param jwks JSON Web Key Set formatted object.
25
28
  */
26
29
  export declare function createLocalJWKSet<KeyLikeType extends KeyLike = KeyLike>(jwks: JSONWebKeySet): (protectedHeader?: JWSHeaderParameters, token?: FlattenedJWSInput) => Promise<KeyLikeType>;
@@ -25,14 +25,18 @@ export interface RemoteJWKSetOptions {
25
25
  * used outside of Node.js runtime.
26
26
  */
27
27
  agent?: any;
28
- /** Optional headers to be sent with the HTTP request. */
28
+ /**
29
+ * Headers to be sent with the HTTP request. Default is that `User-Agent: jose/v${version}` header
30
+ * is added unless the runtime is a browser in which adding an explicit headers fetch
31
+ * configuration would cause an unnecessary CORS preflight request.
32
+ */
29
33
  headers?: Record<string, string>;
30
34
  }
31
35
  /**
32
- * Returns a function that resolves to a key object downloaded from a remote endpoint returning a
33
- * JSON Web Key Set, that is, for example, an OAuth 2.0 or OIDC jwks_uri. The JSON Web Key Set is
34
- * fetched when no key matches the selection process but only as frequently as the
35
- * `cooldownDuration` option allows to prevent abuse.
36
+ * Returns a function that resolves a JWS JOSE Header to a public key object downloaded from a
37
+ * remote endpoint returning a JSON Web Key Set, that is, for example, an OAuth 2.0 or OIDC
38
+ * jwks_uri. The JSON Web Key Set is fetched when no key matches the selection process but only as
39
+ * frequently as the `cooldownDuration` option allows to prevent abuse.
36
40
  *
37
41
  * It uses the "alg" (JWS Algorithm) Header Parameter to determine the right JWK "kty" (Key Type),
38
42
  * then proceeds to match the JWK "kid" (Key ID) with one found in the JWS Header Parameters (if
@@ -43,6 +47,9 @@ export interface RemoteJWKSetOptions {
43
47
  * multiple keys get matched it is possible to opt-in to iterate over the matched keys and attempt
44
48
  * verification in an iterative manner.
45
49
  *
50
+ * Note: The function's purpose is to resolve public keys used for verifying signatures and will not
51
+ * work for public encryption keys.
52
+ *
46
53
  * @param url URL to fetch the JSON Web Key Set from.
47
54
  * @param options Options for the remote JSON Web Key Set.
48
55
  */
@@ -7,4 +7,4 @@ import type { JWTPayload } from '../types';
7
7
  *
8
8
  * @param jwt JWT token in compact JWS serialization.
9
9
  */
10
- export declare function decodeJwt(jwt: string): JWTPayload;
10
+ export declare function decodeJwt<PayloadType = JWTPayload>(jwt: string): PayloadType & JWTPayload;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jose",
3
- "version": "5.0.1",
3
+ "version": "5.1.0",
4
4
  "description": "'JSON Web Almost Everything' - JWA, JWS, JWE, JWT, JWK, JWKS for Node.js, Browser, Cloudflare Workers, Deno, Bun, and other Web-interoperable runtimes",
5
5
  "keywords": [
6
6
  "browser",