cauthy-auth 0.11.0 → 0.12.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/svelte.js CHANGED
@@ -30,6 +30,15 @@ module.exports = __toCommonJS(svelte_exports);
30
30
  function canUsePasskeys() {
31
31
  return typeof window !== "undefined" && typeof window.PublicKeyCredential === "function" && typeof navigator !== "undefined" && typeof navigator.credentials?.get === "function";
32
32
  }
33
+ async function canAutofillPasskeys() {
34
+ if (!canUsePasskeys()) return false;
35
+ const pkc = window.PublicKeyCredential;
36
+ try {
37
+ return typeof pkc.isConditionalMediationAvailable === "function" && await pkc.isConditionalMediationAvailable() === true;
38
+ } catch {
39
+ return false;
40
+ }
41
+ }
33
42
  function bytes(text2) {
34
43
  const b64 = text2.replace(/-/g, "+").replace(/_/g, "/").padEnd(Math.ceil(text2.length / 4) * 4, "=");
35
44
  const bin = atob(b64);
@@ -45,9 +54,9 @@ function text(buf) {
45
54
  return btoa(bin).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
46
55
  }
47
56
  var withId = (list) => (list ?? []).map((c) => ({ ...c, id: bytes(c.id) }));
48
- async function getPasskey(options) {
57
+ async function getPasskey(options, how = {}) {
49
58
  const publicKey = { ...options, challenge: bytes(options.challenge), allowCredentials: withId(options.allowCredentials) };
50
- const cred = await navigator.credentials.get({ publicKey });
59
+ const cred = await navigator.credentials.get({ publicKey, ...how });
51
60
  if (cred === null) throw new DOMException("No passkey was chosen.", "NotAllowedError");
52
61
  const r = cred.response;
53
62
  return {
@@ -167,6 +176,24 @@ function firstProblem(...checks) {
167
176
  }
168
177
 
169
178
  // src/client.ts
179
+ var googleScript = null;
180
+ function loadGoogleIdentity() {
181
+ const ready = () => window.google?.accounts?.id;
182
+ if (ready()) return Promise.resolve(ready());
183
+ googleScript ??= new Promise((resolve, reject) => {
184
+ const script = document.createElement("script");
185
+ script.src = "https://accounts.google.com/gsi/client";
186
+ script.async = true;
187
+ script.onload = () => ready() ? resolve(ready()) : reject(new Error("google identity services did not load"));
188
+ script.onerror = () => {
189
+ googleScript = null;
190
+ reject(new Error("google identity services did not load"));
191
+ };
192
+ document.head.appendChild(script);
193
+ });
194
+ return googleScript;
195
+ }
196
+ var PASSKEY_SECONDS = 300;
170
197
  var UNAVAILABLE = { code: "unavailable", message: "Sign-in is unavailable right now. Try again in a moment." };
171
198
  function createAuthClient(options = {}) {
172
199
  const base = (options.basePath ?? "/api/auth").replace(/\/+$/, "");
@@ -250,6 +277,66 @@ function createAuthClient(options = {}) {
250
277
  window.location.assign(passkeyUrl(o));
251
278
  return { ok: true, status: "redirected" };
252
279
  },
280
+ async passkeyAutofill(o = {}) {
281
+ const cancelled = { ok: false, error: { code: "cancelled", message: "No passkey was chosen." } };
282
+ const m = await methods();
283
+ if (!m.passkeyHere || !await canAutofillPasskeys()) return cancelled;
284
+ for (; ; ) {
285
+ if (o.signal?.aborted) return cancelled;
286
+ const begun = await post("/sign-in/passkey/begin", {}, null);
287
+ if (!begun.ok) return begun;
288
+ const renew = new AbortController();
289
+ const stop = () => renew.abort();
290
+ o.signal?.addEventListener("abort", stop, { once: true });
291
+ const timer = setTimeout(stop, (PASSKEY_SECONDS - 30) * 1e3);
292
+ let credential;
293
+ try {
294
+ credential = await getPasskey(begun.options, { mediation: "conditional", signal: renew.signal });
295
+ } catch (e) {
296
+ if (o.signal?.aborted) return cancelled;
297
+ if (renew.signal.aborted) continue;
298
+ return { ok: false, error: ceremonyError(e) };
299
+ } finally {
300
+ clearTimeout(timer);
301
+ o.signal?.removeEventListener("abort", stop);
302
+ }
303
+ return post("/sign-in/passkey/finish", { credential }, null);
304
+ }
305
+ },
306
+ async googleOneTap(o = {}) {
307
+ const cancelled = { ok: false, error: { code: "cancelled", message: "No Google account was chosen." } };
308
+ if (typeof window === "undefined" || o.signal?.aborted) return cancelled;
309
+ const begun = await post("/sign-in/google-one-tap/begin", {}, null);
310
+ if (!begun.ok) return begun;
311
+ let gis;
312
+ try {
313
+ gis = await loadGoogleIdentity();
314
+ } catch {
315
+ return { ok: false, error: { code: "unavailable", message: "Google could not be reached. Try again, or sign in another way." } };
316
+ }
317
+ const credential = await new Promise((done) => {
318
+ o.signal?.addEventListener("abort", () => {
319
+ gis.cancel();
320
+ done(null);
321
+ }, { once: true });
322
+ gis.initialize({
323
+ client_id: begun.clientId,
324
+ nonce: begun.nonce,
325
+ context: o.context ?? "signin",
326
+ callback: (r) => done(r.credential ?? null),
327
+ auto_select: false,
328
+ cancel_on_tap_outside: true,
329
+ itp_support: true,
330
+ use_fedcm_for_prompt: true
331
+ });
332
+ gis.prompt((moment) => {
333
+ if (moment.isNotDisplayed?.() || moment.isSkippedMoment?.()) done(null);
334
+ else if (moment.isDismissedMoment?.() && moment.getDismissedReason?.() !== "credential_returned") done(null);
335
+ });
336
+ });
337
+ if (credential === null) return cancelled;
338
+ return post("/sign-in/google-one-tap", { credential }, null);
339
+ },
253
340
  socialUrl,
254
341
  social(provider, o) {
255
342
  window.location.assign(socialUrl(provider, o));