@bitplanet/deva-sdk 0.8.2 → 0.8.4
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/dist/index.es.js +154 -130
- package/package.json +1 -1
package/dist/index.es.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import { jsx as
|
|
2
|
-
import { useState as
|
|
3
|
-
import { f as
|
|
4
|
-
const
|
|
1
|
+
import { jsx as re } from "react/jsx-runtime";
|
|
2
|
+
import { useState as C, useCallback as l, useEffect as y, useLayoutEffect as ne, useRef as $, useContext as te } from "react";
|
|
3
|
+
import { f as W, D as H, u as Y, e as G } from "./config-CL6PuR8K.js";
|
|
4
|
+
const oe = () => {
|
|
5
5
|
const e = new Uint8Array(32);
|
|
6
|
-
return crypto.getRandomValues(e),
|
|
7
|
-
},
|
|
6
|
+
return crypto.getRandomValues(e), q(e);
|
|
7
|
+
}, q = (e) => btoa(String.fromCharCode(...new Uint8Array(e))).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, ""), se = async (e) => {
|
|
8
8
|
const n = new TextEncoder().encode(e), o = await crypto.subtle.digest("SHA-256", n);
|
|
9
|
-
return
|
|
10
|
-
},
|
|
11
|
-
if (!
|
|
9
|
+
return q(o);
|
|
10
|
+
}, ae = (e) => Math.round(Date.now() / 1e3 + Number(e)), B = (e) => Math.round(Date.now()) / 1e3 + 30 >= e, L = typeof window < "u", ce = (e, r) => {
|
|
11
|
+
if (!L)
|
|
12
12
|
return r;
|
|
13
13
|
try {
|
|
14
14
|
const n = sessionStorage.getItem(e);
|
|
@@ -17,21 +17,21 @@ const te = () => {
|
|
|
17
17
|
return console.error(n), r;
|
|
18
18
|
}
|
|
19
19
|
};
|
|
20
|
-
function
|
|
21
|
-
const [n, o] =
|
|
20
|
+
function m(e, r) {
|
|
21
|
+
const [n, o] = C(
|
|
22
22
|
() => ce(e, r)
|
|
23
|
-
), i =
|
|
23
|
+
), i = l(
|
|
24
24
|
(t) => {
|
|
25
25
|
try {
|
|
26
|
-
o(t),
|
|
26
|
+
o(t), L && sessionStorage.setItem(e, JSON.stringify(t));
|
|
27
27
|
} catch (s) {
|
|
28
28
|
console.error(s);
|
|
29
29
|
}
|
|
30
30
|
},
|
|
31
31
|
[e]
|
|
32
32
|
);
|
|
33
|
-
return
|
|
34
|
-
if (!
|
|
33
|
+
return y(() => {
|
|
34
|
+
if (!L)
|
|
35
35
|
return;
|
|
36
36
|
const t = sessionStorage.getItem(e);
|
|
37
37
|
if (t)
|
|
@@ -40,8 +40,8 @@ function h(e, r) {
|
|
|
40
40
|
} catch (s) {
|
|
41
41
|
console.error(s);
|
|
42
42
|
}
|
|
43
|
-
}, []),
|
|
44
|
-
if (!
|
|
43
|
+
}, []), y(() => {
|
|
44
|
+
if (!L)
|
|
45
45
|
return;
|
|
46
46
|
const t = (s) => {
|
|
47
47
|
if (s.storageArea === window.sessionStorage && s.key === e)
|
|
@@ -59,9 +59,9 @@ function h(e, r) {
|
|
|
59
59
|
return window.addEventListener("storage", t, !1), () => window.removeEventListener("storage", t, !1);
|
|
60
60
|
}, [e, r]), [n, i];
|
|
61
61
|
}
|
|
62
|
-
class
|
|
62
|
+
class E extends Error {
|
|
63
63
|
}
|
|
64
|
-
|
|
64
|
+
E.prototype.name = "InvalidTokenError";
|
|
65
65
|
function ie(e) {
|
|
66
66
|
return decodeURIComponent(atob(e).replace(/(.)/g, (r, n) => {
|
|
67
67
|
let o = n.charCodeAt(0).toString(16).toUpperCase();
|
|
@@ -90,29 +90,29 @@ function de(e) {
|
|
|
90
90
|
}
|
|
91
91
|
function ue(e, r) {
|
|
92
92
|
if (typeof e != "string")
|
|
93
|
-
throw new
|
|
93
|
+
throw new E("Invalid token specified: must be a string");
|
|
94
94
|
r || (r = {});
|
|
95
95
|
const n = r.header === !0 ? 0 : 1, o = e.split(".")[n];
|
|
96
96
|
if (typeof o != "string")
|
|
97
|
-
throw new
|
|
97
|
+
throw new E(`Invalid token specified: missing part #${n + 1}`);
|
|
98
98
|
let i;
|
|
99
99
|
try {
|
|
100
100
|
i = de(o);
|
|
101
101
|
} catch (t) {
|
|
102
|
-
throw new
|
|
102
|
+
throw new E(`Invalid token specified: invalid base64 for part #${n + 1} (${t.message})`);
|
|
103
103
|
}
|
|
104
104
|
try {
|
|
105
105
|
return JSON.parse(i);
|
|
106
106
|
} catch (t) {
|
|
107
|
-
throw new
|
|
107
|
+
throw new E(`Invalid token specified: invalid json for part #${n + 1} (${t.message})`);
|
|
108
108
|
}
|
|
109
109
|
}
|
|
110
|
-
const fe = typeof window < "u" ?
|
|
110
|
+
const fe = typeof window < "u" ? ne : y;
|
|
111
111
|
function le(e, r, n = [], o, i) {
|
|
112
|
-
const t =
|
|
112
|
+
const t = $(e);
|
|
113
113
|
fe(() => {
|
|
114
114
|
t.current = e;
|
|
115
|
-
}, n),
|
|
115
|
+
}, n), y(() => {
|
|
116
116
|
if (!r && r !== 0)
|
|
117
117
|
return;
|
|
118
118
|
const s = o(() => {
|
|
@@ -126,15 +126,15 @@ function le(e, r, n = [], o, i) {
|
|
|
126
126
|
function he(e, r, n = []) {
|
|
127
127
|
le(e, r, n, setInterval, clearInterval);
|
|
128
128
|
}
|
|
129
|
-
const
|
|
129
|
+
const U = typeof window < "u" && typeof document < "u", N = (...e) => {
|
|
130
130
|
console.log("[sdk/provider]", ...e);
|
|
131
131
|
}, pe = (e) => {
|
|
132
|
-
const r =
|
|
133
|
-
|
|
132
|
+
const r = G(e), { data: n, error: o, isLoading: i } = Y(
|
|
133
|
+
U ? `${r}/.well-known/openid-configuration` : null
|
|
134
134
|
);
|
|
135
135
|
return { openIdConfig: n, isOpenIdConfigLoading: i, error: o };
|
|
136
136
|
}, ge = (e, r) => {
|
|
137
|
-
const { data: n, error: o, isLoading: i } =
|
|
137
|
+
const { data: n, error: o, isLoading: i } = Y(
|
|
138
138
|
r ? e : !1,
|
|
139
139
|
{
|
|
140
140
|
headers: {
|
|
@@ -147,12 +147,12 @@ const $ = typeof window < "u" && typeof document < "u", A = (...e) => {
|
|
|
147
147
|
isUserInfoLoading: i,
|
|
148
148
|
userInfoError: o
|
|
149
149
|
};
|
|
150
|
-
}, we = (e, r, n) =>
|
|
150
|
+
}, we = (e, r, n) => l(async () => {
|
|
151
151
|
if (!r || !n) {
|
|
152
|
-
|
|
152
|
+
N("no refresh token or token endpoint");
|
|
153
153
|
return;
|
|
154
154
|
}
|
|
155
|
-
return
|
|
155
|
+
return W(n, {
|
|
156
156
|
method: "POST",
|
|
157
157
|
headers: {
|
|
158
158
|
"Content-Type": "application/x-www-form-urlencoded"
|
|
@@ -162,7 +162,7 @@ const $ = typeof window < "u" && typeof document < "u", A = (...e) => {
|
|
|
162
162
|
refresh_token: r
|
|
163
163
|
})
|
|
164
164
|
});
|
|
165
|
-
}, [r, e, n]), me = (e, r, n) =>
|
|
165
|
+
}, [r, e, n]), me = (e, r, n) => l(async () => {
|
|
166
166
|
!n || !e || await fetch(n, {
|
|
167
167
|
method: "POST",
|
|
168
168
|
headers: {
|
|
@@ -180,11 +180,11 @@ function _e({
|
|
|
180
180
|
children: n,
|
|
181
181
|
env: o
|
|
182
182
|
}) {
|
|
183
|
-
const i =
|
|
184
|
-
|
|
185
|
-
const
|
|
186
|
-
|
|
187
|
-
o === "development" && !
|
|
183
|
+
const i = G(o), { openIdConfig: t, isOpenIdConfigLoading: s, error: V } = pe(o);
|
|
184
|
+
V && console.error("[sdk/provider] error fetching openid info: ", V);
|
|
185
|
+
const M = $(!1), J = $(!1);
|
|
186
|
+
y(() => {
|
|
187
|
+
o === "development" && !J.current && (J.current = !0, console.warn(
|
|
188
188
|
`
|
|
189
189
|
========================================
|
|
190
190
|
⚠️ WARNING: DEVELOPMENT ENVIRONMENT ⚠️
|
|
@@ -198,134 +198,158 @@ Please verify your environment settings and use 'staging' or 'production' instea
|
|
|
198
198
|
`
|
|
199
199
|
));
|
|
200
200
|
}, [o]);
|
|
201
|
-
const [
|
|
201
|
+
const [K, g] = m("code_verifier", ""), [x, k] = m(
|
|
202
202
|
"auth_in_progress",
|
|
203
203
|
!1
|
|
204
|
-
), [
|
|
204
|
+
), [Q, S] = m("id_token", null), [u, T] = m(
|
|
205
205
|
"access_token",
|
|
206
206
|
null
|
|
207
|
-
), [
|
|
207
|
+
), [h, R] = m(
|
|
208
208
|
"refresh_token",
|
|
209
209
|
null
|
|
210
|
-
), [
|
|
210
|
+
), [w, b] = m("expires_in", 0), [P, p] = C(!0), v = l(
|
|
211
211
|
(a) => {
|
|
212
|
-
let d =
|
|
213
|
-
|
|
212
|
+
let d = w;
|
|
213
|
+
T(a.access_token), S(a.id_token);
|
|
214
214
|
try {
|
|
215
|
-
const
|
|
216
|
-
d = Math.round(Number(
|
|
217
|
-
} catch (
|
|
215
|
+
const f = ue(a.id_token);
|
|
216
|
+
d = Math.round(Number(f.exp) - Date.now() / 1e3);
|
|
217
|
+
} catch (f) {
|
|
218
218
|
console.warn(
|
|
219
|
-
`[sdk/provider]: Failed to decode idToken: ${
|
|
219
|
+
`[sdk/provider]: Failed to decode idToken: ${f.message}`
|
|
220
220
|
);
|
|
221
221
|
}
|
|
222
222
|
const c = a.expires_in ?? d;
|
|
223
|
-
|
|
223
|
+
b(ae(c)), R(a.refresh_token), g("");
|
|
224
224
|
},
|
|
225
225
|
[
|
|
226
|
-
|
|
227
|
-
k,
|
|
226
|
+
T,
|
|
228
227
|
S,
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
228
|
+
b,
|
|
229
|
+
R,
|
|
230
|
+
g,
|
|
231
|
+
w
|
|
232
232
|
]
|
|
233
|
-
), [
|
|
233
|
+
), [X, O] = C(null), { userInfo: Z } = ge(
|
|
234
234
|
t == null ? void 0 : t.userinfo_endpoint,
|
|
235
|
-
|
|
236
|
-
),
|
|
237
|
-
|
|
235
|
+
P ? null : u
|
|
236
|
+
), _ = l(() => {
|
|
237
|
+
g(""), k(!1), T(""), S(""), b(0), R("");
|
|
238
238
|
}, [
|
|
239
|
-
l,
|
|
240
239
|
g,
|
|
241
|
-
v,
|
|
242
240
|
k,
|
|
241
|
+
T,
|
|
243
242
|
S,
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
243
|
+
b,
|
|
244
|
+
R
|
|
245
|
+
]), j = me(
|
|
246
|
+
u,
|
|
247
247
|
e,
|
|
248
248
|
t == null ? void 0 : t.revocation_endpoint
|
|
249
|
-
),
|
|
250
|
-
await
|
|
251
|
-
}, [
|
|
252
|
-
|
|
249
|
+
), F = l(async () => {
|
|
250
|
+
await j(), _(), O(null);
|
|
251
|
+
}, [_, j]), A = we(
|
|
252
|
+
e,
|
|
253
|
+
h,
|
|
254
|
+
t == null ? void 0 : t.token_endpoint
|
|
255
|
+
), z = l(async () => {
|
|
256
|
+
if (u && !B(w))
|
|
257
|
+
return;
|
|
258
|
+
if (h) {
|
|
259
|
+
if (s)
|
|
260
|
+
return;
|
|
261
|
+
p(!0);
|
|
262
|
+
try {
|
|
263
|
+
const f = await A();
|
|
264
|
+
if (f) {
|
|
265
|
+
v(f);
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
} catch (f) {
|
|
269
|
+
console.error("[sdk/provider]: Error refreshing token", f);
|
|
270
|
+
} finally {
|
|
271
|
+
p(!1);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
if (_(), k(!0), s)
|
|
253
275
|
return;
|
|
254
|
-
const a =
|
|
255
|
-
|
|
276
|
+
const a = oe(), d = await se(a);
|
|
277
|
+
g(a);
|
|
256
278
|
const c = new URL((t == null ? void 0 : t.authorization_endpoint) ?? "");
|
|
257
279
|
c.searchParams.append("client_id", e), c.searchParams.append("redirect_uri", r), c.searchParams.append("response_type", "code"), c.searchParams.append("code_challenge", d), c.searchParams.append("code_challenge_method", "S256"), window.location.assign(c.toString());
|
|
258
280
|
}, [
|
|
281
|
+
u,
|
|
282
|
+
w,
|
|
283
|
+
h,
|
|
284
|
+
A,
|
|
285
|
+
v,
|
|
286
|
+
p,
|
|
259
287
|
e,
|
|
260
288
|
r,
|
|
261
|
-
l,
|
|
262
|
-
m,
|
|
263
289
|
g,
|
|
290
|
+
_,
|
|
291
|
+
k,
|
|
264
292
|
s,
|
|
265
293
|
t
|
|
266
|
-
]),
|
|
267
|
-
e,
|
|
268
|
-
w,
|
|
269
|
-
t == null ? void 0 : t.token_endpoint
|
|
270
|
-
), x = u(
|
|
294
|
+
]), D = l(
|
|
271
295
|
async (a = !1) => {
|
|
272
|
-
if (!
|
|
273
|
-
|
|
296
|
+
if (!u) {
|
|
297
|
+
N("no access token");
|
|
274
298
|
return;
|
|
275
299
|
}
|
|
276
|
-
if (!(!
|
|
277
|
-
if (
|
|
278
|
-
|
|
300
|
+
if (!(!B(w) && !a)) {
|
|
301
|
+
if (P && !a) {
|
|
302
|
+
N("already refreshing");
|
|
279
303
|
return;
|
|
280
304
|
}
|
|
281
|
-
if (!
|
|
282
|
-
|
|
305
|
+
if (!h) {
|
|
306
|
+
N("no refresh token");
|
|
283
307
|
return;
|
|
284
308
|
}
|
|
285
|
-
|
|
309
|
+
p(!0);
|
|
286
310
|
try {
|
|
287
|
-
const d = await
|
|
288
|
-
d &&
|
|
311
|
+
const d = await A();
|
|
312
|
+
d && v(d);
|
|
289
313
|
} catch (d) {
|
|
290
314
|
console.error("[sdk/provider]: Error refreshing token", d);
|
|
291
315
|
} finally {
|
|
292
|
-
|
|
316
|
+
p(!1);
|
|
293
317
|
}
|
|
294
318
|
}
|
|
295
319
|
},
|
|
296
320
|
[
|
|
297
|
-
|
|
321
|
+
u,
|
|
322
|
+
h,
|
|
323
|
+
A,
|
|
324
|
+
v,
|
|
325
|
+
P,
|
|
298
326
|
w,
|
|
299
|
-
|
|
300
|
-
L,
|
|
301
|
-
T,
|
|
302
|
-
E,
|
|
303
|
-
R
|
|
327
|
+
p
|
|
304
328
|
]
|
|
305
|
-
),
|
|
329
|
+
), ee = 1e4 * Math.random();
|
|
306
330
|
he(
|
|
307
331
|
() => {
|
|
308
|
-
|
|
332
|
+
D();
|
|
309
333
|
},
|
|
310
|
-
|
|
311
|
-
[
|
|
312
|
-
),
|
|
334
|
+
U && h ? 1e3 + ee : null,
|
|
335
|
+
[D]
|
|
336
|
+
), y(() => {
|
|
313
337
|
let a = "";
|
|
314
|
-
if (
|
|
315
|
-
if (
|
|
316
|
-
|
|
338
|
+
if (U && !s) {
|
|
339
|
+
if (x) {
|
|
340
|
+
p(!1);
|
|
317
341
|
const d = new URLSearchParams(window.location.search);
|
|
318
342
|
if (a = d.get("code") ?? "", !a) {
|
|
319
343
|
const c = d.get("error_description") ?? "An unknown error occurred";
|
|
320
344
|
console.error(
|
|
321
345
|
`[sdk/provider]: ${c}
|
|
322
346
|
Expected to find a '?code=' parameter in the URL but found none`
|
|
323
|
-
),
|
|
347
|
+
), O(c), _();
|
|
324
348
|
return;
|
|
325
349
|
}
|
|
326
|
-
if (
|
|
350
|
+
if (k(!1), M.current)
|
|
327
351
|
return;
|
|
328
|
-
|
|
352
|
+
M.current = !0, W((t == null ? void 0 : t.token_endpoint) ?? "", {
|
|
329
353
|
method: "POST",
|
|
330
354
|
headers: {
|
|
331
355
|
"Content-Type": "application/x-www-form-urlencoded"
|
|
@@ -334,49 +358,49 @@ Expected to find a '?code=' parameter in the URL but found none`
|
|
|
334
358
|
client_id: e,
|
|
335
359
|
redirect_uri: r,
|
|
336
360
|
code: a,
|
|
337
|
-
code_verifier:
|
|
361
|
+
code_verifier: K,
|
|
338
362
|
grant_type: "authorization_code"
|
|
339
363
|
})
|
|
340
|
-
}).then(
|
|
341
|
-
console.error("[sdk/provider]: error in fetching", c),
|
|
364
|
+
}).then(v).catch((c) => {
|
|
365
|
+
console.error("[sdk/provider]: error in fetching", c), O("An error occurred while fetching tokens");
|
|
342
366
|
}), window.history.replaceState({}, document.title, window.location.pathname);
|
|
343
367
|
}
|
|
344
|
-
|
|
368
|
+
D(!0);
|
|
345
369
|
}
|
|
346
370
|
}, [s]);
|
|
347
|
-
const
|
|
348
|
-
isAuthenticated: !!
|
|
349
|
-
accessToken:
|
|
350
|
-
user:
|
|
351
|
-
login:
|
|
352
|
-
logout:
|
|
353
|
-
isReady: !s && !
|
|
371
|
+
const I = {
|
|
372
|
+
isAuthenticated: !!u && !P,
|
|
373
|
+
accessToken: u,
|
|
374
|
+
user: Z ?? null,
|
|
375
|
+
login: z,
|
|
376
|
+
logout: F,
|
|
377
|
+
isReady: !s && !x
|
|
354
378
|
};
|
|
355
|
-
return /* @__PURE__ */
|
|
356
|
-
|
|
379
|
+
return /* @__PURE__ */ re(
|
|
380
|
+
H.Provider,
|
|
357
381
|
{
|
|
358
382
|
value: {
|
|
359
383
|
clientId: e,
|
|
360
384
|
redirectUri: r,
|
|
361
385
|
env: o,
|
|
362
|
-
login:
|
|
363
|
-
logout:
|
|
364
|
-
authInProgress:
|
|
365
|
-
authError:
|
|
366
|
-
idToken:
|
|
367
|
-
accessToken:
|
|
368
|
-
refreshToken:
|
|
369
|
-
user:
|
|
370
|
-
isAuthenticated:
|
|
371
|
-
isReady:
|
|
386
|
+
login: z,
|
|
387
|
+
logout: F,
|
|
388
|
+
authInProgress: x,
|
|
389
|
+
authError: X,
|
|
390
|
+
idToken: Q,
|
|
391
|
+
accessToken: u,
|
|
392
|
+
refreshToken: h,
|
|
393
|
+
user: I.user,
|
|
394
|
+
isAuthenticated: I.isAuthenticated,
|
|
395
|
+
isReady: I.isReady,
|
|
372
396
|
url: i
|
|
373
397
|
},
|
|
374
|
-
children: typeof n == "function" ? n(
|
|
398
|
+
children: typeof n == "function" ? n(I) : n
|
|
375
399
|
}
|
|
376
400
|
);
|
|
377
401
|
}
|
|
378
402
|
const Ee = () => {
|
|
379
|
-
const e =
|
|
403
|
+
const e = te(H);
|
|
380
404
|
if (!e)
|
|
381
405
|
throw new Error("useDeva must be used within a DevaProvider");
|
|
382
406
|
return {
|