@logto/node 1.1.0 → 1.1.2
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 +4 -19
- package/lib/index.js +87 -84
- package/lib/index.mjs +62 -0
- package/lib/index.test.d.ts +1 -0
- package/lib/types.d.ts +17 -0
- package/lib/utils/generators.d.ts +17 -0
- package/lib/utils/generators.js +37 -0
- package/lib/utils/generators.mjs +33 -0
- package/lib/utils/generators.test.d.ts +1 -0
- package/package.json +16 -27
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/module.d.mts +0 -25
- package/lib/module.mjs +0 -79
- package/lib/module.mjs.map +0 -1
package/lib/index.d.ts
CHANGED
|
@@ -1,25 +1,10 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
}
|
|
6
|
-
}
|
|
7
|
-
export type LogtoContext = {
|
|
8
|
-
isAuthenticated: boolean;
|
|
9
|
-
claims?: IdTokenClaims;
|
|
10
|
-
accessToken?: string;
|
|
11
|
-
userInfo?: UserInfoResponse;
|
|
12
|
-
};
|
|
13
|
-
export type GetContextParameters = {
|
|
14
|
-
fetchUserInfo?: boolean;
|
|
15
|
-
getAccessToken?: boolean;
|
|
16
|
-
resource?: string;
|
|
17
|
-
};
|
|
1
|
+
import type { LogtoConfig, ClientAdapter } from '@logto/client';
|
|
2
|
+
import BaseClient from '@logto/client';
|
|
3
|
+
import type { GetContextParameters, LogtoContext } from './types';
|
|
4
|
+
export type { LogtoContext, GetContextParameters } from './types';
|
|
18
5
|
export type { IdTokenClaims, LogtoErrorCode, LogtoConfig, LogtoClientErrorCode, Storage, StorageKey, InteractionMode, } from '@logto/client';
|
|
19
6
|
export { LogtoError, OidcError, Prompt, LogtoRequestError, LogtoClientError, ReservedScope, UserScope, } from '@logto/client';
|
|
20
7
|
export default class LogtoClient extends BaseClient {
|
|
21
8
|
constructor(config: LogtoConfig, adapter: Pick<ClientAdapter, 'navigate' | 'storage'>);
|
|
22
9
|
getContext: ({ getAccessToken, resource, fetchUserInfo, }?: GetContextParameters) => Promise<LogtoContext>;
|
|
23
10
|
}
|
|
24
|
-
|
|
25
|
-
//# sourceMappingURL=index.d.ts.map
|
package/lib/index.js
CHANGED
|
@@ -1,95 +1,98 @@
|
|
|
1
|
-
|
|
2
|
-
var $C4LKF$silverhandessentials = require("@silverhand/essentials");
|
|
3
|
-
var $C4LKF$nodefetch = require("node-fetch");
|
|
4
|
-
var $C4LKF$crypto = require("crypto");
|
|
5
|
-
var $C4LKF$jsbase64 = require("js-base64");
|
|
6
|
-
|
|
7
|
-
function $parcel$interopDefault(a) {
|
|
8
|
-
return a && a.__esModule ? a.default : a;
|
|
9
|
-
}
|
|
10
|
-
function $parcel$defineInteropFlag(a) {
|
|
11
|
-
Object.defineProperty(a, '__esModule', {value: true, configurable: true});
|
|
12
|
-
}
|
|
13
|
-
function $parcel$export(e, n, v, s) {
|
|
14
|
-
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
$parcel$defineInteropFlag(module.exports);
|
|
18
|
-
|
|
19
|
-
$parcel$export(module.exports, "default", () => $8c9551714715c14b$export$2e2bcd8739ae039);
|
|
20
|
-
$parcel$export(module.exports, "LogtoError", () => $8c9551714715c14b$re_export$LogtoError);
|
|
21
|
-
$parcel$export(module.exports, "OidcError", () => $8c9551714715c14b$re_export$OidcError);
|
|
22
|
-
$parcel$export(module.exports, "Prompt", () => $8c9551714715c14b$re_export$Prompt);
|
|
23
|
-
$parcel$export(module.exports, "LogtoRequestError", () => $8c9551714715c14b$re_export$LogtoRequestError);
|
|
24
|
-
$parcel$export(module.exports, "LogtoClientError", () => $8c9551714715c14b$re_export$LogtoClientError);
|
|
25
|
-
$parcel$export(module.exports, "ReservedScope", () => $8c9551714715c14b$re_export$ReservedScope);
|
|
26
|
-
$parcel$export(module.exports, "UserScope", () => $8c9551714715c14b$re_export$UserScope);
|
|
1
|
+
'use strict';
|
|
27
2
|
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
28
4
|
|
|
5
|
+
var BaseClient = require('@logto/client');
|
|
6
|
+
var essentials = require('@silverhand/essentials');
|
|
7
|
+
var fetch = require('node-fetch');
|
|
8
|
+
var generators = require('./utils/generators.js');
|
|
29
9
|
|
|
30
|
-
|
|
10
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
31
11
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
*/ const $5ffebdf4968dbe4e$var$generateRandomString = (length = 64)=>(0, $C4LKF$jsbase64.fromUint8Array)((0, $C4LKF$crypto.randomFillSync)(new Uint8Array(length)), true);
|
|
35
|
-
const $5ffebdf4968dbe4e$export$9ccd2716e53a229b = ()=>$5ffebdf4968dbe4e$var$generateRandomString();
|
|
36
|
-
const $5ffebdf4968dbe4e$export$cf1891f923f5943a = ()=>$5ffebdf4968dbe4e$var$generateRandomString();
|
|
37
|
-
const $5ffebdf4968dbe4e$export$414b01b1f867308a = async (codeVerifier)=>{
|
|
38
|
-
const encodedCodeVerifier = new TextEncoder().encode(codeVerifier);
|
|
39
|
-
const hash = (0, $C4LKF$crypto.createHash)("sha256");
|
|
40
|
-
hash.update(encodedCodeVerifier);
|
|
41
|
-
const codeChallenge = hash.digest();
|
|
42
|
-
return (0, $C4LKF$jsbase64.fromUint8Array)(codeChallenge, true);
|
|
43
|
-
};
|
|
12
|
+
var BaseClient__default = /*#__PURE__*/_interopDefault(BaseClient);
|
|
13
|
+
var fetch__default = /*#__PURE__*/_interopDefault(fetch);
|
|
44
14
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
class $8c9551714715c14b$export$2e2bcd8739ae039 extends (0, ($parcel$interopDefault($C4LKF$logtoclient))) {
|
|
48
|
-
constructor(config, adapter){
|
|
15
|
+
class LogtoClient extends BaseClient__default.default {
|
|
16
|
+
constructor(config, adapter) {
|
|
49
17
|
super(config, {
|
|
50
18
|
...adapter,
|
|
51
|
-
requester:
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
19
|
+
requester: BaseClient.createRequester(config.appSecret
|
|
20
|
+
? async (...args) => {
|
|
21
|
+
const [input, init] = args;
|
|
22
|
+
return fetch__default.default(input, {
|
|
23
|
+
...init,
|
|
24
|
+
headers: {
|
|
25
|
+
Authorization: `basic ${Buffer.from(
|
|
26
|
+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
27
|
+
`${config.appId}:${config.appSecret}`, 'utf8').toString('base64')}`,
|
|
28
|
+
...init?.headers,
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
: fetch__default.default),
|
|
33
|
+
generateCodeChallenge: generators.generateCodeChallenge,
|
|
34
|
+
generateCodeVerifier: generators.generateCodeVerifier,
|
|
35
|
+
generateState: generators.generateState,
|
|
65
36
|
});
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
37
|
+
this.getContext = async ({ getAccessToken, resource, fetchUserInfo, } = {}) => {
|
|
38
|
+
const isAuthenticated = await this.isAuthenticated();
|
|
39
|
+
if (!isAuthenticated) {
|
|
40
|
+
return {
|
|
41
|
+
isAuthenticated,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
const claims = await this.getIdTokenClaims();
|
|
45
|
+
if (!getAccessToken) {
|
|
46
|
+
return {
|
|
47
|
+
isAuthenticated,
|
|
48
|
+
claims,
|
|
49
|
+
userInfo: essentials.conditional(fetchUserInfo && (await this.fetchUserInfo())),
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
try {
|
|
53
|
+
const accessToken = await this.getAccessToken(resource);
|
|
54
|
+
return {
|
|
55
|
+
isAuthenticated,
|
|
56
|
+
claims: await this.getIdTokenClaims(),
|
|
57
|
+
userInfo: essentials.conditional(fetchUserInfo && (await this.fetchUserInfo())),
|
|
58
|
+
accessToken,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
return {
|
|
63
|
+
isAuthenticated: false,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
71
66
|
};
|
|
72
|
-
|
|
73
|
-
if (!getAccessToken) return {
|
|
74
|
-
isAuthenticated: isAuthenticated,
|
|
75
|
-
claims: claims,
|
|
76
|
-
userInfo: (0, $C4LKF$silverhandessentials.conditional)(fetchUserInfo && await this.fetchUserInfo())
|
|
77
|
-
};
|
|
78
|
-
try {
|
|
79
|
-
const accessToken = await this.getAccessToken(resource);
|
|
80
|
-
return {
|
|
81
|
-
isAuthenticated: isAuthenticated,
|
|
82
|
-
claims: await this.getIdTokenClaims(),
|
|
83
|
-
userInfo: (0, $C4LKF$silverhandessentials.conditional)(fetchUserInfo && await this.fetchUserInfo()),
|
|
84
|
-
accessToken: accessToken
|
|
85
|
-
};
|
|
86
|
-
} catch {
|
|
87
|
-
return {
|
|
88
|
-
isAuthenticated: false
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
};
|
|
67
|
+
}
|
|
92
68
|
}
|
|
93
69
|
|
|
94
|
-
|
|
95
|
-
|
|
70
|
+
Object.defineProperty(exports, 'LogtoClientError', {
|
|
71
|
+
enumerable: true,
|
|
72
|
+
get: function () { return BaseClient.LogtoClientError; }
|
|
73
|
+
});
|
|
74
|
+
Object.defineProperty(exports, 'LogtoError', {
|
|
75
|
+
enumerable: true,
|
|
76
|
+
get: function () { return BaseClient.LogtoError; }
|
|
77
|
+
});
|
|
78
|
+
Object.defineProperty(exports, 'LogtoRequestError', {
|
|
79
|
+
enumerable: true,
|
|
80
|
+
get: function () { return BaseClient.LogtoRequestError; }
|
|
81
|
+
});
|
|
82
|
+
Object.defineProperty(exports, 'OidcError', {
|
|
83
|
+
enumerable: true,
|
|
84
|
+
get: function () { return BaseClient.OidcError; }
|
|
85
|
+
});
|
|
86
|
+
Object.defineProperty(exports, 'Prompt', {
|
|
87
|
+
enumerable: true,
|
|
88
|
+
get: function () { return BaseClient.Prompt; }
|
|
89
|
+
});
|
|
90
|
+
Object.defineProperty(exports, 'ReservedScope', {
|
|
91
|
+
enumerable: true,
|
|
92
|
+
get: function () { return BaseClient.ReservedScope; }
|
|
93
|
+
});
|
|
94
|
+
Object.defineProperty(exports, 'UserScope', {
|
|
95
|
+
enumerable: true,
|
|
96
|
+
get: function () { return BaseClient.UserScope; }
|
|
97
|
+
});
|
|
98
|
+
exports.default = LogtoClient;
|
package/lib/index.mjs
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import BaseClient, { createRequester } from '@logto/client';
|
|
2
|
+
export { LogtoClientError, LogtoError, LogtoRequestError, OidcError, Prompt, ReservedScope, UserScope } from '@logto/client';
|
|
3
|
+
import { conditional } from '@silverhand/essentials';
|
|
4
|
+
import fetch from 'node-fetch';
|
|
5
|
+
import { generateCodeChallenge, generateCodeVerifier, generateState } from './utils/generators.mjs';
|
|
6
|
+
|
|
7
|
+
class LogtoClient extends BaseClient {
|
|
8
|
+
constructor(config, adapter) {
|
|
9
|
+
super(config, {
|
|
10
|
+
...adapter,
|
|
11
|
+
requester: createRequester(config.appSecret
|
|
12
|
+
? async (...args) => {
|
|
13
|
+
const [input, init] = args;
|
|
14
|
+
return fetch(input, {
|
|
15
|
+
...init,
|
|
16
|
+
headers: {
|
|
17
|
+
Authorization: `basic ${Buffer.from(
|
|
18
|
+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
19
|
+
`${config.appId}:${config.appSecret}`, 'utf8').toString('base64')}`,
|
|
20
|
+
...init?.headers,
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
: fetch),
|
|
25
|
+
generateCodeChallenge,
|
|
26
|
+
generateCodeVerifier,
|
|
27
|
+
generateState,
|
|
28
|
+
});
|
|
29
|
+
this.getContext = async ({ getAccessToken, resource, fetchUserInfo, } = {}) => {
|
|
30
|
+
const isAuthenticated = await this.isAuthenticated();
|
|
31
|
+
if (!isAuthenticated) {
|
|
32
|
+
return {
|
|
33
|
+
isAuthenticated,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
const claims = await this.getIdTokenClaims();
|
|
37
|
+
if (!getAccessToken) {
|
|
38
|
+
return {
|
|
39
|
+
isAuthenticated,
|
|
40
|
+
claims,
|
|
41
|
+
userInfo: conditional(fetchUserInfo && (await this.fetchUserInfo())),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
try {
|
|
45
|
+
const accessToken = await this.getAccessToken(resource);
|
|
46
|
+
return {
|
|
47
|
+
isAuthenticated,
|
|
48
|
+
claims: await this.getIdTokenClaims(),
|
|
49
|
+
userInfo: conditional(fetchUserInfo && (await this.fetchUserInfo())),
|
|
50
|
+
accessToken,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
return {
|
|
55
|
+
isAuthenticated: false,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export { LogtoClient as default };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/lib/types.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { IdTokenClaims, UserInfoResponse } from '@logto/client';
|
|
2
|
+
declare module 'http' {
|
|
3
|
+
interface IncomingMessage {
|
|
4
|
+
user: LogtoContext;
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
export type LogtoContext = {
|
|
8
|
+
isAuthenticated: boolean;
|
|
9
|
+
claims?: IdTokenClaims;
|
|
10
|
+
accessToken?: string;
|
|
11
|
+
userInfo?: UserInfoResponse;
|
|
12
|
+
};
|
|
13
|
+
export type GetContextParameters = {
|
|
14
|
+
fetchUserInfo?: boolean;
|
|
15
|
+
getAccessToken?: boolean;
|
|
16
|
+
resource?: string;
|
|
17
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generates random string for state and encodes them in url safe base64
|
|
3
|
+
*/
|
|
4
|
+
export declare const generateState: () => string;
|
|
5
|
+
/**
|
|
6
|
+
* Generates code verifier
|
|
7
|
+
*
|
|
8
|
+
* @link [Client Creates a Code Verifier](https://datatracker.ietf.org/doc/html/rfc7636#section-4.1)
|
|
9
|
+
*/
|
|
10
|
+
export declare const generateCodeVerifier: () => string;
|
|
11
|
+
/**
|
|
12
|
+
* Calculates the S256 PKCE code challenge for an arbitrary code verifier and encodes it in url safe base64
|
|
13
|
+
*
|
|
14
|
+
* @param {String} codeVerifier Code verifier to calculate the S256 code challenge for
|
|
15
|
+
* @link [Client Creates the Code Challenge](https://datatracker.ietf.org/doc/html/rfc7636#section-4.2)
|
|
16
|
+
*/
|
|
17
|
+
export declare const generateCodeChallenge: (codeVerifier: string) => Promise<string>;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var crypto = require('crypto');
|
|
4
|
+
var jsBase64 = require('js-base64');
|
|
5
|
+
|
|
6
|
+
/** @link [Proof Key for Code Exchange by OAuth Public Clients](https://datatracker.ietf.org/doc/html/rfc7636) */
|
|
7
|
+
/**
|
|
8
|
+
* @param length The length of the raw random data.
|
|
9
|
+
*/
|
|
10
|
+
const generateRandomString = (length = 64) => jsBase64.fromUint8Array(crypto.randomFillSync(new Uint8Array(length)), true);
|
|
11
|
+
/**
|
|
12
|
+
* Generates random string for state and encodes them in url safe base64
|
|
13
|
+
*/
|
|
14
|
+
const generateState = () => generateRandomString();
|
|
15
|
+
/**
|
|
16
|
+
* Generates code verifier
|
|
17
|
+
*
|
|
18
|
+
* @link [Client Creates a Code Verifier](https://datatracker.ietf.org/doc/html/rfc7636#section-4.1)
|
|
19
|
+
*/
|
|
20
|
+
const generateCodeVerifier = () => generateRandomString();
|
|
21
|
+
/**
|
|
22
|
+
* Calculates the S256 PKCE code challenge for an arbitrary code verifier and encodes it in url safe base64
|
|
23
|
+
*
|
|
24
|
+
* @param {String} codeVerifier Code verifier to calculate the S256 code challenge for
|
|
25
|
+
* @link [Client Creates the Code Challenge](https://datatracker.ietf.org/doc/html/rfc7636#section-4.2)
|
|
26
|
+
*/
|
|
27
|
+
const generateCodeChallenge = async (codeVerifier) => {
|
|
28
|
+
const encodedCodeVerifier = new TextEncoder().encode(codeVerifier);
|
|
29
|
+
const hash = crypto.createHash('sha256');
|
|
30
|
+
hash.update(encodedCodeVerifier);
|
|
31
|
+
const codeChallenge = hash.digest();
|
|
32
|
+
return jsBase64.fromUint8Array(codeChallenge, true);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
exports.generateCodeChallenge = generateCodeChallenge;
|
|
36
|
+
exports.generateCodeVerifier = generateCodeVerifier;
|
|
37
|
+
exports.generateState = generateState;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { createHash, randomFillSync } from 'crypto';
|
|
2
|
+
import { fromUint8Array } from 'js-base64';
|
|
3
|
+
|
|
4
|
+
/** @link [Proof Key for Code Exchange by OAuth Public Clients](https://datatracker.ietf.org/doc/html/rfc7636) */
|
|
5
|
+
/**
|
|
6
|
+
* @param length The length of the raw random data.
|
|
7
|
+
*/
|
|
8
|
+
const generateRandomString = (length = 64) => fromUint8Array(randomFillSync(new Uint8Array(length)), true);
|
|
9
|
+
/**
|
|
10
|
+
* Generates random string for state and encodes them in url safe base64
|
|
11
|
+
*/
|
|
12
|
+
const generateState = () => generateRandomString();
|
|
13
|
+
/**
|
|
14
|
+
* Generates code verifier
|
|
15
|
+
*
|
|
16
|
+
* @link [Client Creates a Code Verifier](https://datatracker.ietf.org/doc/html/rfc7636#section-4.1)
|
|
17
|
+
*/
|
|
18
|
+
const generateCodeVerifier = () => generateRandomString();
|
|
19
|
+
/**
|
|
20
|
+
* Calculates the S256 PKCE code challenge for an arbitrary code verifier and encodes it in url safe base64
|
|
21
|
+
*
|
|
22
|
+
* @param {String} codeVerifier Code verifier to calculate the S256 code challenge for
|
|
23
|
+
* @link [Client Creates the Code Challenge](https://datatracker.ietf.org/doc/html/rfc7636#section-4.2)
|
|
24
|
+
*/
|
|
25
|
+
const generateCodeChallenge = async (codeVerifier) => {
|
|
26
|
+
const encodedCodeVerifier = new TextEncoder().encode(codeVerifier);
|
|
27
|
+
const hash = createHash('sha256');
|
|
28
|
+
hash.update(encodedCodeVerifier);
|
|
29
|
+
const codeChallenge = hash.digest();
|
|
30
|
+
return fromUint8Array(codeChallenge, true);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export { generateCodeChallenge, generateCodeVerifier, generateState };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@logto/node",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.2",
|
|
4
4
|
"source": "./src/index.ts",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"exports": {
|
|
7
7
|
"require": "./lib/index.js",
|
|
8
|
-
"import": "./lib/
|
|
8
|
+
"import": "./lib/index.mjs"
|
|
9
9
|
},
|
|
10
|
-
"module": "./lib/
|
|
10
|
+
"module": "./lib/index.mjs",
|
|
11
11
|
"types": "./lib/index.d.ts",
|
|
12
12
|
"files": [
|
|
13
13
|
"lib"
|
|
@@ -22,35 +22,33 @@
|
|
|
22
22
|
"dev:tsc": "tsc -p tsconfig.build.json -w --preserveWatchOutput",
|
|
23
23
|
"precommit": "lint-staged",
|
|
24
24
|
"check": "tsc --noEmit",
|
|
25
|
-
"build": "rm -rf lib/ &&
|
|
25
|
+
"build": "rm -rf lib/ && tsc -p tsconfig.build.json --noEmit && rollup -c",
|
|
26
26
|
"lint": "eslint --ext .ts src",
|
|
27
27
|
"test": "jest",
|
|
28
28
|
"test:coverage": "jest --silent --coverage",
|
|
29
29
|
"prepack": "pnpm test"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@logto/client": "^1.1.
|
|
33
|
-
"@silverhand/essentials": "^
|
|
32
|
+
"@logto/client": "^1.1.2",
|
|
33
|
+
"@silverhand/essentials": "^2.6.1",
|
|
34
34
|
"js-base64": "^3.7.4",
|
|
35
35
|
"node-fetch": "^2.6.7"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
|
-
"@jest/types": "^
|
|
39
|
-
"@parcel/core": "^2.8.3",
|
|
40
|
-
"@parcel/packager-ts": "^2.8.3",
|
|
41
|
-
"@parcel/transformer-typescript-types": "^2.8.3",
|
|
38
|
+
"@jest/types": "^29.5.0",
|
|
42
39
|
"@silverhand/eslint-config": "^2.0.0",
|
|
43
40
|
"@silverhand/ts-config": "^1.0.0",
|
|
44
|
-
"@
|
|
45
|
-
"
|
|
46
|
-
"jest": "^
|
|
41
|
+
"@swc/core": "^1.3.7",
|
|
42
|
+
"@swc/jest": "^0.2.24",
|
|
43
|
+
"@types/jest": "^29.5.0",
|
|
44
|
+
"@types/node": "^18.15.11",
|
|
45
|
+
"eslint": "^8.38.0",
|
|
46
|
+
"jest": "^29.5.0",
|
|
47
47
|
"jest-location-mock": "^1.0.9",
|
|
48
48
|
"jest-matcher-specific-error": "^1.0.0",
|
|
49
49
|
"lint-staged": "^13.0.0",
|
|
50
|
-
"
|
|
51
|
-
"
|
|
52
|
-
"ts-jest": "^27.0.4",
|
|
53
|
-
"typescript": "4.9.5"
|
|
50
|
+
"prettier": "^2.8.7",
|
|
51
|
+
"typescript": "^5.0.0"
|
|
54
52
|
},
|
|
55
53
|
"eslintConfig": {
|
|
56
54
|
"extends": "@silverhand"
|
|
@@ -59,14 +57,5 @@
|
|
|
59
57
|
"publishConfig": {
|
|
60
58
|
"access": "public"
|
|
61
59
|
},
|
|
62
|
-
"
|
|
63
|
-
"main": {
|
|
64
|
-
"context": "node",
|
|
65
|
-
"includeNodeModules": false,
|
|
66
|
-
"engines": {
|
|
67
|
-
"node": ">=18.12.0"
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
},
|
|
71
|
-
"gitHead": "f24174a6f840b8db968ad3886878d1b6e92b1b9d"
|
|
60
|
+
"gitHead": "9e9a8b0887ef67baa7c3c564590bb06e7801d03e"
|
|
72
61
|
}
|
package/lib/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"mappings":";AAEA,eAAe,MAAM,CAAC;IAGpB,UAAU,eAAe;QACvB,IAAI,EAAE,YAAY,CAAC;KACpB;CACF;AAED,2BAA2B;IACzB,eAAe,EAAE,OAAO,CAAC;IACzB,MAAM,CAAC,EAAE,aAAa,CAAC;IACvB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,EAAE,gBAAgB,CAAC;CAC7B,CAAC;AAEF,mCAAmC;IACjC,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB,CAAC;AEXF,YAAY,EACV,aAAa,EACb,cAAc,EACd,WAAW,EACX,oBAAoB,EACpB,OAAO,EACP,UAAU,EACV,eAAe,GAChB,MAAM,eAAe,CAAC;AAEvB,OAAO,EACL,UAAU,EACV,SAAS,EACT,MAAM,EACN,iBAAiB,EACjB,gBAAgB,EAChB,aAAa,EACb,SAAS,GACV,MAAM,eAAe,CAAC;AAEvB,gCAAiC,SAAQ,UAAU;gBACrC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,IAAI,CAAC,aAAa,EAAE,UAAU,GAAG,SAAS,CAAC;IA4BrF,UAAU,kDAIP,oBAAoB,KAAQ,QAAQ,YAAY,CAAC,CAiClD;CACH","sources":["packages/node/src/src/types.ts","packages/node/src/src/utils/generators.ts","packages/node/src/src/index.ts","packages/node/src/index.ts"],"sourcesContent":[null,null,null,"import type { LogtoConfig, ClientAdapter } from '@logto/client';\nimport BaseClient, { createRequester } from '@logto/client';\nimport { conditional } from '@silverhand/essentials';\nimport fetch from 'node-fetch';\n\nimport type { GetContextParameters, LogtoContext } from './types';\nimport { generateCodeChallenge, generateCodeVerifier, generateState } from './utils/generators';\n\nexport type { LogtoContext, GetContextParameters } from './types';\n\nexport type {\n IdTokenClaims,\n LogtoErrorCode,\n LogtoConfig,\n LogtoClientErrorCode,\n Storage,\n StorageKey,\n InteractionMode,\n} from '@logto/client';\n\nexport {\n LogtoError,\n OidcError,\n Prompt,\n LogtoRequestError,\n LogtoClientError,\n ReservedScope,\n UserScope,\n} from '@logto/client';\n\nexport default class LogtoClient extends BaseClient {\n constructor(config: LogtoConfig, adapter: Pick<ClientAdapter, 'navigate' | 'storage'>) {\n super(config, {\n ...adapter,\n requester: createRequester(\n config.appSecret\n ? async (...args: Parameters<typeof fetch>) => {\n const [input, init] = args;\n\n return fetch(input, {\n ...init,\n headers: {\n Authorization: `basic ${Buffer.from(\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n `${config.appId}:${config.appSecret}`,\n 'utf8'\n ).toString('base64')}`,\n ...init?.headers,\n },\n });\n }\n : fetch\n ),\n generateCodeChallenge,\n generateCodeVerifier,\n generateState,\n });\n }\n\n getContext = async ({\n getAccessToken,\n resource,\n fetchUserInfo,\n }: GetContextParameters = {}): Promise<LogtoContext> => {\n const isAuthenticated = await this.isAuthenticated();\n\n if (!isAuthenticated) {\n return {\n isAuthenticated,\n };\n }\n\n const claims = await this.getIdTokenClaims();\n\n if (!getAccessToken) {\n return {\n isAuthenticated,\n claims,\n userInfo: conditional(fetchUserInfo && (await this.fetchUserInfo())),\n };\n }\n\n try {\n const accessToken = await this.getAccessToken(resource);\n\n return {\n isAuthenticated,\n claims: await this.getIdTokenClaims(),\n userInfo: conditional(fetchUserInfo && (await this.fetchUserInfo())),\n accessToken,\n };\n } catch {\n return {\n isAuthenticated: false,\n };\n }\n };\n}\n"],"names":[],"version":3,"file":"index.d.ts.map"}
|
package/lib/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;ACAA,+GAA+G,GAC/G;;AAIA;;CAEC,GACD,MAAM,6CAAuB,CAAC,SAAS,EAAE,GACvC,CAAA,GAAA,8BAAa,EAAE,CAAA,GAAA,4BAAa,EAAE,IAAI,WAAW,UAAU,IAAI;AAKtD,MAAM,4CAAgB,IAAM;AAO5B,MAAM,4CAAuB,IAAM;AAQnC,MAAM,4CAAwB,OAAO,eAA0C;IACpF,MAAM,sBAAsB,IAAI,cAAc,MAAM,CAAC;IACrD,MAAM,OAAO,CAAA,GAAA,wBAAS,EAAE;IACxB,KAAK,MAAM,CAAC;IACZ,MAAM,gBAAgB,KAAK,MAAM;IAEjC,OAAO,CAAA,GAAA,8BAAc,AAAD,EAAE,eAAe,IAAI;AAC3C;;;;ADNe,uDAA0B,CAAA,GAAA,4CAAS;IAChD,YAAY,MAAmB,EAAE,OAAoD,CAAE;QACrF,KAAK,CAAC,QAAQ;YACZ,GAAG,OAAO;YACV,WAAW,CAAA,GAAA,kCAAc,EACvB,OAAO,SAAS,GACZ,OAAO,GAAG,OAAmC;gBAC3C,MAAM,CAAC,OAAO,KAAK,GAAG;gBAEtB,OAAO,CAAA,GAAA,0CAAK,AAAD,EAAE,OAAO;oBAClB,GAAG,IAAI;oBACP,SAAS;wBACP,eAAe,CAAC,MAAM,EAAE,OAAO,IAAI,CACjC,4EAA4E;wBAC5E,CAAC,EAAE,OAAO,KAAK,CAAC,CAAC,EAAE,OAAO,SAAS,CAAC,CAAC,EACrC,QACA,QAAQ,CAAC,UAAU,CAAC;wBACtB,GAAG,MAAM,OAAO;oBAClB;gBACF;YACF,IACA,CAAA,GAAA,0CAAI,CAAC;mCAEX;kCACA;2BACA;QACF;IACF;IAEA,aAAa,OAAO,kBAClB,eAAc,YACd,SAAQ,iBACR,cAAa,EACQ,GAAG,CAAC,CAAC,GAA4B;QACtD,MAAM,kBAAkB,MAAM,IAAI,CAAC,eAAe;QAElD,IAAI,CAAC,iBACH,OAAO;6BACL;QACF;QAGF,MAAM,SAAS,MAAM,IAAI,CAAC,gBAAgB;QAE1C,IAAI,CAAC,gBACH,OAAO;6BACL;oBACA;YACA,UAAU,CAAA,GAAA,uCAAU,EAAE,iBAAkB,MAAM,IAAI,CAAC,aAAa;QAClE;QAGF,IAAI;YACF,MAAM,cAAc,MAAM,IAAI,CAAC,cAAc,CAAC;YAE9C,OAAO;iCACL;gBACA,QAAQ,MAAM,IAAI,CAAC,gBAAgB;gBACnC,UAAU,CAAA,GAAA,uCAAU,EAAE,iBAAkB,MAAM,IAAI,CAAC,aAAa;6BAChE;YACF;QACF,EAAE,OAAM;YACN,OAAO;gBACL,iBAAiB,KAAK;YACxB;QACF;IACF,EAAE;AACJ","sources":["packages/node/src/index.ts","packages/node/src/utils/generators.ts"],"sourcesContent":["import type { LogtoConfig, ClientAdapter } from '@logto/client';\nimport BaseClient, { createRequester } from '@logto/client';\nimport { conditional } from '@silverhand/essentials';\nimport fetch from 'node-fetch';\n\nimport type { GetContextParameters, LogtoContext } from './types';\nimport { generateCodeChallenge, generateCodeVerifier, generateState } from './utils/generators';\n\nexport type { LogtoContext, GetContextParameters } from './types';\n\nexport type {\n IdTokenClaims,\n LogtoErrorCode,\n LogtoConfig,\n LogtoClientErrorCode,\n Storage,\n StorageKey,\n InteractionMode,\n} from '@logto/client';\n\nexport {\n LogtoError,\n OidcError,\n Prompt,\n LogtoRequestError,\n LogtoClientError,\n ReservedScope,\n UserScope,\n} from '@logto/client';\n\nexport default class LogtoClient extends BaseClient {\n constructor(config: LogtoConfig, adapter: Pick<ClientAdapter, 'navigate' | 'storage'>) {\n super(config, {\n ...adapter,\n requester: createRequester(\n config.appSecret\n ? async (...args: Parameters<typeof fetch>) => {\n const [input, init] = args;\n\n return fetch(input, {\n ...init,\n headers: {\n Authorization: `basic ${Buffer.from(\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n `${config.appId}:${config.appSecret}`,\n 'utf8'\n ).toString('base64')}`,\n ...init?.headers,\n },\n });\n }\n : fetch\n ),\n generateCodeChallenge,\n generateCodeVerifier,\n generateState,\n });\n }\n\n getContext = async ({\n getAccessToken,\n resource,\n fetchUserInfo,\n }: GetContextParameters = {}): Promise<LogtoContext> => {\n const isAuthenticated = await this.isAuthenticated();\n\n if (!isAuthenticated) {\n return {\n isAuthenticated,\n };\n }\n\n const claims = await this.getIdTokenClaims();\n\n if (!getAccessToken) {\n return {\n isAuthenticated,\n claims,\n userInfo: conditional(fetchUserInfo && (await this.fetchUserInfo())),\n };\n }\n\n try {\n const accessToken = await this.getAccessToken(resource);\n\n return {\n isAuthenticated,\n claims: await this.getIdTokenClaims(),\n userInfo: conditional(fetchUserInfo && (await this.fetchUserInfo())),\n accessToken,\n };\n } catch {\n return {\n isAuthenticated: false,\n };\n }\n };\n}\n","/** @link [Proof Key for Code Exchange by OAuth Public Clients](https://datatracker.ietf.org/doc/html/rfc7636) */\nimport { randomFillSync, createHash } from 'crypto';\n\nimport { fromUint8Array } from 'js-base64';\n\n/**\n * @param length The length of the raw random data.\n */\nconst generateRandomString = (length = 64) =>\n fromUint8Array(randomFillSync(new Uint8Array(length)), true);\n\n/**\n * Generates random string for state and encodes them in url safe base64\n */\nexport const generateState = () => generateRandomString();\n\n/**\n * Generates code verifier\n *\n * @link [Client Creates a Code Verifier](https://datatracker.ietf.org/doc/html/rfc7636#section-4.1)\n */\nexport const generateCodeVerifier = () => generateRandomString();\n\n/**\n * Calculates the S256 PKCE code challenge for an arbitrary code verifier and encodes it in url safe base64\n *\n * @param {String} codeVerifier Code verifier to calculate the S256 code challenge for\n * @link [Client Creates the Code Challenge](https://datatracker.ietf.org/doc/html/rfc7636#section-4.2)\n */\nexport const generateCodeChallenge = async (codeVerifier: string): Promise<string> => {\n const encodedCodeVerifier = new TextEncoder().encode(codeVerifier);\n const hash = createHash('sha256');\n hash.update(encodedCodeVerifier);\n const codeChallenge = hash.digest();\n\n return fromUint8Array(codeChallenge, true);\n};\n"],"names":[],"version":3,"file":"index.js.map","sourceRoot":"../../../"}
|
package/lib/module.d.mts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import BaseClient, { IdTokenClaims, UserInfoResponse, LogtoConfig, ClientAdapter } from "@logto/client";
|
|
2
|
-
declare module 'http' {
|
|
3
|
-
interface IncomingMessage {
|
|
4
|
-
user: LogtoContext;
|
|
5
|
-
}
|
|
6
|
-
}
|
|
7
|
-
export type LogtoContext = {
|
|
8
|
-
isAuthenticated: boolean;
|
|
9
|
-
claims?: IdTokenClaims;
|
|
10
|
-
accessToken?: string;
|
|
11
|
-
userInfo?: UserInfoResponse;
|
|
12
|
-
};
|
|
13
|
-
export type GetContextParameters = {
|
|
14
|
-
fetchUserInfo?: boolean;
|
|
15
|
-
getAccessToken?: boolean;
|
|
16
|
-
resource?: string;
|
|
17
|
-
};
|
|
18
|
-
export type { IdTokenClaims, LogtoErrorCode, LogtoConfig, LogtoClientErrorCode, Storage, StorageKey, InteractionMode, } from '@logto/client';
|
|
19
|
-
export { LogtoError, OidcError, Prompt, LogtoRequestError, LogtoClientError, ReservedScope, UserScope, } from '@logto/client';
|
|
20
|
-
export default class LogtoClient extends BaseClient {
|
|
21
|
-
constructor(config: LogtoConfig, adapter: Pick<ClientAdapter, 'navigate' | 'storage'>);
|
|
22
|
-
getContext: ({ getAccessToken, resource, fetchUserInfo, }?: GetContextParameters) => Promise<LogtoContext>;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
//# sourceMappingURL=index.d.ts.map
|
package/lib/module.mjs
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
import {Buffer as $jetHk$Buffer} from "buffer";
|
|
2
|
-
import $jetHk$logtoclient, {createRequester as $jetHk$createRequester, LogtoError as $0aca7ca5e7992ee0$re_export$LogtoError, OidcError as $0aca7ca5e7992ee0$re_export$OidcError, Prompt as $0aca7ca5e7992ee0$re_export$Prompt, LogtoRequestError as $0aca7ca5e7992ee0$re_export$LogtoRequestError, LogtoClientError as $0aca7ca5e7992ee0$re_export$LogtoClientError, ReservedScope as $0aca7ca5e7992ee0$re_export$ReservedScope, UserScope as $0aca7ca5e7992ee0$re_export$UserScope} from "@logto/client";
|
|
3
|
-
import {conditional as $jetHk$conditional} from "@silverhand/essentials";
|
|
4
|
-
import $jetHk$nodefetch from "node-fetch";
|
|
5
|
-
import {randomFillSync as $jetHk$randomFillSync, createHash as $jetHk$createHash} from "crypto";
|
|
6
|
-
import {fromUint8Array as $jetHk$fromUint8Array} from "js-base64";
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
/** @link [Proof Key for Code Exchange by OAuth Public Clients](https://datatracker.ietf.org/doc/html/rfc7636) */
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* @param length The length of the raw random data.
|
|
15
|
-
*/ const $91cf1946314e24ae$var$generateRandomString = (length = 64)=>(0, $jetHk$fromUint8Array)((0, $jetHk$randomFillSync)(new Uint8Array(length)), true);
|
|
16
|
-
const $91cf1946314e24ae$export$9ccd2716e53a229b = ()=>$91cf1946314e24ae$var$generateRandomString();
|
|
17
|
-
const $91cf1946314e24ae$export$cf1891f923f5943a = ()=>$91cf1946314e24ae$var$generateRandomString();
|
|
18
|
-
const $91cf1946314e24ae$export$414b01b1f867308a = async (codeVerifier)=>{
|
|
19
|
-
const encodedCodeVerifier = new TextEncoder().encode(codeVerifier);
|
|
20
|
-
const hash = (0, $jetHk$createHash)("sha256");
|
|
21
|
-
hash.update(encodedCodeVerifier);
|
|
22
|
-
const codeChallenge = hash.digest();
|
|
23
|
-
return (0, $jetHk$fromUint8Array)(codeChallenge, true);
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
var $0aca7ca5e7992ee0$require$Buffer = $jetHk$Buffer;
|
|
30
|
-
class $0aca7ca5e7992ee0$export$2e2bcd8739ae039 extends (0, $jetHk$logtoclient) {
|
|
31
|
-
constructor(config, adapter){
|
|
32
|
-
super(config, {
|
|
33
|
-
...adapter,
|
|
34
|
-
requester: (0, $jetHk$createRequester)(config.appSecret ? async (...args)=>{
|
|
35
|
-
const [input, init] = args;
|
|
36
|
-
return (0, $jetHk$nodefetch)(input, {
|
|
37
|
-
...init,
|
|
38
|
-
headers: {
|
|
39
|
-
Authorization: `basic ${$0aca7ca5e7992ee0$require$Buffer.from(// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
40
|
-
`${config.appId}:${config.appSecret}`, "utf8").toString("base64")}`,
|
|
41
|
-
...init?.headers
|
|
42
|
-
}
|
|
43
|
-
});
|
|
44
|
-
} : (0, $jetHk$nodefetch)),
|
|
45
|
-
generateCodeChallenge: $91cf1946314e24ae$export$414b01b1f867308a,
|
|
46
|
-
generateCodeVerifier: $91cf1946314e24ae$export$cf1891f923f5943a,
|
|
47
|
-
generateState: $91cf1946314e24ae$export$9ccd2716e53a229b
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
getContext = async ({ getAccessToken: getAccessToken , resource: resource , fetchUserInfo: fetchUserInfo } = {})=>{
|
|
51
|
-
const isAuthenticated = await this.isAuthenticated();
|
|
52
|
-
if (!isAuthenticated) return {
|
|
53
|
-
isAuthenticated: isAuthenticated
|
|
54
|
-
};
|
|
55
|
-
const claims = await this.getIdTokenClaims();
|
|
56
|
-
if (!getAccessToken) return {
|
|
57
|
-
isAuthenticated: isAuthenticated,
|
|
58
|
-
claims: claims,
|
|
59
|
-
userInfo: (0, $jetHk$conditional)(fetchUserInfo && await this.fetchUserInfo())
|
|
60
|
-
};
|
|
61
|
-
try {
|
|
62
|
-
const accessToken = await this.getAccessToken(resource);
|
|
63
|
-
return {
|
|
64
|
-
isAuthenticated: isAuthenticated,
|
|
65
|
-
claims: await this.getIdTokenClaims(),
|
|
66
|
-
userInfo: (0, $jetHk$conditional)(fetchUserInfo && await this.fetchUserInfo()),
|
|
67
|
-
accessToken: accessToken
|
|
68
|
-
};
|
|
69
|
-
} catch {
|
|
70
|
-
return {
|
|
71
|
-
isAuthenticated: false
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
export {$0aca7ca5e7992ee0$export$2e2bcd8739ae039 as default, $0aca7ca5e7992ee0$re_export$LogtoError as LogtoError, $0aca7ca5e7992ee0$re_export$OidcError as OidcError, $0aca7ca5e7992ee0$re_export$Prompt as Prompt, $0aca7ca5e7992ee0$re_export$LogtoRequestError as LogtoRequestError, $0aca7ca5e7992ee0$re_export$LogtoClientError as LogtoClientError, $0aca7ca5e7992ee0$re_export$ReservedScope as ReservedScope, $0aca7ca5e7992ee0$re_export$UserScope as UserScope};
|
|
79
|
-
//# sourceMappingURL=module.mjs.map
|
package/lib/module.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"mappings":";;;;;;;AAAA;;;ACAA,+GAA+G,GAC/G;;AAIA;;CAEC,GACD,MAAM,6CAAuB,CAAC,SAAS,EAAE,GACvC,CAAA,GAAA,qBAAa,EAAE,CAAA,GAAA,qBAAa,EAAE,IAAI,WAAW,UAAU,IAAI;AAKtD,MAAM,4CAAgB,IAAM;AAO5B,MAAM,4CAAuB,IAAM;AAQnC,MAAM,4CAAwB,OAAO,eAA0C;IACpF,MAAM,sBAAsB,IAAI,cAAc,MAAM,CAAC;IACrD,MAAM,OAAO,CAAA,GAAA,iBAAS,EAAE;IACxB,KAAK,MAAM,CAAC;IACZ,MAAM,gBAAgB,KAAK,MAAM;IAEjC,OAAO,CAAA,GAAA,qBAAc,AAAD,EAAE,eAAe,IAAI;AAC3C;;;;;;ADNe,uDAA0B,CAAA,GAAA,kBAAS;IAChD,YAAY,MAAmB,EAAE,OAAoD,CAAE;QACrF,KAAK,CAAC,QAAQ;YACZ,GAAG,OAAO;YACV,WAAW,CAAA,GAAA,sBAAc,EACvB,OAAO,SAAS,GACZ,OAAO,GAAG,OAAmC;gBAC3C,MAAM,CAAC,OAAO,KAAK,GAAG;gBAEtB,OAAO,CAAA,GAAA,gBAAK,AAAD,EAAE,OAAO;oBAClB,GAAG,IAAI;oBACP,SAAS;wBACP,eAAe,CAAC,MAAM,EAAE,iCAAO,IAAI,CACjC,4EAA4E;wBAC5E,CAAC,EAAE,OAAO,KAAK,CAAC,CAAC,EAAE,OAAO,SAAS,CAAC,CAAC,EACrC,QACA,QAAQ,CAAC,UAAU,CAAC;wBACtB,GAAG,MAAM,OAAO;oBAClB;gBACF;YACF,IACA,CAAA,GAAA,gBAAI,CAAC;mCAEX;kCACA;2BACA;QACF;IACF;IAEA,aAAa,OAAO,kBAClB,eAAc,YACd,SAAQ,iBACR,cAAa,EACQ,GAAG,CAAC,CAAC,GAA4B;QACtD,MAAM,kBAAkB,MAAM,IAAI,CAAC,eAAe;QAElD,IAAI,CAAC,iBACH,OAAO;6BACL;QACF;QAGF,MAAM,SAAS,MAAM,IAAI,CAAC,gBAAgB;QAE1C,IAAI,CAAC,gBACH,OAAO;6BACL;oBACA;YACA,UAAU,CAAA,GAAA,kBAAU,EAAE,iBAAkB,MAAM,IAAI,CAAC,aAAa;QAClE;QAGF,IAAI;YACF,MAAM,cAAc,MAAM,IAAI,CAAC,cAAc,CAAC;YAE9C,OAAO;iCACL;gBACA,QAAQ,MAAM,IAAI,CAAC,gBAAgB;gBACnC,UAAU,CAAA,GAAA,kBAAU,EAAE,iBAAkB,MAAM,IAAI,CAAC,aAAa;6BAChE;YACF;QACF,EAAE,OAAM;YACN,OAAO;gBACL,iBAAiB,KAAK;YACxB;QACF;IACF,EAAE;AACJ","sources":["packages/node/src/index.ts","packages/node/src/utils/generators.ts"],"sourcesContent":["import type { LogtoConfig, ClientAdapter } from '@logto/client';\nimport BaseClient, { createRequester } from '@logto/client';\nimport { conditional } from '@silverhand/essentials';\nimport fetch from 'node-fetch';\n\nimport type { GetContextParameters, LogtoContext } from './types';\nimport { generateCodeChallenge, generateCodeVerifier, generateState } from './utils/generators';\n\nexport type { LogtoContext, GetContextParameters } from './types';\n\nexport type {\n IdTokenClaims,\n LogtoErrorCode,\n LogtoConfig,\n LogtoClientErrorCode,\n Storage,\n StorageKey,\n InteractionMode,\n} from '@logto/client';\n\nexport {\n LogtoError,\n OidcError,\n Prompt,\n LogtoRequestError,\n LogtoClientError,\n ReservedScope,\n UserScope,\n} from '@logto/client';\n\nexport default class LogtoClient extends BaseClient {\n constructor(config: LogtoConfig, adapter: Pick<ClientAdapter, 'navigate' | 'storage'>) {\n super(config, {\n ...adapter,\n requester: createRequester(\n config.appSecret\n ? async (...args: Parameters<typeof fetch>) => {\n const [input, init] = args;\n\n return fetch(input, {\n ...init,\n headers: {\n Authorization: `basic ${Buffer.from(\n // eslint-disable-next-line @typescript-eslint/restrict-template-expressions\n `${config.appId}:${config.appSecret}`,\n 'utf8'\n ).toString('base64')}`,\n ...init?.headers,\n },\n });\n }\n : fetch\n ),\n generateCodeChallenge,\n generateCodeVerifier,\n generateState,\n });\n }\n\n getContext = async ({\n getAccessToken,\n resource,\n fetchUserInfo,\n }: GetContextParameters = {}): Promise<LogtoContext> => {\n const isAuthenticated = await this.isAuthenticated();\n\n if (!isAuthenticated) {\n return {\n isAuthenticated,\n };\n }\n\n const claims = await this.getIdTokenClaims();\n\n if (!getAccessToken) {\n return {\n isAuthenticated,\n claims,\n userInfo: conditional(fetchUserInfo && (await this.fetchUserInfo())),\n };\n }\n\n try {\n const accessToken = await this.getAccessToken(resource);\n\n return {\n isAuthenticated,\n claims: await this.getIdTokenClaims(),\n userInfo: conditional(fetchUserInfo && (await this.fetchUserInfo())),\n accessToken,\n };\n } catch {\n return {\n isAuthenticated: false,\n };\n }\n };\n}\n","/** @link [Proof Key for Code Exchange by OAuth Public Clients](https://datatracker.ietf.org/doc/html/rfc7636) */\nimport { randomFillSync, createHash } from 'crypto';\n\nimport { fromUint8Array } from 'js-base64';\n\n/**\n * @param length The length of the raw random data.\n */\nconst generateRandomString = (length = 64) =>\n fromUint8Array(randomFillSync(new Uint8Array(length)), true);\n\n/**\n * Generates random string for state and encodes them in url safe base64\n */\nexport const generateState = () => generateRandomString();\n\n/**\n * Generates code verifier\n *\n * @link [Client Creates a Code Verifier](https://datatracker.ietf.org/doc/html/rfc7636#section-4.1)\n */\nexport const generateCodeVerifier = () => generateRandomString();\n\n/**\n * Calculates the S256 PKCE code challenge for an arbitrary code verifier and encodes it in url safe base64\n *\n * @param {String} codeVerifier Code verifier to calculate the S256 code challenge for\n * @link [Client Creates the Code Challenge](https://datatracker.ietf.org/doc/html/rfc7636#section-4.2)\n */\nexport const generateCodeChallenge = async (codeVerifier: string): Promise<string> => {\n const encodedCodeVerifier = new TextEncoder().encode(codeVerifier);\n const hash = createHash('sha256');\n hash.update(encodedCodeVerifier);\n const codeChallenge = hash.digest();\n\n return fromUint8Array(codeChallenge, true);\n};\n"],"names":[],"version":3,"file":"module.mjs.map"}
|