@logto/node 2.2.1 → 2.3.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/edge/index.cjs +1 -1
- package/lib/src/client.cjs +29 -27
- package/lib/src/client.d.ts +1 -1
- package/lib/src/client.js +23 -21
- package/lib/src/index.cjs +12 -12
- package/lib/src/types.d.ts +2 -0
- package/package.json +6 -6
package/lib/edge/index.cjs
CHANGED
package/lib/src/client.cjs
CHANGED
|
@@ -12,7 +12,7 @@ var BaseClient__default = /*#__PURE__*/_interopDefault(BaseClient);
|
|
|
12
12
|
class LogtoNodeBaseClient extends BaseClient__default.default {
|
|
13
13
|
constructor() {
|
|
14
14
|
super(...arguments);
|
|
15
|
-
this.getContext = async ({ getAccessToken, resource, fetchUserInfo, } = {}) => {
|
|
15
|
+
this.getContext = async ({ getAccessToken, resource, fetchUserInfo, getOrganizationToken, } = {}) => {
|
|
16
16
|
const isAuthenticated = await this.isAuthenticated();
|
|
17
17
|
if (!isAuthenticated) {
|
|
18
18
|
return {
|
|
@@ -20,58 +20,60 @@ class LogtoNodeBaseClient extends BaseClient__default.default {
|
|
|
20
20
|
};
|
|
21
21
|
}
|
|
22
22
|
const claims = await this.getIdTokenClaims();
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
const accessToken = await this.getAccessToken(resource);
|
|
32
|
-
const accessTokenClaims = await this.getAccessTokenClaims(resource);
|
|
33
|
-
return {
|
|
34
|
-
isAuthenticated,
|
|
35
|
-
claims: await this.getIdTokenClaims(),
|
|
36
|
-
userInfo: essentials.conditional(fetchUserInfo && (await this.fetchUserInfo())),
|
|
37
|
-
accessToken,
|
|
38
|
-
scopes: accessTokenClaims.scope?.split(' '),
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
catch {
|
|
23
|
+
const { accessToken, accessTokenClaims } = getAccessToken
|
|
24
|
+
? {
|
|
25
|
+
accessToken: await essentials.trySafe(async () => this.getAccessToken(resource)),
|
|
26
|
+
accessTokenClaims: await essentials.trySafe(async () => this.getAccessTokenClaims(resource)),
|
|
27
|
+
}
|
|
28
|
+
: { accessToken: undefined, accessTokenClaims: undefined };
|
|
29
|
+
if (getAccessToken && !accessToken) {
|
|
30
|
+
// Failed to get access token, the user is not authenticated
|
|
42
31
|
return {
|
|
43
32
|
isAuthenticated: false,
|
|
44
33
|
};
|
|
45
34
|
}
|
|
35
|
+
const organizationTokens = essentials.conditional(getOrganizationToken &&
|
|
36
|
+
claims.organizations &&
|
|
37
|
+
Object.fromEntries(await Promise.all(claims.organizations.map(async (organizationId) => [
|
|
38
|
+
organizationId,
|
|
39
|
+
await this.getOrganizationToken(organizationId),
|
|
40
|
+
]))));
|
|
41
|
+
return {
|
|
42
|
+
isAuthenticated,
|
|
43
|
+
claims,
|
|
44
|
+
userInfo: essentials.conditional(fetchUserInfo && (await this.fetchUserInfo())),
|
|
45
|
+
...essentials.conditional(getAccessToken && { accessToken, scopes: accessTokenClaims?.scope?.split(' ') }),
|
|
46
|
+
organizationTokens,
|
|
47
|
+
};
|
|
46
48
|
};
|
|
47
49
|
}
|
|
48
50
|
}
|
|
49
51
|
|
|
50
|
-
Object.defineProperty(exports,
|
|
52
|
+
Object.defineProperty(exports, "LogtoClientError", {
|
|
51
53
|
enumerable: true,
|
|
52
54
|
get: function () { return BaseClient.LogtoClientError; }
|
|
53
55
|
});
|
|
54
|
-
Object.defineProperty(exports,
|
|
56
|
+
Object.defineProperty(exports, "LogtoError", {
|
|
55
57
|
enumerable: true,
|
|
56
58
|
get: function () { return BaseClient.LogtoError; }
|
|
57
59
|
});
|
|
58
|
-
Object.defineProperty(exports,
|
|
60
|
+
Object.defineProperty(exports, "LogtoRequestError", {
|
|
59
61
|
enumerable: true,
|
|
60
62
|
get: function () { return BaseClient.LogtoRequestError; }
|
|
61
63
|
});
|
|
62
|
-
Object.defineProperty(exports,
|
|
64
|
+
Object.defineProperty(exports, "OidcError", {
|
|
63
65
|
enumerable: true,
|
|
64
66
|
get: function () { return BaseClient.OidcError; }
|
|
65
67
|
});
|
|
66
|
-
Object.defineProperty(exports,
|
|
68
|
+
Object.defineProperty(exports, "Prompt", {
|
|
67
69
|
enumerable: true,
|
|
68
70
|
get: function () { return BaseClient.Prompt; }
|
|
69
71
|
});
|
|
70
|
-
Object.defineProperty(exports,
|
|
72
|
+
Object.defineProperty(exports, "ReservedScope", {
|
|
71
73
|
enumerable: true,
|
|
72
74
|
get: function () { return BaseClient.ReservedScope; }
|
|
73
75
|
});
|
|
74
|
-
Object.defineProperty(exports,
|
|
76
|
+
Object.defineProperty(exports, "UserScope", {
|
|
75
77
|
enumerable: true,
|
|
76
78
|
get: function () { return BaseClient.UserScope; }
|
|
77
79
|
});
|
package/lib/src/client.d.ts
CHANGED
|
@@ -4,5 +4,5 @@ export type { LogtoContext, GetContextParameters } from './types.js';
|
|
|
4
4
|
export type { IdTokenClaims, LogtoErrorCode, LogtoConfig, LogtoClientErrorCode, Storage, StorageKey, InteractionMode, } from '@logto/client';
|
|
5
5
|
export { LogtoError, LogtoRequestError, LogtoClientError, OidcError, Prompt, ReservedScope, UserScope, } from '@logto/client';
|
|
6
6
|
export default class LogtoNodeBaseClient extends BaseClient {
|
|
7
|
-
getContext: ({ getAccessToken, resource, fetchUserInfo, }?: GetContextParameters) => Promise<LogtoContext>;
|
|
7
|
+
getContext: ({ getAccessToken, resource, fetchUserInfo, getOrganizationToken, }?: GetContextParameters) => Promise<LogtoContext>;
|
|
8
8
|
}
|
package/lib/src/client.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import BaseClient from '@logto/client';
|
|
2
2
|
export { LogtoClientError, LogtoError, LogtoRequestError, OidcError, Prompt, ReservedScope, UserScope } from '@logto/client';
|
|
3
|
-
import { conditional } from '@silverhand/essentials';
|
|
3
|
+
import { trySafe, conditional } from '@silverhand/essentials';
|
|
4
4
|
|
|
5
5
|
class LogtoNodeBaseClient extends BaseClient {
|
|
6
6
|
constructor() {
|
|
7
7
|
super(...arguments);
|
|
8
|
-
this.getContext = async ({ getAccessToken, resource, fetchUserInfo, } = {}) => {
|
|
8
|
+
this.getContext = async ({ getAccessToken, resource, fetchUserInfo, getOrganizationToken, } = {}) => {
|
|
9
9
|
const isAuthenticated = await this.isAuthenticated();
|
|
10
10
|
if (!isAuthenticated) {
|
|
11
11
|
return {
|
|
@@ -13,29 +13,31 @@ class LogtoNodeBaseClient extends BaseClient {
|
|
|
13
13
|
};
|
|
14
14
|
}
|
|
15
15
|
const claims = await this.getIdTokenClaims();
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const accessToken = await this.getAccessToken(resource);
|
|
25
|
-
const accessTokenClaims = await this.getAccessTokenClaims(resource);
|
|
26
|
-
return {
|
|
27
|
-
isAuthenticated,
|
|
28
|
-
claims: await this.getIdTokenClaims(),
|
|
29
|
-
userInfo: conditional(fetchUserInfo && (await this.fetchUserInfo())),
|
|
30
|
-
accessToken,
|
|
31
|
-
scopes: accessTokenClaims.scope?.split(' '),
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
catch {
|
|
16
|
+
const { accessToken, accessTokenClaims } = getAccessToken
|
|
17
|
+
? {
|
|
18
|
+
accessToken: await trySafe(async () => this.getAccessToken(resource)),
|
|
19
|
+
accessTokenClaims: await trySafe(async () => this.getAccessTokenClaims(resource)),
|
|
20
|
+
}
|
|
21
|
+
: { accessToken: undefined, accessTokenClaims: undefined };
|
|
22
|
+
if (getAccessToken && !accessToken) {
|
|
23
|
+
// Failed to get access token, the user is not authenticated
|
|
35
24
|
return {
|
|
36
25
|
isAuthenticated: false,
|
|
37
26
|
};
|
|
38
27
|
}
|
|
28
|
+
const organizationTokens = conditional(getOrganizationToken &&
|
|
29
|
+
claims.organizations &&
|
|
30
|
+
Object.fromEntries(await Promise.all(claims.organizations.map(async (organizationId) => [
|
|
31
|
+
organizationId,
|
|
32
|
+
await this.getOrganizationToken(organizationId),
|
|
33
|
+
]))));
|
|
34
|
+
return {
|
|
35
|
+
isAuthenticated,
|
|
36
|
+
claims,
|
|
37
|
+
userInfo: conditional(fetchUserInfo && (await this.fetchUserInfo())),
|
|
38
|
+
...conditional(getAccessToken && { accessToken, scopes: accessTokenClaims?.scope?.split(' ') }),
|
|
39
|
+
organizationTokens,
|
|
40
|
+
};
|
|
39
41
|
};
|
|
40
42
|
}
|
|
41
43
|
}
|
package/lib/src/index.cjs
CHANGED
|
@@ -36,51 +36,51 @@ class LogtoClient extends client.default {
|
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
Object.defineProperty(exports,
|
|
39
|
+
Object.defineProperty(exports, "LogtoClientError", {
|
|
40
40
|
enumerable: true,
|
|
41
41
|
get: function () { return BaseClient.LogtoClientError; }
|
|
42
42
|
});
|
|
43
|
-
Object.defineProperty(exports,
|
|
43
|
+
Object.defineProperty(exports, "LogtoError", {
|
|
44
44
|
enumerable: true,
|
|
45
45
|
get: function () { return BaseClient.LogtoError; }
|
|
46
46
|
});
|
|
47
|
-
Object.defineProperty(exports,
|
|
47
|
+
Object.defineProperty(exports, "LogtoRequestError", {
|
|
48
48
|
enumerable: true,
|
|
49
49
|
get: function () { return BaseClient.LogtoRequestError; }
|
|
50
50
|
});
|
|
51
|
-
Object.defineProperty(exports,
|
|
51
|
+
Object.defineProperty(exports, "OidcError", {
|
|
52
52
|
enumerable: true,
|
|
53
53
|
get: function () { return BaseClient.OidcError; }
|
|
54
54
|
});
|
|
55
|
-
Object.defineProperty(exports,
|
|
55
|
+
Object.defineProperty(exports, "PersistKey", {
|
|
56
56
|
enumerable: true,
|
|
57
57
|
get: function () { return BaseClient.PersistKey; }
|
|
58
58
|
});
|
|
59
|
-
Object.defineProperty(exports,
|
|
59
|
+
Object.defineProperty(exports, "Prompt", {
|
|
60
60
|
enumerable: true,
|
|
61
61
|
get: function () { return BaseClient.Prompt; }
|
|
62
62
|
});
|
|
63
|
-
Object.defineProperty(exports,
|
|
63
|
+
Object.defineProperty(exports, "ReservedResource", {
|
|
64
64
|
enumerable: true,
|
|
65
65
|
get: function () { return BaseClient.ReservedResource; }
|
|
66
66
|
});
|
|
67
|
-
Object.defineProperty(exports,
|
|
67
|
+
Object.defineProperty(exports, "ReservedScope", {
|
|
68
68
|
enumerable: true,
|
|
69
69
|
get: function () { return BaseClient.ReservedScope; }
|
|
70
70
|
});
|
|
71
|
-
Object.defineProperty(exports,
|
|
71
|
+
Object.defineProperty(exports, "UserScope", {
|
|
72
72
|
enumerable: true,
|
|
73
73
|
get: function () { return BaseClient.UserScope; }
|
|
74
74
|
});
|
|
75
|
-
Object.defineProperty(exports,
|
|
75
|
+
Object.defineProperty(exports, "buildOrganizationUrn", {
|
|
76
76
|
enumerable: true,
|
|
77
77
|
get: function () { return BaseClient.buildOrganizationUrn; }
|
|
78
78
|
});
|
|
79
|
-
Object.defineProperty(exports,
|
|
79
|
+
Object.defineProperty(exports, "getOrganizationIdFromUrn", {
|
|
80
80
|
enumerable: true,
|
|
81
81
|
get: function () { return BaseClient.getOrganizationIdFromUrn; }
|
|
82
82
|
});
|
|
83
|
-
Object.defineProperty(exports,
|
|
83
|
+
Object.defineProperty(exports, "organizationUrnPrefix", {
|
|
84
84
|
enumerable: true,
|
|
85
85
|
get: function () { return BaseClient.organizationUrnPrefix; }
|
|
86
86
|
});
|
package/lib/src/types.d.ts
CHANGED
|
@@ -10,9 +10,11 @@ export type LogtoContext = {
|
|
|
10
10
|
accessToken?: string;
|
|
11
11
|
userInfo?: UserInfoResponse;
|
|
12
12
|
scopes?: string[];
|
|
13
|
+
organizationTokens?: Record<string, string>;
|
|
13
14
|
};
|
|
14
15
|
export type GetContextParameters = {
|
|
15
16
|
fetchUserInfo?: boolean;
|
|
16
17
|
getAccessToken?: boolean;
|
|
17
18
|
resource?: string;
|
|
19
|
+
getOrganizationToken?: boolean;
|
|
18
20
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@logto/node",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./lib/src/index.cjs",
|
|
6
6
|
"module": "./lib/src/index.js",
|
|
@@ -27,18 +27,18 @@
|
|
|
27
27
|
"directory": "packages/node"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@logto/client": "^2.3.
|
|
31
|
-
"@silverhand/essentials": "^2.
|
|
30
|
+
"@logto/client": "^2.3.3",
|
|
31
|
+
"@silverhand/essentials": "^2.8.7",
|
|
32
32
|
"js-base64": "^3.7.4",
|
|
33
33
|
"node-fetch": "^2.6.7"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"@silverhand/eslint-config": "^
|
|
37
|
-
"@silverhand/ts-config": "^
|
|
36
|
+
"@silverhand/eslint-config": "^5.0.0",
|
|
37
|
+
"@silverhand/ts-config": "^5.0.0",
|
|
38
38
|
"@swc/core": "^1.3.7",
|
|
39
39
|
"@swc/jest": "^0.2.24",
|
|
40
40
|
"@types/jest": "^29.5.0",
|
|
41
|
-
"@types/node": "^
|
|
41
|
+
"@types/node": "^20.0.0",
|
|
42
42
|
"eslint": "^8.44.0",
|
|
43
43
|
"jest": "^29.5.0",
|
|
44
44
|
"jest-location-mock": "^2.0.0",
|