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