postex-auth-sdk-stage 1.2.4 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +31 -3
- package/dist/auth.d.ts +20 -1
- package/dist/postex-auth-sdk-stage.es.js +168 -150
- package/dist/postex-auth-sdk-stage.es.js.map +1 -1
- package/dist/postex-auth-sdk-stage.iife.js +1 -1
- package/dist/postex-auth-sdk-stage.iife.js.map +1 -1
- package/dist/postex-auth-sdk-stage.umd.js +1 -1
- package/dist/postex-auth-sdk-stage.umd.js.map +1 -1
- package/package.json +1 -1
|
@@ -21,9 +21,9 @@ function k(a) {
|
|
|
21
21
|
throw new Error("Invalid base64: expected non-empty string");
|
|
22
22
|
const e = a.replace(/\s/g, "").replace(/-/g, "+").replace(/_/g, "/"), n = e.length % 4, t = n > 0 ? e + "=".repeat(4 - n) : e;
|
|
23
23
|
try {
|
|
24
|
-
const
|
|
25
|
-
for (let
|
|
26
|
-
s[
|
|
24
|
+
const r = atob(t), s = new Uint8Array(r.length);
|
|
25
|
+
for (let o = 0; o < r.length; o++)
|
|
26
|
+
s[o] = r.charCodeAt(o);
|
|
27
27
|
return s.buffer;
|
|
28
28
|
} catch {
|
|
29
29
|
throw new Error(
|
|
@@ -31,7 +31,7 @@ function k(a) {
|
|
|
31
31
|
);
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
|
-
function
|
|
34
|
+
function m(a) {
|
|
35
35
|
return y(a).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/g, "");
|
|
36
36
|
}
|
|
37
37
|
function J(a) {
|
|
@@ -81,8 +81,8 @@ async function G(a) {
|
|
|
81
81
|
try {
|
|
82
82
|
const e = await h();
|
|
83
83
|
return new Promise((n, t) => {
|
|
84
|
-
const
|
|
85
|
-
|
|
84
|
+
const r = e.transaction(u, "readwrite"), o = r.objectStore(u).put(a, S);
|
|
85
|
+
o.onerror = () => t(o.error), o.onsuccess = () => n(), r.oncomplete = () => e.close();
|
|
86
86
|
});
|
|
87
87
|
} catch {
|
|
88
88
|
}
|
|
@@ -112,8 +112,8 @@ async function X(a) {
|
|
|
112
112
|
try {
|
|
113
113
|
const e = await h();
|
|
114
114
|
return new Promise((n, t) => {
|
|
115
|
-
const
|
|
116
|
-
|
|
115
|
+
const r = e.transaction(u, "readwrite"), o = r.objectStore(u).put(a, E);
|
|
116
|
+
o.onerror = () => t(o.error), o.onsuccess = () => n(), r.oncomplete = () => e.close();
|
|
117
117
|
});
|
|
118
118
|
} catch {
|
|
119
119
|
}
|
|
@@ -137,7 +137,7 @@ function D(a) {
|
|
|
137
137
|
y: a.y
|
|
138
138
|
};
|
|
139
139
|
}
|
|
140
|
-
async function
|
|
140
|
+
async function q(a) {
|
|
141
141
|
const e = JSON.stringify({
|
|
142
142
|
crv: a.crv,
|
|
143
143
|
kty: a.kty,
|
|
@@ -147,14 +147,14 @@ async function B(a) {
|
|
|
147
147
|
"SHA-256",
|
|
148
148
|
new TextEncoder().encode(e)
|
|
149
149
|
);
|
|
150
|
-
return
|
|
150
|
+
return m(n);
|
|
151
151
|
}
|
|
152
|
-
async function
|
|
152
|
+
async function B(a) {
|
|
153
153
|
try {
|
|
154
154
|
const e = await h();
|
|
155
155
|
return new Promise((n, t) => {
|
|
156
|
-
const
|
|
157
|
-
|
|
156
|
+
const r = e.transaction(u, "readwrite"), o = r.objectStore(u).put(a, x);
|
|
157
|
+
o.onerror = () => t(o.error), o.onsuccess = () => n(), r.oncomplete = () => e.close();
|
|
158
158
|
});
|
|
159
159
|
} catch (e) {
|
|
160
160
|
console.error("Failed to store DPoP private key:", e);
|
|
@@ -164,8 +164,8 @@ async function Z(a) {
|
|
|
164
164
|
try {
|
|
165
165
|
const e = await h();
|
|
166
166
|
return new Promise((n, t) => {
|
|
167
|
-
const
|
|
168
|
-
|
|
167
|
+
const r = e.transaction(u, "readwrite"), o = r.objectStore(u).put(a, I);
|
|
168
|
+
o.onerror = () => t(o.error), o.onsuccess = () => n(), r.oncomplete = () => e.close();
|
|
169
169
|
});
|
|
170
170
|
} catch (e) {
|
|
171
171
|
console.error("Failed to store DPoP public key JWK:", e);
|
|
@@ -193,19 +193,19 @@ async function R() {
|
|
|
193
193
|
return null;
|
|
194
194
|
}
|
|
195
195
|
}
|
|
196
|
-
async function
|
|
196
|
+
async function N() {
|
|
197
197
|
try {
|
|
198
198
|
const a = await h();
|
|
199
199
|
return new Promise((e, n) => {
|
|
200
|
-
const t = a.transaction(u, "readwrite"),
|
|
201
|
-
|
|
200
|
+
const t = a.transaction(u, "readwrite"), r = t.objectStore(u);
|
|
201
|
+
r.delete(x), r.delete(I), t.onerror = () => n(t.error), t.oncomplete = () => {
|
|
202
202
|
a.close(), e();
|
|
203
203
|
};
|
|
204
204
|
});
|
|
205
205
|
} catch {
|
|
206
206
|
}
|
|
207
207
|
}
|
|
208
|
-
async function
|
|
208
|
+
async function g() {
|
|
209
209
|
const a = await crypto.subtle.generateKey(
|
|
210
210
|
{
|
|
211
211
|
name: "ECDSA",
|
|
@@ -214,44 +214,44 @@ async function T() {
|
|
|
214
214
|
!1,
|
|
215
215
|
// Private key is non-extractable
|
|
216
216
|
["sign", "verify"]
|
|
217
|
-
), e = await crypto.subtle.exportKey("jwk", a.publicKey), n = D(e), t = await
|
|
218
|
-
return await
|
|
217
|
+
), e = await crypto.subtle.exportKey("jwk", a.publicKey), n = D(e), t = await q(n);
|
|
218
|
+
return await B(a.privateKey), await Z(n), { publicKey: n, thumbprint: t };
|
|
219
219
|
}
|
|
220
220
|
async function w(a, e, n) {
|
|
221
221
|
try {
|
|
222
|
-
const t = await A(),
|
|
223
|
-
if (!t || !
|
|
222
|
+
const t = await A(), r = await R();
|
|
223
|
+
if (!t || !r) return null;
|
|
224
224
|
const s = {
|
|
225
225
|
typ: "dpop+jwt",
|
|
226
226
|
alg: "ES256",
|
|
227
|
-
jwk:
|
|
227
|
+
jwk: r
|
|
228
228
|
// Public key in JWK format
|
|
229
|
-
},
|
|
229
|
+
}, o = new URL(
|
|
230
230
|
e,
|
|
231
231
|
typeof window < "u" ? window.location.origin : void 0
|
|
232
|
-
),
|
|
232
|
+
), c = `${o.origin}${o.pathname}`, d = {
|
|
233
233
|
jti: crypto.randomUUID(),
|
|
234
234
|
htm: a.toUpperCase(),
|
|
235
|
-
htu:
|
|
235
|
+
htu: c,
|
|
236
236
|
iat: Math.floor(Date.now() / 1e3)
|
|
237
237
|
};
|
|
238
238
|
if (n) {
|
|
239
239
|
const P = new TextEncoder().encode(n), $ = await crypto.subtle.digest("SHA-256", P);
|
|
240
|
-
d.ath =
|
|
240
|
+
d.ath = m($);
|
|
241
241
|
}
|
|
242
|
-
const l =
|
|
242
|
+
const l = m(
|
|
243
243
|
new TextEncoder().encode(JSON.stringify(s)).buffer
|
|
244
|
-
),
|
|
244
|
+
), i = m(
|
|
245
245
|
new TextEncoder().encode(JSON.stringify(d)).buffer
|
|
246
|
-
), p = `${l}.${
|
|
246
|
+
), p = `${l}.${i}`, f = await crypto.subtle.sign(
|
|
247
247
|
{
|
|
248
248
|
name: "ECDSA",
|
|
249
249
|
hash: { name: "SHA-256" }
|
|
250
250
|
},
|
|
251
251
|
t,
|
|
252
252
|
new TextEncoder().encode(p)
|
|
253
|
-
), b =
|
|
254
|
-
return `${l}.${
|
|
253
|
+
), b = m(f);
|
|
254
|
+
return `${l}.${i}.${b}`;
|
|
255
255
|
} catch (t) {
|
|
256
256
|
return console.error("Failed to generate DPoP proof:", t), null;
|
|
257
257
|
}
|
|
@@ -259,12 +259,12 @@ async function w(a, e, n) {
|
|
|
259
259
|
async function ee() {
|
|
260
260
|
return await A() !== null;
|
|
261
261
|
}
|
|
262
|
-
const te =
|
|
262
|
+
const te = B, U = {
|
|
263
263
|
isWebAuthnSupported: K,
|
|
264
264
|
isConditionalUISupported: j,
|
|
265
265
|
arrayBufferToBase64: y,
|
|
266
266
|
base64ToArrayBuffer: k,
|
|
267
|
-
arrayBufferToBase64url:
|
|
267
|
+
arrayBufferToBase64url: m,
|
|
268
268
|
base64urlToArrayBuffer: M,
|
|
269
269
|
stringToArrayBuffer: _,
|
|
270
270
|
uint8ArrayToString: W,
|
|
@@ -275,25 +275,26 @@ const te = q, N = {
|
|
|
275
275
|
setPasskeyMobileNumber: X,
|
|
276
276
|
clearPasskeyMobileNumber: Q,
|
|
277
277
|
toMinimalJWK: D,
|
|
278
|
-
calculateThumbprint:
|
|
278
|
+
calculateThumbprint: q,
|
|
279
279
|
getDPoPKey: A,
|
|
280
280
|
getDPoPPublicKeyJWK: R,
|
|
281
|
-
clearDPoPKey:
|
|
282
|
-
generateDPoPKeyPair:
|
|
281
|
+
clearDPoPKey: N,
|
|
282
|
+
generateDPoPKeyPair: g,
|
|
283
283
|
generateDPoPProof: w,
|
|
284
284
|
isDPoPEnabled: ee,
|
|
285
285
|
storeDPoPKey: te
|
|
286
286
|
};
|
|
287
|
-
typeof window < "u" && (window.WebAuthn =
|
|
287
|
+
typeof window < "u" && (window.WebAuthn = U);
|
|
288
288
|
const ne = typeof window < "u" ? window : globalThis;
|
|
289
|
-
ne.WebAuthn =
|
|
290
|
-
const
|
|
289
|
+
ne.WebAuthn = U;
|
|
290
|
+
const T = "postex-auth-token", se = "postexglobal", re = {
|
|
291
291
|
xstak: "https://auth-stage.xstak.com/public/v1",
|
|
292
292
|
postex: "https://auth-stage.postex.pk/public/v1",
|
|
293
293
|
callcourier: "https://auth-stage.callcourier.com.pk/public/v1",
|
|
294
|
-
postexglobal: "https://auth-stage.postexglobal.com/public/v1"
|
|
294
|
+
postexglobal: "https://auth-stage.postexglobal.com/public/v1",
|
|
295
|
+
postexsa: "https://auth-stage.postex.sa/public/v1"
|
|
295
296
|
};
|
|
296
|
-
class
|
|
297
|
+
class oe extends Error {
|
|
297
298
|
constructor(e, n, t) {
|
|
298
299
|
super(t ?? `Request failed with status ${e}`), this.response = { status: e, data: n }, this.name = "AuthSDKFetchError";
|
|
299
300
|
}
|
|
@@ -305,7 +306,7 @@ class C {
|
|
|
305
306
|
}
|
|
306
307
|
getBaseUrl() {
|
|
307
308
|
const e = this.config.appId ?? se;
|
|
308
|
-
return
|
|
309
|
+
return re[e];
|
|
309
310
|
}
|
|
310
311
|
normalizeAuthIdentifier(e) {
|
|
311
312
|
return typeof e == "string" ? { email: e } : {
|
|
@@ -314,35 +315,35 @@ class C {
|
|
|
314
315
|
};
|
|
315
316
|
}
|
|
316
317
|
buildUrl(e, n) {
|
|
317
|
-
const t = this.getBaseUrl().replace(/\/$/, ""),
|
|
318
|
+
const t = this.getBaseUrl().replace(/\/$/, ""), r = e.startsWith("/") ? e : `/${e}`, s = `${t}${r}`;
|
|
318
319
|
if (!n || Object.keys(n).length === 0) return s;
|
|
319
|
-
const
|
|
320
|
-
return `${s}?${
|
|
320
|
+
const o = new URLSearchParams(n).toString();
|
|
321
|
+
return `${s}?${o}`;
|
|
321
322
|
}
|
|
322
323
|
async request(e, n, t) {
|
|
323
|
-
const
|
|
324
|
+
const r = this.buildUrl(n, t?.params), s = {
|
|
324
325
|
"Content-Type": "application/json",
|
|
325
326
|
Accept: "application/json",
|
|
326
327
|
"X-API-Key": this.config.apiKey ?? "",
|
|
327
328
|
...t?.headers
|
|
328
|
-
},
|
|
329
|
+
}, o = {
|
|
329
330
|
method: e,
|
|
330
331
|
credentials: "include",
|
|
331
332
|
headers: s
|
|
332
333
|
};
|
|
333
|
-
t?.body !== void 0 && t?.body !== null && (
|
|
334
|
-
const
|
|
335
|
-
if (!
|
|
336
|
-
let
|
|
334
|
+
t?.body !== void 0 && t?.body !== null && (o.body = JSON.stringify(t.body));
|
|
335
|
+
const c = await fetch(r, o);
|
|
336
|
+
if (!c.ok) {
|
|
337
|
+
let i;
|
|
337
338
|
try {
|
|
338
|
-
const p = await
|
|
339
|
-
|
|
339
|
+
const p = await c.text();
|
|
340
|
+
i = p ? JSON.parse(p) : void 0;
|
|
340
341
|
} catch {
|
|
341
|
-
|
|
342
|
+
i = void 0;
|
|
342
343
|
}
|
|
343
|
-
throw
|
|
344
|
+
throw c.status === 401 && await this.clearTokens(), new oe(c.status, i);
|
|
344
345
|
}
|
|
345
|
-
const d = await
|
|
346
|
+
const d = await c.text();
|
|
346
347
|
return { data: d ? JSON.parse(d) : {} };
|
|
347
348
|
}
|
|
348
349
|
/**
|
|
@@ -352,16 +353,16 @@ class C {
|
|
|
352
353
|
* @param url - Full request URL
|
|
353
354
|
*/
|
|
354
355
|
async getRequestAuthHeaders(e, n) {
|
|
355
|
-
const t = localStorage.getItem(
|
|
356
|
+
const t = localStorage.getItem(T);
|
|
356
357
|
if (!t) return {};
|
|
357
|
-
const
|
|
358
|
+
const r = n.startsWith("http") ? n : `${this.getBaseUrl()}${n}`, s = await w(
|
|
358
359
|
e.toUpperCase(),
|
|
359
|
-
|
|
360
|
+
r,
|
|
360
361
|
t
|
|
361
|
-
),
|
|
362
|
+
), o = {
|
|
362
363
|
Authorization: `Bearer ${t}`
|
|
363
364
|
};
|
|
364
|
-
return s && (
|
|
365
|
+
return s && (o.DPoP = s), o;
|
|
365
366
|
}
|
|
366
367
|
/**
|
|
367
368
|
* GET /auth/status - Check if client has trusted device session and what auth method is available.
|
|
@@ -382,12 +383,29 @@ class C {
|
|
|
382
383
|
async initiateAuth(e) {
|
|
383
384
|
const n = this.normalizeAuthIdentifier(e), t = await this.request("POST", "/auth/initiate", {
|
|
384
385
|
body: n
|
|
385
|
-
}),
|
|
386
|
+
}), r = t.data.data ?? t.data;
|
|
386
387
|
return {
|
|
387
|
-
status:
|
|
388
|
-
challenge:
|
|
389
|
-
credentialIds:
|
|
390
|
-
rp:
|
|
388
|
+
status: r.status,
|
|
389
|
+
challenge: r.challenge,
|
|
390
|
+
credentialIds: r.credentialIds,
|
|
391
|
+
rp: r.rp
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* POST /otp/initiate - Direct OTP initiation using email or mobile number.
|
|
396
|
+
* Requires at least one identifier: email or mobileNumber.
|
|
397
|
+
*/
|
|
398
|
+
async initiateOTP(e) {
|
|
399
|
+
const n = this.normalizeAuthIdentifier(e), t = n.email?.trim(), r = n.mobileNumber?.trim();
|
|
400
|
+
if (!t && !r)
|
|
401
|
+
throw new Error("Either mobileNumber or email is required");
|
|
402
|
+
const s = {};
|
|
403
|
+
t && (s.email = t), r && (s.mobileNumber = r);
|
|
404
|
+
const c = (await this.request("POST", "/otp/initiate", {
|
|
405
|
+
body: s
|
|
406
|
+
})).data;
|
|
407
|
+
return {
|
|
408
|
+
message: c.message ?? c.data?.message
|
|
391
409
|
};
|
|
392
410
|
}
|
|
393
411
|
/**
|
|
@@ -395,26 +413,26 @@ class C {
|
|
|
395
413
|
* Stores tokens from the response.
|
|
396
414
|
*/
|
|
397
415
|
async verifyOTP(e) {
|
|
398
|
-
await
|
|
416
|
+
await g();
|
|
399
417
|
const n = await w(
|
|
400
418
|
"POST",
|
|
401
419
|
`${this.getBaseUrl()}/otp/verify`
|
|
402
420
|
), t = await this.request("POST", "/otp/verify", {
|
|
403
421
|
body: { otp: e },
|
|
404
422
|
headers: n ? { DPoP: n } : {}
|
|
405
|
-
}),
|
|
406
|
-
return
|
|
407
|
-
accessToken:
|
|
408
|
-
refreshToken:
|
|
423
|
+
}), r = t.data.data ?? t.data, s = r.AuthenticationResult ?? r, o = s.access_token ?? s.accessToken ?? s.AccessToken, c = s.refresh_token ?? s.refreshToken ?? s.RefreshToken, d = s.id_token ?? s.idToken ?? s.IdToken, l = s.expires_in ?? s.expiresIn ?? s.ExpiresIn ?? 3600, i = s.token_type ?? s.tokenType ?? s.TokenType ?? "Bearer";
|
|
424
|
+
return o && await this.storeTokens({
|
|
425
|
+
accessToken: o,
|
|
426
|
+
refreshToken: c ?? "",
|
|
409
427
|
idToken: d ?? "",
|
|
410
428
|
expiresIn: l ?? 3600,
|
|
411
|
-
tokenType:
|
|
429
|
+
tokenType: i ?? "Bearer"
|
|
412
430
|
}), {
|
|
413
|
-
access_token:
|
|
414
|
-
refresh_token:
|
|
431
|
+
access_token: o,
|
|
432
|
+
refresh_token: c ?? "",
|
|
415
433
|
id_token: d ?? "",
|
|
416
434
|
expires_in: l,
|
|
417
|
-
token_type:
|
|
435
|
+
token_type: i,
|
|
418
436
|
verified: s.verified ?? s.Verified ?? !0,
|
|
419
437
|
email: s.email ?? s.Email ?? "",
|
|
420
438
|
...s
|
|
@@ -440,29 +458,29 @@ class C {
|
|
|
440
458
|
mobileNumber: e,
|
|
441
459
|
otp: n
|
|
442
460
|
}) {
|
|
443
|
-
await
|
|
461
|
+
await g();
|
|
444
462
|
const t = await w(
|
|
445
463
|
"POST",
|
|
446
464
|
`${this.getBaseUrl()}/otp/signup/verify`
|
|
447
|
-
),
|
|
465
|
+
), r = await this.request("POST", "/otp/signup/verify", {
|
|
448
466
|
body: { mobileNumber: e, otp: n },
|
|
449
467
|
headers: t ? { DPoP: t } : {}
|
|
450
|
-
}), s =
|
|
451
|
-
return
|
|
452
|
-
accessToken:
|
|
468
|
+
}), s = r.data.data ?? r.data, o = s.AuthenticationResult ?? s, c = o.access_token ?? o.accessToken ?? o.AccessToken, d = o.refresh_token ?? o.refreshToken ?? o.RefreshToken, l = o.id_token ?? o.idToken ?? o.IdToken, i = o.expires_in ?? o.expiresIn ?? o.ExpiresIn ?? 3600, p = o.token_type ?? o.tokenType ?? o.TokenType ?? "Bearer";
|
|
469
|
+
return c && await this.storeTokens({
|
|
470
|
+
accessToken: c,
|
|
453
471
|
refreshToken: d ?? "",
|
|
454
472
|
idToken: l ?? "",
|
|
455
|
-
expiresIn:
|
|
473
|
+
expiresIn: i ?? 3600,
|
|
456
474
|
tokenType: p ?? "Bearer"
|
|
457
475
|
}), {
|
|
458
|
-
access_token:
|
|
476
|
+
access_token: c,
|
|
459
477
|
refresh_token: d ?? "",
|
|
460
478
|
id_token: l ?? "",
|
|
461
|
-
expires_in:
|
|
479
|
+
expires_in: i,
|
|
462
480
|
token_type: p,
|
|
463
|
-
verified:
|
|
464
|
-
email:
|
|
465
|
-
...
|
|
481
|
+
verified: o.verified ?? o.Verified ?? !0,
|
|
482
|
+
email: o.email ?? o.Email ?? "",
|
|
483
|
+
...o
|
|
466
484
|
};
|
|
467
485
|
}
|
|
468
486
|
/**
|
|
@@ -474,11 +492,11 @@ class C {
|
|
|
474
492
|
}) {
|
|
475
493
|
const t = await this.request("POST", "/otp/signup/resend", {
|
|
476
494
|
body: { email: n, mobileNumber: e }
|
|
477
|
-
}),
|
|
495
|
+
}), r = t.data.data ?? t.data;
|
|
478
496
|
return {
|
|
479
|
-
success:
|
|
480
|
-
message:
|
|
481
|
-
...
|
|
497
|
+
success: r.success ?? !0,
|
|
498
|
+
message: r.message,
|
|
499
|
+
...r
|
|
482
500
|
};
|
|
483
501
|
}
|
|
484
502
|
/**
|
|
@@ -502,18 +520,18 @@ class C {
|
|
|
502
520
|
{
|
|
503
521
|
body: {}
|
|
504
522
|
}
|
|
505
|
-
), n = e.data.data ?? e.data, t = n.AuthenticationResult ?? n,
|
|
506
|
-
return
|
|
507
|
-
accessToken:
|
|
523
|
+
), n = e.data.data ?? e.data, t = n.AuthenticationResult ?? n, r = t.access_token ?? t.accessToken ?? t.AccessToken, s = t.refresh_token ?? t.refreshToken ?? t.RefreshToken, o = t.id_token ?? t.idToken ?? t.IdToken, c = t.expires_in ?? t.expiresIn ?? t.ExpiresIn ?? 3600, d = t.token_type ?? t.tokenType ?? t.TokenType ?? "Bearer";
|
|
524
|
+
return r && await this.storeTokens({
|
|
525
|
+
accessToken: r,
|
|
508
526
|
refreshToken: s ?? "",
|
|
509
|
-
idToken:
|
|
510
|
-
expiresIn:
|
|
527
|
+
idToken: o ?? "",
|
|
528
|
+
expiresIn: c ?? 3600,
|
|
511
529
|
tokenType: d ?? "Bearer"
|
|
512
530
|
}), {
|
|
513
|
-
access_token:
|
|
531
|
+
access_token: r,
|
|
514
532
|
refresh_token: s ?? "",
|
|
515
|
-
id_token:
|
|
516
|
-
expires_in:
|
|
533
|
+
id_token: o ?? "",
|
|
534
|
+
expires_in: c,
|
|
517
535
|
token_type: d,
|
|
518
536
|
verified: t.verified ?? t.Verified ?? !0,
|
|
519
537
|
email: t.email ?? t.Email ?? "",
|
|
@@ -541,7 +559,7 @@ class C {
|
|
|
541
559
|
}
|
|
542
560
|
else
|
|
543
561
|
t = _(e);
|
|
544
|
-
const
|
|
562
|
+
const r = (n.pubKeyCredParams ?? [
|
|
545
563
|
{ type: "public-key", alg: -7 },
|
|
546
564
|
{ type: "public-key", alg: -257 }
|
|
547
565
|
]).map((f) => ({
|
|
@@ -550,7 +568,7 @@ class C {
|
|
|
550
568
|
})), s = n.excludeCredentials?.map((f) => ({
|
|
551
569
|
type: "public-key",
|
|
552
570
|
id: k(f.id)
|
|
553
|
-
})),
|
|
571
|
+
})), o = {
|
|
554
572
|
challenge: k(n.challenge),
|
|
555
573
|
rp: {
|
|
556
574
|
name: n.rp?.name ?? "XPay",
|
|
@@ -561,7 +579,7 @@ class C {
|
|
|
561
579
|
name: n.user?.name ?? e,
|
|
562
580
|
displayName: n.user?.displayName ?? e
|
|
563
581
|
},
|
|
564
|
-
pubKeyCredParams:
|
|
582
|
+
pubKeyCredParams: r,
|
|
565
583
|
excludeCredentials: s,
|
|
566
584
|
timeout: n.timeout ?? 6e4,
|
|
567
585
|
attestation: n.attestation ?? "none",
|
|
@@ -569,18 +587,18 @@ class C {
|
|
|
569
587
|
residentKey: "required",
|
|
570
588
|
userVerification: "required"
|
|
571
589
|
}
|
|
572
|
-
},
|
|
573
|
-
publicKey:
|
|
590
|
+
}, c = await navigator.credentials.create({
|
|
591
|
+
publicKey: o
|
|
574
592
|
});
|
|
575
|
-
if (!
|
|
576
|
-
const d =
|
|
593
|
+
if (!c) throw new Error("Credential creation failed");
|
|
594
|
+
const d = c.response, l = {
|
|
577
595
|
clientDataJSON: y(d.clientDataJSON),
|
|
578
596
|
attestationObject: y(d.attestationObject),
|
|
579
|
-
rawId: y(
|
|
597
|
+
rawId: y(c.rawId)
|
|
580
598
|
};
|
|
581
599
|
if (!l.rawId) throw new Error("Raw ID is required");
|
|
582
|
-
await
|
|
583
|
-
const
|
|
600
|
+
await g();
|
|
601
|
+
const i = await w(
|
|
584
602
|
"POST",
|
|
585
603
|
`${this.getBaseUrl()}/webauthn/register/challenge`
|
|
586
604
|
), p = await this.request(
|
|
@@ -588,7 +606,7 @@ class C {
|
|
|
588
606
|
"/webauthn/register/challenge",
|
|
589
607
|
{
|
|
590
608
|
body: l,
|
|
591
|
-
headers:
|
|
609
|
+
headers: i ? { DPoP: i } : {}
|
|
592
610
|
}
|
|
593
611
|
);
|
|
594
612
|
return p.data.data ?? p.data;
|
|
@@ -601,7 +619,7 @@ class C {
|
|
|
601
619
|
rp: n,
|
|
602
620
|
credentialIds: t
|
|
603
621
|
}) {
|
|
604
|
-
const
|
|
622
|
+
const r = await navigator.credentials.get({
|
|
605
623
|
publicKey: {
|
|
606
624
|
challenge: k(e),
|
|
607
625
|
rpId: n?.host ?? void 0,
|
|
@@ -615,78 +633,78 @@ class C {
|
|
|
615
633
|
userVerification: "required"
|
|
616
634
|
}
|
|
617
635
|
});
|
|
618
|
-
if (!
|
|
619
|
-
const s =
|
|
636
|
+
if (!r) throw new Error("Authentication failed");
|
|
637
|
+
const s = r.response, o = {
|
|
620
638
|
clientDataJSON: y(s.clientDataJSON),
|
|
621
639
|
authenticatorData: y(
|
|
622
640
|
s.authenticatorData
|
|
623
641
|
),
|
|
624
642
|
signature: y(s.signature),
|
|
625
|
-
rawId: y(
|
|
643
|
+
rawId: y(r.rawId),
|
|
626
644
|
userHandle: s.userHandle ? y(s.userHandle) : ""
|
|
627
645
|
};
|
|
628
|
-
await
|
|
629
|
-
const
|
|
646
|
+
await g();
|
|
647
|
+
const c = await w(
|
|
630
648
|
"POST",
|
|
631
649
|
`${this.getBaseUrl()}/webauthn/authenticate/challenge`
|
|
632
650
|
), d = await this.request(
|
|
633
651
|
"POST",
|
|
634
652
|
"/webauthn/authenticate/challenge",
|
|
635
653
|
{
|
|
636
|
-
body:
|
|
637
|
-
headers:
|
|
654
|
+
body: o,
|
|
655
|
+
headers: c ? { DPoP: c } : {}
|
|
638
656
|
}
|
|
639
|
-
), l = d.data.data ?? d.data,
|
|
657
|
+
), l = d.data.data ?? d.data, i = l.AuthenticationResult ?? l, p = i.AccessToken ?? i.accessToken ?? i.access_token, f = i.RefreshToken ?? i.refreshToken ?? i.refresh_token, b = i.IdToken ?? i.idToken ?? i.id_token;
|
|
640
658
|
return p && await this.storeTokens({
|
|
641
659
|
accessToken: p,
|
|
642
660
|
refreshToken: f ?? "",
|
|
643
661
|
idToken: b ?? "",
|
|
644
|
-
expiresIn:
|
|
645
|
-
tokenType:
|
|
662
|
+
expiresIn: i.expiresIn ?? i.ExpiresIn ?? 3600,
|
|
663
|
+
tokenType: i.tokenType ?? i.token_type ?? "Bearer"
|
|
646
664
|
}), {
|
|
647
665
|
access_token: p,
|
|
648
666
|
refresh_token: f ?? "",
|
|
649
667
|
id_token: b,
|
|
650
|
-
email:
|
|
651
|
-
name:
|
|
652
|
-
expiresIn:
|
|
653
|
-
tokenType:
|
|
654
|
-
...
|
|
668
|
+
email: i.email,
|
|
669
|
+
name: i.name ?? i.userName,
|
|
670
|
+
expiresIn: i.expiresIn ?? i.ExpiresIn,
|
|
671
|
+
tokenType: i.tokenType ?? i.token_type,
|
|
672
|
+
...i
|
|
655
673
|
};
|
|
656
674
|
}
|
|
657
675
|
/**
|
|
658
676
|
* GET /webauthn/credentials/:username - Get user's passkey status.
|
|
659
677
|
*/
|
|
660
678
|
async getPasskeyStatus(e) {
|
|
661
|
-
const t = `/webauthn/credentials/${encodeURIComponent(e)}`,
|
|
662
|
-
headers:
|
|
663
|
-
}),
|
|
679
|
+
const t = `/webauthn/credentials/${encodeURIComponent(e)}`, r = await this.signRequest("GET", t), s = await this.request("GET", t, {
|
|
680
|
+
headers: r.headers
|
|
681
|
+
}), o = s.data.data ?? s.data;
|
|
664
682
|
return {
|
|
665
|
-
...
|
|
666
|
-
hasCredentials: !!(
|
|
683
|
+
...o,
|
|
684
|
+
hasCredentials: !!(o.hasCredentials ?? o.credentialId)
|
|
667
685
|
};
|
|
668
686
|
}
|
|
669
687
|
/**
|
|
670
688
|
* DELETE /webauthn/credentials/:username - Remove passkey.
|
|
671
689
|
*/
|
|
672
690
|
async removePasskey(e) {
|
|
673
|
-
const t = `/webauthn/credentials/${encodeURIComponent(e)}`,
|
|
691
|
+
const t = `/webauthn/credentials/${encodeURIComponent(e)}`, r = await this.signRequest("DELETE", t);
|
|
674
692
|
await this.request("DELETE", t, {
|
|
675
|
-
headers:
|
|
693
|
+
headers: r.headers
|
|
676
694
|
});
|
|
677
695
|
}
|
|
678
696
|
/**
|
|
679
697
|
* Signs a request with DPoP and Authorization headers (internal use).
|
|
680
698
|
*/
|
|
681
699
|
async signRequest(e, n, t = {}) {
|
|
682
|
-
const
|
|
700
|
+
const r = n.startsWith("http") ? n : `${this.getBaseUrl()}${n}`, s = await this.getRequestAuthHeaders(e, r);
|
|
683
701
|
return { ...t, headers: { ...t.headers, ...s } };
|
|
684
702
|
}
|
|
685
703
|
/**
|
|
686
704
|
* Replaces native fetch or Axios with a DPoP-signed version.
|
|
687
705
|
*/
|
|
688
706
|
async authenticatedFetch(e, n) {
|
|
689
|
-
const t = typeof e == "string" ? e : e instanceof URL ? e.toString() : e.url,
|
|
707
|
+
const t = typeof e == "string" ? e : e instanceof URL ? e.toString() : e.url, r = n?.method || "GET", s = await this.signRequest(r, t, {
|
|
690
708
|
headers: n?.headers
|
|
691
709
|
});
|
|
692
710
|
return fetch(e, { ...n, headers: s.headers });
|
|
@@ -695,13 +713,13 @@ class C {
|
|
|
695
713
|
* Store tokens from /webauthn/authenticate (per spec: sessionStorage preferred).
|
|
696
714
|
*/
|
|
697
715
|
async storeTokens(e) {
|
|
698
|
-
localStorage.setItem(
|
|
716
|
+
localStorage.setItem(T, e.accessToken), e.refreshToken && localStorage.setItem(v, e.refreshToken), e.idToken && localStorage.setItem(O, e.idToken);
|
|
699
717
|
}
|
|
700
718
|
/**
|
|
701
719
|
* Clear stored tokens (call on logout).
|
|
702
720
|
*/
|
|
703
721
|
async clearTokens() {
|
|
704
|
-
localStorage.removeItem(
|
|
722
|
+
localStorage.removeItem(T), localStorage.removeItem(O), localStorage.removeItem(v);
|
|
705
723
|
}
|
|
706
724
|
/**
|
|
707
725
|
* POST /auth/refresh - Refresh access token using server-stored refresh token.
|
|
@@ -711,17 +729,17 @@ class C {
|
|
|
711
729
|
async refreshToken() {
|
|
712
730
|
const e = await this.request("POST", "/auth/refresh", {
|
|
713
731
|
body: {}
|
|
714
|
-
}), n = e.data.data ?? e.data, t = n.access_token,
|
|
732
|
+
}), n = e.data.data ?? e.data, t = n.access_token, r = n.id_token ?? "", s = n.token_type ?? "Bearer", o = n.expires_in ?? 3600;
|
|
715
733
|
return t && await this.storeTokens({
|
|
716
734
|
accessToken: t,
|
|
717
|
-
idToken:
|
|
718
|
-
expiresIn:
|
|
735
|
+
idToken: r,
|
|
736
|
+
expiresIn: o,
|
|
719
737
|
tokenType: s
|
|
720
738
|
}), {
|
|
721
739
|
access_token: t,
|
|
722
|
-
id_token:
|
|
740
|
+
id_token: r,
|
|
723
741
|
token_type: s,
|
|
724
|
-
expires_in:
|
|
742
|
+
expires_in: o
|
|
725
743
|
};
|
|
726
744
|
}
|
|
727
745
|
/**
|
|
@@ -731,13 +749,13 @@ class C {
|
|
|
731
749
|
async logout() {
|
|
732
750
|
try {
|
|
733
751
|
const e = await this.getAccessToken(), n = {};
|
|
734
|
-
e && (n.Authorization = `Bearer ${e}`), await this.request("POST", "/auth/logout", { body: {}, headers: n }),
|
|
752
|
+
e && (n.Authorization = `Bearer ${e}`), await this.request("POST", "/auth/logout", { body: {}, headers: n }), N();
|
|
735
753
|
} catch {
|
|
736
754
|
}
|
|
737
755
|
await this.clearTokens();
|
|
738
756
|
}
|
|
739
757
|
async getAccessToken() {
|
|
740
|
-
return localStorage.getItem(
|
|
758
|
+
return localStorage.getItem(T);
|
|
741
759
|
}
|
|
742
760
|
}
|
|
743
761
|
typeof window < "u" && (window.AuthSDK = C);
|
|
@@ -745,16 +763,16 @@ const ae = typeof window < "u" ? window : globalThis;
|
|
|
745
763
|
ae.AuthSDK = C;
|
|
746
764
|
export {
|
|
747
765
|
C as AuthSDK,
|
|
748
|
-
|
|
766
|
+
oe as AuthSDKFetchError,
|
|
749
767
|
y as arrayBufferToBase64,
|
|
750
|
-
|
|
768
|
+
m as arrayBufferToBase64url,
|
|
751
769
|
k as base64ToArrayBuffer,
|
|
752
770
|
M as base64urlToArrayBuffer,
|
|
753
|
-
|
|
754
|
-
|
|
771
|
+
q as calculateThumbprint,
|
|
772
|
+
N as clearDPoPKey,
|
|
755
773
|
V as clearPasskeyEmail,
|
|
756
774
|
Q as clearPasskeyMobileNumber,
|
|
757
|
-
|
|
775
|
+
g as generateDPoPKeyPair,
|
|
758
776
|
w as generateDPoPProof,
|
|
759
777
|
A as getDPoPKey,
|
|
760
778
|
R as getDPoPPublicKeyJWK,
|