cauthy-auth 0.10.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/README.md +3 -2
- package/dist/cli.js +299 -20
- package/dist/client.d.mts +88 -5
- package/dist/client.d.ts +88 -5
- package/dist/client.js +202 -11
- package/dist/client.js.map +1 -1
- package/dist/client.mjs +200 -11
- package/dist/client.mjs.map +1 -1
- package/dist/index.js +128 -5
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +128 -5
- package/dist/index.mjs.map +1 -1
- package/dist/react.d.mts +110 -5
- package/dist/react.d.ts +110 -5
- package/dist/react.js +338 -20
- package/dist/react.js.map +1 -1
- package/dist/react.mjs +335 -20
- package/dist/react.mjs.map +1 -1
- package/dist/svelte.d.mts +1 -1
- package/dist/svelte.d.ts +1 -1
- package/dist/svelte.js +198 -11
- package/dist/svelte.js.map +1 -1
- package/dist/svelte.mjs +198 -11
- package/dist/svelte.mjs.map +1 -1
- package/dist/vue.d.mts +1 -1
- package/dist/vue.d.ts +1 -1
- package/dist/vue.js +198 -11
- package/dist/vue.js.map +1 -1
- package/dist/vue.mjs +198 -11
- package/dist/vue.mjs.map +1 -1
- package/package.json +1 -1
package/dist/vue.mjs
CHANGED
|
@@ -1,6 +1,82 @@
|
|
|
1
1
|
// src/vue.ts
|
|
2
2
|
import { inject, shallowReadonly, shallowRef } from "vue";
|
|
3
3
|
|
|
4
|
+
// src/passkey.ts
|
|
5
|
+
function canUsePasskeys() {
|
|
6
|
+
return typeof window !== "undefined" && typeof window.PublicKeyCredential === "function" && typeof navigator !== "undefined" && typeof navigator.credentials?.get === "function";
|
|
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
|
+
}
|
|
17
|
+
function bytes(text2) {
|
|
18
|
+
const b64 = text2.replace(/-/g, "+").replace(/_/g, "/").padEnd(Math.ceil(text2.length / 4) * 4, "=");
|
|
19
|
+
const bin = atob(b64);
|
|
20
|
+
const out = new Uint8Array(bin.length);
|
|
21
|
+
for (let i = 0; i < bin.length; i++) out[i] = bin.charCodeAt(i);
|
|
22
|
+
return out.buffer;
|
|
23
|
+
}
|
|
24
|
+
function text(buf) {
|
|
25
|
+
if (!buf) return null;
|
|
26
|
+
const view = new Uint8Array(buf);
|
|
27
|
+
let bin = "";
|
|
28
|
+
for (let i = 0; i < view.byteLength; i++) bin += String.fromCharCode(view[i]);
|
|
29
|
+
return btoa(bin).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
30
|
+
}
|
|
31
|
+
var withId = (list) => (list ?? []).map((c) => ({ ...c, id: bytes(c.id) }));
|
|
32
|
+
async function getPasskey(options, how = {}) {
|
|
33
|
+
const publicKey = { ...options, challenge: bytes(options.challenge), allowCredentials: withId(options.allowCredentials) };
|
|
34
|
+
const cred = await navigator.credentials.get({ publicKey, ...how });
|
|
35
|
+
if (cred === null) throw new DOMException("No passkey was chosen.", "NotAllowedError");
|
|
36
|
+
const r = cred.response;
|
|
37
|
+
return {
|
|
38
|
+
id: cred.id,
|
|
39
|
+
rawId: text(cred.rawId),
|
|
40
|
+
type: cred.type,
|
|
41
|
+
response: {
|
|
42
|
+
authenticatorData: text(r.authenticatorData),
|
|
43
|
+
clientDataJSON: text(r.clientDataJSON),
|
|
44
|
+
signature: text(r.signature),
|
|
45
|
+
userHandle: text(r.userHandle)
|
|
46
|
+
},
|
|
47
|
+
clientExtensionResults: cred.getClientExtensionResults?.() ?? {}
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
async function makePasskey(options) {
|
|
51
|
+
const publicKey = {
|
|
52
|
+
...options,
|
|
53
|
+
challenge: bytes(options.challenge),
|
|
54
|
+
user: { ...options.user, id: bytes(options.user.id) },
|
|
55
|
+
excludeCredentials: withId(options.excludeCredentials)
|
|
56
|
+
};
|
|
57
|
+
const cred = await navigator.credentials.create({ publicKey });
|
|
58
|
+
if (cred === null) throw new DOMException("No passkey was made.", "NotAllowedError");
|
|
59
|
+
const r = cred.response;
|
|
60
|
+
return {
|
|
61
|
+
id: cred.id,
|
|
62
|
+
rawId: text(cred.rawId),
|
|
63
|
+
type: cred.type,
|
|
64
|
+
response: {
|
|
65
|
+
clientDataJSON: text(r.clientDataJSON),
|
|
66
|
+
attestationObject: text(r.attestationObject),
|
|
67
|
+
transports: typeof r.getTransports === "function" ? r.getTransports() : []
|
|
68
|
+
},
|
|
69
|
+
clientExtensionResults: cred.getClientExtensionResults?.() ?? {}
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
function ceremonyError(e) {
|
|
73
|
+
const name = e instanceof Error || e instanceof DOMException ? e.name : "";
|
|
74
|
+
if (name === "NotAllowedError" || name === "AbortError") return { code: "cancelled", message: "The passkey was not used. Try again when you are ready." };
|
|
75
|
+
if (name === "InvalidStateError") return { code: "passkey_exists", message: "This device already has a passkey for this account." };
|
|
76
|
+
if (name === "SecurityError") return { code: "passkey_unavailable", message: "Passkeys cannot be used on this page." };
|
|
77
|
+
return { code: "passkey_failed", message: "The passkey could not be used. Try again, or sign in another way." };
|
|
78
|
+
}
|
|
79
|
+
|
|
4
80
|
// src/theme.ts
|
|
5
81
|
function fromWire(wire) {
|
|
6
82
|
const names = {
|
|
@@ -75,6 +151,24 @@ function firstProblem(...checks) {
|
|
|
75
151
|
}
|
|
76
152
|
|
|
77
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;
|
|
78
172
|
var UNAVAILABLE = { code: "unavailable", message: "Sign-in is unavailable right now. Try again in a moment." };
|
|
79
173
|
function createAuthClient(options = {}) {
|
|
80
174
|
const base = (options.basePath ?? "/api/auth").replace(/\/+$/, "");
|
|
@@ -117,6 +211,28 @@ function createAuthClient(options = {}) {
|
|
|
117
211
|
if (o.locale) q.set("locale", o.locale);
|
|
118
212
|
return `${base}/login?${q}`;
|
|
119
213
|
};
|
|
214
|
+
async function methods() {
|
|
215
|
+
try {
|
|
216
|
+
const res = await doFetch(`${base}/methods`, { credentials: "same-origin", headers: { accept: "application/json" } });
|
|
217
|
+
if (res.ok) {
|
|
218
|
+
const m = await res.json();
|
|
219
|
+
return { providers: [], signUp: true, magicLink: false, passkey: false, ...m, passkeyHere: m.passkeyHere === true, passkeyOnCauthy: m.passkeyOnCauthy ?? m.passkey === true };
|
|
220
|
+
}
|
|
221
|
+
} catch {
|
|
222
|
+
}
|
|
223
|
+
return { providers: [], signUp: true, magicLink: false, passkey: false, passkeyHere: false, passkeyOnCauthy: false };
|
|
224
|
+
}
|
|
225
|
+
async function ceremony(begin, finish, form, run, more = {}) {
|
|
226
|
+
const begun = await post(begin, form, null);
|
|
227
|
+
if (!begun.ok) return begun;
|
|
228
|
+
let credential;
|
|
229
|
+
try {
|
|
230
|
+
credential = await run(begun.options);
|
|
231
|
+
} catch (e) {
|
|
232
|
+
return { ok: false, error: ceremonyError(e) };
|
|
233
|
+
}
|
|
234
|
+
return post(finish, { credential, ...more }, null);
|
|
235
|
+
}
|
|
120
236
|
return {
|
|
121
237
|
signIn: {
|
|
122
238
|
email: (f) => post("/sign-in/email", { email: f.email, password: f.password }, firstProblem(checkEmail(f.email), checkPassword(f.password))),
|
|
@@ -125,8 +241,76 @@ function createAuthClient(options = {}) {
|
|
|
125
241
|
magicLinkVerify: (f) => post("/sign-in/magic-link/verify", { token: f.token }, null),
|
|
126
242
|
emailCode: (f) => post("/sign-in/email-code", { code: f.code }, checkCode(f.code)),
|
|
127
243
|
passkeyUrl,
|
|
128
|
-
passkey(o) {
|
|
244
|
+
async passkey(o) {
|
|
245
|
+
const m = await methods();
|
|
246
|
+
if (m.passkeyHere && canUsePasskeys()) {
|
|
247
|
+
const r = await ceremony("/sign-in/passkey/begin", "/sign-in/passkey/finish", {}, getPasskey);
|
|
248
|
+
if (r.ok || r.error.code !== "passkey_unavailable" || !m.passkeyOnCauthy) return r;
|
|
249
|
+
} else if (!m.passkeyOnCauthy) {
|
|
250
|
+
return { ok: false, error: { code: "passkey_unavailable", message: "Passkeys cannot be used here." } };
|
|
251
|
+
}
|
|
129
252
|
window.location.assign(passkeyUrl(o));
|
|
253
|
+
return { ok: true, status: "redirected" };
|
|
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);
|
|
130
314
|
},
|
|
131
315
|
socialUrl,
|
|
132
316
|
social(provider, o) {
|
|
@@ -170,7 +354,16 @@ function createAuthClient(options = {}) {
|
|
|
170
354
|
const body = await read("/me/sessions");
|
|
171
355
|
return body && Array.isArray(body.sessions) ? body.sessions : [];
|
|
172
356
|
},
|
|
173
|
-
signOutEverywhere: () => post("/me/sessions/end", {}, null)
|
|
357
|
+
signOutEverywhere: () => post("/me/sessions/end", {}, null),
|
|
358
|
+
async passkeys() {
|
|
359
|
+
const body = await read("/me/passkeys");
|
|
360
|
+
return body && Array.isArray(body.passkeys) ? body.passkeys : [];
|
|
361
|
+
},
|
|
362
|
+
addPasskey(f = {}) {
|
|
363
|
+
if (!canUsePasskeys()) return Promise.resolve({ ok: false, error: { code: "passkey_unavailable", message: "This browser cannot make passkeys." } });
|
|
364
|
+
return ceremony("/me/passkeys/add/begin", "/me/passkeys/add/finish", { password: f.password ?? "" }, makePasskey, { name: f.name ?? "" });
|
|
365
|
+
},
|
|
366
|
+
removePasskey: (id) => post("/me/passkeys/remove", { id }, null)
|
|
174
367
|
},
|
|
175
368
|
async getSession() {
|
|
176
369
|
try {
|
|
@@ -185,14 +378,7 @@ function createAuthClient(options = {}) {
|
|
|
185
378
|
looked ??= read("/look").then((wire) => wire && typeof wire === "object" ? fromWire(wire) : null);
|
|
186
379
|
return looked;
|
|
187
380
|
},
|
|
188
|
-
|
|
189
|
-
try {
|
|
190
|
-
const res = await doFetch(`${base}/methods`, { credentials: "same-origin", headers: { accept: "application/json" } });
|
|
191
|
-
if (res.ok) return await res.json();
|
|
192
|
-
} catch {
|
|
193
|
-
}
|
|
194
|
-
return { providers: [], signUp: true, magicLink: false, passkey: false };
|
|
195
|
-
}
|
|
381
|
+
methods
|
|
196
382
|
};
|
|
197
383
|
}
|
|
198
384
|
|
|
@@ -288,7 +474,8 @@ function createSessionStore(options = {}) {
|
|
|
288
474
|
email: (f) => then(client.signIn.email(f)),
|
|
289
475
|
mfa: (f) => then(client.signIn.mfa(f)),
|
|
290
476
|
magicLinkVerify: (f) => then(client.signIn.magicLinkVerify(f)),
|
|
291
|
-
emailCode: (f) => then(client.signIn.emailCode(f))
|
|
477
|
+
emailCode: (f) => then(client.signIn.emailCode(f)),
|
|
478
|
+
passkey: (o) => then(client.signIn.passkey(o))
|
|
292
479
|
},
|
|
293
480
|
signUp: { email: (f) => then(client.signUp.email(f)) },
|
|
294
481
|
signOut: () => then(client.signOut()),
|