@logto/browser 0.1.4 → 0.1.10
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.d.ts +3 -3
- package/lib/index.js +16 -11
- package/package.json +6 -6
package/lib/index.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { IdTokenClaims, Requester, UserInfoResponse } from '@logto/js';
|
|
2
2
|
import { Nullable } from '@silverhand/essentials';
|
|
3
3
|
import { Infer } from 'superstruct';
|
|
4
|
-
export type { IdTokenClaims, UserInfoResponse } from '@logto/js';
|
|
4
|
+
export type { IdTokenClaims, UserInfoResponse, LogtoErrorCode } from '@logto/js';
|
|
5
|
+
export { LogtoError, OidcError } from '@logto/js';
|
|
5
6
|
export * from './errors';
|
|
6
7
|
export declare type LogtoConfig = {
|
|
7
8
|
endpoint: string;
|
|
8
|
-
|
|
9
|
+
appId: string;
|
|
9
10
|
scopes?: string[];
|
|
10
11
|
resources?: string[];
|
|
11
12
|
usingPersistStorage?: boolean;
|
|
@@ -41,7 +42,6 @@ export default class LogtoClient {
|
|
|
41
42
|
protected readonly requester: Requester;
|
|
42
43
|
protected readonly accessTokenMap: Map<string, AccessToken>;
|
|
43
44
|
private readonly getAccessTokenPromiseMap;
|
|
44
|
-
private _refreshToken;
|
|
45
45
|
private _idToken;
|
|
46
46
|
constructor(logtoConfig: LogtoConfig, requester?: <T>(input: RequestInfo, init?: RequestInit | undefined) => Promise<T>);
|
|
47
47
|
get isAuthenticated(): boolean;
|
package/lib/index.js
CHANGED
|
@@ -13,13 +13,16 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
13
13
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
14
14
|
};
|
|
15
15
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
-
exports.LogtoSignInSessionItemSchema = void 0;
|
|
16
|
+
exports.LogtoSignInSessionItemSchema = exports.OidcError = exports.LogtoError = void 0;
|
|
17
17
|
const js_1 = require("@logto/js");
|
|
18
18
|
const jose_1 = require("jose");
|
|
19
19
|
const lodash_once_1 = __importDefault(require("lodash.once"));
|
|
20
20
|
const superstruct_1 = require("superstruct");
|
|
21
21
|
const errors_1 = require("./errors");
|
|
22
22
|
const utils_1 = require("./utils");
|
|
23
|
+
var js_2 = require("@logto/js");
|
|
24
|
+
Object.defineProperty(exports, "LogtoError", { enumerable: true, get: function () { return js_2.LogtoError; } });
|
|
25
|
+
Object.defineProperty(exports, "OidcError", { enumerable: true, get: function () { return js_2.OidcError; } });
|
|
23
26
|
__exportStar(require("./errors"), exports);
|
|
24
27
|
exports.LogtoSignInSessionItemSchema = (0, superstruct_1.type)({
|
|
25
28
|
redirectUri: (0, superstruct_1.string)(),
|
|
@@ -33,9 +36,8 @@ class LogtoClient {
|
|
|
33
36
|
this.accessTokenMap = new Map();
|
|
34
37
|
this.getAccessTokenPromiseMap = new Map();
|
|
35
38
|
this.logtoConfig = logtoConfig;
|
|
36
|
-
this.logtoStorageKey = (0, utils_1.buildLogtoKey)(logtoConfig.
|
|
39
|
+
this.logtoStorageKey = (0, utils_1.buildLogtoKey)(logtoConfig.appId);
|
|
37
40
|
this.requester = requester;
|
|
38
|
-
this._refreshToken = localStorage.getItem((0, utils_1.buildRefreshTokenKey)(this.logtoStorageKey));
|
|
39
41
|
this._idToken = localStorage.getItem((0, utils_1.buildIdTokenKey)(this.logtoStorageKey));
|
|
40
42
|
}
|
|
41
43
|
get isAuthenticated() {
|
|
@@ -64,10 +66,9 @@ class LogtoClient {
|
|
|
64
66
|
sessionStorage.setItem(this.logtoStorageKey, jsonItem);
|
|
65
67
|
}
|
|
66
68
|
get refreshToken() {
|
|
67
|
-
return this.
|
|
69
|
+
return localStorage.getItem((0, utils_1.buildRefreshTokenKey)(this.logtoStorageKey));
|
|
68
70
|
}
|
|
69
71
|
set refreshToken(refreshToken) {
|
|
70
|
-
this._refreshToken = refreshToken;
|
|
71
72
|
const refreshTokenKey = (0, utils_1.buildRefreshTokenKey)(this.logtoStorageKey);
|
|
72
73
|
if (!refreshToken) {
|
|
73
74
|
localStorage.removeItem(refreshTokenKey);
|
|
@@ -87,6 +88,7 @@ class LogtoClient {
|
|
|
87
88
|
}
|
|
88
89
|
localStorage.setItem(idTokenKey, idToken);
|
|
89
90
|
}
|
|
91
|
+
// eslint-disable-next-line complexity
|
|
90
92
|
async getAccessToken(resource) {
|
|
91
93
|
if (!this.idToken) {
|
|
92
94
|
throw new errors_1.LogtoClientError('not_authenticated');
|
|
@@ -134,7 +136,7 @@ class LogtoClient {
|
|
|
134
136
|
return (0, js_1.fetchUserInfo)(userinfoEndpoint, accessToken, this.requester);
|
|
135
137
|
}
|
|
136
138
|
async signIn(redirectUri) {
|
|
137
|
-
const { clientId, resources, scopes: customScopes } = this.logtoConfig;
|
|
139
|
+
const { appId: clientId, resources, scopes: customScopes } = this.logtoConfig;
|
|
138
140
|
const { authorizationEndpoint } = await this.getOidcConfig();
|
|
139
141
|
const codeVerifier = (0, js_1.generateCodeVerifier)();
|
|
140
142
|
const codeChallenge = await (0, js_1.generateCodeChallenge)(codeVerifier);
|
|
@@ -150,6 +152,8 @@ class LogtoClient {
|
|
|
150
152
|
resources,
|
|
151
153
|
});
|
|
152
154
|
this.signInSession = { redirectUri, codeVerifier, state };
|
|
155
|
+
this.refreshToken = null;
|
|
156
|
+
this.idToken = null;
|
|
153
157
|
window.location.assign(signInUri);
|
|
154
158
|
}
|
|
155
159
|
isSignInRedirected(url) {
|
|
@@ -168,7 +172,7 @@ class LogtoClient {
|
|
|
168
172
|
}
|
|
169
173
|
const { redirectUri, state, codeVerifier } = signInSession;
|
|
170
174
|
const code = (0, js_1.verifyAndParseCodeFromCallbackUri)(callbackUri, redirectUri, state);
|
|
171
|
-
const { clientId } = logtoConfig;
|
|
175
|
+
const { appId: clientId } = logtoConfig;
|
|
172
176
|
const { tokenEndpoint } = await this.getOidcConfig();
|
|
173
177
|
const codeTokenResponse = await (0, js_1.fetchTokenByAuthorizationCode)({
|
|
174
178
|
clientId,
|
|
@@ -179,12 +183,13 @@ class LogtoClient {
|
|
|
179
183
|
}, requester);
|
|
180
184
|
await this.verifyIdToken(codeTokenResponse.idToken);
|
|
181
185
|
this.saveCodeToken(codeTokenResponse);
|
|
186
|
+
this.signInSession = null;
|
|
182
187
|
}
|
|
183
188
|
async signOut(postLogoutRedirectUri) {
|
|
184
189
|
if (!this.idToken) {
|
|
185
190
|
throw new errors_1.LogtoClientError('not_authenticated');
|
|
186
191
|
}
|
|
187
|
-
const { clientId } = this.logtoConfig;
|
|
192
|
+
const { appId: clientId } = this.logtoConfig;
|
|
188
193
|
const { endSessionEndpoint, revocationEndpoint } = await this.getOidcConfig();
|
|
189
194
|
if (this.refreshToken) {
|
|
190
195
|
try {
|
|
@@ -210,7 +215,7 @@ class LogtoClient {
|
|
|
210
215
|
}
|
|
211
216
|
try {
|
|
212
217
|
const accessTokenKey = (0, utils_1.buildAccessTokenKey)(resource);
|
|
213
|
-
const { clientId } = this.logtoConfig;
|
|
218
|
+
const { appId: clientId } = this.logtoConfig;
|
|
214
219
|
const { tokenEndpoint } = await this.getOidcConfig();
|
|
215
220
|
const { accessToken, refreshToken, idToken, scope, expiresIn } = await (0, js_1.fetchTokenByRefreshToken)({ clientId, tokenEndpoint, refreshToken: this.refreshToken, resource }, this.requester);
|
|
216
221
|
this.accessTokenMap.set(accessTokenKey, {
|
|
@@ -239,11 +244,11 @@ class LogtoClient {
|
|
|
239
244
|
return (0, jose_1.createRemoteJWKSet)(new URL(jwksUri));
|
|
240
245
|
}
|
|
241
246
|
async verifyIdToken(idToken) {
|
|
242
|
-
const {
|
|
247
|
+
const { appId } = this.logtoConfig;
|
|
243
248
|
const { issuer } = await this.getOidcConfig();
|
|
244
249
|
const jwtVerifyGetKey = await this.getJwtVerifyGetKey();
|
|
245
250
|
try {
|
|
246
|
-
await (0, js_1.verifyIdToken)(idToken,
|
|
251
|
+
await (0, js_1.verifyIdToken)(idToken, appId, issuer, jwtVerifyGetKey);
|
|
247
252
|
}
|
|
248
253
|
catch (error) {
|
|
249
254
|
throw new errors_1.LogtoClientError('invalid_id_token', error);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@logto/browser",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.10",
|
|
4
4
|
"main": "./lib/index.js",
|
|
5
5
|
"exports": "./lib/index.js",
|
|
6
6
|
"typings": "./lib/index.d.ts",
|
|
@@ -21,10 +21,10 @@
|
|
|
21
21
|
"lint": "eslint --ext .ts src",
|
|
22
22
|
"test": "jest",
|
|
23
23
|
"test:coverage": "jest --silent --coverage",
|
|
24
|
-
"prepack": "pnpm test
|
|
24
|
+
"prepack": "pnpm test"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@logto/js": "^0.1.
|
|
27
|
+
"@logto/js": "^0.1.10",
|
|
28
28
|
"@silverhand/essentials": "^1.1.6",
|
|
29
29
|
"jose": "^4.5.0",
|
|
30
30
|
"lodash.get": "^4.4.2",
|
|
@@ -33,8 +33,8 @@
|
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@jest/types": "^27.5.1",
|
|
36
|
-
"@silverhand/eslint-config": "^0.
|
|
37
|
-
"@silverhand/ts-config": "^0.
|
|
36
|
+
"@silverhand/eslint-config": "^0.14.0",
|
|
37
|
+
"@silverhand/ts-config": "^0.14.0",
|
|
38
38
|
"@types/jest": "^27.4.0",
|
|
39
39
|
"@types/lodash.get": "^4.4.6",
|
|
40
40
|
"@types/lodash.once": "^4.1.6",
|
|
@@ -55,5 +55,5 @@
|
|
|
55
55
|
"publishConfig": {
|
|
56
56
|
"access": "public"
|
|
57
57
|
},
|
|
58
|
-
"gitHead": "
|
|
58
|
+
"gitHead": "ea6c7b435be8f74baf7fb76336bc0ca7cb7304c4"
|
|
59
59
|
}
|