@logto/client 2.2.3 → 2.2.4

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.
package/lib/index.cjs CHANGED
@@ -24,6 +24,21 @@ var requester = require('./utils/requester.cjs');
24
24
  class LogtoClient {
25
25
  constructor(logtoConfig, adapter) {
26
26
  this.getOidcConfig = memoize.memoize(this.#getOidcConfig);
27
+ /**
28
+ * Get the access token from the storage.
29
+ *
30
+ * - If the access token has expired, it will try to fetch a new one using the Refresh Token.
31
+ * - If there's an ongoing Promise to fetch the access token, it will return the Promise.
32
+ *
33
+ * If you want to get the access token claims, use {@link getAccessTokenClaims} instead.
34
+ *
35
+ * @param resource The resource that the access token is granted for. If not
36
+ * specified, the access token will be used for OpenID Connect or the default
37
+ * resource, as specified in the Logto Console.
38
+ * @returns The access token string.
39
+ * @throws LogtoClientError if the user is not authenticated.
40
+ */
41
+ this.getAccessToken = memoize.memoize(this.#getAccessToken);
27
42
  this.getJwtVerifyGetKey = once.once(this.#getJwtVerifyGetKey);
28
43
  this.accessTokenMap = new Map();
29
44
  this.logtoConfig = {
@@ -53,36 +68,6 @@ class LogtoClient {
53
68
  async getIdToken() {
54
69
  return this.adapter.storage.getItem('idToken');
55
70
  }
56
- /**
57
- * Get the Access Token from the storage. If the Access Token has expired, it
58
- * will try to fetch a new one using the Refresh Token.
59
- *
60
- * If you want to get the Access Token claims, use {@link getAccessTokenClaims} instead.
61
- *
62
- * @param resource The resource that the Access Token is granted for. If not
63
- * specified, the Access Token will be used for OpenID Connect or the default
64
- * resource, as specified in the Logto Console.
65
- * @returns The Access Token string.
66
- * @throws LogtoClientError if the user is not authenticated.
67
- */
68
- async getAccessToken(resource) {
69
- if (!(await this.getIdToken())) {
70
- throw new errors.LogtoClientError('not_authenticated');
71
- }
72
- const accessTokenKey = index$2.buildAccessTokenKey(resource);
73
- const accessToken = this.accessTokenMap.get(accessTokenKey);
74
- if (accessToken && accessToken.expiresAt > Date.now() / 1000) {
75
- return accessToken.token;
76
- }
77
- // Since the access token has expired, delete it from the map.
78
- if (accessToken) {
79
- this.accessTokenMap.delete(accessTokenKey);
80
- }
81
- /**
82
- * Need to fetch a new access token using refresh token.
83
- */
84
- return this.getAccessTokenByRefreshToken(resource);
85
- }
86
71
  /**
87
72
  * Get the ID Token claims.
88
73
  */
@@ -94,10 +79,10 @@ class LogtoClient {
94
79
  return js.decodeIdToken(idToken);
95
80
  }
96
81
  /**
97
- * Get the Access Token claims for the specified resource.
82
+ * Get the access token claims for the specified resource.
98
83
  *
99
- * @param resource The resource that the Access Token is granted for. If not
100
- * specified, the Access Token will be used for OpenID Connect or the default
84
+ * @param resource The resource that the access token is granted for. If not
85
+ * specified, the access token will be used for OpenID Connect or the default
101
86
  * resource, as specified in the Logto Console.
102
87
  */
103
88
  async getAccessTokenClaims(resource) {
@@ -358,6 +343,24 @@ class LogtoClient {
358
343
  const cachedJwkSet = new remoteJwkSet.CachedRemoteJwkSet(new URL(jwksUri), this.adapter);
359
344
  return async (...args) => cachedJwkSet.getKey(...args);
360
345
  }
346
+ async #getAccessToken(resource) {
347
+ if (!(await this.getIdToken())) {
348
+ throw new errors.LogtoClientError('not_authenticated');
349
+ }
350
+ const accessTokenKey = index$2.buildAccessTokenKey(resource);
351
+ const accessToken = this.accessTokenMap.get(accessTokenKey);
352
+ if (accessToken && accessToken.expiresAt > Date.now() / 1000) {
353
+ return accessToken.token;
354
+ }
355
+ // Since the access token has expired, delete it from the map.
356
+ if (accessToken) {
357
+ this.accessTokenMap.delete(accessTokenKey);
358
+ }
359
+ /**
360
+ * Need to fetch a new access token using refresh token.
361
+ */
362
+ return this.getAccessTokenByRefreshToken(resource);
363
+ }
361
364
  }
362
365
 
363
366
  Object.defineProperty(exports, 'LogtoError', {
package/lib/index.d.ts CHANGED
@@ -19,8 +19,8 @@ export * from './types/index.js';
19
19
  */
20
20
  export default class LogtoClient {
21
21
  #private;
22
- protected readonly logtoConfig: LogtoConfig;
23
- protected readonly getOidcConfig: (this: unknown) => Promise<import("@silverhand/essentials").KeysToCamelCase<{
22
+ readonly logtoConfig: LogtoConfig;
23
+ readonly getOidcConfig: (this: unknown) => Promise<import("@silverhand/essentials").KeysToCamelCase<{
24
24
  authorization_endpoint: string;
25
25
  token_endpoint: string;
26
26
  userinfo_endpoint: string;
@@ -29,6 +29,21 @@ export default class LogtoClient {
29
29
  jwks_uri: string;
30
30
  issuer: string;
31
31
  }>>;
32
+ /**
33
+ * Get the access token from the storage.
34
+ *
35
+ * - If the access token has expired, it will try to fetch a new one using the Refresh Token.
36
+ * - If there's an ongoing Promise to fetch the access token, it will return the Promise.
37
+ *
38
+ * If you want to get the access token claims, use {@link getAccessTokenClaims} instead.
39
+ *
40
+ * @param resource The resource that the access token is granted for. If not
41
+ * specified, the access token will be used for OpenID Connect or the default
42
+ * resource, as specified in the Logto Console.
43
+ * @returns The access token string.
44
+ * @throws LogtoClientError if the user is not authenticated.
45
+ */
46
+ readonly getAccessToken: (this: unknown, resource?: string | undefined) => Promise<string>;
32
47
  protected readonly getJwtVerifyGetKey: (...args: unknown[]) => Promise<JWTVerifyGetKey>;
33
48
  protected readonly adapter: ClientAdapterInstance;
34
49
  protected readonly accessTokenMap: Map<string, AccessToken>;
@@ -46,28 +61,15 @@ export default class LogtoClient {
46
61
  * use {@link getIdTokenClaims} instead.
47
62
  */
48
63
  getIdToken(): Promise<Nullable<string>>;
49
- /**
50
- * Get the Access Token from the storage. If the Access Token has expired, it
51
- * will try to fetch a new one using the Refresh Token.
52
- *
53
- * If you want to get the Access Token claims, use {@link getAccessTokenClaims} instead.
54
- *
55
- * @param resource The resource that the Access Token is granted for. If not
56
- * specified, the Access Token will be used for OpenID Connect or the default
57
- * resource, as specified in the Logto Console.
58
- * @returns The Access Token string.
59
- * @throws LogtoClientError if the user is not authenticated.
60
- */
61
- getAccessToken(resource?: string): Promise<string>;
62
64
  /**
63
65
  * Get the ID Token claims.
64
66
  */
65
67
  getIdTokenClaims(): Promise<IdTokenClaims>;
66
68
  /**
67
- * Get the Access Token claims for the specified resource.
69
+ * Get the access token claims for the specified resource.
68
70
  *
69
- * @param resource The resource that the Access Token is granted for. If not
70
- * specified, the Access Token will be used for OpenID Connect or the default
71
+ * @param resource The resource that the access token is granted for. If not
72
+ * specified, the access token will be used for OpenID Connect or the default
71
73
  * resource, as specified in the Logto Console.
72
74
  */
73
75
  getAccessTokenClaims(resource?: string): Promise<AccessTokenClaims>;
package/lib/index.js CHANGED
@@ -21,6 +21,21 @@ export { createRequester } from './utils/requester.js';
21
21
  class LogtoClient {
22
22
  constructor(logtoConfig, adapter) {
23
23
  this.getOidcConfig = memoize(this.#getOidcConfig);
24
+ /**
25
+ * Get the access token from the storage.
26
+ *
27
+ * - If the access token has expired, it will try to fetch a new one using the Refresh Token.
28
+ * - If there's an ongoing Promise to fetch the access token, it will return the Promise.
29
+ *
30
+ * If you want to get the access token claims, use {@link getAccessTokenClaims} instead.
31
+ *
32
+ * @param resource The resource that the access token is granted for. If not
33
+ * specified, the access token will be used for OpenID Connect or the default
34
+ * resource, as specified in the Logto Console.
35
+ * @returns The access token string.
36
+ * @throws LogtoClientError if the user is not authenticated.
37
+ */
38
+ this.getAccessToken = memoize(this.#getAccessToken);
24
39
  this.getJwtVerifyGetKey = once(this.#getJwtVerifyGetKey);
25
40
  this.accessTokenMap = new Map();
26
41
  this.logtoConfig = {
@@ -50,36 +65,6 @@ class LogtoClient {
50
65
  async getIdToken() {
51
66
  return this.adapter.storage.getItem('idToken');
52
67
  }
53
- /**
54
- * Get the Access Token from the storage. If the Access Token has expired, it
55
- * will try to fetch a new one using the Refresh Token.
56
- *
57
- * If you want to get the Access Token claims, use {@link getAccessTokenClaims} instead.
58
- *
59
- * @param resource The resource that the Access Token is granted for. If not
60
- * specified, the Access Token will be used for OpenID Connect or the default
61
- * resource, as specified in the Logto Console.
62
- * @returns The Access Token string.
63
- * @throws LogtoClientError if the user is not authenticated.
64
- */
65
- async getAccessToken(resource) {
66
- if (!(await this.getIdToken())) {
67
- throw new LogtoClientError('not_authenticated');
68
- }
69
- const accessTokenKey = buildAccessTokenKey(resource);
70
- const accessToken = this.accessTokenMap.get(accessTokenKey);
71
- if (accessToken && accessToken.expiresAt > Date.now() / 1000) {
72
- return accessToken.token;
73
- }
74
- // Since the access token has expired, delete it from the map.
75
- if (accessToken) {
76
- this.accessTokenMap.delete(accessTokenKey);
77
- }
78
- /**
79
- * Need to fetch a new access token using refresh token.
80
- */
81
- return this.getAccessTokenByRefreshToken(resource);
82
- }
83
68
  /**
84
69
  * Get the ID Token claims.
85
70
  */
@@ -91,10 +76,10 @@ class LogtoClient {
91
76
  return decodeIdToken(idToken);
92
77
  }
93
78
  /**
94
- * Get the Access Token claims for the specified resource.
79
+ * Get the access token claims for the specified resource.
95
80
  *
96
- * @param resource The resource that the Access Token is granted for. If not
97
- * specified, the Access Token will be used for OpenID Connect or the default
81
+ * @param resource The resource that the access token is granted for. If not
82
+ * specified, the access token will be used for OpenID Connect or the default
98
83
  * resource, as specified in the Logto Console.
99
84
  */
100
85
  async getAccessTokenClaims(resource) {
@@ -355,6 +340,24 @@ class LogtoClient {
355
340
  const cachedJwkSet = new CachedRemoteJwkSet(new URL(jwksUri), this.adapter);
356
341
  return async (...args) => cachedJwkSet.getKey(...args);
357
342
  }
343
+ async #getAccessToken(resource) {
344
+ if (!(await this.getIdToken())) {
345
+ throw new LogtoClientError('not_authenticated');
346
+ }
347
+ const accessTokenKey = buildAccessTokenKey(resource);
348
+ const accessToken = this.accessTokenMap.get(accessTokenKey);
349
+ if (accessToken && accessToken.expiresAt > Date.now() / 1000) {
350
+ return accessToken.token;
351
+ }
352
+ // Since the access token has expired, delete it from the map.
353
+ if (accessToken) {
354
+ this.accessTokenMap.delete(accessTokenKey);
355
+ }
356
+ /**
357
+ * Need to fetch a new access token using refresh token.
358
+ */
359
+ return this.getAccessTokenByRefreshToken(resource);
360
+ }
358
361
  }
359
362
 
360
363
  export { CacheKey, LogtoClientError, PersistKey, LogtoClient as default, isLogtoAccessTokenMap, isLogtoSignInSessionItem };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@logto/client",
3
- "version": "2.2.3",
3
+ "version": "2.2.4",
4
4
  "type": "module",
5
5
  "main": "./lib/index.cjs",
6
6
  "module": "./lib/index.js",
@@ -36,7 +36,7 @@
36
36
  "eslint": "^8.44.0",
37
37
  "jest": "^29.5.0",
38
38
  "jest-matcher-specific-error": "^1.0.0",
39
- "lint-staged": "^14.0.0",
39
+ "lint-staged": "^15.0.0",
40
40
  "nock": "^13.3.0",
41
41
  "prettier": "^3.0.0",
42
42
  "text-encoder": "^0.0.4",