@palbase/web 7.0.0 → 7.1.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.
@@ -6,7 +6,7 @@ import {
6
6
  import {
7
7
  __configure,
8
8
  getRuntime
9
- } from "../chunk-7SNSLY5M.js";
9
+ } from "../chunk-P6TWUWZC.js";
10
10
  import "../chunk-ACCJV6FV.js";
11
11
  import "../chunk-CFDU23TB.js";
12
12
  import "../chunk-PZ5AY32C.js";
@@ -1487,6 +1487,162 @@ var PalbeAnalytics = class {
1487
1487
  }
1488
1488
  };
1489
1489
 
1490
+ // src/passkey.ts
1491
+ function base64UrlToBytes(value) {
1492
+ const padded = value.replace(/-/g, "+").replace(/_/g, "/");
1493
+ const binary = atob(padded.padEnd(padded.length + (4 - padded.length % 4) % 4, "="));
1494
+ const bytes = new Uint8Array(new ArrayBuffer(binary.length));
1495
+ for (let i = 0; i < binary.length; i += 1) bytes[i] = binary.charCodeAt(i);
1496
+ return bytes;
1497
+ }
1498
+ function bytesToBase64Url(buffer) {
1499
+ const bytes = new Uint8Array(buffer);
1500
+ let binary = "";
1501
+ for (const b of bytes) binary += String.fromCharCode(b);
1502
+ return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
1503
+ }
1504
+ function requireString(source, key) {
1505
+ const value = source[key];
1506
+ if (typeof value !== "string" || value === "") {
1507
+ throw new Error(`passkey options are missing ${key}`);
1508
+ }
1509
+ return value;
1510
+ }
1511
+ function toCreationOptions(publicKey) {
1512
+ const rp = publicKey.rp;
1513
+ const user = publicKey.user;
1514
+ if (!user?.id) {
1515
+ throw new Error("passkey options carry no user id \u2014 the credential would be unusable");
1516
+ }
1517
+ const excludeCredentials = Array.isArray(publicKey.excludeCredentials) ? publicKey.excludeCredentials.map((c) => ({
1518
+ id: base64UrlToBytes(c.id),
1519
+ type: "public-key"
1520
+ })) : void 0;
1521
+ return {
1522
+ challenge: base64UrlToBytes(requireString(publicKey, "challenge")),
1523
+ rp: { id: rp?.id, name: rp?.name ?? rp?.id ?? "" },
1524
+ user: {
1525
+ id: base64UrlToBytes(user.id),
1526
+ name: user.name ?? "",
1527
+ displayName: user.displayName ?? user.name ?? ""
1528
+ },
1529
+ pubKeyCredParams: publicKey.pubKeyCredParams ?? [],
1530
+ authenticatorSelection: publicKey.authenticatorSelection,
1531
+ timeout: publicKey.timeout,
1532
+ attestation: publicKey.attestation,
1533
+ excludeCredentials
1534
+ };
1535
+ }
1536
+ function toRequestOptions(publicKey) {
1537
+ const allowCredentials = Array.isArray(publicKey.allowCredentials) ? publicKey.allowCredentials.map((c) => ({
1538
+ id: base64UrlToBytes(c.id),
1539
+ type: "public-key"
1540
+ })) : void 0;
1541
+ return {
1542
+ challenge: base64UrlToBytes(requireString(publicKey, "challenge")),
1543
+ rpId: publicKey.rpId,
1544
+ timeout: publicKey.timeout,
1545
+ userVerification: publicKey.userVerification,
1546
+ allowCredentials
1547
+ };
1548
+ }
1549
+ function attestationToJSON(credential) {
1550
+ const response = credential.response;
1551
+ return {
1552
+ id: credential.id,
1553
+ rawId: bytesToBase64Url(credential.rawId),
1554
+ type: credential.type,
1555
+ response: {
1556
+ clientDataJSON: bytesToBase64Url(response.clientDataJSON),
1557
+ attestationObject: bytesToBase64Url(response.attestationObject)
1558
+ }
1559
+ };
1560
+ }
1561
+ function assertionToJSON(credential) {
1562
+ const response = credential.response;
1563
+ return {
1564
+ id: credential.id,
1565
+ rawId: bytesToBase64Url(credential.rawId),
1566
+ type: credential.type,
1567
+ response: {
1568
+ clientDataJSON: bytesToBase64Url(response.clientDataJSON),
1569
+ authenticatorData: bytesToBase64Url(response.authenticatorData),
1570
+ signature: bytesToBase64Url(response.signature),
1571
+ // The user handle is how palauth resolves WHO signed in on a discoverable
1572
+ // login — there is no identifier in the request.
1573
+ userHandle: response.userHandle ? bytesToBase64Url(response.userHandle) : null
1574
+ }
1575
+ };
1576
+ }
1577
+ function isPasskeySupported() {
1578
+ return typeof window !== "undefined" && typeof window.PublicKeyCredential === "function" && typeof navigator !== "undefined" && navigator.credentials !== void 0;
1579
+ }
1580
+ function ensureSupported() {
1581
+ if (!isPasskeySupported()) {
1582
+ throw new Error("This browser cannot use passkeys \u2014 offer a password or e-mail sign-in");
1583
+ }
1584
+ }
1585
+ async function createCredential(publicKey) {
1586
+ const credential = await navigator.credentials.create({
1587
+ publicKey: toCreationOptions(publicKey)
1588
+ });
1589
+ if (credential === null) {
1590
+ throw new Error("the browser returned no credential");
1591
+ }
1592
+ return credential;
1593
+ }
1594
+ async function getCredential(publicKey) {
1595
+ const credential = await navigator.credentials.get({
1596
+ publicKey: toRequestOptions(publicKey)
1597
+ });
1598
+ if (credential === null) {
1599
+ throw new Error("the browser returned no credential");
1600
+ }
1601
+ return credential;
1602
+ }
1603
+ async function passkeySignIn(rt) {
1604
+ ensureSupported();
1605
+ const envelope = await palbeRequest(rt, "POST", "/auth/webauthn/login/begin", {
1606
+ body: {}
1607
+ });
1608
+ const credential = await getCredential(envelope.options.publicKey);
1609
+ const raw = await palbeRequest(rt, "POST", "/auth/webauthn/login/finish", {
1610
+ body: assertionToJSON(credential)
1611
+ });
1612
+ const result = asWireAuthResult(raw);
1613
+ if (result === null) {
1614
+ throw new Error("passkey sign-in returned an unusable session");
1615
+ }
1616
+ return result;
1617
+ }
1618
+ async function passkeySignUp(rt, email, displayName) {
1619
+ ensureSupported();
1620
+ const begun = await palbeRequest(rt, "POST", "/auth/webauthn/signup/begin", {
1621
+ body: { email, user_name: displayName }
1622
+ });
1623
+ const credential = await createCredential(begun.options.publicKey);
1624
+ await palbeRequest(
1625
+ rt,
1626
+ "POST",
1627
+ `/auth/webauthn/signup/finish?user_id=${encodeURIComponent(begun.user_id)}`,
1628
+ { body: attestationToJSON(credential) }
1629
+ );
1630
+ return await passkeySignIn(rt);
1631
+ }
1632
+ async function passkeyRegister(rt, name) {
1633
+ ensureSupported();
1634
+ const envelope = await palbeRequest(
1635
+ rt,
1636
+ "POST",
1637
+ "/auth/webauthn/register/begin",
1638
+ { body: { name } }
1639
+ );
1640
+ const credential = await createCredential(envelope.options.publicKey);
1641
+ await palbeRequest(rt, "POST", "/auth/webauthn/register/finish", {
1642
+ body: attestationToJSON(credential)
1643
+ });
1644
+ }
1645
+
1490
1646
  // src/auth-facade.ts
1491
1647
  function mapWireUser(raw) {
1492
1648
  return {
@@ -1841,6 +1997,39 @@ var PalbeAuth = class {
1841
1997
  return this.adopt(data);
1842
1998
  });
1843
1999
  }
2000
+ /**
2001
+ * Sign in with a passkey stored for this Environment. No identifier is typed:
2002
+ * the browser lists the accounts it holds and the user picks one.
2003
+ *
2004
+ * Throws when the browser cannot run a ceremony — check `passkeysSupported`
2005
+ * first and keep a password or e-mail path beside the button.
2006
+ */
2007
+ async signInWithPasskey() {
2008
+ return this.withSigningIn(async () => this.adoptWire(await passkeySignIn(this.rt)));
2009
+ }
2010
+ /**
2011
+ * Create an account whose only credential is a passkey — the e-mail is the
2012
+ * identifier, the passkey is the credential, and no password ever exists.
2013
+ */
2014
+ async signUpWithPasskey(email, displayName) {
2015
+ return this.withSigningIn(
2016
+ async () => this.adoptWire(await passkeySignUp(this.rt, email, displayName))
2017
+ );
2018
+ }
2019
+ /**
2020
+ * Add a passkey to the signed-in account, so the next sign-in is one tap.
2021
+ *
2022
+ * Rejects with a 403 `reauthentication_required` when the session is not
2023
+ * recent: a passkey outlives a password reset, so enrolling one takes more
2024
+ * than a valid session.
2025
+ */
2026
+ async registerPasskey(name) {
2027
+ await passkeyRegister(this.rt, name);
2028
+ }
2029
+ /** Whether this browser can run a passkey ceremony at all. */
2030
+ get passkeysSupported() {
2031
+ return isPasskeySupported();
2032
+ }
1844
2033
  async signInWithOAuth(params) {
1845
2034
  const { redirect = true, ...opts } = params;
1846
2035
  const { url } = unwrap(await this.rt.authClient.getOAuthURL(opts));
@@ -9084,7 +9273,7 @@ function defaultSessionStorage(key) {
9084
9273
  }
9085
9274
 
9086
9275
  // src/version.ts
9087
- var VERSION = "7.0.0";
9276
+ var VERSION = "7.1.0";
9088
9277
 
9089
9278
  // src/runtime.ts
9090
9279
  function buildRuntime(config) {