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