@palbase/web 6.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.
Files changed (37) hide show
  1. package/dist/{analytics-facade-Cp3d4QI-.d.ts → analytics-facade-CZIwbsvG.d.ts} +65 -1
  2. package/dist/{analytics-facade-CaluA4bW.d.cts → analytics-facade-esr6cOBN.d.cts} +65 -1
  3. package/dist/{chunk-BNAGRUIQ.js → chunk-ACCJV6FV.js} +32 -2
  4. package/dist/chunk-ACCJV6FV.js.map +1 -0
  5. package/dist/{chunk-KOJ4OAAR.js → chunk-P6TWUWZC.js} +209 -3
  6. package/dist/chunk-P6TWUWZC.js.map +1 -0
  7. package/dist/{chunk-OWLTZG2V.js → chunk-ZC6Q3GPX.js} +2 -2
  8. package/dist/index.cjs +207 -1
  9. package/dist/index.cjs.map +1 -1
  10. package/dist/index.d.cts +3 -3
  11. package/dist/index.d.ts +3 -3
  12. package/dist/index.js +2 -2
  13. package/dist/internal.cjs +238 -2
  14. package/dist/internal.cjs.map +1 -1
  15. package/dist/internal.d.cts +3 -3
  16. package/dist/internal.d.ts +3 -3
  17. package/dist/internal.js +2 -2
  18. package/dist/next/client.cjs +238 -2
  19. package/dist/next/client.cjs.map +1 -1
  20. package/dist/next/client.js +2 -2
  21. package/dist/next/index.cjs +238 -2
  22. package/dist/next/index.cjs.map +1 -1
  23. package/dist/next/index.d.cts +2 -2
  24. package/dist/next/index.d.ts +2 -2
  25. package/dist/next/index.js +3 -3
  26. package/dist/next/proxy.js +2 -2
  27. package/dist/{pb-DUK5qy81.d.ts → pb-AWwslm5m.d.ts} +1 -1
  28. package/dist/{pb-DvV6ftmf.d.cts → pb-BT-vKA4j.d.cts} +1 -1
  29. package/dist/react/index.cjs +1 -1
  30. package/dist/react/index.cjs.map +1 -1
  31. package/dist/react/index.d.cts +1 -1
  32. package/dist/react/index.d.ts +1 -1
  33. package/dist/react/index.js +2 -2
  34. package/package.json +1 -1
  35. package/dist/chunk-BNAGRUIQ.js.map +0 -1
  36. package/dist/chunk-KOJ4OAAR.js.map +0 -1
  37. /package/dist/{chunk-OWLTZG2V.js.map → chunk-ZC6Q3GPX.js.map} +0 -0
@@ -6,8 +6,8 @@ import {
6
6
  import {
7
7
  __configure,
8
8
  getRuntime
9
- } from "../chunk-KOJ4OAAR.js";
10
- import "../chunk-BNAGRUIQ.js";
9
+ } from "../chunk-P6TWUWZC.js";
10
+ import "../chunk-ACCJV6FV.js";
11
11
  import "../chunk-CFDU23TB.js";
12
12
  import "../chunk-PZ5AY32C.js";
13
13
 
@@ -166,6 +166,27 @@ var PalbaseError = class extends Error {
166
166
  this.details = details;
167
167
  }
168
168
  };
169
+ function detectPlatform() {
170
+ if (typeof Deno !== "undefined") {
171
+ return "deno";
172
+ }
173
+ if (process?.versions) {
174
+ if ("bun" in process.versions) {
175
+ return "bun";
176
+ }
177
+ if ("node" in process.versions) {
178
+ return "node";
179
+ }
180
+ }
181
+ if (typeof navigator !== "undefined" && navigator.product === "ReactNative") {
182
+ return "react-native";
183
+ }
184
+ return "browser";
185
+ }
186
+ function wirePlatform() {
187
+ const host = detectPlatform();
188
+ return host === "browser" ? "web" : host;
189
+ }
169
190
  var PALBASE_DEFAULT_HOST = "api.palbase.studio";
170
191
  var API_KEY_RE2 = /^pb_([a-z0-9]{4,24})_c[A-Za-z0-9]{20}$/;
171
192
  function parseEnvironmentRef(apiKey) {
@@ -251,8 +272,17 @@ var HttpClient = class _HttpClient {
251
272
  }
252
273
  buildHeaders(options) {
253
274
  const headers = {
254
- "Content-Type": "application/json"
275
+ "Content-Type": "application/json",
276
+ // Client identity, the web counterpart of the iOS SDK's
277
+ // ClientInfo.augment(). The server reads these to resolve flag targeting
278
+ // conditions and to label telemetry, so an app declares nothing and calls
279
+ // nothing — whatever the SDK can know, it sends.
280
+ "X-Platform": wirePlatform()
255
281
  };
282
+ const appVersion = this.options?.appVersion?.trim();
283
+ if (appVersion) {
284
+ headers["X-Palbase-Client-Version"] = appVersion;
285
+ }
256
286
  const effectiveKey = this.apiKey;
257
287
  if (effectiveKey) {
258
288
  headers["apikey"] = effectiveKey;
@@ -1457,6 +1487,162 @@ var PalbeAnalytics = class {
1457
1487
  }
1458
1488
  };
1459
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
+
1460
1646
  // src/auth-facade.ts
1461
1647
  function mapWireUser(raw) {
1462
1648
  return {
@@ -1593,6 +1779,23 @@ var PalbeAuth = class {
1593
1779
  const raw = await palbeRequest(this.rt, "GET", "/auth/user");
1594
1780
  return mapWireUser(raw);
1595
1781
  }
1782
+ /**
1783
+ * Which credentials this account actually has.
1784
+ *
1785
+ * An account reached through Google has no password, so showing it a
1786
+ * "current password" field gives the user something they can never fill —
1787
+ * and `getUser()` cannot tell you, because GET /auth/user carries no
1788
+ * credential information. Read this before rendering any re-auth or
1789
+ * change-password form.
1790
+ */
1791
+ async getSignInMethods() {
1792
+ const raw = await palbeRequest(this.rt, "GET", "/auth/me");
1793
+ return {
1794
+ password: raw.has_password,
1795
+ mfa: raw.has_mfa,
1796
+ providers: raw.providers ?? null
1797
+ };
1798
+ }
1596
1799
  async refreshUser() {
1597
1800
  const user = await this.getUser();
1598
1801
  const changed = !usersEqual(user, this.cachedUser);
@@ -1794,6 +1997,39 @@ var PalbeAuth = class {
1794
1997
  return this.adopt(data);
1795
1998
  });
1796
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
+ }
1797
2033
  async signInWithOAuth(params) {
1798
2034
  const { redirect = true, ...opts } = params;
1799
2035
  const { url } = unwrap(await this.rt.authClient.getOAuthURL(opts));
@@ -9037,7 +9273,7 @@ function defaultSessionStorage(key) {
9037
9273
  }
9038
9274
 
9039
9275
  // src/version.ts
9040
- var VERSION = "6.0.0";
9276
+ var VERSION = "7.1.0";
9041
9277
 
9042
9278
  // src/runtime.ts
9043
9279
  function buildRuntime(config) {