@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.
- package/lib/adapter/defaults.cjs +28 -0
- package/lib/adapter/defaults.d.ts +10 -0
- package/lib/adapter/defaults.js +25 -0
- package/lib/adapter/index.d.ts +3 -3
- package/lib/adapter/types.d.ts +29 -7
- package/lib/client.cjs +383 -0
- package/lib/client.d.ts +153 -0
- package/lib/client.js +381 -0
- package/lib/index.cjs +11 -381
- package/lib/index.d.ts +6 -149
- package/lib/index.js +10 -382
- package/lib/mock.d.ts +0 -1
- package/lib/shim.cjs +66 -0
- package/lib/shim.d.ts +13 -0
- package/lib/shim.js +7 -0
- package/lib/utils/requester.cjs +1 -0
- package/lib/utils/requester.js +1 -0
- package/package.json +14 -6
- package/lib/remote-jwk-set.cjs +0 -62
- package/lib/remote-jwk-set.d.ts +0 -10
- package/lib/remote-jwk-set.js +0 -60
- /package/lib/{remote-jwk-set.test.d.ts → adapter/defaults.test.d.ts} +0 -0
package/lib/index.cjs
CHANGED
|
@@ -2,19 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
+
var defaults = require('./adapter/defaults.cjs');
|
|
6
|
+
var client = require('./client.cjs');
|
|
5
7
|
var js = require('@logto/js');
|
|
6
|
-
var jose = require('jose');
|
|
7
|
-
var index$1 = require('./adapter/index.cjs');
|
|
8
8
|
var errors = require('./errors.cjs');
|
|
9
|
-
|
|
10
|
-
var index = require('./types/index.cjs');
|
|
11
|
-
var index$2 = require('./utils/index.cjs');
|
|
12
|
-
var memoize = require('./utils/memoize.cjs');
|
|
13
|
-
var once = require('./utils/once.cjs');
|
|
9
|
+
require('@silverhand/essentials');
|
|
14
10
|
var types = require('./adapter/types.cjs');
|
|
15
11
|
var requester = require('./utils/requester.cjs');
|
|
12
|
+
var index = require('./types/index.cjs');
|
|
16
13
|
|
|
17
|
-
/* eslint-disable max-lines */
|
|
18
14
|
/**
|
|
19
15
|
* The Logto base client class that provides the essential methods for
|
|
20
16
|
* interacting with the Logto server.
|
|
@@ -22,379 +18,13 @@ var requester = require('./utils/requester.cjs');
|
|
|
22
18
|
* It also provides an adapter object that allows the customizations of the
|
|
23
19
|
* client behavior for different environments.
|
|
24
20
|
*/
|
|
25
|
-
class LogtoClient {
|
|
26
|
-
constructor(logtoConfig, adapter) {
|
|
27
|
-
|
|
28
|
-
* Get the OIDC configuration from the discovery endpoint. This method will
|
|
29
|
-
* only fetch the configuration once and cache the result.
|
|
30
|
-
*/
|
|
31
|
-
this.getOidcConfig = once.once(this.#getOidcConfig);
|
|
32
|
-
/**
|
|
33
|
-
* Get the access token from the storage with refresh strategy.
|
|
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
|
-
this.getAccessToken = memoize.memoize(this.#getAccessToken);
|
|
47
|
-
/**
|
|
48
|
-
* Get the access token for the specified organization from the storage with refresh strategy.
|
|
49
|
-
*
|
|
50
|
-
* Scope {@link UserScope.Organizations} is required in the config to use organization-related
|
|
51
|
-
* methods.
|
|
52
|
-
*
|
|
53
|
-
* @param organizationId The ID of the organization that the access token is granted for.
|
|
54
|
-
* @returns The access token string.
|
|
55
|
-
* @throws LogtoClientError if the user is not authenticated.
|
|
56
|
-
* @remarks
|
|
57
|
-
* It uses the same refresh strategy as {@link getAccessToken}.
|
|
58
|
-
*/
|
|
59
|
-
this.getOrganizationToken = memoize.memoize(this.#getOrganizationToken);
|
|
60
|
-
/**
|
|
61
|
-
* Handle the sign-in callback by parsing the authorization code from the
|
|
62
|
-
* callback URI and exchanging it for the tokens.
|
|
63
|
-
*
|
|
64
|
-
* @param callbackUri The callback URI, including the search params, that the user is redirected to after the sign-in flow is completed.
|
|
65
|
-
* The origin and pathname of this URI must match the origin and pathname of the redirect URI specified in {@link signIn}.
|
|
66
|
-
* In many cases you'll probably end up passing `window.location.href` as the argument to this function.
|
|
67
|
-
* @throws LogtoClientError if the sign-in session is not found.
|
|
68
|
-
*/
|
|
69
|
-
this.handleSignInCallback = memoize.memoize(this.#handleSignInCallback);
|
|
70
|
-
this.getJwtVerifyGetKey = once.once(this.#getJwtVerifyGetKey);
|
|
71
|
-
this.accessTokenMap = new Map();
|
|
72
|
-
this.logtoConfig = index.normalizeLogtoConfig(logtoConfig);
|
|
73
|
-
this.adapter = new index$1.ClientAdapterInstance(adapter);
|
|
74
|
-
void this.loadAccessTokenMap();
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Check if the user is authenticated by checking if the ID token exists.
|
|
78
|
-
*/
|
|
79
|
-
async isAuthenticated() {
|
|
80
|
-
return Boolean(await this.getIdToken());
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* Get the Refresh Token from the storage.
|
|
84
|
-
*/
|
|
85
|
-
async getRefreshToken() {
|
|
86
|
-
return this.adapter.storage.getItem('refreshToken');
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* Get the ID Token from the storage. If you want to get the ID Token claims,
|
|
90
|
-
* use {@link getIdTokenClaims} instead.
|
|
91
|
-
*/
|
|
92
|
-
async getIdToken() {
|
|
93
|
-
return this.adapter.storage.getItem('idToken');
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* Get the ID Token claims.
|
|
97
|
-
*/
|
|
98
|
-
async getIdTokenClaims() {
|
|
99
|
-
const idToken = await this.getIdToken();
|
|
100
|
-
if (!idToken) {
|
|
101
|
-
throw new errors.LogtoClientError('not_authenticated');
|
|
102
|
-
}
|
|
103
|
-
return js.decodeIdToken(idToken);
|
|
104
|
-
}
|
|
105
|
-
/**
|
|
106
|
-
* Get the access token claims for the specified resource.
|
|
107
|
-
*
|
|
108
|
-
* @param resource The resource that the access token is granted for. If not
|
|
109
|
-
* specified, the access token will be used for OpenID Connect or the default
|
|
110
|
-
* resource, as specified in the Logto Console.
|
|
111
|
-
*/
|
|
112
|
-
async getAccessTokenClaims(resource) {
|
|
113
|
-
const accessToken = await this.getAccessToken(resource);
|
|
114
|
-
return js.decodeAccessToken(accessToken);
|
|
115
|
-
}
|
|
116
|
-
/**
|
|
117
|
-
* Get the organization token claims for the specified organization.
|
|
118
|
-
*
|
|
119
|
-
* @param organizationId The ID of the organization that the access token is granted for.
|
|
120
|
-
*/
|
|
121
|
-
async getOrganizationTokenClaims(organizationId) {
|
|
122
|
-
const accessToken = await this.getOrganizationToken(organizationId);
|
|
123
|
-
return js.decodeAccessToken(accessToken);
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* Get the user information from the Userinfo Endpoint.
|
|
127
|
-
*
|
|
128
|
-
* Note the Userinfo Endpoint will return more claims than the ID Token. See
|
|
129
|
-
* {@link https://docs.logto.io/docs/recipes/integrate-logto/vanilla-js/#fetch-user-information | Fetch user information}
|
|
130
|
-
* for more information.
|
|
131
|
-
*
|
|
132
|
-
* @returns The user information.
|
|
133
|
-
* @throws LogtoClientError if the user is not authenticated.
|
|
134
|
-
*/
|
|
135
|
-
async fetchUserInfo() {
|
|
136
|
-
const { userinfoEndpoint } = await this.getOidcConfig();
|
|
137
|
-
const accessToken = await this.getAccessToken();
|
|
138
|
-
if (!accessToken) {
|
|
139
|
-
throw new errors.LogtoClientError('fetch_user_info_failed');
|
|
140
|
-
}
|
|
141
|
-
return js.fetchUserInfo(userinfoEndpoint, accessToken, this.adapter.requester);
|
|
142
|
-
}
|
|
143
|
-
/**
|
|
144
|
-
* Start the sign-in flow with the specified redirect URI. The URI must be
|
|
145
|
-
* registered in the Logto Console.
|
|
146
|
-
*
|
|
147
|
-
* The user will be redirected to that URI after the sign-in flow is completed,
|
|
148
|
-
* and the client will be able to get the authorization code from the URI.
|
|
149
|
-
* To fetch the tokens from the authorization code, use {@link handleSignInCallback}
|
|
150
|
-
* after the user is redirected in the callback URI.
|
|
151
|
-
*
|
|
152
|
-
* @param redirectUri The redirect URI that the user will be redirected to after the sign-in flow is completed.
|
|
153
|
-
* @param interactionMode The interaction mode to be used for the authorization request. Note it's not
|
|
154
|
-
* a part of the OIDC standard, but a Logto-specific extension. Defaults to `signIn`.
|
|
155
|
-
*
|
|
156
|
-
* @see {@link https://docs.logto.io/docs/recipes/integrate-logto/vanilla-js/#sign-in | Sign in} for more information.
|
|
157
|
-
* @see {@link InteractionMode}
|
|
158
|
-
*/
|
|
159
|
-
async signIn(redirectUri, interactionMode) {
|
|
160
|
-
const { appId: clientId, prompt, resources, scopes } = this.logtoConfig;
|
|
161
|
-
const { authorizationEndpoint } = await this.getOidcConfig();
|
|
162
|
-
const codeVerifier = this.adapter.generateCodeVerifier();
|
|
163
|
-
const codeChallenge = await this.adapter.generateCodeChallenge(codeVerifier);
|
|
164
|
-
const state = this.adapter.generateState();
|
|
165
|
-
const signInUri = js.generateSignInUri({
|
|
166
|
-
authorizationEndpoint,
|
|
167
|
-
clientId,
|
|
168
|
-
redirectUri,
|
|
169
|
-
codeChallenge,
|
|
170
|
-
state,
|
|
171
|
-
scopes,
|
|
172
|
-
resources,
|
|
173
|
-
prompt,
|
|
174
|
-
interactionMode,
|
|
175
|
-
});
|
|
176
|
-
await Promise.all([
|
|
177
|
-
this.setSignInSession({ redirectUri, codeVerifier, state }),
|
|
178
|
-
this.setRefreshToken(null),
|
|
179
|
-
this.setIdToken(null),
|
|
180
|
-
]);
|
|
181
|
-
await this.adapter.navigate(signInUri);
|
|
182
|
-
}
|
|
183
|
-
/**
|
|
184
|
-
* Check if the user is redirected from the sign-in page by checking if the
|
|
185
|
-
* current URL matches the redirect URI in the sign-in session.
|
|
186
|
-
*
|
|
187
|
-
* If there's no sign-in session, it will return `false`.
|
|
188
|
-
*
|
|
189
|
-
* @param url The current URL.
|
|
190
|
-
*/
|
|
191
|
-
async isSignInRedirected(url) {
|
|
192
|
-
const signInSession = await this.getSignInSession();
|
|
193
|
-
if (!signInSession) {
|
|
194
|
-
return false;
|
|
195
|
-
}
|
|
196
|
-
const { redirectUri } = signInSession;
|
|
197
|
-
const { origin, pathname } = new URL(url);
|
|
198
|
-
return `${origin}${pathname}` === redirectUri;
|
|
199
|
-
}
|
|
200
|
-
/**
|
|
201
|
-
* Start the sign-out flow with the specified redirect URI. The URI must be
|
|
202
|
-
* registered in the Logto Console.
|
|
203
|
-
*
|
|
204
|
-
* It will also revoke all the tokens and clean up the storage.
|
|
205
|
-
*
|
|
206
|
-
* The user will be redirected that URI after the sign-out flow is completed.
|
|
207
|
-
* If the `postLogoutRedirectUri` is not specified, the user will be redirected
|
|
208
|
-
* to a default page.
|
|
209
|
-
*/
|
|
210
|
-
async signOut(postLogoutRedirectUri) {
|
|
211
|
-
const { appId: clientId } = this.logtoConfig;
|
|
212
|
-
const { endSessionEndpoint, revocationEndpoint } = await this.getOidcConfig();
|
|
213
|
-
const refreshToken = await this.getRefreshToken();
|
|
214
|
-
if (refreshToken) {
|
|
215
|
-
try {
|
|
216
|
-
await js.revoke(revocationEndpoint, clientId, refreshToken, this.adapter.requester);
|
|
217
|
-
}
|
|
218
|
-
catch {
|
|
219
|
-
// Do nothing at this point, as we don't want to break the sign-out flow even if the revocation is failed
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
const url = js.generateSignOutUri({
|
|
223
|
-
endSessionEndpoint,
|
|
224
|
-
postLogoutRedirectUri,
|
|
225
|
-
clientId,
|
|
226
|
-
});
|
|
227
|
-
this.accessTokenMap.clear();
|
|
228
|
-
await Promise.all([
|
|
229
|
-
this.setRefreshToken(null),
|
|
230
|
-
this.setIdToken(null),
|
|
231
|
-
this.adapter.storage.removeItem('accessToken'),
|
|
232
|
-
]);
|
|
233
|
-
await this.adapter.navigate(url);
|
|
234
|
-
}
|
|
235
|
-
async getSignInSession() {
|
|
236
|
-
const jsonItem = await this.adapter.storage.getItem('signInSession');
|
|
237
|
-
if (!jsonItem) {
|
|
238
|
-
return null;
|
|
239
|
-
}
|
|
240
|
-
const item = JSON.parse(jsonItem);
|
|
241
|
-
if (!index.isLogtoSignInSessionItem(item)) {
|
|
242
|
-
throw new errors.LogtoClientError('sign_in_session.invalid');
|
|
243
|
-
}
|
|
244
|
-
return item;
|
|
245
|
-
}
|
|
246
|
-
async setSignInSession(value) {
|
|
247
|
-
return this.adapter.setStorageItem(types.PersistKey.SignInSession, value && JSON.stringify(value));
|
|
248
|
-
}
|
|
249
|
-
async setIdToken(value) {
|
|
250
|
-
return this.adapter.setStorageItem(types.PersistKey.IdToken, value);
|
|
251
|
-
}
|
|
252
|
-
async setRefreshToken(value) {
|
|
253
|
-
return this.adapter.setStorageItem(types.PersistKey.RefreshToken, value);
|
|
254
|
-
}
|
|
255
|
-
async getAccessTokenByRefreshToken(resource, organizationId) {
|
|
256
|
-
const currentRefreshToken = await this.getRefreshToken();
|
|
257
|
-
if (!currentRefreshToken) {
|
|
258
|
-
throw new errors.LogtoClientError('not_authenticated');
|
|
259
|
-
}
|
|
260
|
-
const accessTokenKey = index$2.buildAccessTokenKey(resource, organizationId);
|
|
261
|
-
const { appId: clientId } = this.logtoConfig;
|
|
262
|
-
const { tokenEndpoint } = await this.getOidcConfig();
|
|
263
|
-
const requestedAt = Math.round(Date.now() / 1000);
|
|
264
|
-
const { accessToken, refreshToken, idToken, scope, expiresIn } = await js.fetchTokenByRefreshToken({
|
|
265
|
-
clientId,
|
|
266
|
-
tokenEndpoint,
|
|
267
|
-
refreshToken: currentRefreshToken,
|
|
268
|
-
resource,
|
|
269
|
-
organizationId,
|
|
270
|
-
}, this.adapter.requester);
|
|
271
|
-
this.accessTokenMap.set(accessTokenKey, {
|
|
272
|
-
token: accessToken,
|
|
273
|
-
scope,
|
|
274
|
-
/** The `expiresAt` variable provides an approximate estimation of the actual `exp` property
|
|
275
|
-
* in the token claims. It is utilized by the client to determine if the cached access token
|
|
276
|
-
* has expired and when a new access token should be requested.
|
|
277
|
-
*/
|
|
278
|
-
expiresAt: requestedAt + expiresIn,
|
|
279
|
-
});
|
|
280
|
-
await this.saveAccessTokenMap();
|
|
281
|
-
if (refreshToken) {
|
|
282
|
-
await this.setRefreshToken(refreshToken);
|
|
283
|
-
}
|
|
284
|
-
if (idToken) {
|
|
285
|
-
await this.verifyIdToken(idToken);
|
|
286
|
-
await this.setIdToken(idToken);
|
|
287
|
-
}
|
|
288
|
-
return accessToken;
|
|
289
|
-
}
|
|
290
|
-
async verifyIdToken(idToken) {
|
|
291
|
-
const { appId } = this.logtoConfig;
|
|
292
|
-
const { issuer } = await this.getOidcConfig();
|
|
293
|
-
const jwtVerifyGetKey = await this.getJwtVerifyGetKey();
|
|
294
|
-
await js.verifyIdToken(idToken, appId, issuer, jwtVerifyGetKey);
|
|
295
|
-
}
|
|
296
|
-
async saveAccessTokenMap() {
|
|
297
|
-
const data = {};
|
|
298
|
-
for (const [key, accessToken] of this.accessTokenMap.entries()) {
|
|
299
|
-
// eslint-disable-next-line @silverhand/fp/no-mutation
|
|
300
|
-
data[key] = accessToken;
|
|
301
|
-
}
|
|
302
|
-
await this.adapter.storage.setItem('accessToken', JSON.stringify(data));
|
|
303
|
-
}
|
|
304
|
-
async loadAccessTokenMap() {
|
|
305
|
-
const raw = await this.adapter.storage.getItem('accessToken');
|
|
306
|
-
if (!raw) {
|
|
307
|
-
return;
|
|
308
|
-
}
|
|
309
|
-
try {
|
|
310
|
-
const json = JSON.parse(raw);
|
|
311
|
-
if (!index.isLogtoAccessTokenMap(json)) {
|
|
312
|
-
return;
|
|
313
|
-
}
|
|
314
|
-
this.accessTokenMap.clear();
|
|
315
|
-
for (const [key, accessToken] of Object.entries(json)) {
|
|
316
|
-
this.accessTokenMap.set(key, accessToken);
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
catch (error) {
|
|
320
|
-
console.warn(error);
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
async #getOidcConfig() {
|
|
324
|
-
return this.adapter.getWithCache(types.CacheKey.OpenidConfig, async () => {
|
|
325
|
-
return js.fetchOidcConfig(index$2.getDiscoveryEndpoint(this.logtoConfig.endpoint), this.adapter.requester);
|
|
326
|
-
});
|
|
327
|
-
}
|
|
328
|
-
async #getJwtVerifyGetKey() {
|
|
329
|
-
const { jwksUri } = await this.getOidcConfig();
|
|
330
|
-
if (!this.adapter.unstable_cache) {
|
|
331
|
-
return jose.createRemoteJWKSet(new URL(jwksUri));
|
|
332
|
-
}
|
|
333
|
-
const cachedJwkSet = new remoteJwkSet.CachedRemoteJwkSet(new URL(jwksUri), this.adapter);
|
|
334
|
-
return async (...args) => cachedJwkSet.getKey(...args);
|
|
335
|
-
}
|
|
336
|
-
async #getAccessToken(resource, organizationId) {
|
|
337
|
-
if (!(await this.isAuthenticated())) {
|
|
338
|
-
throw new errors.LogtoClientError('not_authenticated');
|
|
339
|
-
}
|
|
340
|
-
const accessTokenKey = index$2.buildAccessTokenKey(resource, organizationId);
|
|
341
|
-
const accessToken = this.accessTokenMap.get(accessTokenKey);
|
|
342
|
-
if (accessToken && accessToken.expiresAt > Date.now() / 1000) {
|
|
343
|
-
return accessToken.token;
|
|
344
|
-
}
|
|
345
|
-
// Since the access token has expired, delete it from the map.
|
|
346
|
-
if (accessToken) {
|
|
347
|
-
this.accessTokenMap.delete(accessTokenKey);
|
|
348
|
-
}
|
|
349
|
-
/**
|
|
350
|
-
* Need to fetch a new access token using refresh token.
|
|
351
|
-
*/
|
|
352
|
-
return this.getAccessTokenByRefreshToken(resource, organizationId);
|
|
353
|
-
}
|
|
354
|
-
async #getOrganizationToken(organizationId) {
|
|
355
|
-
if (!this.logtoConfig.scopes?.includes(js.UserScope.Organizations)) {
|
|
356
|
-
throw new errors.LogtoClientError('missing_scope_organizations');
|
|
357
|
-
}
|
|
358
|
-
return this.getAccessToken(undefined, organizationId);
|
|
359
|
-
}
|
|
360
|
-
async #handleSignInCallback(callbackUri) {
|
|
361
|
-
const { requester } = this.adapter;
|
|
362
|
-
const signInSession = await this.getSignInSession();
|
|
363
|
-
if (!signInSession) {
|
|
364
|
-
throw new errors.LogtoClientError('sign_in_session.not_found');
|
|
365
|
-
}
|
|
366
|
-
const { redirectUri, state, codeVerifier } = signInSession;
|
|
367
|
-
const code = js.verifyAndParseCodeFromCallbackUri(callbackUri, redirectUri, state);
|
|
368
|
-
// NOTE: Will add scope to accessTokenKey when needed. (Linear issue LOG-1589)
|
|
369
|
-
const accessTokenKey = index$2.buildAccessTokenKey();
|
|
370
|
-
const { appId: clientId } = this.logtoConfig;
|
|
371
|
-
const { tokenEndpoint } = await this.getOidcConfig();
|
|
372
|
-
const requestedAt = Math.round(Date.now() / 1000);
|
|
373
|
-
const { idToken, refreshToken, accessToken, scope, expiresIn } = await js.fetchTokenByAuthorizationCode({
|
|
374
|
-
clientId,
|
|
375
|
-
tokenEndpoint,
|
|
376
|
-
redirectUri,
|
|
377
|
-
codeVerifier,
|
|
378
|
-
code,
|
|
379
|
-
}, requester);
|
|
380
|
-
await this.verifyIdToken(idToken);
|
|
381
|
-
await this.setRefreshToken(refreshToken ?? null);
|
|
382
|
-
await this.setIdToken(idToken);
|
|
383
|
-
this.accessTokenMap.set(accessTokenKey, {
|
|
384
|
-
token: accessToken,
|
|
385
|
-
scope,
|
|
386
|
-
/** The `expiresAt` variable provides an approximate estimation of the actual `exp` property
|
|
387
|
-
* in the token claims. It is utilized by the client to determine if the cached access token
|
|
388
|
-
* has expired and when a new access token should be requested.
|
|
389
|
-
*/
|
|
390
|
-
expiresAt: requestedAt + expiresIn,
|
|
391
|
-
});
|
|
392
|
-
await this.saveAccessTokenMap();
|
|
393
|
-
await this.setSignInSession(null);
|
|
21
|
+
class LogtoClient extends client.StandardLogtoClient {
|
|
22
|
+
constructor(logtoConfig, adapter, buildJwtVerifier) {
|
|
23
|
+
super(logtoConfig, adapter, buildJwtVerifier ?? ((client) => new defaults.DefaultJwtVerifier(client)));
|
|
394
24
|
}
|
|
395
25
|
}
|
|
396
|
-
/* eslint-enable max-lines */
|
|
397
26
|
|
|
27
|
+
exports.StandardLogtoClient = client.StandardLogtoClient;
|
|
398
28
|
Object.defineProperty(exports, "LogtoError", {
|
|
399
29
|
enumerable: true,
|
|
400
30
|
get: function () { return js.LogtoError; }
|
|
@@ -436,9 +66,6 @@ Object.defineProperty(exports, "organizationUrnPrefix", {
|
|
|
436
66
|
get: function () { return js.organizationUrnPrefix; }
|
|
437
67
|
});
|
|
438
68
|
exports.LogtoClientError = errors.LogtoClientError;
|
|
439
|
-
exports.isLogtoAccessTokenMap = index.isLogtoAccessTokenMap;
|
|
440
|
-
exports.isLogtoSignInSessionItem = index.isLogtoSignInSessionItem;
|
|
441
|
-
exports.normalizeLogtoConfig = index.normalizeLogtoConfig;
|
|
442
69
|
Object.defineProperty(exports, "CacheKey", {
|
|
443
70
|
enumerable: true,
|
|
444
71
|
get: function () { return types.CacheKey; }
|
|
@@ -448,4 +75,7 @@ Object.defineProperty(exports, "PersistKey", {
|
|
|
448
75
|
get: function () { return types.PersistKey; }
|
|
449
76
|
});
|
|
450
77
|
exports.createRequester = requester.createRequester;
|
|
78
|
+
exports.isLogtoAccessTokenMap = index.isLogtoAccessTokenMap;
|
|
79
|
+
exports.isLogtoSignInSessionItem = index.isLogtoSignInSessionItem;
|
|
80
|
+
exports.normalizeLogtoConfig = index.normalizeLogtoConfig;
|
|
451
81
|
exports.default = LogtoClient;
|
package/lib/index.d.ts
CHANGED
|
@@ -1,15 +1,7 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
import type { AccessToken, LogtoConfig, LogtoSignInSessionItem } from './types/index.js';
|
|
6
|
-
export type { IdTokenClaims, LogtoErrorCode, UserInfoResponse, InteractionMode } from '@logto/js';
|
|
7
|
-
export { LogtoError, LogtoRequestError, OidcError, Prompt, ReservedScope, ReservedResource, UserScope, organizationUrnPrefix, buildOrganizationUrn, getOrganizationIdFromUrn, } from '@logto/js';
|
|
8
|
-
export * from './errors.js';
|
|
9
|
-
export type { Storage, StorageKey, ClientAdapter } from './adapter/index.js';
|
|
10
|
-
export { PersistKey, CacheKey } from './adapter/index.js';
|
|
11
|
-
export { createRequester } from './utils/index.js';
|
|
12
|
-
export * from './types/index.js';
|
|
1
|
+
import { type ClientAdapter, type JwtVerifier } from './adapter/index.js';
|
|
2
|
+
import { StandardLogtoClient } from './client.js';
|
|
3
|
+
import type { LogtoConfig } from './types/index.js';
|
|
4
|
+
export * from './shim.js';
|
|
13
5
|
/**
|
|
14
6
|
* The Logto base client class that provides the essential methods for
|
|
15
7
|
* interacting with the Logto server.
|
|
@@ -17,141 +9,6 @@ export * from './types/index.js';
|
|
|
17
9
|
* It also provides an adapter object that allows the customizations of the
|
|
18
10
|
* client behavior for different environments.
|
|
19
11
|
*/
|
|
20
|
-
export default class LogtoClient {
|
|
21
|
-
|
|
22
|
-
readonly logtoConfig: LogtoConfig;
|
|
23
|
-
/**
|
|
24
|
-
* Get the OIDC configuration from the discovery endpoint. This method will
|
|
25
|
-
* only fetch the configuration once and cache the result.
|
|
26
|
-
*/
|
|
27
|
-
readonly getOidcConfig: () => Promise<OidcConfigResponse>;
|
|
28
|
-
/**
|
|
29
|
-
* Get the access token from the storage with refresh strategy.
|
|
30
|
-
*
|
|
31
|
-
* - If the access token has expired, it will try to fetch a new one using the Refresh Token.
|
|
32
|
-
* - If there's an ongoing Promise to fetch the access token, it will return the Promise.
|
|
33
|
-
*
|
|
34
|
-
* If you want to get the access token claims, use {@link getAccessTokenClaims} instead.
|
|
35
|
-
*
|
|
36
|
-
* @param resource The resource that the access token is granted for. If not
|
|
37
|
-
* specified, the access token will be used for OpenID Connect or the default
|
|
38
|
-
* resource, as specified in the Logto Console.
|
|
39
|
-
* @returns The access token string.
|
|
40
|
-
* @throws LogtoClientError if the user is not authenticated.
|
|
41
|
-
*/
|
|
42
|
-
readonly getAccessToken: (this: unknown, resource?: string | undefined, organizationId?: string | undefined) => Promise<string>;
|
|
43
|
-
/**
|
|
44
|
-
* Get the access token for the specified organization from the storage with refresh strategy.
|
|
45
|
-
*
|
|
46
|
-
* Scope {@link UserScope.Organizations} is required in the config to use organization-related
|
|
47
|
-
* methods.
|
|
48
|
-
*
|
|
49
|
-
* @param organizationId The ID of the organization that the access token is granted for.
|
|
50
|
-
* @returns The access token string.
|
|
51
|
-
* @throws LogtoClientError if the user is not authenticated.
|
|
52
|
-
* @remarks
|
|
53
|
-
* It uses the same refresh strategy as {@link getAccessToken}.
|
|
54
|
-
*/
|
|
55
|
-
readonly getOrganizationToken: (this: unknown, organizationId: string) => Promise<string>;
|
|
56
|
-
/**
|
|
57
|
-
* Handle the sign-in callback by parsing the authorization code from the
|
|
58
|
-
* callback URI and exchanging it for the tokens.
|
|
59
|
-
*
|
|
60
|
-
* @param callbackUri The callback URI, including the search params, that the user is redirected to after the sign-in flow is completed.
|
|
61
|
-
* The origin and pathname of this URI must match the origin and pathname of the redirect URI specified in {@link signIn}.
|
|
62
|
-
* In many cases you'll probably end up passing `window.location.href` as the argument to this function.
|
|
63
|
-
* @throws LogtoClientError if the sign-in session is not found.
|
|
64
|
-
*/
|
|
65
|
-
readonly handleSignInCallback: (this: unknown, callbackUri: string) => Promise<void>;
|
|
66
|
-
protected readonly getJwtVerifyGetKey: (...args: unknown[]) => Promise<JWTVerifyGetKey>;
|
|
67
|
-
protected readonly adapter: ClientAdapterInstance;
|
|
68
|
-
protected readonly accessTokenMap: Map<string, AccessToken>;
|
|
69
|
-
constructor(logtoConfig: LogtoConfig, adapter: ClientAdapter);
|
|
70
|
-
/**
|
|
71
|
-
* Check if the user is authenticated by checking if the ID token exists.
|
|
72
|
-
*/
|
|
73
|
-
isAuthenticated(): Promise<boolean>;
|
|
74
|
-
/**
|
|
75
|
-
* Get the Refresh Token from the storage.
|
|
76
|
-
*/
|
|
77
|
-
getRefreshToken(): Promise<Nullable<string>>;
|
|
78
|
-
/**
|
|
79
|
-
* Get the ID Token from the storage. If you want to get the ID Token claims,
|
|
80
|
-
* use {@link getIdTokenClaims} instead.
|
|
81
|
-
*/
|
|
82
|
-
getIdToken(): Promise<Nullable<string>>;
|
|
83
|
-
/**
|
|
84
|
-
* Get the ID Token claims.
|
|
85
|
-
*/
|
|
86
|
-
getIdTokenClaims(): Promise<IdTokenClaims>;
|
|
87
|
-
/**
|
|
88
|
-
* Get the access token claims for the specified resource.
|
|
89
|
-
*
|
|
90
|
-
* @param resource The resource that the access token is granted for. If not
|
|
91
|
-
* specified, the access token will be used for OpenID Connect or the default
|
|
92
|
-
* resource, as specified in the Logto Console.
|
|
93
|
-
*/
|
|
94
|
-
getAccessTokenClaims(resource?: string): Promise<AccessTokenClaims>;
|
|
95
|
-
/**
|
|
96
|
-
* Get the organization token claims for the specified organization.
|
|
97
|
-
*
|
|
98
|
-
* @param organizationId The ID of the organization that the access token is granted for.
|
|
99
|
-
*/
|
|
100
|
-
getOrganizationTokenClaims(organizationId: string): Promise<AccessTokenClaims>;
|
|
101
|
-
/**
|
|
102
|
-
* Get the user information from the Userinfo Endpoint.
|
|
103
|
-
*
|
|
104
|
-
* Note the Userinfo Endpoint will return more claims than the ID Token. See
|
|
105
|
-
* {@link https://docs.logto.io/docs/recipes/integrate-logto/vanilla-js/#fetch-user-information | Fetch user information}
|
|
106
|
-
* for more information.
|
|
107
|
-
*
|
|
108
|
-
* @returns The user information.
|
|
109
|
-
* @throws LogtoClientError if the user is not authenticated.
|
|
110
|
-
*/
|
|
111
|
-
fetchUserInfo(): Promise<UserInfoResponse>;
|
|
112
|
-
/**
|
|
113
|
-
* Start the sign-in flow with the specified redirect URI. The URI must be
|
|
114
|
-
* registered in the Logto Console.
|
|
115
|
-
*
|
|
116
|
-
* The user will be redirected to that URI after the sign-in flow is completed,
|
|
117
|
-
* and the client will be able to get the authorization code from the URI.
|
|
118
|
-
* To fetch the tokens from the authorization code, use {@link handleSignInCallback}
|
|
119
|
-
* after the user is redirected in the callback URI.
|
|
120
|
-
*
|
|
121
|
-
* @param redirectUri The redirect URI that the user will be redirected to after the sign-in flow is completed.
|
|
122
|
-
* @param interactionMode The interaction mode to be used for the authorization request. Note it's not
|
|
123
|
-
* a part of the OIDC standard, but a Logto-specific extension. Defaults to `signIn`.
|
|
124
|
-
*
|
|
125
|
-
* @see {@link https://docs.logto.io/docs/recipes/integrate-logto/vanilla-js/#sign-in | Sign in} for more information.
|
|
126
|
-
* @see {@link InteractionMode}
|
|
127
|
-
*/
|
|
128
|
-
signIn(redirectUri: string, interactionMode?: InteractionMode): Promise<void>;
|
|
129
|
-
/**
|
|
130
|
-
* Check if the user is redirected from the sign-in page by checking if the
|
|
131
|
-
* current URL matches the redirect URI in the sign-in session.
|
|
132
|
-
*
|
|
133
|
-
* If there's no sign-in session, it will return `false`.
|
|
134
|
-
*
|
|
135
|
-
* @param url The current URL.
|
|
136
|
-
*/
|
|
137
|
-
isSignInRedirected(url: string): Promise<boolean>;
|
|
138
|
-
/**
|
|
139
|
-
* Start the sign-out flow with the specified redirect URI. The URI must be
|
|
140
|
-
* registered in the Logto Console.
|
|
141
|
-
*
|
|
142
|
-
* It will also revoke all the tokens and clean up the storage.
|
|
143
|
-
*
|
|
144
|
-
* The user will be redirected that URI after the sign-out flow is completed.
|
|
145
|
-
* If the `postLogoutRedirectUri` is not specified, the user will be redirected
|
|
146
|
-
* to a default page.
|
|
147
|
-
*/
|
|
148
|
-
signOut(postLogoutRedirectUri?: string): Promise<void>;
|
|
149
|
-
protected getSignInSession(): Promise<Nullable<LogtoSignInSessionItem>>;
|
|
150
|
-
protected setSignInSession(value: Nullable<LogtoSignInSessionItem>): Promise<void>;
|
|
151
|
-
private setIdToken;
|
|
152
|
-
private setRefreshToken;
|
|
153
|
-
private getAccessTokenByRefreshToken;
|
|
154
|
-
private verifyIdToken;
|
|
155
|
-
private saveAccessTokenMap;
|
|
156
|
-
private loadAccessTokenMap;
|
|
12
|
+
export default class LogtoClient extends StandardLogtoClient {
|
|
13
|
+
constructor(logtoConfig: LogtoConfig, adapter: ClientAdapter, buildJwtVerifier?: (client: StandardLogtoClient) => JwtVerifier);
|
|
157
14
|
}
|