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