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.mjs CHANGED
@@ -2,6 +2,15 @@
2
2
  function canUsePasskeys() {
3
3
  return typeof window !== "undefined" && typeof window.PublicKeyCredential === "function" && typeof navigator !== "undefined" && typeof navigator.credentials?.get === "function";
4
4
  }
5
+ async function canAutofillPasskeys() {
6
+ if (!canUsePasskeys()) return false;
7
+ const pkc = window.PublicKeyCredential;
8
+ try {
9
+ return typeof pkc.isConditionalMediationAvailable === "function" && await pkc.isConditionalMediationAvailable() === true;
10
+ } catch {
11
+ return false;
12
+ }
13
+ }
5
14
  function bytes(text2) {
6
15
  const b64 = text2.replace(/-/g, "+").replace(/_/g, "/").padEnd(Math.ceil(text2.length / 4) * 4, "=");
7
16
  const bin = atob(b64);
@@ -17,9 +26,9 @@ function text(buf) {
17
26
  return btoa(bin).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
18
27
  }
19
28
  var withId = (list) => (list ?? []).map((c) => ({ ...c, id: bytes(c.id) }));
20
- async function getPasskey(options) {
29
+ async function getPasskey(options, how = {}) {
21
30
  const publicKey = { ...options, challenge: bytes(options.challenge), allowCredentials: withId(options.allowCredentials) };
22
- const cred = await navigator.credentials.get({ publicKey });
31
+ const cred = await navigator.credentials.get({ publicKey, ...how });
23
32
  if (cred === null) throw new DOMException("No passkey was chosen.", "NotAllowedError");
24
33
  const r = cred.response;
25
34
  return {
@@ -139,6 +148,24 @@ function firstProblem(...checks) {
139
148
  }
140
149
 
141
150
  // src/client.ts
151
+ var googleScript = null;
152
+ function loadGoogleIdentity() {
153
+ const ready = () => window.google?.accounts?.id;
154
+ if (ready()) return Promise.resolve(ready());
155
+ googleScript ??= new Promise((resolve, reject) => {
156
+ const script = document.createElement("script");
157
+ script.src = "https://accounts.google.com/gsi/client";
158
+ script.async = true;
159
+ script.onload = () => ready() ? resolve(ready()) : reject(new Error("google identity services did not load"));
160
+ script.onerror = () => {
161
+ googleScript = null;
162
+ reject(new Error("google identity services did not load"));
163
+ };
164
+ document.head.appendChild(script);
165
+ });
166
+ return googleScript;
167
+ }
168
+ var PASSKEY_SECONDS = 300;
142
169
  var UNAVAILABLE = { code: "unavailable", message: "Sign-in is unavailable right now. Try again in a moment." };
143
170
  function createAuthClient(options = {}) {
144
171
  const base = (options.basePath ?? "/api/auth").replace(/\/+$/, "");
@@ -222,6 +249,66 @@ function createAuthClient(options = {}) {
222
249
  window.location.assign(passkeyUrl(o));
223
250
  return { ok: true, status: "redirected" };
224
251
  },
252
+ async passkeyAutofill(o = {}) {
253
+ const cancelled = { ok: false, error: { code: "cancelled", message: "No passkey was chosen." } };
254
+ const m = await methods();
255
+ if (!m.passkeyHere || !await canAutofillPasskeys()) return cancelled;
256
+ for (; ; ) {
257
+ if (o.signal?.aborted) return cancelled;
258
+ const begun = await post("/sign-in/passkey/begin", {}, null);
259
+ if (!begun.ok) return begun;
260
+ const renew = new AbortController();
261
+ const stop = () => renew.abort();
262
+ o.signal?.addEventListener("abort", stop, { once: true });
263
+ const timer = setTimeout(stop, (PASSKEY_SECONDS - 30) * 1e3);
264
+ let credential;
265
+ try {
266
+ credential = await getPasskey(begun.options, { mediation: "conditional", signal: renew.signal });
267
+ } catch (e) {
268
+ if (o.signal?.aborted) return cancelled;
269
+ if (renew.signal.aborted) continue;
270
+ return { ok: false, error: ceremonyError(e) };
271
+ } finally {
272
+ clearTimeout(timer);
273
+ o.signal?.removeEventListener("abort", stop);
274
+ }
275
+ return post("/sign-in/passkey/finish", { credential }, null);
276
+ }
277
+ },
278
+ async googleOneTap(o = {}) {
279
+ const cancelled = { ok: false, error: { code: "cancelled", message: "No Google account was chosen." } };
280
+ if (typeof window === "undefined" || o.signal?.aborted) return cancelled;
281
+ const begun = await post("/sign-in/google-one-tap/begin", {}, null);
282
+ if (!begun.ok) return begun;
283
+ let gis;
284
+ try {
285
+ gis = await loadGoogleIdentity();
286
+ } catch {
287
+ return { ok: false, error: { code: "unavailable", message: "Google could not be reached. Try again, or sign in another way." } };
288
+ }
289
+ const credential = await new Promise((done) => {
290
+ o.signal?.addEventListener("abort", () => {
291
+ gis.cancel();
292
+ done(null);
293
+ }, { once: true });
294
+ gis.initialize({
295
+ client_id: begun.clientId,
296
+ nonce: begun.nonce,
297
+ context: o.context ?? "signin",
298
+ callback: (r) => done(r.credential ?? null),
299
+ auto_select: false,
300
+ cancel_on_tap_outside: true,
301
+ itp_support: true,
302
+ use_fedcm_for_prompt: true
303
+ });
304
+ gis.prompt((moment) => {
305
+ if (moment.isNotDisplayed?.() || moment.isSkippedMoment?.()) done(null);
306
+ else if (moment.isDismissedMoment?.() && moment.getDismissedReason?.() !== "credential_returned") done(null);
307
+ });
308
+ });
309
+ if (credential === null) return cancelled;
310
+ return post("/sign-in/google-one-tap", { credential }, null);
311
+ },
225
312
  socialUrl,
226
313
  social(provider, o) {
227
314
  window.location.assign(socialUrl(provider, o));