@palbase/web 7.0.0 → 7.2.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-VXQUBBHG.js";
10
10
  import "../chunk-ACCJV6FV.js";
11
11
  import "../chunk-CFDU23TB.js";
12
12
  import "../chunk-PZ5AY32C.js";
@@ -964,6 +964,20 @@ var AuthClient = class {
964
964
  body: params
965
965
  });
966
966
  }
967
+ /**
968
+ * Give a PASSWORDLESS account its first password.
969
+ *
970
+ * Separate from `changePassword` because that one cannot serve the case: it
971
+ * sends a current password and palauth answers an account that has none with
972
+ * 401. Requires a RECENT sign-in (403 `reauthentication_required` otherwise,
973
+ * the same gate passkey enrollment carries); an account that already has a
974
+ * password gets 409 `password_already_set`.
975
+ */
976
+ async setPassword(params) {
977
+ return this.httpClient.request("POST", "/auth/password/set", {
978
+ body: params
979
+ });
980
+ }
967
981
  // ── Token ───────────────────────────────────────────────
968
982
  async refresh() {
969
983
  const refreshToken = this.tokenManager.getRefreshToken();
@@ -1487,6 +1501,162 @@ var PalbeAnalytics = class {
1487
1501
  }
1488
1502
  };
1489
1503
 
1504
+ // src/passkey.ts
1505
+ function base64UrlToBytes(value) {
1506
+ const padded = value.replace(/-/g, "+").replace(/_/g, "/");
1507
+ const binary = atob(padded.padEnd(padded.length + (4 - padded.length % 4) % 4, "="));
1508
+ const bytes = new Uint8Array(new ArrayBuffer(binary.length));
1509
+ for (let i = 0; i < binary.length; i += 1) bytes[i] = binary.charCodeAt(i);
1510
+ return bytes;
1511
+ }
1512
+ function bytesToBase64Url(buffer) {
1513
+ const bytes = new Uint8Array(buffer);
1514
+ let binary = "";
1515
+ for (const b of bytes) binary += String.fromCharCode(b);
1516
+ return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
1517
+ }
1518
+ function requireString(source, key) {
1519
+ const value = source[key];
1520
+ if (typeof value !== "string" || value === "") {
1521
+ throw new Error(`passkey options are missing ${key}`);
1522
+ }
1523
+ return value;
1524
+ }
1525
+ function toCreationOptions(publicKey) {
1526
+ const rp = publicKey.rp;
1527
+ const user = publicKey.user;
1528
+ if (!user?.id) {
1529
+ throw new Error("passkey options carry no user id \u2014 the credential would be unusable");
1530
+ }
1531
+ const excludeCredentials = Array.isArray(publicKey.excludeCredentials) ? publicKey.excludeCredentials.map((c) => ({
1532
+ id: base64UrlToBytes(c.id),
1533
+ type: "public-key"
1534
+ })) : void 0;
1535
+ return {
1536
+ challenge: base64UrlToBytes(requireString(publicKey, "challenge")),
1537
+ rp: { id: rp?.id, name: rp?.name ?? rp?.id ?? "" },
1538
+ user: {
1539
+ id: base64UrlToBytes(user.id),
1540
+ name: user.name ?? "",
1541
+ displayName: user.displayName ?? user.name ?? ""
1542
+ },
1543
+ pubKeyCredParams: publicKey.pubKeyCredParams ?? [],
1544
+ authenticatorSelection: publicKey.authenticatorSelection,
1545
+ timeout: publicKey.timeout,
1546
+ attestation: publicKey.attestation,
1547
+ excludeCredentials
1548
+ };
1549
+ }
1550
+ function toRequestOptions(publicKey) {
1551
+ const allowCredentials = Array.isArray(publicKey.allowCredentials) ? publicKey.allowCredentials.map((c) => ({
1552
+ id: base64UrlToBytes(c.id),
1553
+ type: "public-key"
1554
+ })) : void 0;
1555
+ return {
1556
+ challenge: base64UrlToBytes(requireString(publicKey, "challenge")),
1557
+ rpId: publicKey.rpId,
1558
+ timeout: publicKey.timeout,
1559
+ userVerification: publicKey.userVerification,
1560
+ allowCredentials
1561
+ };
1562
+ }
1563
+ function attestationToJSON(credential) {
1564
+ const response = credential.response;
1565
+ return {
1566
+ id: credential.id,
1567
+ rawId: bytesToBase64Url(credential.rawId),
1568
+ type: credential.type,
1569
+ response: {
1570
+ clientDataJSON: bytesToBase64Url(response.clientDataJSON),
1571
+ attestationObject: bytesToBase64Url(response.attestationObject)
1572
+ }
1573
+ };
1574
+ }
1575
+ function assertionToJSON(credential) {
1576
+ const response = credential.response;
1577
+ return {
1578
+ id: credential.id,
1579
+ rawId: bytesToBase64Url(credential.rawId),
1580
+ type: credential.type,
1581
+ response: {
1582
+ clientDataJSON: bytesToBase64Url(response.clientDataJSON),
1583
+ authenticatorData: bytesToBase64Url(response.authenticatorData),
1584
+ signature: bytesToBase64Url(response.signature),
1585
+ // The user handle is how palauth resolves WHO signed in on a discoverable
1586
+ // login — there is no identifier in the request.
1587
+ userHandle: response.userHandle ? bytesToBase64Url(response.userHandle) : null
1588
+ }
1589
+ };
1590
+ }
1591
+ function isPasskeySupported() {
1592
+ return typeof window !== "undefined" && typeof window.PublicKeyCredential === "function" && typeof navigator !== "undefined" && navigator.credentials !== void 0;
1593
+ }
1594
+ function ensureSupported() {
1595
+ if (!isPasskeySupported()) {
1596
+ throw new Error("This browser cannot use passkeys \u2014 offer a password or e-mail sign-in");
1597
+ }
1598
+ }
1599
+ async function createCredential(publicKey) {
1600
+ const credential = await navigator.credentials.create({
1601
+ publicKey: toCreationOptions(publicKey)
1602
+ });
1603
+ if (credential === null) {
1604
+ throw new Error("the browser returned no credential");
1605
+ }
1606
+ return credential;
1607
+ }
1608
+ async function getCredential(publicKey) {
1609
+ const credential = await navigator.credentials.get({
1610
+ publicKey: toRequestOptions(publicKey)
1611
+ });
1612
+ if (credential === null) {
1613
+ throw new Error("the browser returned no credential");
1614
+ }
1615
+ return credential;
1616
+ }
1617
+ async function passkeySignIn(rt) {
1618
+ ensureSupported();
1619
+ const envelope = await palbeRequest(rt, "POST", "/auth/webauthn/login/begin", {
1620
+ body: {}
1621
+ });
1622
+ const credential = await getCredential(envelope.options.publicKey);
1623
+ const raw = await palbeRequest(rt, "POST", "/auth/webauthn/login/finish", {
1624
+ body: assertionToJSON(credential)
1625
+ });
1626
+ const result = asWireAuthResult(raw);
1627
+ if (result === null) {
1628
+ throw new Error("passkey sign-in returned an unusable session");
1629
+ }
1630
+ return result;
1631
+ }
1632
+ async function passkeySignUp(rt, email, displayName) {
1633
+ ensureSupported();
1634
+ const begun = await palbeRequest(rt, "POST", "/auth/webauthn/signup/begin", {
1635
+ body: { email, user_name: displayName }
1636
+ });
1637
+ const credential = await createCredential(begun.options.publicKey);
1638
+ await palbeRequest(
1639
+ rt,
1640
+ "POST",
1641
+ `/auth/webauthn/signup/finish?user_id=${encodeURIComponent(begun.user_id)}`,
1642
+ { body: attestationToJSON(credential) }
1643
+ );
1644
+ return await passkeySignIn(rt);
1645
+ }
1646
+ async function passkeyRegister(rt, name) {
1647
+ ensureSupported();
1648
+ const envelope = await palbeRequest(
1649
+ rt,
1650
+ "POST",
1651
+ "/auth/webauthn/register/begin",
1652
+ { body: { name } }
1653
+ );
1654
+ const credential = await createCredential(envelope.options.publicKey);
1655
+ await palbeRequest(rt, "POST", "/auth/webauthn/register/finish", {
1656
+ body: attestationToJSON(credential)
1657
+ });
1658
+ }
1659
+
1490
1660
  // src/auth-facade.ts
1491
1661
  function mapWireUser(raw) {
1492
1662
  return {
@@ -1818,6 +1988,26 @@ var PalbeAuth = class {
1818
1988
  })
1819
1989
  );
1820
1990
  }
1991
+ /**
1992
+ * Give a passwordless account its FIRST password, in-app.
1993
+ *
1994
+ * For accounts created with a passkey or a social provider: they have no
1995
+ * current password, so `updatePassword` cannot serve them — it sends one and
1996
+ * the server answers 401. Before this existed the only route was the
1997
+ * password-RESET e-mail, which asks someone already signed in to go find
1998
+ * their inbox.
1999
+ *
2000
+ * Requires a RECENT sign-in, not merely a session: a first password is a
2001
+ * credential that outlives the rest, so the server answers 403
2002
+ * `reauthentication_required` on a stale one — the same gate
2003
+ * `registerPasskey` carries. An account that already has a password gets 409
2004
+ * `password_already_set`; change it with `updatePassword` instead.
2005
+ *
2006
+ * The session stays valid — no re-login.
2007
+ */
2008
+ async setPassword(newPassword) {
2009
+ unwrap(await this.rt.authClient.setPassword({ new_password: newPassword }));
2010
+ }
1821
2011
  async verifyEmail(params) {
1822
2012
  unwrap(await this.rt.authClient.verifyEmail(params));
1823
2013
  }
@@ -1841,6 +2031,39 @@ var PalbeAuth = class {
1841
2031
  return this.adopt(data);
1842
2032
  });
1843
2033
  }
2034
+ /**
2035
+ * Sign in with a passkey stored for this Environment. No identifier is typed:
2036
+ * the browser lists the accounts it holds and the user picks one.
2037
+ *
2038
+ * Throws when the browser cannot run a ceremony — check `passkeysSupported`
2039
+ * first and keep a password or e-mail path beside the button.
2040
+ */
2041
+ async signInWithPasskey() {
2042
+ return this.withSigningIn(async () => this.adoptWire(await passkeySignIn(this.rt)));
2043
+ }
2044
+ /**
2045
+ * Create an account whose only credential is a passkey — the e-mail is the
2046
+ * identifier, the passkey is the credential, and no password ever exists.
2047
+ */
2048
+ async signUpWithPasskey(email, displayName) {
2049
+ return this.withSigningIn(
2050
+ async () => this.adoptWire(await passkeySignUp(this.rt, email, displayName))
2051
+ );
2052
+ }
2053
+ /**
2054
+ * Add a passkey to the signed-in account, so the next sign-in is one tap.
2055
+ *
2056
+ * Rejects with a 403 `reauthentication_required` when the session is not
2057
+ * recent: a passkey outlives a password reset, so enrolling one takes more
2058
+ * than a valid session.
2059
+ */
2060
+ async registerPasskey(name) {
2061
+ await passkeyRegister(this.rt, name);
2062
+ }
2063
+ /** Whether this browser can run a passkey ceremony at all. */
2064
+ get passkeysSupported() {
2065
+ return isPasskeySupported();
2066
+ }
1844
2067
  async signInWithOAuth(params) {
1845
2068
  const { redirect = true, ...opts } = params;
1846
2069
  const { url } = unwrap(await this.rt.authClient.getOAuthURL(opts));
@@ -9084,7 +9307,7 @@ function defaultSessionStorage(key) {
9084
9307
  }
9085
9308
 
9086
9309
  // src/version.ts
9087
- var VERSION = "7.0.0";
9310
+ var VERSION = "7.2.0";
9088
9311
 
9089
9312
  // src/runtime.ts
9090
9313
  function buildRuntime(config) {