@descope/node-sdk 1.0.4-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/LICENSE +21 -0
- package/README.md +5 -0
- package/dist/constants.d.ts +2 -0
- package/dist/constants.js +3 -0
- package/dist/helpers.d.ts +4 -0
- package/dist/helpers.js +61 -0
- package/dist/index.d.ts +251 -0
- package/dist/index.js +72 -0
- package/dist/index.test.d.ts +1 -0
- package/dist/types.d.ts +10 -0
- package/dist/types.js +1 -0
- package/package.json +89 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Descope
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { SdkResponse } from '@descope/core-js-sdk';
|
|
2
|
+
export declare const withCookie: <T extends any[], U extends Promise<SdkResponse>>(fn: (...args: T) => U) => (...args: T) => Promise<SdkResponse>;
|
|
3
|
+
export declare const wrapWith: <T extends Record<string, any>>(obj: T, path: string | string[], wrappingFn: Function) => void;
|
|
4
|
+
export declare const bulkWrapWith: (obj: Parameters<typeof wrapWith>[0], paths: string[], wrappingFn: Parameters<typeof wrapWith>[2]) => void;
|
package/dist/helpers.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { refreshTokenCookieName, sessionTokenCookieName } from "./constants.js";
|
|
2
|
+
|
|
3
|
+
const generateCookie = (name, value) => `${name}=${value};`;
|
|
4
|
+
|
|
5
|
+
const getCookieValue = (cookie, name) => {
|
|
6
|
+
const match = cookie === null || cookie === void 0 ? void 0 : cookie.match(RegExp(`(?:^|;\\s*)${name}=([^;]*)`));
|
|
7
|
+
return match ? match[1] : null;
|
|
8
|
+
}; // eslint-disable-next-line import/prefer-default-export
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
export const withCookie = fn => async (...args) => {
|
|
12
|
+
const resp = await fn(...args); // eslint-disable-next-line prefer-const
|
|
13
|
+
|
|
14
|
+
let {
|
|
15
|
+
sessionJwt,
|
|
16
|
+
refreshJwt
|
|
17
|
+
} = resp.data;
|
|
18
|
+
let cookie = generateCookie(sessionTokenCookieName, sessionJwt);
|
|
19
|
+
|
|
20
|
+
if (!refreshJwt) {
|
|
21
|
+
var _resp$response;
|
|
22
|
+
|
|
23
|
+
cookie += ((_resp$response = resp.response) === null || _resp$response === void 0 ? void 0 : _resp$response.headers.get('set-cookie')) || '';
|
|
24
|
+
refreshJwt = getCookieValue(cookie, refreshTokenCookieName);
|
|
25
|
+
} else {
|
|
26
|
+
cookie += generateCookie(refreshTokenCookieName, refreshJwt);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return { ...resp,
|
|
30
|
+
data: { ...resp.data,
|
|
31
|
+
refreshJwt,
|
|
32
|
+
cookie
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
export const wrapWith = (obj, path, wrappingFn // eslint-disable-next-line consistent-return
|
|
37
|
+
) => {
|
|
38
|
+
if (!obj) return obj;
|
|
39
|
+
const pathSections = typeof path === 'string' ? path.split('.') : path;
|
|
40
|
+
const section = pathSections.shift() || '';
|
|
41
|
+
|
|
42
|
+
if (pathSections.length === 0 || section === '*') {
|
|
43
|
+
const wrap = key => {
|
|
44
|
+
if (key && typeof obj[key] === 'function') {
|
|
45
|
+
// eslint-disable-next-line no-param-reassign
|
|
46
|
+
obj[key] = wrappingFn(obj[key]);
|
|
47
|
+
} else {
|
|
48
|
+
throw Error(`cannot wrap value at key "${key.toString()}"`);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
if (section === '*') {
|
|
53
|
+
Object.keys(obj).forEach(wrap);
|
|
54
|
+
} else {
|
|
55
|
+
wrap(section);
|
|
56
|
+
}
|
|
57
|
+
} else {
|
|
58
|
+
wrapWith(obj[section], pathSections, wrappingFn);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
export const bulkWrapWith = (obj, paths, wrappingFn) => paths.forEach(path => wrapWith(obj, path, wrappingFn));
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
import { KeyLike, JWTHeaderParameters } from 'jose';
|
|
2
|
+
import { AuthenticationInfo } from './types';
|
|
3
|
+
export type { DeliveryMethod, OAuthProvider } from '@descope/core-js-sdk';
|
|
4
|
+
declare const _default: (args_0: {
|
|
5
|
+
projectId: string;
|
|
6
|
+
logger?: {
|
|
7
|
+
error: {
|
|
8
|
+
(...data: any[]): void;
|
|
9
|
+
(message?: any, ...optionalParams: any[]): void;
|
|
10
|
+
};
|
|
11
|
+
debug: {
|
|
12
|
+
(...data: any[]): void;
|
|
13
|
+
(message?: any, ...optionalParams: any[]): void;
|
|
14
|
+
};
|
|
15
|
+
log: {
|
|
16
|
+
(...data: any[]): void;
|
|
17
|
+
(message?: any, ...optionalParams: any[]): void;
|
|
18
|
+
};
|
|
19
|
+
} | undefined;
|
|
20
|
+
baseUrl?: string | undefined;
|
|
21
|
+
}) => {
|
|
22
|
+
getKey(header: JWTHeaderParameters): Promise<KeyLike | Uint8Array>;
|
|
23
|
+
validateToken(token: string): Promise<AuthenticationInfo>;
|
|
24
|
+
validateSession(sessionToken: string, refreshToken: string): Promise<AuthenticationInfo | undefined>;
|
|
25
|
+
otp: {
|
|
26
|
+
verify: {
|
|
27
|
+
email: (identifier: string, code: string) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
28
|
+
sms: (identifier: string, code: string) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
29
|
+
whatsapp: (identifier: string, code: string) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
30
|
+
};
|
|
31
|
+
signIn: {
|
|
32
|
+
email: (identifier: string) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
33
|
+
sms: (identifier: string) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
34
|
+
whatsapp: (identifier: string) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
35
|
+
};
|
|
36
|
+
signUp: {
|
|
37
|
+
email: (identifier: string, user?: {
|
|
38
|
+
email?: string | undefined;
|
|
39
|
+
name?: string | undefined;
|
|
40
|
+
phone?: string | undefined;
|
|
41
|
+
} | undefined) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
42
|
+
sms: (identifier: string, user?: {
|
|
43
|
+
email?: string | undefined;
|
|
44
|
+
name?: string | undefined;
|
|
45
|
+
phone?: string | undefined;
|
|
46
|
+
} | undefined) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
47
|
+
whatsapp: (identifier: string, user?: {
|
|
48
|
+
email?: string | undefined;
|
|
49
|
+
name?: string | undefined;
|
|
50
|
+
phone?: string | undefined;
|
|
51
|
+
} | undefined) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
52
|
+
};
|
|
53
|
+
signUpOrIn: {
|
|
54
|
+
email: (identifier: string) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
55
|
+
sms: (identifier: string) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
56
|
+
whatsapp: (identifier: string) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
57
|
+
};
|
|
58
|
+
update: {
|
|
59
|
+
email: (identifier: string, email: string, token?: string | undefined) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
60
|
+
phone: {
|
|
61
|
+
email: (identifier: string, phone: string) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
62
|
+
sms: (identifier: string, phone: string) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
63
|
+
whatsapp: (identifier: string, phone: string) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
magicLink: {
|
|
68
|
+
verify: (token: string) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
69
|
+
signIn: {
|
|
70
|
+
email: (identifier: string, uri: string) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
71
|
+
sms: (identifier: string, uri: string) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
72
|
+
whatsapp: (identifier: string, uri: string) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
73
|
+
};
|
|
74
|
+
signUp: {
|
|
75
|
+
email: (identifier: string, uri: string, user?: {
|
|
76
|
+
email?: string | undefined;
|
|
77
|
+
name?: string | undefined;
|
|
78
|
+
phone?: string | undefined;
|
|
79
|
+
} | undefined) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
80
|
+
sms: (identifier: string, uri: string, user?: {
|
|
81
|
+
email?: string | undefined;
|
|
82
|
+
name?: string | undefined;
|
|
83
|
+
phone?: string | undefined;
|
|
84
|
+
} | undefined) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
85
|
+
whatsapp: (identifier: string, uri: string, user?: {
|
|
86
|
+
email?: string | undefined;
|
|
87
|
+
name?: string | undefined;
|
|
88
|
+
phone?: string | undefined;
|
|
89
|
+
} | undefined) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
90
|
+
};
|
|
91
|
+
signUpOrIn: {
|
|
92
|
+
email: (identifier: string, uri: string) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
93
|
+
sms: (identifier: string, uri: string) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
94
|
+
whatsapp: (identifier: string, uri: string) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
95
|
+
};
|
|
96
|
+
update: {
|
|
97
|
+
email: (identifier: string, email: string, uri: string, token?: string | undefined) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
98
|
+
phone: {
|
|
99
|
+
email: (identifier: string, phone: string) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
100
|
+
sms: (identifier: string, phone: string) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
101
|
+
whatsapp: (identifier: string, phone: string) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
102
|
+
};
|
|
103
|
+
};
|
|
104
|
+
crossDevice: {
|
|
105
|
+
verify: (token: string) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
106
|
+
signIn: {
|
|
107
|
+
email: (identifier: string, uri: string) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
108
|
+
sms: (identifier: string, uri: string) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
109
|
+
whatsapp: (identifier: string, uri: string) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
110
|
+
};
|
|
111
|
+
signUpOrIn: {
|
|
112
|
+
email: (identifier: string, uri: string) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
113
|
+
sms: (identifier: string, uri: string) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
114
|
+
whatsapp: (identifier: string, uri: string) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
115
|
+
};
|
|
116
|
+
signUp: {
|
|
117
|
+
email: (identifier: string, uri: string, user?: {
|
|
118
|
+
email?: string | undefined;
|
|
119
|
+
name?: string | undefined;
|
|
120
|
+
phone?: string | undefined;
|
|
121
|
+
} | undefined) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
122
|
+
sms: (identifier: string, uri: string, user?: {
|
|
123
|
+
email?: string | undefined;
|
|
124
|
+
name?: string | undefined;
|
|
125
|
+
phone?: string | undefined;
|
|
126
|
+
} | undefined) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
127
|
+
whatsapp: (identifier: string, uri: string, user?: {
|
|
128
|
+
email?: string | undefined;
|
|
129
|
+
name?: string | undefined;
|
|
130
|
+
phone?: string | undefined;
|
|
131
|
+
} | undefined) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
132
|
+
};
|
|
133
|
+
waitForSession: (pendingRef: string, config?: {
|
|
134
|
+
pollingIntervalMs: number;
|
|
135
|
+
timeoutMs: number;
|
|
136
|
+
} | undefined) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
137
|
+
update: {
|
|
138
|
+
email: (identifier: string, email: string, uri: string, token?: string | undefined) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
139
|
+
phone: {
|
|
140
|
+
email: (identifier: string, phone: string) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
141
|
+
sms: (identifier: string, phone: string) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
142
|
+
whatsapp: (identifier: string, phone: string) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
143
|
+
};
|
|
144
|
+
};
|
|
145
|
+
};
|
|
146
|
+
};
|
|
147
|
+
oauth: {
|
|
148
|
+
exchange: (code: string) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
149
|
+
start: {
|
|
150
|
+
facebook: <B extends {
|
|
151
|
+
redirect: boolean;
|
|
152
|
+
}>(config?: B | undefined) => Promise<B extends {
|
|
153
|
+
redirect: true;
|
|
154
|
+
} ? undefined : import("@descope/core-js-sdk").SdkResponse>;
|
|
155
|
+
github: <B_1 extends {
|
|
156
|
+
redirect: boolean;
|
|
157
|
+
}>(config?: B_1 | undefined) => Promise<B_1 extends {
|
|
158
|
+
redirect: true;
|
|
159
|
+
} ? undefined : import("@descope/core-js-sdk").SdkResponse>;
|
|
160
|
+
google: <B_2 extends {
|
|
161
|
+
redirect: boolean;
|
|
162
|
+
}>(config?: B_2 | undefined) => Promise<B_2 extends {
|
|
163
|
+
redirect: true;
|
|
164
|
+
} ? undefined : import("@descope/core-js-sdk").SdkResponse>;
|
|
165
|
+
microsoft: <B_3 extends {
|
|
166
|
+
redirect: boolean;
|
|
167
|
+
}>(config?: B_3 | undefined) => Promise<B_3 extends {
|
|
168
|
+
redirect: true;
|
|
169
|
+
} ? undefined : import("@descope/core-js-sdk").SdkResponse>;
|
|
170
|
+
gitlab: <B_4 extends {
|
|
171
|
+
redirect: boolean;
|
|
172
|
+
}>(config?: B_4 | undefined) => Promise<B_4 extends {
|
|
173
|
+
redirect: true;
|
|
174
|
+
} ? undefined : import("@descope/core-js-sdk").SdkResponse>;
|
|
175
|
+
apple: <B_5 extends {
|
|
176
|
+
redirect: boolean;
|
|
177
|
+
}>(config?: B_5 | undefined) => Promise<B_5 extends {
|
|
178
|
+
redirect: true;
|
|
179
|
+
} ? undefined : import("@descope/core-js-sdk").SdkResponse>;
|
|
180
|
+
};
|
|
181
|
+
};
|
|
182
|
+
saml: {
|
|
183
|
+
exchange: (code: string) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
184
|
+
start: <B_1 extends {
|
|
185
|
+
redirect: boolean;
|
|
186
|
+
}>(tenantNameOrEmail: string, config?: B_1 | undefined) => Promise<B_1 extends {
|
|
187
|
+
redirect: true;
|
|
188
|
+
} ? undefined : import("@descope/core-js-sdk").SdkResponse>;
|
|
189
|
+
};
|
|
190
|
+
totp: {
|
|
191
|
+
signUp: (identifier: string, user?: {
|
|
192
|
+
email?: string | undefined;
|
|
193
|
+
name?: string | undefined;
|
|
194
|
+
phone?: string | undefined;
|
|
195
|
+
} | undefined) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
196
|
+
verify: (identifier: string, code: string) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
197
|
+
update: (identifier: string, token?: string | undefined) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
198
|
+
};
|
|
199
|
+
webauthn: {
|
|
200
|
+
signUp: {
|
|
201
|
+
start: (identifier: string, origin: string, name: string) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
202
|
+
finish: (transactionId: string, response: string) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
203
|
+
};
|
|
204
|
+
signIn: {
|
|
205
|
+
start: (identifier: string, origin: string) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
206
|
+
finish: (transactionId: string, response: string) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
207
|
+
};
|
|
208
|
+
add: {
|
|
209
|
+
start: (identifier: string, origin: string, token: string) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
210
|
+
finish: (transactionId: string, response: string) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
211
|
+
};
|
|
212
|
+
};
|
|
213
|
+
flow: {
|
|
214
|
+
start: (flowId: string) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
215
|
+
next: (executionId: string, stepId: string, actionId: string, input?: Record<string, FormDataEntryValue> | undefined) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
216
|
+
};
|
|
217
|
+
refresh: (token?: string | undefined) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
218
|
+
logout: (token?: string | undefined) => Promise<import("@descope/core-js-sdk").SdkResponse>;
|
|
219
|
+
isJwtExpired: (token: string) => boolean;
|
|
220
|
+
httpClient: {
|
|
221
|
+
get: (path: string, config?: {
|
|
222
|
+
headers?: HeadersInit | undefined;
|
|
223
|
+
queryParams?: {
|
|
224
|
+
[key: string]: string;
|
|
225
|
+
} | undefined;
|
|
226
|
+
token?: string | undefined;
|
|
227
|
+
} | undefined) => Promise<globalThis.Response>;
|
|
228
|
+
post: (path: string, body?: any, config?: {
|
|
229
|
+
headers?: HeadersInit | undefined;
|
|
230
|
+
queryParams?: {
|
|
231
|
+
[key: string]: string;
|
|
232
|
+
} | undefined;
|
|
233
|
+
token?: string | undefined;
|
|
234
|
+
} | undefined) => Promise<globalThis.Response>;
|
|
235
|
+
put: (path: string, body?: any, config?: {
|
|
236
|
+
headers?: HeadersInit | undefined;
|
|
237
|
+
queryParams?: {
|
|
238
|
+
[key: string]: string;
|
|
239
|
+
} | undefined;
|
|
240
|
+
token?: string | undefined;
|
|
241
|
+
} | undefined) => Promise<globalThis.Response>;
|
|
242
|
+
delete: (path: string, body?: any, config?: {
|
|
243
|
+
headers?: HeadersInit | undefined;
|
|
244
|
+
queryParams?: {
|
|
245
|
+
[key: string]: string;
|
|
246
|
+
} | undefined;
|
|
247
|
+
token?: string | undefined;
|
|
248
|
+
} | undefined) => Promise<globalThis.Response>;
|
|
249
|
+
};
|
|
250
|
+
};
|
|
251
|
+
export default _default;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import createSdk from '@descope/core-js-sdk';
|
|
2
|
+
import { jwtVerify, importJWK } from 'jose';
|
|
3
|
+
import fetch, { Headers, Response, Request } from 'node-fetch';
|
|
4
|
+
import { bulkWrapWith, withCookie } from "./helpers.js";
|
|
5
|
+
|
|
6
|
+
if (!globalThis.fetch) {
|
|
7
|
+
// @ts-ignore
|
|
8
|
+
globalThis.fetch = fetch; // @ts-ignore
|
|
9
|
+
|
|
10
|
+
globalThis.Headers = Headers; // @ts-ignore
|
|
11
|
+
|
|
12
|
+
globalThis.Request = Request; // @ts-ignore
|
|
13
|
+
|
|
14
|
+
globalThis.Response = Response;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export default ((...args) => {
|
|
18
|
+
const sdk = createSdk(...args);
|
|
19
|
+
bulkWrapWith(sdk, ['otp.verify.*', 'magicLink.verify', 'magicLink.crossDevice.signUp.*', 'magicLink.crossDevice.signIn.*', 'oauth.exchange', 'saml.exchange', 'totp.verify', 'webauthn.signIn.finish', 'webauthn.signUp.finish', 'refresh'], withCookie);
|
|
20
|
+
const {
|
|
21
|
+
projectId,
|
|
22
|
+
logger
|
|
23
|
+
} = args[0];
|
|
24
|
+
const keys = {};
|
|
25
|
+
|
|
26
|
+
const fetchKeys = async () => {
|
|
27
|
+
const publicKeys = (await sdk.httpClient.get(`keys/${projectId}`).then(resp => resp.json())) || [];
|
|
28
|
+
const kidJwksPairs = await Promise.all(publicKeys.map(async key => [key.kid, await importJWK(key)]));
|
|
29
|
+
return kidJwksPairs.reduce((acc, [kid, jwk]) => kid ? { ...acc,
|
|
30
|
+
[kid.toString()]: jwk
|
|
31
|
+
} : acc, {});
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
return { ...sdk,
|
|
35
|
+
|
|
36
|
+
async getKey(header) {
|
|
37
|
+
if (!(header !== null && header !== void 0 && header.kid)) throw Error('header.kid must not be empty');
|
|
38
|
+
if (keys[header.kid]) return keys[header.kid]; // do we need to fetch once or every time?
|
|
39
|
+
|
|
40
|
+
Object.assign(keys, await fetchKeys());
|
|
41
|
+
if (!keys[header.kid]) throw Error('failed to fetch matching key');
|
|
42
|
+
return keys[header.kid];
|
|
43
|
+
},
|
|
44
|
+
|
|
45
|
+
async validateToken(token) {
|
|
46
|
+
const res = await jwtVerify(token, this.getKey, {
|
|
47
|
+
algorithms: ['ES384']
|
|
48
|
+
});
|
|
49
|
+
return {
|
|
50
|
+
token: res.payload
|
|
51
|
+
};
|
|
52
|
+
},
|
|
53
|
+
|
|
54
|
+
async validateSession(sessionToken, refreshToken) {
|
|
55
|
+
if (!sessionToken) throw Error('session token must not be empty');
|
|
56
|
+
|
|
57
|
+
try {
|
|
58
|
+
const token = await this.validateToken(sessionToken);
|
|
59
|
+
return token;
|
|
60
|
+
} catch (error) {
|
|
61
|
+
try {
|
|
62
|
+
await this.validateToken(refreshToken);
|
|
63
|
+
return (await this.refresh(refreshToken)).data;
|
|
64
|
+
} catch (refreshTokenErr) {
|
|
65
|
+
logger === null || logger === void 0 ? void 0 : logger.error('failed to validate refresh token', refreshTokenErr);
|
|
66
|
+
throw Error('could not validate tokens');
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
};
|
|
72
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/types.d.ts
ADDED
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@descope/node-sdk",
|
|
3
|
+
"version": "1.0.4-alpha.0",
|
|
4
|
+
"description": "Node.js library used to integrate with Descope",
|
|
5
|
+
"typings": "./dist/index.d.ts",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
"require": "./dist/index.js",
|
|
9
|
+
"import": "./module.mjs"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"dist/**/*"
|
|
13
|
+
],
|
|
14
|
+
"type": "module",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git://github.com/descope/node-sdk.git"
|
|
18
|
+
},
|
|
19
|
+
"engines": {
|
|
20
|
+
"node": ">= 14.0.0"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "rm -rf dist types && babel lib --out-dir dist --extensions '.ts' && tsc --declaration --outDir dist --emitDeclarationOnly",
|
|
24
|
+
"format": "prettier --write lib",
|
|
25
|
+
"check-format": "prettier --check lib",
|
|
26
|
+
"size": "npm run build && size-limit",
|
|
27
|
+
"lint": "eslint lib/**/*.ts --fix",
|
|
28
|
+
"leaks": "bash ./scripts/gitleaks/gitleaks.sh",
|
|
29
|
+
"test": "jest --forceExit --config jest.config.ts --coverage --ci --detectOpenHandles .*test.*",
|
|
30
|
+
"prepublishOnly": "npm run build"
|
|
31
|
+
},
|
|
32
|
+
"author": "Descope",
|
|
33
|
+
"bugs": {
|
|
34
|
+
"url": "git://github.com/descope/node-sdk/issues"
|
|
35
|
+
},
|
|
36
|
+
"license": "MIT",
|
|
37
|
+
"keywords": [
|
|
38
|
+
"descope",
|
|
39
|
+
"authentication"
|
|
40
|
+
],
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@babel/cli": "^7.17.10",
|
|
43
|
+
"@babel/core": "^7.17.12",
|
|
44
|
+
"@babel/preset-env": "^7.17.12",
|
|
45
|
+
"@babel/preset-typescript": "^7.17.12",
|
|
46
|
+
"@size-limit/preset-small-lib": "^8.0.0",
|
|
47
|
+
"@types/jest": "^28.0.0",
|
|
48
|
+
"@types/jsonwebtoken": "^8.5.8",
|
|
49
|
+
"@types/node": "^15.14.9",
|
|
50
|
+
"@types/node-fetch": "^2.6.1",
|
|
51
|
+
"@typescript-eslint/eslint-plugin": "^5.25.0",
|
|
52
|
+
"@typescript-eslint/parser": "^5.27.0",
|
|
53
|
+
"babel-plugin-add-import-extension": "^1.6.0",
|
|
54
|
+
"eslint": "^8.15.0",
|
|
55
|
+
"eslint-config-airbnb-base": "^15.0.0",
|
|
56
|
+
"eslint-config-airbnb-typescript": "^17.0.0",
|
|
57
|
+
"eslint-config-prettier": "^8.5.0",
|
|
58
|
+
"eslint-import-resolver-typescript": "^3.0.0",
|
|
59
|
+
"eslint-plugin-import": "^2.26.0",
|
|
60
|
+
"eslint-plugin-jest": "^26.4.6",
|
|
61
|
+
"eslint-plugin-jest-dom": "^4.0.2",
|
|
62
|
+
"eslint-plugin-jest-formatting": "^3.1.0",
|
|
63
|
+
"eslint-plugin-no-only-tests": "^3.0.0",
|
|
64
|
+
"eslint-plugin-prefer-arrow": "^1.2.3",
|
|
65
|
+
"eslint-plugin-prettier": "^4.0.0",
|
|
66
|
+
"husky": "^8.0.1",
|
|
67
|
+
"jest": "^28.1.0",
|
|
68
|
+
"jsdoc": "^3.6.10",
|
|
69
|
+
"nock": "^13.2.4",
|
|
70
|
+
"prettier": "2.6.2",
|
|
71
|
+
"ts-jest": "^28.0.3",
|
|
72
|
+
"ts-node": "^10.8.2",
|
|
73
|
+
"typescript": "^4.6.4"
|
|
74
|
+
},
|
|
75
|
+
"size-limit": [
|
|
76
|
+
{
|
|
77
|
+
"limit": "100 kb",
|
|
78
|
+
"path": "dist/*",
|
|
79
|
+
"ignore": [
|
|
80
|
+
"node:*"
|
|
81
|
+
]
|
|
82
|
+
}
|
|
83
|
+
],
|
|
84
|
+
"dependencies": {
|
|
85
|
+
"@descope/core-js-sdk": "0.0.41-alpha.6",
|
|
86
|
+
"jose": "4.8.1",
|
|
87
|
+
"node-fetch": "2.6.7"
|
|
88
|
+
}
|
|
89
|
+
}
|