summaryrefslogtreecommitdiff
path: root/app/routes/auth.register.tsx
blob: 7258d384fe3eb7bcae5ff0940b0578676429b5ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { useState } from "react";
import { redirect } from "react-router";
import type { Route } from "./+types/auth.register";
import { getSession } from "~/lib/session.server";
import type { RegistrationResponseJSON, AuthenticatorTransportFuture } from "@simplewebauthn/server";

export async function loader({ request }: Route.LoaderArgs) {
  const session = await getSession(request.headers.get("Cookie"));
  if (session.get("authenticated")) return redirect("/");
  return {};
}

function b64urlToBuffer(b64url: string): ArrayBuffer {
  const base64 = b64url.replace(/-/g, "+").replace(/_/g, "/");
  const pad = "=".repeat((4 - (base64.length % 4)) % 4);
  const binary = atob(base64 + pad);
  const bytes = new Uint8Array(binary.length);
  for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
  return bytes.buffer;
}

function bufferToB64url(buf: ArrayBuffer): string {
  const bytes = new Uint8Array(buf);
  let binary = "";
  for (let i = 0; i < bytes.byteLength; i++) binary += String.fromCharCode(bytes[i]);
  return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
}

export default function Register() {
  const [token, setToken] = useState("");
  const [status, setStatus] = useState<"idle" | "loading" | "error">("idle");
  const [errorMsg, setErrorMsg] = useState("");
  const [step, setStep] = useState("");

  async function handleRegister() {
    const tick = () => new Promise<void>((r) => setTimeout(r, 50));
    setStatus("loading");
    setErrorMsg("");
    setStep("");
    try {
      setStep("1a. fetch 中…"); await tick();
      const optRes = await fetch("/api/passkey?intent=reg-options", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ token }),
      });
      setStep("1b. fetch 完了 status=" + optRes.status); await tick();
      const rawText = await optRes.text();
      setStep("1c. body=" + rawText.slice(0, 120)); await tick();
      if (!optRes.ok) {
        throw new Error("HTTP " + optRes.status + ": " + rawText.slice(0, 200));
      }
      let optJSON: Record<string, unknown>;
      try {
        optJSON = JSON.parse(rawText);
      } catch (e) {
        throw new Error("JSON.parse 失敗: " + String(e) + " | body: " + rawText.slice(0, 100));
      }
      setStep("1d. JSON パース OK"); await tick();

      const userId = b64urlToBuffer(optJSON.user as never as string);
      const userObj = optJSON.user as Record<string, string>;
      const challenge = b64urlToBuffer(optJSON.challenge as string);
      setStep("2. credentials.create() 呼び出し中…"); await tick();

      const credential = await navigator.credentials.create({
        publicKey: {
          rp: { name: (optJSON.rp as Record<string, string>).name },
          user: {
            id: b64urlToBuffer(userObj.id),
            name: userObj.name,
            displayName: userObj.displayName ?? userObj.name,
          },
          challenge,
          pubKeyCredParams: optJSON.pubKeyCredParams as PublicKeyCredentialParameters[],
        },
      }) as PublicKeyCredential | null;

      if (!credential) throw new Error("クレデンシャルの作成に失敗しました");
      setStep("3. credential 取得 OK → サーバー送信中…"); await tick();

      const attestation = credential.response as AuthenticatorAttestationResponse;
      const regResponse: RegistrationResponseJSON = {
        id: credential.id,
        rawId: bufferToB64url(credential.rawId),
        response: {
          clientDataJSON: bufferToB64url(attestation.clientDataJSON),
          attestationObject: bufferToB64url(attestation.attestationObject),
          transports: attestation.getTransports
            ? (attestation.getTransports() as AuthenticatorTransportFuture[])
            : [],
        },
        authenticatorAttachment: credential.authenticatorAttachment ?? undefined,
        clientExtensionResults: credential.getClientExtensionResults(),
        type: "public-key",
      };

      const verRes = await fetch("/api/passkey?intent=reg-verify", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify(regResponse),
      });
      if (!verRes.ok) {
        const err = await verRes.json();
        throw new Error(err.error ?? "登録に失敗しました");
      }

      window.location.href = "/";
    } catch (e) {
      setStatus("error");
      setErrorMsg(e instanceof Error ? `${e.name}: ${e.message}` : String(e));
    }
  }

  return (
    <div className="wrap">
      <header className="site-header">
        <h1>log</h1>
      </header>
      <div className="auth-box">
        <h2>パスキーを登録 [debug]</h2>
        <p>登録トークンを入力してパスキーを作成します。</p>
        <input
          type="password"
          placeholder="登録トークン"
          value={token}
          onChange={(e) => setToken(e.target.value)}
          onKeyDown={(e) => e.key === "Enter" && handleRegister()}
          style={{
            width: "100%",
            marginBottom: ".75rem",
            padding: ".5rem .75rem",
            border: "1px solid #d1d5db",
            borderRadius: "4px",
            fontSize: ".875rem",
            fontFamily: "ui-monospace, monospace",
          }}
        />
        <button className="btn" onClick={handleRegister} disabled={status === "loading" || !token}>
          {status === "loading" ? "登録中…" : "パスキーを登録"}
        </button>
        {step && (
          <p style={{ marginTop: ".75rem", fontSize: ".75rem", color: "#6b7280", wordBreak: "break-all" }}>{step}</p>
        )}
        {status === "error" && (
          <p className="error-msg" style={{ marginTop: ".5rem" }}>{errorMsg}</p>
        )}
      </div>
    </div>
  );
}