cauthy-auth 0.22.0 → 0.23.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/dist/index.mjs CHANGED
@@ -1143,6 +1143,9 @@ function createCauthy(given = {}) {
1143
1143
  "/me/mfa/phone/remove",
1144
1144
  "/sign-in/mfa/methods",
1145
1145
  "/sign-in/mfa/send",
1146
+ "/sign-in/mfa/push",
1147
+ "/sign-in/mfa/push/events",
1148
+ "/sign-in/mfa/push/finish",
1146
1149
  "/me/sessions",
1147
1150
  "/me/sessions/end",
1148
1151
  "/me/email",
@@ -1372,6 +1375,28 @@ function createCauthy(given = {}) {
1372
1375
  }
1373
1376
  return json({ account: accountOf(res.data) }, 200, mine.headers);
1374
1377
  }
1378
+ if (route === "/sign-in/mfa/push/events" && !post) {
1379
+ const sealed = readCookies(request.headers.get("cookie")).get(stepCookie);
1380
+ const step = sealed ? await unseal(sealed, await key) : null;
1381
+ if (step === null || step.kind !== "mfa") return problem(400, "step_expired", "Sign in again: the code was asked for too long ago.");
1382
+ let upstream;
1383
+ try {
1384
+ upstream = await doFetch(`${issuer}/v1/auth/mfa/push/events`, {
1385
+ headers: { "x-tenant-key": tenant, "x-mfa-token": step.token, accept: "text/event-stream" },
1386
+ signal: request.signal
1387
+ });
1388
+ } catch {
1389
+ return problem(503, "unavailable", "Cauthy could not be reached.");
1390
+ }
1391
+ if (!upstream.ok || !upstream.body) {
1392
+ const b2 = await upstream.json().catch(() => null);
1393
+ return problem(upstream.status, String(b2?.errors?.[0]?.code ?? "error"), String(b2?.errors?.[0]?.message ?? "The request could not be completed."));
1394
+ }
1395
+ return new Response(upstream.body, {
1396
+ status: 200,
1397
+ headers: { "content-type": "text/event-stream", "cache-control": "no-cache, no-store", "x-accel-buffering": "no" }
1398
+ });
1399
+ }
1375
1400
  if (!post) return new Response("Method not allowed", { status: 405, headers: { allow: "POST" } });
1376
1401
  if (!ownRequest(request)) return problem(403, "cross_site", "This request did not come from this application.");
1377
1402
  if (route === "/sign-out") {
@@ -1423,6 +1448,19 @@ function createCauthy(given = {}) {
1423
1448
  if (res.error) return problem(res.status, res.error.code, res.error.message, "code");
1424
1449
  return signedIn(request, res.data, [writeCookie(stepCookie, "", attrs(request, 0))]);
1425
1450
  }
1451
+ if (route === "/sign-in/mfa/push" || route === "/sign-in/mfa/push/finish") {
1452
+ const sealed = readCookies(request.headers.get("cookie")).get(stepCookie);
1453
+ const step = sealed ? await unseal(sealed, await key) : null;
1454
+ if (step === null || step.kind !== "mfa") return problem(400, "step_expired", "Sign in again: the code was asked for too long ago.");
1455
+ if (route === "/sign-in/mfa/push") {
1456
+ const res2 = await ask(request, "POST", "/v1/auth/mfa/push", { mfa_token: step.token });
1457
+ if (res2.error) return problem(res2.status, res2.error.code, res2.error.message);
1458
+ return json({ status: "sent", number: Number(res2.data?.number ?? 0) });
1459
+ }
1460
+ const res = await ask(request, "POST", "/v1/auth/mfa/challenge", { mfa_token: step.token, push: true });
1461
+ if (res.error) return problem(res.status, res.error.code, res.error.message);
1462
+ return signedIn(request, res.data, [writeCookie(stepCookie, "", attrs(request, 0))]);
1463
+ }
1426
1464
  if (route === "/sign-in/mfa/methods" || route === "/sign-in/mfa/send") {
1427
1465
  const sealed = readCookies(request.headers.get("cookie")).get(stepCookie);
1428
1466
  const step = sealed ? await unseal(sealed, await key) : null;
@@ -1431,7 +1469,13 @@ function createCauthy(given = {}) {
1431
1469
  const res2 = await ask(request, "POST", "/v1/auth/mfa/methods", { mfa_token: step.token });
1432
1470
  if (res2.error) return problem(res2.status, res2.error.code, res2.error.message);
1433
1471
  const p = res2.data?.phone;
1434
- return json({ status: "ok", totp: res2.data?.totp === true, phone: p ? { number: String(p.number ?? ""), channel: String(p.channel ?? "sms") } : null });
1472
+ const push = res2.data?.push;
1473
+ return json({
1474
+ status: "ok",
1475
+ totp: res2.data?.totp === true,
1476
+ phone: p ? { number: String(p.number ?? ""), channel: String(p.channel ?? "sms") } : null,
1477
+ push: push ? { devices: Number(push.devices ?? 0) } : null
1478
+ });
1435
1479
  }
1436
1480
  const res = await ask(request, "POST", "/v1/auth/mfa/send", { mfa_token: step.token });
1437
1481
  if (res.error) return problem(res.status, res.error.code, res.error.message);