@logto/client 2.3.3 → 3.0.0-alpha.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.
@@ -1,60 +0,0 @@
1
- import { isObject } from '@silverhand/essentials';
2
- import { createLocalJWKSet } from 'jose';
3
- import { CacheKey } from './adapter/types.js';
4
-
5
- // Edited from jose's internal util `isJWKSLike`
6
- function isJwkSetLike(jwkSet) {
7
- return Boolean(jwkSet &&
8
- typeof jwkSet === 'object' &&
9
- 'keys' in jwkSet &&
10
- Array.isArray(jwkSet.keys) &&
11
- jwkSet.keys.every((element) => isObject(element)));
12
- }
13
- class CachedRemoteJwkSet {
14
- constructor(url, adapter) {
15
- this.url = url;
16
- this.adapter = adapter;
17
- if (!adapter.unstable_cache) {
18
- throw new Error("No cache found in the client adapter. Use `createRemoteJWKSet()` from 'jose' instead.");
19
- }
20
- }
21
- async getKey(...args) {
22
- if (!this.jwkSet) {
23
- this.jwkSet = await this.#load();
24
- }
25
- try {
26
- return await this.#getLocalKey(...args);
27
- }
28
- catch (error) {
29
- // Jose does not export the error definition
30
- // Found in https://github.com/panva/jose/blob/d5b3cb672736112b1e1e31ac4d5e9cd641675206/src/util/errors.ts#L347
31
- if (error instanceof Error && 'code' in error && error.code === 'ERR_JWKS_NO_MATCHING_KEY') {
32
- this.jwkSet = await this.#load();
33
- return this.#getLocalKey(...args);
34
- }
35
- throw error;
36
- }
37
- }
38
- async #load() {
39
- return this.adapter.getWithCache(CacheKey.Jwks, async () => {
40
- const controller = new AbortController();
41
- const response = await fetch(this.url, { signal: controller.signal, redirect: 'manual' });
42
- if (!response.ok) {
43
- throw new Error('Expected OK from the JSON Web Key Set HTTP response');
44
- }
45
- const json = await response.json();
46
- if (!isJwkSetLike(json)) {
47
- throw new Error('JSON Web Key Set malformed');
48
- }
49
- return json;
50
- });
51
- }
52
- async #getLocalKey(...args) {
53
- if (!this.jwkSet) {
54
- throw new Error('No local JWK Set found.');
55
- }
56
- return createLocalJWKSet(this.jwkSet)(...args);
57
- }
58
- }
59
-
60
- export { CachedRemoteJwkSet };