@spacelr/sdk 0.12.0 → 0.14.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
@@ -1824,13 +1824,14 @@ function mapProfile(u) {
1824
1824
  const name = u.name ?? ([u.firstName, u.lastName].filter(Boolean).join(" ") || void 0) ?? u.displayName;
1825
1825
  return {
1826
1826
  id: u.id ?? u._id ?? u.userId ?? u.sub ?? "",
1827
- email: u.email ?? "",
1827
+ // Honest null for email-less (passkey-only) accounts (#780), not a '' coercion.
1828
+ email: u.email ?? null,
1828
1829
  username: u.username,
1829
1830
  name,
1830
1831
  roles: u.roles ?? []
1831
1832
  };
1832
1833
  }
1833
- var AuthModule = class {
1834
+ var AuthModule = class _AuthModule {
1834
1835
  constructor(http, tokenManager, config) {
1835
1836
  this.stateListeners = /* @__PURE__ */ new Set();
1836
1837
  this.lastEmittedState = null;
@@ -2146,6 +2147,82 @@ var AuthModule = class {
2146
2147
  await this.storeTokensFromLogin(result);
2147
2148
  return result;
2148
2149
  }
2150
+ /**
2151
+ * Fetch WebAuthn attestation options to begin an email-less passkey SIGNUP
2152
+ * (#780) — creating a brand-new account with no email and no password. The
2153
+ * project must have passkeys AND public registration enabled. No account is
2154
+ * created until {@link verifyPasskeySignup} succeeds.
2155
+ *
2156
+ * IMPORTANT (identity routing, B8): a signup ceremony always mints a NEW
2157
+ * account. Before offering signup, the app MUST first attempt a discoverable
2158
+ * passkey login ({@link beginPasskeyLogin} with no email); only offer signup
2159
+ * when the user has no existing passkey. Skipping this creates duplicate,
2160
+ * fragmented accounts. {@link createUserWithPasskey} bundles that fallback.
2161
+ */
2162
+ async beginPasskeySignup(displayName) {
2163
+ return this.http.request({
2164
+ method: "POST",
2165
+ path: "/auth/passkey/signup/options",
2166
+ body: displayName ? { displayName } : {}
2167
+ });
2168
+ }
2169
+ /**
2170
+ * Verify a WebAuthn attestation produced by `navigator.credentials.create()`
2171
+ * against the options from {@link beginPasskeySignup}, materializing the
2172
+ * email-less account and returning JWT tokens (the caller is now logged in).
2173
+ */
2174
+ async verifyPasskeySignup(response) {
2175
+ const result = await this.http.request({
2176
+ method: "POST",
2177
+ path: "/auth/passkey/signup/verify",
2178
+ body: { response }
2179
+ });
2180
+ await this.storeTokensFromLogin(result);
2181
+ return result;
2182
+ }
2183
+ /**
2184
+ * One-call email-less passkey signup with the B1 idempotency fallback baked
2185
+ * in. The browser WebAuthn functions are injected (the SDK stays transport-
2186
+ * only and does not depend on `@simplewebauthn/browser`):
2187
+ *
2188
+ * 1. `beginPasskeySignup` → `startRegistration` → `verifyPasskeySignup`.
2189
+ * 2. If verify fails with `challenge_expired`, a network/timeout error, or
2190
+ * `passkey_already_registered` (the account was in fact created but the
2191
+ * response was lost, or the passkey already exists), fall back to a fresh
2192
+ * discoverable LOGIN ceremony and return that session. That fallback IS the
2193
+ * idempotency mechanism — the server never mints a session from a bare
2194
+ * credential lookup.
2195
+ * 3. If the login fallback also fails (genuinely no credential), the original
2196
+ * error is surfaced so the app can restart signup.
2197
+ */
2198
+ async createUserWithPasskey(ceremony) {
2199
+ const options = await this.beginPasskeySignup(ceremony.displayName);
2200
+ const attestation = await ceremony.startRegistration(options);
2201
+ try {
2202
+ return await this.verifyPasskeySignup(attestation);
2203
+ } catch (error) {
2204
+ if (!_AuthModule.isSignupFallbackError(error)) {
2205
+ throw error;
2206
+ }
2207
+ try {
2208
+ const loginOptions = await this.beginPasskeyLogin();
2209
+ const assertion = await ceremony.startAuthentication(loginOptions);
2210
+ return await this.verifyPasskeyLogin(assertion);
2211
+ } catch {
2212
+ throw error;
2213
+ }
2214
+ }
2215
+ }
2216
+ static isSignupFallbackError(error) {
2217
+ if (error instanceof SpacelrNetworkError || error instanceof SpacelrTimeoutError) {
2218
+ return true;
2219
+ }
2220
+ if (error instanceof SpacelrError) {
2221
+ const message = (error.message || "").toLowerCase();
2222
+ return error.statusCode === 409 || message.includes("challenge_expired") || message.includes("already_registered");
2223
+ }
2224
+ return false;
2225
+ }
2149
2226
  /**
2150
2227
  * Fetch WebAuthn attestation options to register a new passkey for the
2151
2228
  * currently authenticated user.
@@ -2212,6 +2289,53 @@ var AuthModule = class {
2212
2289
  });
2213
2290
  this.emitState("authenticated");
2214
2291
  }
2292
+ // ─── Recover-with-phrase (#784) ───
2293
+ // For an email-less passkey account (#780) that has lost every passkey. The
2294
+ // caller derives an opaque 32-byte base64url secret from a BIP39 phrase and
2295
+ // passes it here — the SDK is transport-only and never sees or generates the
2296
+ // phrase (no wordlist in the bundle).
2297
+ /**
2298
+ * Bind (or rotate) a recovery secret to the signed-in account, so it can be
2299
+ * redeemed later if every passkey is lost. Authenticated — the server rejects
2300
+ * a stale session (fresh-auth), so call this shortly after login/signup. Only
2301
+ * email-less accounts may bind. `secret` is a 43-char base64url string.
2302
+ */
2303
+ async bindRecoveryPhrase(secret) {
2304
+ return this.http.request({
2305
+ method: "POST",
2306
+ path: "/auth/recovery/bind",
2307
+ body: { secret },
2308
+ authenticated: true
2309
+ });
2310
+ }
2311
+ /**
2312
+ * Redeem a recovery secret to restore access to the SAME account without a
2313
+ * passkey/email/password. UNAUTHENTICATED (the caller has no session yet).
2314
+ * Single-use: after a successful redeem the client should bind a fresh phrase
2315
+ * and add a new passkey. Installs the returned tokens into the SDK session,
2316
+ * exactly like a passkey login.
2317
+ */
2318
+ async redeemRecoveryPhrase(secret) {
2319
+ const result = await this.http.request({
2320
+ method: "POST",
2321
+ path: "/auth/recovery/redeem",
2322
+ body: { secret }
2323
+ });
2324
+ await this.storeTokensFromLogin(result);
2325
+ return result;
2326
+ }
2327
+ /**
2328
+ * Whether the signed-in account has an active recovery binding, and when it
2329
+ * was last bound/rotated — so the app can prompt the user to set or refresh
2330
+ * their recovery phrase. Authenticated.
2331
+ */
2332
+ async getRecoveryStatus() {
2333
+ return this.http.request({
2334
+ method: "GET",
2335
+ path: "/auth/recovery/status",
2336
+ authenticated: true
2337
+ });
2338
+ }
2215
2339
  async storeTokensFromRegister(response) {
2216
2340
  if (!response.access_token) return;
2217
2341
  const expiresAt = response.expires_in ? Math.floor(Date.now() / 1e3) + response.expires_in : void 0;