| 131 | } |
| 132 | |
| 133 | async function post( |
| 134 | username: string, |
| 135 | creds: PublicKeyCredential, |
| 136 | create = false |
| 137 | ) { |
| 138 | const { attestationObject, clientDataJSON, signature, authenticatorData } = |
| 139 | creds.response as AuthResponse |
| 140 | const data = { |
| 141 | id: creds.id, |
| 142 | raw_id: asBase64(creds.rawId), |
| 143 | response: { |
| 144 | attestation_object: asBase64(attestationObject), |
| 145 | client_data_json: asBase64(clientDataJSON), |
| 146 | signature: asBase64(signature), |
| 147 | authenticator_data: asBase64(authenticatorData) |
| 148 | } |
| 149 | } |
| 150 | const req = await fetch( |
| 151 | `${API_HOST}/${create ? 'register' : 'auth'}/${encodeURIComponent( |
| 152 | username |
| 153 | )}`, |
| 154 | { |
| 155 | credentials: 'same-origin', |
| 156 | method: 'POST', |
| 157 | body: JSON.stringify(data), |
| 158 | headers: { 'content-type': 'application/json' } |
| 159 | } |
| 160 | ) |
| 161 | |
| 162 | if (req.status !== 200) { |
| 163 | throw new Error(`Unexpected response ${req.status}: ${await req.text()}`) |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | export async function register(username: string): Promise<boolean> { |
| 168 | try { |