@glideidentity/glide-be-node-magical-auth 3.2.0 → 3.2.1
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 +87 -9
- package/dist/index.cjs +297 -16
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +72 -8
- package/dist/index.d.ts +72 -8
- package/dist/index.js +292 -14
- package/dist/index.js.map +1 -1
- package/dist/{types-BPuPzIcL.d.cts → types-t9HAB-7Q.d.cts} +153 -40
- package/dist/{types-BPuPzIcL.d.ts → types-t9HAB-7Q.d.ts} +153 -40
- package/dist/types.d.cts +1 -1
- package/dist/types.d.ts +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -38,6 +38,7 @@ var ErrorCode = {
|
|
|
38
38
|
CARRIER_NOT_ELIGIBLE: "CARRIER_NOT_ELIGIBLE",
|
|
39
39
|
CARRIER_REJECTED: "CARRIER_REJECTED",
|
|
40
40
|
CARRIER_APP_UNAVAILABLE: "CARRIER_APP_UNAVAILABLE",
|
|
41
|
+
CARRIER_TEMPORARILY_UNAVAILABLE: "CARRIER_TEMPORARILY_UNAVAILABLE",
|
|
41
42
|
UNSUPPORTED_PLATFORM: "UNSUPPORTED_PLATFORM",
|
|
42
43
|
PHONE_NUMBER_MISMATCH: "PHONE_NUMBER_MISMATCH",
|
|
43
44
|
INVALID_CREDENTIAL_FORMAT: "INVALID_CREDENTIAL_FORMAT",
|
|
@@ -154,6 +155,14 @@ var CarrierAppUnavailableError = class extends MagicalAuthError {
|
|
|
154
155
|
this.name = "CarrierAppUnavailableError";
|
|
155
156
|
}
|
|
156
157
|
};
|
|
158
|
+
var CarrierTemporarilyUnavailableError = class extends MagicalAuthError {
|
|
159
|
+
retryAfter;
|
|
160
|
+
constructor(message, retryAfter, requestId, details) {
|
|
161
|
+
super(message, "CARRIER_TEMPORARILY_UNAVAILABLE", 422, requestId, details);
|
|
162
|
+
this.name = "CarrierTemporarilyUnavailableError";
|
|
163
|
+
this.retryAfter = retryAfter;
|
|
164
|
+
}
|
|
165
|
+
};
|
|
157
166
|
var UnsupportedPlatformError = class extends MagicalAuthError {
|
|
158
167
|
constructor(message = "Platform not supported", requestId) {
|
|
159
168
|
super(message, "UNSUPPORTED_PLATFORM", 422, requestId);
|
|
@@ -253,7 +262,8 @@ var VerificationRecordErrorCode = {
|
|
|
253
262
|
ORIGIN_MISMATCH: "ORIGIN_MISMATCH",
|
|
254
263
|
UNSUPPORTED_VERSION: "UNSUPPORTED_VERSION",
|
|
255
264
|
REVOKED_KEY: "REVOKED_KEY",
|
|
256
|
-
RECORD_UNAVAILABLE: "RECORD_UNAVAILABLE"
|
|
265
|
+
RECORD_UNAVAILABLE: "RECORD_UNAVAILABLE",
|
|
266
|
+
STEP_UP_REJECTED: "STEP_UP_REJECTED"
|
|
257
267
|
};
|
|
258
268
|
var VerificationRecordError = class extends MagicalAuthError {
|
|
259
269
|
constructor(code, message) {
|
|
@@ -288,6 +298,8 @@ function createErrorFromResponse(response, headers) {
|
|
|
288
298
|
});
|
|
289
299
|
}
|
|
290
300
|
function createTypedError(code, message, request_id, details, status, headers) {
|
|
301
|
+
const rawRetry = details?.retry_after_seconds;
|
|
302
|
+
const detailRetry = typeof rawRetry === "number" && Number.isFinite(rawRetry) && rawRetry >= 0 && Number.isInteger(rawRetry) ? rawRetry : void 0;
|
|
291
303
|
switch (code) {
|
|
292
304
|
case "BAD_REQUEST":
|
|
293
305
|
return new BadRequestError(message, request_id);
|
|
@@ -316,6 +328,13 @@ function createTypedError(code, message, request_id, details, status, headers) {
|
|
|
316
328
|
request_id,
|
|
317
329
|
details
|
|
318
330
|
);
|
|
331
|
+
case "CARRIER_TEMPORARILY_UNAVAILABLE":
|
|
332
|
+
return new CarrierTemporarilyUnavailableError(
|
|
333
|
+
message,
|
|
334
|
+
parseRetryAfter(headers) ?? detailRetry,
|
|
335
|
+
request_id,
|
|
336
|
+
details
|
|
337
|
+
);
|
|
319
338
|
case "UNSUPPORTED_PLATFORM":
|
|
320
339
|
return new UnsupportedPlatformError(message, request_id);
|
|
321
340
|
case "INVALID_CREDENTIAL_FORMAT":
|
|
@@ -338,12 +357,15 @@ function createTypedError(code, message, request_id, details, status, headers) {
|
|
|
338
357
|
case "MISSING_BINDING_COOKIE":
|
|
339
358
|
return new MissingBindingCookieError(message, request_id);
|
|
340
359
|
case "RATE_LIMIT_EXCEEDED":
|
|
341
|
-
return new RateLimitError(message, parseRetryAfter(headers), request_id);
|
|
360
|
+
return new RateLimitError(message, parseRetryAfter(headers) ?? detailRetry, request_id);
|
|
342
361
|
case "INTERNAL_SERVER_ERROR":
|
|
343
362
|
return new InternalServerError(message, request_id);
|
|
344
363
|
case "SERVICE_UNAVAILABLE":
|
|
345
364
|
return new ServiceUnavailableError(message, request_id);
|
|
346
365
|
default:
|
|
366
|
+
if (status === 429) {
|
|
367
|
+
return new RateLimitError(message, parseRetryAfter(headers) ?? detailRetry, request_id);
|
|
368
|
+
}
|
|
347
369
|
return new MagicalAuthError(
|
|
348
370
|
message,
|
|
349
371
|
code,
|
|
@@ -380,7 +402,9 @@ var TokenManager = class {
|
|
|
380
402
|
* in progress, subsequent calls will await the same promise.
|
|
381
403
|
*
|
|
382
404
|
* @returns A valid access token
|
|
383
|
-
* @throws {
|
|
405
|
+
* @throws {RateLimitError} If the token endpoint returns 429 (includes Retry-After when present)
|
|
406
|
+
* @throws {ServiceUnavailableError} If the token endpoint returns 5xx or temporarily_unavailable
|
|
407
|
+
* @throws {UnauthorizedError} For other token fetch failures (e.g. 401)
|
|
384
408
|
*/
|
|
385
409
|
async getToken() {
|
|
386
410
|
if (this.token && Date.now() < this.expiresAt) {
|
|
@@ -418,8 +442,10 @@ var TokenManager = class {
|
|
|
418
442
|
});
|
|
419
443
|
if (!response.ok) {
|
|
420
444
|
let errorMessage2 = "Token request failed";
|
|
445
|
+
let oauthError;
|
|
421
446
|
try {
|
|
422
447
|
const errorBody = await response.json();
|
|
448
|
+
oauthError = errorBody.error;
|
|
423
449
|
if (errorBody.error_description) {
|
|
424
450
|
errorMessage2 = errorBody.error_description;
|
|
425
451
|
} else if (errorBody.error) {
|
|
@@ -427,6 +453,15 @@ var TokenManager = class {
|
|
|
427
453
|
}
|
|
428
454
|
} catch {
|
|
429
455
|
}
|
|
456
|
+
if (response.status === 429) {
|
|
457
|
+
throw new RateLimitError(errorMessage2, parseRetryAfter(response.headers));
|
|
458
|
+
}
|
|
459
|
+
if (response.status >= 500 || oauthError === "temporarily_unavailable") {
|
|
460
|
+
throw new ServiceUnavailableError(errorMessage2);
|
|
461
|
+
}
|
|
462
|
+
if (response.status === 401 || oauthError === "invalid_client") {
|
|
463
|
+
throw new UnauthorizedError(errorMessage2);
|
|
464
|
+
}
|
|
430
465
|
throw new UnauthorizedError(errorMessage2);
|
|
431
466
|
}
|
|
432
467
|
const data = await response.json();
|
|
@@ -588,12 +623,14 @@ function validateCookieAttr(name, value) {
|
|
|
588
623
|
throw new Error("Invalid cookie path: must start with '/'");
|
|
589
624
|
}
|
|
590
625
|
}
|
|
591
|
-
function getCompletionPageHtml(completeEndpoint) {
|
|
626
|
+
function getCompletionPageHtml(completeEndpoint, options) {
|
|
592
627
|
if (!completeEndpoint.startsWith("/") || completeEndpoint.startsWith("//")) {
|
|
593
628
|
throw new Error(
|
|
594
629
|
'completeEndpoint must be a relative path starting with "/" (e.g., "/api/glide/complete"). Cross-origin and protocol-relative endpoints (e.g., "//evil.com/...") are not supported because the page relies on credentials: "include" for cookie-based device binding, which requires same-origin requests.'
|
|
595
630
|
);
|
|
596
631
|
}
|
|
632
|
+
const serializedCompleteEndpoint = JSON.stringify(completeEndpoint).replace(/</g, "\\u003c");
|
|
633
|
+
const beacon = options?.beacon === true;
|
|
597
634
|
return `<!DOCTYPE html>
|
|
598
635
|
<html lang="en">
|
|
599
636
|
<head>
|
|
@@ -661,20 +698,157 @@ function getCompletionPageHtml(completeEndpoint) {
|
|
|
661
698
|
<div class="badge">Action required</div>
|
|
662
699
|
</div>
|
|
663
700
|
<script>
|
|
664
|
-
(async function() {
|
|
701
|
+
${beacon ? `(async function() {
|
|
665
702
|
var fragment = new URLSearchParams(window.location.hash.substring(1));
|
|
666
703
|
var aggCode = fragment.get('agg_code');
|
|
667
704
|
var sessionKey = fragment.get('session_key');
|
|
705
|
+
var closeSent = false;
|
|
706
|
+
var survivedSent = false;
|
|
707
|
+
var closeTrigger = '';
|
|
708
|
+
var hiddenAt = 0;
|
|
709
|
+
|
|
710
|
+
function pageFields() {
|
|
711
|
+
var o = { has_agg_code: !!aggCode, has_session_key: !!sessionKey };
|
|
712
|
+
if (sessionKey && /^[A-Za-z0-9_-]{1,64}$/.test(sessionKey)) o.session_key = sessionKey;
|
|
713
|
+
return o;
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
function sendPageBeacon(event, extra) {
|
|
717
|
+
try {
|
|
718
|
+
var payload = Object.assign({ event: event }, pageFields(), extra || {});
|
|
719
|
+
var url = window.location.pathname;
|
|
720
|
+
var body = JSON.stringify(payload);
|
|
721
|
+
try {
|
|
722
|
+
if (navigator.sendBeacon && navigator.sendBeacon(url, new Blob([body], { type: 'application/json' }))) return;
|
|
723
|
+
} catch (e) {}
|
|
724
|
+
fetch(url, {
|
|
725
|
+
method: 'POST',
|
|
726
|
+
headers: { 'Content-Type': 'application/json' },
|
|
727
|
+
body: body,
|
|
728
|
+
keepalive: true,
|
|
729
|
+
credentials: 'same-origin'
|
|
730
|
+
}).catch(function() {});
|
|
731
|
+
} catch (e) {}
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
function showCloseHint() {
|
|
735
|
+
document.getElementById('hint').textContent = 'You may close this tab.';
|
|
736
|
+
document.getElementById('hint').classList.remove('hidden');
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
sendPageBeacon('page_start');
|
|
668
740
|
|
|
669
741
|
window.history.replaceState(null, '', window.location.pathname + window.location.search);
|
|
670
742
|
|
|
671
743
|
if (!aggCode || !sessionKey) {
|
|
744
|
+
sendPageBeacon('invalid_link');
|
|
672
745
|
showError('Invalid verification link', ['This verification link is not valid.', 'Please go back and try again.'], 'link');
|
|
673
746
|
return;
|
|
674
747
|
}
|
|
675
748
|
|
|
676
749
|
try {
|
|
677
|
-
var res = await fetch(${
|
|
750
|
+
var res = await fetch(${serializedCompleteEndpoint}, {
|
|
751
|
+
method: 'POST',
|
|
752
|
+
credentials: 'include',
|
|
753
|
+
headers: { 'Content-Type': 'application/json' },
|
|
754
|
+
body: JSON.stringify({ agg_code: aggCode, session_key: sessionKey, user_agent: navigator.userAgent })
|
|
755
|
+
});
|
|
756
|
+
|
|
757
|
+
var outcome = { ok: !!res.ok, status: res.status };
|
|
758
|
+
if (res.ok) {
|
|
759
|
+
sendPageBeacon('post_outcome', outcome);
|
|
760
|
+
var signalKey = 'glide_signal_' + sessionKey;
|
|
761
|
+
var sigmetaKey = 'glide_sigmeta_' + sessionKey;
|
|
762
|
+
var signalStored = false;
|
|
763
|
+
try {
|
|
764
|
+
localStorage.setItem(signalKey, sessionKey);
|
|
765
|
+
signalStored = true;
|
|
766
|
+
localStorage.setItem(sigmetaKey, String(Date.now()));
|
|
767
|
+
} catch(e) {}
|
|
768
|
+
if (signalStored) {
|
|
769
|
+
setTimeout(function() {
|
|
770
|
+
try { localStorage.removeItem(signalKey); localStorage.removeItem(sigmetaKey); } catch(e) {}
|
|
771
|
+
}, 600000);
|
|
772
|
+
}
|
|
773
|
+
tryClose();
|
|
774
|
+
} else {
|
|
775
|
+
var code = '';
|
|
776
|
+
try { var data = await res.json(); var raw = data && (data.code || data.error); if (typeof raw === 'string' && /^[A-Za-z0-9_.-]{1,64}$/.test(raw)) { code = raw; outcome.code = raw; } } catch (_) {}
|
|
777
|
+
sendPageBeacon('post_outcome', outcome);
|
|
778
|
+
var err = friendlyError(code);
|
|
779
|
+
showError(err.title, err.points, err.illust);
|
|
780
|
+
}
|
|
781
|
+
} catch (e) {
|
|
782
|
+
sendPageBeacon('post_outcome', { ok: false, status: 0 });
|
|
783
|
+
showError('Connection issue', ['We could not reach the server.', 'Check your internet connection and try again.'], 'wifi');
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
function friendlyError(code) {
|
|
787
|
+
switch (code) {
|
|
788
|
+
case 'MISSING_BINDING_COOKIE':
|
|
789
|
+
case 'BROWSER_MISMATCH':
|
|
790
|
+
return { title: 'Open in your default browser', points: ['Carrier verification must be started from your default browser.', 'The redirect will always open in your default browser to complete.'], illust: 'browser' };
|
|
791
|
+
default:
|
|
792
|
+
return { title: 'Verification could not be completed', points: ['Something went wrong during verification.', 'Please go back and try again.'], illust: 'retry' };
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
function reportClose(trigger) {
|
|
797
|
+
if (closeSent) return;
|
|
798
|
+
closeSent = true;
|
|
799
|
+
closeTrigger = trigger;
|
|
800
|
+
sendPageBeacon('close', {
|
|
801
|
+
close_outcome: (trigger === 'timeout' && document.visibilityState === 'visible') ? 'failed' : 'unknown',
|
|
802
|
+
trigger: trigger,
|
|
803
|
+
visibility_state: document.visibilityState
|
|
804
|
+
});
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
function reportTabSurvived() {
|
|
808
|
+
if (!closeSent || survivedSent) return;
|
|
809
|
+
survivedSent = true;
|
|
810
|
+
var hiddenMs = hiddenAt ? Date.now() - hiddenAt : 0;
|
|
811
|
+
if (hiddenMs < 0) hiddenMs = 0;
|
|
812
|
+
sendPageBeacon('tab_survived', { hidden_ms: hiddenMs, trigger: closeTrigger });
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
function tryClose() {
|
|
816
|
+
document.querySelector('.spinner').classList.add('hidden');
|
|
817
|
+
if ((document.hidden || document.visibilityState === 'hidden') && !hiddenAt) hiddenAt = Date.now();
|
|
818
|
+
document.addEventListener('visibilitychange', function() {
|
|
819
|
+
if (document.visibilityState === 'visible') {
|
|
820
|
+
showCloseHint();
|
|
821
|
+
reportTabSurvived();
|
|
822
|
+
hiddenAt = 0;
|
|
823
|
+
return;
|
|
824
|
+
}
|
|
825
|
+
if (!hiddenAt) hiddenAt = Date.now();
|
|
826
|
+
reportClose('visibilitychange');
|
|
827
|
+
});
|
|
828
|
+
window.addEventListener('pagehide', function() {
|
|
829
|
+
if (!hiddenAt) hiddenAt = Date.now();
|
|
830
|
+
reportClose('pagehide');
|
|
831
|
+
});
|
|
832
|
+
window.close();
|
|
833
|
+
try { window.open('', '_self').close(); } catch(e) {}
|
|
834
|
+
setTimeout(function() {
|
|
835
|
+
reportClose('timeout');
|
|
836
|
+
if (document.visibilityState === 'visible') showCloseHint();
|
|
837
|
+
}, 300);
|
|
838
|
+
}` : `(async function() {
|
|
839
|
+
var fragment = new URLSearchParams(window.location.hash.substring(1));
|
|
840
|
+
var aggCode = fragment.get('agg_code');
|
|
841
|
+
var sessionKey = fragment.get('session_key');
|
|
842
|
+
|
|
843
|
+
window.history.replaceState(null, '', window.location.pathname + window.location.search);
|
|
844
|
+
|
|
845
|
+
if (!aggCode || !sessionKey) {
|
|
846
|
+
showError('Invalid verification link', ['This verification link is not valid.', 'Please go back and try again.'], 'link');
|
|
847
|
+
return;
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
try {
|
|
851
|
+
var res = await fetch(${serializedCompleteEndpoint}, {
|
|
678
852
|
method: 'POST',
|
|
679
853
|
credentials: 'include',
|
|
680
854
|
headers: { 'Content-Type': 'application/json' },
|
|
@@ -724,7 +898,7 @@ function getCompletionPageHtml(completeEndpoint) {
|
|
|
724
898
|
document.getElementById('hint').textContent = 'You may close this tab.';
|
|
725
899
|
document.getElementById('hint').classList.remove('hidden');
|
|
726
900
|
}, 300);
|
|
727
|
-
}
|
|
901
|
+
}`}
|
|
728
902
|
|
|
729
903
|
function showError(title, points, illust) {
|
|
730
904
|
document.getElementById('loading').classList.add('hidden');
|
|
@@ -759,8 +933,11 @@ var AuthenticationStrategy = {
|
|
|
759
933
|
TS43: "ts43",
|
|
760
934
|
LINK: "link",
|
|
761
935
|
DESKTOP: "desktop",
|
|
936
|
+
PASSKEY: "passkey",
|
|
937
|
+
CALL: "call",
|
|
762
938
|
NONE: ""
|
|
763
939
|
};
|
|
940
|
+
var SessionMetadataParentStrategy = "parent_strategy";
|
|
764
941
|
var SessionStatus = {
|
|
765
942
|
PENDING: "pending",
|
|
766
943
|
SCANNED: "scanned",
|
|
@@ -818,6 +995,18 @@ function validateGetPhoneNumberRequest(request) {
|
|
|
818
995
|
throw new ValidationError("Request validation failed", errors);
|
|
819
996
|
}
|
|
820
997
|
}
|
|
998
|
+
function validateDeletePasskeysRequest(request) {
|
|
999
|
+
const errors = {};
|
|
1000
|
+
if (!/^\+[1-9]\d{6,14}$/.test(request?.phone_number ?? "")) {
|
|
1001
|
+
errors.phone_number = "phone_number must be E.164";
|
|
1002
|
+
}
|
|
1003
|
+
if (!request?.origin) {
|
|
1004
|
+
errors.origin = "origin is required";
|
|
1005
|
+
}
|
|
1006
|
+
if (Object.keys(errors).length > 0) {
|
|
1007
|
+
throw new ValidationError("Request validation failed", errors);
|
|
1008
|
+
}
|
|
1009
|
+
}
|
|
821
1010
|
function validateSessionId(sessionId) {
|
|
822
1011
|
if (!sessionId) {
|
|
823
1012
|
throw new ValidationError("Request validation failed", {
|
|
@@ -1142,10 +1331,26 @@ function checkClaimShape(obj, iat, exp) {
|
|
|
1142
1331
|
if (!signOffs) {
|
|
1143
1332
|
throw malformed("sign_offs must be an array of well-formed entries");
|
|
1144
1333
|
}
|
|
1334
|
+
const factors = readFactors(obj);
|
|
1335
|
+
if (!factors) {
|
|
1336
|
+
throw malformed("factors must be an array of strings");
|
|
1337
|
+
}
|
|
1338
|
+
if (factors.length === 1 && factors[0] === "passkey") {
|
|
1339
|
+
if (signOffs.some((s) => s.role === CARRIER_ROLE)) {
|
|
1340
|
+
throw malformed("a passkey-only record must have no carrier sign-off");
|
|
1341
|
+
}
|
|
1342
|
+
return { signOffs, factors };
|
|
1343
|
+
}
|
|
1145
1344
|
if (!oneCarrierSignOff(signOffs)) {
|
|
1146
1345
|
throw malformed("sign_offs must have exactly one carrier:<id> carrier entry");
|
|
1147
1346
|
}
|
|
1148
|
-
return signOffs;
|
|
1347
|
+
return { signOffs, factors };
|
|
1348
|
+
}
|
|
1349
|
+
function readFactors(obj) {
|
|
1350
|
+
const raw = member(obj, "factors");
|
|
1351
|
+
if (raw === void 0) return ["carrier"];
|
|
1352
|
+
if (!Array.isArray(raw) || !raw.every((f) => typeof f === "string")) return null;
|
|
1353
|
+
return raw;
|
|
1149
1354
|
}
|
|
1150
1355
|
function readSignOffs(raw) {
|
|
1151
1356
|
if (!Array.isArray(raw)) return null;
|
|
@@ -1203,7 +1408,7 @@ function checkRecordClaims(payload, want) {
|
|
|
1203
1408
|
throw new VerificationRecordError(VerificationRecordErrorCode.WRONG_AUDIENCE, "aud does not match the expected audience");
|
|
1204
1409
|
}
|
|
1205
1410
|
const { iat, exp } = checkRecordTimes(obj, want);
|
|
1206
|
-
const signOffs = checkClaimShape(obj, iat, exp);
|
|
1411
|
+
const { signOffs, factors } = checkClaimShape(obj, iat, exp);
|
|
1207
1412
|
const verified = member(obj, "verified");
|
|
1208
1413
|
const r = new MemberReader(obj);
|
|
1209
1414
|
const claims = {
|
|
@@ -1215,6 +1420,7 @@ function checkRecordClaims(payload, want) {
|
|
|
1215
1420
|
exp,
|
|
1216
1421
|
verified,
|
|
1217
1422
|
sign_offs: signOffs,
|
|
1423
|
+
factors,
|
|
1218
1424
|
jti: r.str("jti") ?? "",
|
|
1219
1425
|
nonce: r.str("nonce") ?? "",
|
|
1220
1426
|
use_case: r.str("use_case") ?? "",
|
|
@@ -1223,10 +1429,12 @@ function checkRecordClaims(payload, want) {
|
|
|
1223
1429
|
biometric_verification: r.str("biometric_verification") ?? ""
|
|
1224
1430
|
};
|
|
1225
1431
|
assignDefined(claims, "strategy", r.str("strategy"));
|
|
1432
|
+
assignDefined(claims, "action_hash", r.str("action_hash"));
|
|
1226
1433
|
assignDefined(claims, "phone_number_details", readPhoneNumberDetails(r));
|
|
1227
1434
|
assignDefined(claims, "carrier", readCarrier(r));
|
|
1228
1435
|
assignDefined(claims, "sim_swap", readSwap(r, "sim_swap"));
|
|
1229
1436
|
assignDefined(claims, "device_swap", readSwap(r, "device_swap"));
|
|
1437
|
+
assignDefined(claims, "live_call", readLiveCall(r, "live_call"));
|
|
1230
1438
|
if (!r.ok) {
|
|
1231
1439
|
throw new VerificationRecordError(VerificationRecordErrorCode.MALFORMED, "claims have the wrong JSON types");
|
|
1232
1440
|
}
|
|
@@ -1296,6 +1504,19 @@ function readPhoneNumberDetails(r) {
|
|
|
1296
1504
|
r.ok = r.ok && s.ok;
|
|
1297
1505
|
return d;
|
|
1298
1506
|
}
|
|
1507
|
+
function readLiveCall(r, name) {
|
|
1508
|
+
const obj = r.object(name);
|
|
1509
|
+
if (!obj) return void 0;
|
|
1510
|
+
const s = new MemberReader(obj);
|
|
1511
|
+
const status = s.str("status");
|
|
1512
|
+
const info = {
|
|
1513
|
+
status: status ?? ""
|
|
1514
|
+
};
|
|
1515
|
+
assignDefined(info, "carrier_name", s.str("carrier_name"));
|
|
1516
|
+
assignDefined(info, "reason", s.str("reason"));
|
|
1517
|
+
r.ok = r.ok && s.ok && !!status;
|
|
1518
|
+
return info;
|
|
1519
|
+
}
|
|
1299
1520
|
function readSwap(r, name) {
|
|
1300
1521
|
const obj = r.object(name);
|
|
1301
1522
|
if (!obj) return void 0;
|
|
@@ -1414,6 +1635,13 @@ function verifyRecordSignature(record, key) {
|
|
|
1414
1635
|
}
|
|
1415
1636
|
}
|
|
1416
1637
|
|
|
1638
|
+
// src/verification-record/action-hash.ts
|
|
1639
|
+
import { createHash as createHash2 } from "crypto";
|
|
1640
|
+
function actionHash(action) {
|
|
1641
|
+
const canonical = Object.keys(action).sort((a, b) => Buffer.compare(Buffer.from(a), Buffer.from(b))).map((k) => `${k}=${action[k]}`).join("|");
|
|
1642
|
+
return createHash2("sha256").update(canonical).digest("base64url");
|
|
1643
|
+
}
|
|
1644
|
+
|
|
1417
1645
|
// src/client.ts
|
|
1418
1646
|
var MagicalAuthClient = class {
|
|
1419
1647
|
config;
|
|
@@ -1469,9 +1697,10 @@ var MagicalAuthClient = class {
|
|
|
1469
1697
|
/**
|
|
1470
1698
|
* Prepares authentication by checking carrier eligibility and generating protocol-specific requests.
|
|
1471
1699
|
*
|
|
1472
|
-
* Automatically generates `fe_code` / `fe_hash` for device binding. If the
|
|
1473
|
-
*
|
|
1474
|
-
*
|
|
1700
|
+
* Automatically generates `fe_code` / `fe_hash` for device binding. If the strategy or
|
|
1701
|
+
* its fallback is 'link', `feCode` is included in the result so the caller can set it
|
|
1702
|
+
* as an HttpOnly cookie for the link session (`fallback.session` when the fallback is
|
|
1703
|
+
* link) via `buildSetBindingCookieHeader()`.
|
|
1475
1704
|
*
|
|
1476
1705
|
* @param request - Prepare request parameters
|
|
1477
1706
|
* @param options - Per-request options (e.g., correlationId)
|
|
@@ -1498,8 +1727,9 @@ var MagicalAuthClient = class {
|
|
|
1498
1727
|
options
|
|
1499
1728
|
);
|
|
1500
1729
|
const result = { ...response };
|
|
1501
|
-
|
|
1502
|
-
|
|
1730
|
+
const link = response.authentication_strategy === AuthenticationStrategy.LINK ? response : response.fallback;
|
|
1731
|
+
if (link?.authentication_strategy === AuthenticationStrategy.LINK) {
|
|
1732
|
+
const linkData = link.data;
|
|
1503
1733
|
result.feCode = linkData.fe_code ?? feCode;
|
|
1504
1734
|
}
|
|
1505
1735
|
this.config.logger.debug("Auth prepared", {
|
|
@@ -1618,6 +1848,31 @@ var MagicalAuthClient = class {
|
|
|
1618
1848
|
keys: this.recordKeys
|
|
1619
1849
|
});
|
|
1620
1850
|
}
|
|
1851
|
+
/**
|
|
1852
|
+
* Verifies a step-up verification record and requires that it is verified,
|
|
1853
|
+
* carries both the `passkey` and `carrier` factors, is bound to `action` and
|
|
1854
|
+
* is for `phoneNumber` (E.164). The record is accepted past `exp` while its
|
|
1855
|
+
* `iat` is within `maxAgeSeconds` (non-finite, zero or negative means 300).
|
|
1856
|
+
* `expectedAudience` is the record aud to require; unset means this client's ID.
|
|
1857
|
+
*
|
|
1858
|
+
* @throws {VerificationRecordError} `EXPIRED` when older than `maxAgeSeconds`, `STEP_UP_REJECTED` when a step-up check fails, or any record verification code
|
|
1859
|
+
*/
|
|
1860
|
+
async verifyStepUp(record, action, phoneNumber, maxAgeSeconds = 300, expectedAudience) {
|
|
1861
|
+
const maxAge = Number.isFinite(maxAgeSeconds) && maxAgeSeconds > 0 ? maxAgeSeconds : 300;
|
|
1862
|
+
const rec = await this.verifyVerificationRecord(record, { allowExpired: true, maxAuditAgeSeconds: maxAge, expectedAudience });
|
|
1863
|
+
const c = rec.claims;
|
|
1864
|
+
const reject = (message) => new VerificationRecordError(VerificationRecordErrorCode.STEP_UP_REJECTED, message);
|
|
1865
|
+
if (Math.floor(Date.now() / 1e3) - c.iat > maxAge) {
|
|
1866
|
+
throw new VerificationRecordError(VerificationRecordErrorCode.EXPIRED, "record is older than maxAge");
|
|
1867
|
+
}
|
|
1868
|
+
if (!c.verified) throw reject("record is not verified");
|
|
1869
|
+
if (!c.factors.includes("passkey") || !c.factors.includes("carrier")) {
|
|
1870
|
+
throw reject("record lacks the passkey and carrier factors");
|
|
1871
|
+
}
|
|
1872
|
+
if (c.action_hash !== actionHash(action)) throw reject("action_hash does not match the action");
|
|
1873
|
+
if (c.sub !== `tel:${phoneNumber}`) throw reject("sub does not match the phone number");
|
|
1874
|
+
return rec;
|
|
1875
|
+
}
|
|
1621
1876
|
/**
|
|
1622
1877
|
* Retrieves the phone number associated with the device using digital credentials.
|
|
1623
1878
|
*
|
|
@@ -1647,6 +1902,26 @@ var MagicalAuthClient = class {
|
|
|
1647
1902
|
});
|
|
1648
1903
|
return response;
|
|
1649
1904
|
}
|
|
1905
|
+
/**
|
|
1906
|
+
* Deletes every passkey Glide holds for the number on origin, a registered origin of this application.
|
|
1907
|
+
*
|
|
1908
|
+
* @param request - Phone number and origin whose passkeys are deleted
|
|
1909
|
+
* @param options - Per-request options (e.g., correlationId)
|
|
1910
|
+
* @returns The deleted credential ids; empty when there were none
|
|
1911
|
+
* @throws {ValidationError} If phone_number is not E.164 or origin is missing
|
|
1912
|
+
* @throws {ForbiddenError} If origin is not registered to this application
|
|
1913
|
+
* @throws {MagicalAuthError} For other API errors
|
|
1914
|
+
*/
|
|
1915
|
+
async deletePasskeys(request, options) {
|
|
1916
|
+
validateDeletePasskeysRequest(request);
|
|
1917
|
+
const response = await this.doRequest(
|
|
1918
|
+
"POST",
|
|
1919
|
+
"/magic-auth/v2/auth/delete-passkeys",
|
|
1920
|
+
request,
|
|
1921
|
+
options
|
|
1922
|
+
);
|
|
1923
|
+
return { ...response, deleted_credential_ids: response?.deleted_credential_ids ?? [] };
|
|
1924
|
+
}
|
|
1650
1925
|
/**
|
|
1651
1926
|
* Reports when a user clicks the invoke/authenticate button.
|
|
1652
1927
|
*
|
|
@@ -1891,6 +2166,7 @@ export {
|
|
|
1891
2166
|
CarrierAppUnavailableError,
|
|
1892
2167
|
CarrierNotEligibleError,
|
|
1893
2168
|
CarrierRejectedError,
|
|
2169
|
+
CarrierTemporarilyUnavailableError,
|
|
1894
2170
|
DEFAULT_BASE_URL,
|
|
1895
2171
|
DEFAULT_TIMEOUT,
|
|
1896
2172
|
DesktopWaitError,
|
|
@@ -1908,6 +2184,7 @@ export {
|
|
|
1908
2184
|
RateLimitError,
|
|
1909
2185
|
SENSITIVE_KEYS,
|
|
1910
2186
|
ServiceUnavailableError,
|
|
2187
|
+
SessionMetadataParentStrategy,
|
|
1911
2188
|
SessionNotEligibleError,
|
|
1912
2189
|
SessionNotFoundError,
|
|
1913
2190
|
SessionStatus,
|
|
@@ -1919,6 +2196,7 @@ export {
|
|
|
1919
2196
|
ValidationError,
|
|
1920
2197
|
VerificationRecordError,
|
|
1921
2198
|
VerificationRecordErrorCode,
|
|
2199
|
+
actionHash,
|
|
1922
2200
|
assertValidFeCode,
|
|
1923
2201
|
buildClearBindingCookieHeader,
|
|
1924
2202
|
buildSetBindingCookieHeader,
|