diff options
| author | Adam Soltys <[email protected]> | 2026-02-26 16:07:52 -0800 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2026-02-26 16:07:52 -0800 |
| commit | 18df062a099811355cafc2024e30a7c2b68c7984 (patch) | |
| tree | 1de4c341b3111f7ccc6c0e74824b2bc6115d3401 /src/routes/(static)/login | |
| parent | 2a59047dc8fc10560a7fde614238ee821cfb6b1b (diff) | |
| download | coinos-ui-18df062a099811355cafc2024e30a7c2b68c7984.tar.gz coinos-ui-18df062a099811355cafc2024e30a7c2b68c7984.zip | |
client side password auth
Diffstat (limited to 'src/routes/(static)/login')
| -rw-r--r-- | src/routes/(static)/login/+page.server.ts | 54 | ||||
| -rw-r--r-- | src/routes/(static)/login/+page.svelte | 95 |
2 files changed, 139 insertions, 10 deletions
diff --git a/src/routes/(static)/login/+page.server.ts b/src/routes/(static)/login/+page.server.ts index 4200bc01..eb0bf079 100644 --- a/src/routes/(static)/login/+page.server.ts +++ b/src/routes/(static)/login/+page.server.ts @@ -12,7 +12,7 @@ export const load = async ({ parent }) => { export const actions = { login: async ({ cookies, request }) => { const form = await fd(request); - let { username, password, token, loginRedirect, recaptcha } = form; + let { username, password, token, loginRedirect, recaptcha, authPubkey } = form; username = username.split("@")[0]; const user = { @@ -20,6 +20,7 @@ export const actions = { password, token, recaptcha, + authPubkey: authPubkey || undefined, }; if (loginRedirect === "undefined") loginRedirect = undefined; @@ -39,6 +40,57 @@ export const actions = { redirect(307, loginRedirect || `/${user.username}`); }, + passwordAuth: async ({ cookies, fetch, request }) => { + const form = await fd(request); + let { event, challenge, username, loginRedirect, recaptcha, token: twofa } = form; + if (loginRedirect === "null" || loginRedirect === "undefined") loginRedirect = undefined; + event = JSON.parse(event); + + const maxAge = 380 * 24 * 60 * 60; + + const res = await fetch(`${PUBLIC_COINOS_URL}/authKeyLogin`, { + method: "POST", + body: JSON.stringify({ event, challenge, username, recaptcha, twofa }), + headers: { + "content-type": "application/json", + accept: "application/json", + "cf-connecting-ip": request.headers.get("cf-connecting-ip") ?? "", + "x-forwarded-host": request.headers.get("host") ?? "", + }, + }); + + const text = await res.text(); + + if (res.status === 401) { + if (text.startsWith("2fa")) throw new Error("2fa"); + throw new Error(text); + } + + if (!res.ok) { + let message; + try { + ({ message } = JSON.parse(text)); + } catch { + message = text; + } + return fail(400, { error: message || "Login failed" }); + } + + const { user, token } = JSON.parse(text); + if (!token) return fail(400, { error: "Login failed" }); + const { username: uname, language } = user; + + const expires = new Date(); + expires.setSeconds(expires.getSeconds() + maxAge); + + const opts = { path: "/", expires }; + if (language) cookies.set("lang", language, opts); + cookies.set("username", uname, opts); + cookies.set("token", token, opts); + + redirect(307, loginRedirect || `/${uname}`); + }, + passkey: async ({ cookies, fetch, request }) => { const form = await fd(request); let { credential, challengeId, loginRedirect } = form; diff --git a/src/routes/(static)/login/+page.svelte b/src/routes/(static)/login/+page.svelte index 2c4df81e..6d7a6e19 100644 --- a/src/routes/(static)/login/+page.svelte +++ b/src/routes/(static)/login/+page.svelte @@ -79,23 +79,100 @@ }); }); - const enhanceLogin = async ({ formData, cancel }: any) => { + const enhanceLogin = async ({ formData, cancel: cancelForm }: any) => { + const uname = formData.get("username"); + const pw = formData.get("password"); + + let recaptcha: string; try { - const recaptcha = await getRecaptchaToken(); - formData.set("recaptcha", recaptcha as string); + recaptcha = (await getRecaptchaToken()) as string; + formData.set("recaptcha", recaptcha); } catch (err: any) { fail(err.message || "captcha failed"); - cancel(); + cancelForm(); return; } - return async ({ result }: any) => { - if (result.type === "success") { - await invalidateAll(); + try { + const { deriveAuthKeypair, deriveWalletEntropy } = await import("$lib/deriveAuthKey"); + const { sk, pubkey } = await deriveAuthKeypair(uname, pw); + + // Fetch challenge with username to check if authPubkey exists + const { challenge: ch, hasAuthKey } = await ( + await fetch(`/api/challenge?username=${encodeURIComponent(uname)}`) + ).json(); + + if (hasAuthKey) { + // Challenge-response path: sign event with derived key, no password sent + cancelForm(); + + const { finalizeEvent } = await import("nostr-tools"); + const event = finalizeEvent( + { + kind: 27235, + created_at: Math.floor(Date.now() / 1000), + content: "", + tags: [ + ["u", `${window.location.origin}/api/authKeyLogin`], + ["method", "POST"], + ["challenge", ch], + ], + } as any, + sk, + ); + + const fd = new FormData(); + fd.append("event", JSON.stringify(event)); + fd.append("challenge", ch); + fd.append("username", uname); + fd.append("loginRedirect", (redirect ?? "") as string); + fd.append("recaptcha", recaptcha); + fd.append("token", token ?? ""); + + const response = await fetch("/login?/passwordAuth", { + method: "POST", + body: fd, + }); + + const result = deserialize(await response.text()); + + if (result.type === "success" || result.type === "redirect") { + try { + await deriveWalletEntropy(sk); + } catch (e) { + console.log("Wallet entropy derivation failed", e); + } + await invalidateAll(); + } + + applyAction(result); + return; } - applyAction(result); - }; + // Legacy path: send password + authPubkey for migration + formData.set("authPubkey", pubkey); + + return async ({ result }: any) => { + if (result.type === "success" || result.type === "redirect") { + try { + await deriveWalletEntropy(sk); + } catch (e) { + console.log("Wallet entropy derivation failed", e); + } + await invalidateAll(); + } + applyAction(result); + }; + } catch (e: any) { + console.log("Auth key derivation failed, falling back to legacy login", e); + // Fallback: plain password login without migration + return async ({ result }: any) => { + if (result.type === "success") { + await invalidateAll(); + } + applyAction(result); + }; + } }; $effect(() => { |
