@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.
@@ -1,7 +1,7 @@
1
- import { P as PalbeConfig } from './analytics-facade-TB-D4ww8.cjs';
2
- export { d as PalbeRuntime, e as buildRuntime } from './analytics-facade-TB-D4ww8.cjs';
1
+ import { P as PalbeConfig } from './analytics-facade-jyXv7Cu4.cjs';
2
+ export { d as PalbeRuntime, e as buildRuntime } from './analytics-facade-jyXv7Cu4.cjs';
3
3
  import { B as BackendError } from './errors-fDoNdTrJ.cjs';
4
- export { P as PB, c as createBoundClient } from './pb-CiSj4lM3.cjs';
4
+ export { P as PB, c as createBoundClient } from './pb-CfYQGEn0.cjs';
5
5
  import './storage-BPaeSG8K.cjs';
6
6
  import './pooled-flags-4GtDtsiu.js';
7
7
 
@@ -1,7 +1,7 @@
1
- import { P as PalbeConfig } from './analytics-facade-CdTK9Y0s.js';
2
- export { d as PalbeRuntime, e as buildRuntime } from './analytics-facade-CdTK9Y0s.js';
1
+ import { P as PalbeConfig } from './analytics-facade-DLfnVVwL.js';
2
+ export { d as PalbeRuntime, e as buildRuntime } from './analytics-facade-DLfnVVwL.js';
3
3
  import { B as BackendError } from './errors-fDoNdTrJ.js';
4
- export { P as PB, c as createBoundClient } from './pb-B6ZcZy8v.js';
4
+ export { P as PB, c as createBoundClient } from './pb-DdzpEkPY.js';
5
5
  import './storage-BPaeSG8K.js';
6
6
  import './pooled-flags-4GtDtsiu.js';
7
7
 
package/dist/internal.js CHANGED
@@ -6,7 +6,7 @@ import {
6
6
  createBoundClient,
7
7
  getRuntime,
8
8
  onConfigured
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";
@@ -735,6 +735,20 @@ var AuthClient = class {
735
735
  body: params
736
736
  });
737
737
  }
738
+ /**
739
+ * Give a PASSWORDLESS account its first password.
740
+ *
741
+ * Separate from `changePassword` because that one cannot serve the case: it
742
+ * sends a current password and palauth answers an account that has none with
743
+ * 401. Requires a RECENT sign-in (403 `reauthentication_required` otherwise,
744
+ * the same gate passkey enrollment carries); an account that already has a
745
+ * password gets 409 `password_already_set`.
746
+ */
747
+ async setPassword(params) {
748
+ return this.httpClient.request("POST", "/auth/password/set", {
749
+ body: params
750
+ });
751
+ }
738
752
  // ── Token ───────────────────────────────────────────────
739
753
  async refresh() {
740
754
  const refreshToken = this.tokenManager.getRefreshToken();
@@ -1278,6 +1292,162 @@ function asWireAuthResult(raw) {
1278
1292
  return raw;
1279
1293
  }
1280
1294
 
1295
+ // src/passkey.ts
1296
+ function base64UrlToBytes(value) {
1297
+ const padded = value.replace(/-/g, "+").replace(/_/g, "/");
1298
+ const binary = atob(padded.padEnd(padded.length + (4 - padded.length % 4) % 4, "="));
1299
+ const bytes = new Uint8Array(new ArrayBuffer(binary.length));
1300
+ for (let i = 0; i < binary.length; i += 1) bytes[i] = binary.charCodeAt(i);
1301
+ return bytes;
1302
+ }
1303
+ function bytesToBase64Url(buffer) {
1304
+ const bytes = new Uint8Array(buffer);
1305
+ let binary = "";
1306
+ for (const b of bytes) binary += String.fromCharCode(b);
1307
+ return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
1308
+ }
1309
+ function requireString(source, key) {
1310
+ const value = source[key];
1311
+ if (typeof value !== "string" || value === "") {
1312
+ throw new Error(`passkey options are missing ${key}`);
1313
+ }
1314
+ return value;
1315
+ }
1316
+ function toCreationOptions(publicKey) {
1317
+ const rp = publicKey.rp;
1318
+ const user = publicKey.user;
1319
+ if (!user?.id) {
1320
+ throw new Error("passkey options carry no user id \u2014 the credential would be unusable");
1321
+ }
1322
+ const excludeCredentials = Array.isArray(publicKey.excludeCredentials) ? publicKey.excludeCredentials.map((c) => ({
1323
+ id: base64UrlToBytes(c.id),
1324
+ type: "public-key"
1325
+ })) : void 0;
1326
+ return {
1327
+ challenge: base64UrlToBytes(requireString(publicKey, "challenge")),
1328
+ rp: { id: rp?.id, name: rp?.name ?? rp?.id ?? "" },
1329
+ user: {
1330
+ id: base64UrlToBytes(user.id),
1331
+ name: user.name ?? "",
1332
+ displayName: user.displayName ?? user.name ?? ""
1333
+ },
1334
+ pubKeyCredParams: publicKey.pubKeyCredParams ?? [],
1335
+ authenticatorSelection: publicKey.authenticatorSelection,
1336
+ timeout: publicKey.timeout,
1337
+ attestation: publicKey.attestation,
1338
+ excludeCredentials
1339
+ };
1340
+ }
1341
+ function toRequestOptions(publicKey) {
1342
+ const allowCredentials = Array.isArray(publicKey.allowCredentials) ? publicKey.allowCredentials.map((c) => ({
1343
+ id: base64UrlToBytes(c.id),
1344
+ type: "public-key"
1345
+ })) : void 0;
1346
+ return {
1347
+ challenge: base64UrlToBytes(requireString(publicKey, "challenge")),
1348
+ rpId: publicKey.rpId,
1349
+ timeout: publicKey.timeout,
1350
+ userVerification: publicKey.userVerification,
1351
+ allowCredentials
1352
+ };
1353
+ }
1354
+ function attestationToJSON(credential) {
1355
+ const response = credential.response;
1356
+ return {
1357
+ id: credential.id,
1358
+ rawId: bytesToBase64Url(credential.rawId),
1359
+ type: credential.type,
1360
+ response: {
1361
+ clientDataJSON: bytesToBase64Url(response.clientDataJSON),
1362
+ attestationObject: bytesToBase64Url(response.attestationObject)
1363
+ }
1364
+ };
1365
+ }
1366
+ function assertionToJSON(credential) {
1367
+ const response = credential.response;
1368
+ return {
1369
+ id: credential.id,
1370
+ rawId: bytesToBase64Url(credential.rawId),
1371
+ type: credential.type,
1372
+ response: {
1373
+ clientDataJSON: bytesToBase64Url(response.clientDataJSON),
1374
+ authenticatorData: bytesToBase64Url(response.authenticatorData),
1375
+ signature: bytesToBase64Url(response.signature),
1376
+ // The user handle is how palauth resolves WHO signed in on a discoverable
1377
+ // login — there is no identifier in the request.
1378
+ userHandle: response.userHandle ? bytesToBase64Url(response.userHandle) : null
1379
+ }
1380
+ };
1381
+ }
1382
+ function isPasskeySupported() {
1383
+ return typeof window !== "undefined" && typeof window.PublicKeyCredential === "function" && typeof navigator !== "undefined" && navigator.credentials !== void 0;
1384
+ }
1385
+ function ensureSupported() {
1386
+ if (!isPasskeySupported()) {
1387
+ throw new Error("This browser cannot use passkeys \u2014 offer a password or e-mail sign-in");
1388
+ }
1389
+ }
1390
+ async function createCredential(publicKey) {
1391
+ const credential = await navigator.credentials.create({
1392
+ publicKey: toCreationOptions(publicKey)
1393
+ });
1394
+ if (credential === null) {
1395
+ throw new Error("the browser returned no credential");
1396
+ }
1397
+ return credential;
1398
+ }
1399
+ async function getCredential(publicKey) {
1400
+ const credential = await navigator.credentials.get({
1401
+ publicKey: toRequestOptions(publicKey)
1402
+ });
1403
+ if (credential === null) {
1404
+ throw new Error("the browser returned no credential");
1405
+ }
1406
+ return credential;
1407
+ }
1408
+ async function passkeySignIn(rt) {
1409
+ ensureSupported();
1410
+ const envelope = await palbeRequest(rt, "POST", "/auth/webauthn/login/begin", {
1411
+ body: {}
1412
+ });
1413
+ const credential = await getCredential(envelope.options.publicKey);
1414
+ const raw = await palbeRequest(rt, "POST", "/auth/webauthn/login/finish", {
1415
+ body: assertionToJSON(credential)
1416
+ });
1417
+ const result = asWireAuthResult(raw);
1418
+ if (result === null) {
1419
+ throw new Error("passkey sign-in returned an unusable session");
1420
+ }
1421
+ return result;
1422
+ }
1423
+ async function passkeySignUp(rt, email, displayName) {
1424
+ ensureSupported();
1425
+ const begun = await palbeRequest(rt, "POST", "/auth/webauthn/signup/begin", {
1426
+ body: { email, user_name: displayName }
1427
+ });
1428
+ const credential = await createCredential(begun.options.publicKey);
1429
+ await palbeRequest(
1430
+ rt,
1431
+ "POST",
1432
+ `/auth/webauthn/signup/finish?user_id=${encodeURIComponent(begun.user_id)}`,
1433
+ { body: attestationToJSON(credential) }
1434
+ );
1435
+ return await passkeySignIn(rt);
1436
+ }
1437
+ async function passkeyRegister(rt, name) {
1438
+ ensureSupported();
1439
+ const envelope = await palbeRequest(
1440
+ rt,
1441
+ "POST",
1442
+ "/auth/webauthn/register/begin",
1443
+ { body: { name } }
1444
+ );
1445
+ const credential = await createCredential(envelope.options.publicKey);
1446
+ await palbeRequest(rt, "POST", "/auth/webauthn/register/finish", {
1447
+ body: attestationToJSON(credential)
1448
+ });
1449
+ }
1450
+
1281
1451
  // src/auth-facade.ts
1282
1452
  function mapWireUser(raw) {
1283
1453
  return {
@@ -1609,6 +1779,26 @@ var PalbeAuth = class {
1609
1779
  })
1610
1780
  );
1611
1781
  }
1782
+ /**
1783
+ * Give a passwordless account its FIRST password, in-app.
1784
+ *
1785
+ * For accounts created with a passkey or a social provider: they have no
1786
+ * current password, so `updatePassword` cannot serve them — it sends one and
1787
+ * the server answers 401. Before this existed the only route was the
1788
+ * password-RESET e-mail, which asks someone already signed in to go find
1789
+ * their inbox.
1790
+ *
1791
+ * Requires a RECENT sign-in, not merely a session: a first password is a
1792
+ * credential that outlives the rest, so the server answers 403
1793
+ * `reauthentication_required` on a stale one — the same gate
1794
+ * `registerPasskey` carries. An account that already has a password gets 409
1795
+ * `password_already_set`; change it with `updatePassword` instead.
1796
+ *
1797
+ * The session stays valid — no re-login.
1798
+ */
1799
+ async setPassword(newPassword) {
1800
+ unwrap(await this.rt.authClient.setPassword({ new_password: newPassword }));
1801
+ }
1612
1802
  async verifyEmail(params) {
1613
1803
  unwrap(await this.rt.authClient.verifyEmail(params));
1614
1804
  }
@@ -1632,6 +1822,39 @@ var PalbeAuth = class {
1632
1822
  return this.adopt(data);
1633
1823
  });
1634
1824
  }
1825
+ /**
1826
+ * Sign in with a passkey stored for this Environment. No identifier is typed:
1827
+ * the browser lists the accounts it holds and the user picks one.
1828
+ *
1829
+ * Throws when the browser cannot run a ceremony — check `passkeysSupported`
1830
+ * first and keep a password or e-mail path beside the button.
1831
+ */
1832
+ async signInWithPasskey() {
1833
+ return this.withSigningIn(async () => this.adoptWire(await passkeySignIn(this.rt)));
1834
+ }
1835
+ /**
1836
+ * Create an account whose only credential is a passkey — the e-mail is the
1837
+ * identifier, the passkey is the credential, and no password ever exists.
1838
+ */
1839
+ async signUpWithPasskey(email, displayName) {
1840
+ return this.withSigningIn(
1841
+ async () => this.adoptWire(await passkeySignUp(this.rt, email, displayName))
1842
+ );
1843
+ }
1844
+ /**
1845
+ * Add a passkey to the signed-in account, so the next sign-in is one tap.
1846
+ *
1847
+ * Rejects with a 403 `reauthentication_required` when the session is not
1848
+ * recent: a passkey outlives a password reset, so enrolling one takes more
1849
+ * than a valid session.
1850
+ */
1851
+ async registerPasskey(name) {
1852
+ await passkeyRegister(this.rt, name);
1853
+ }
1854
+ /** Whether this browser can run a passkey ceremony at all. */
1855
+ get passkeysSupported() {
1856
+ return isPasskeySupported();
1857
+ }
1635
1858
  async signInWithOAuth(params) {
1636
1859
  const { redirect = true, ...opts } = params;
1637
1860
  const { url } = unwrap(await this.rt.authClient.getOAuthURL(opts));
@@ -8875,7 +9098,7 @@ function defaultSessionStorage(key) {
8875
9098
  }
8876
9099
 
8877
9100
  // src/version.ts
8878
- var VERSION = "7.0.0";
9101
+ var VERSION = "7.2.0";
8879
9102
 
8880
9103
  // src/runtime.ts
8881
9104
  function buildRuntime(config) {