-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Passkeys for authentication (#6)
* Add functional Passkeys with Conditional UI. * Gate logins behind feature flag too. * Require naming Passkeys, track last used time. * Limit Passkey name length. * List and allow removal of Passkeys.
- Loading branch information
Showing
23 changed files
with
1,180 additions
and
255 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { | ||
browserSupportsWebAuthnAutofill, | ||
startAuthentication, | ||
startRegistration, | ||
} from "@simplewebauthn/browser"; | ||
import { | ||
AuthenticationResponseJSON, | ||
PublicKeyCredentialCreationOptionsJSON, | ||
RegistrationResponseJSON, | ||
} from "@simplewebauthn/types"; | ||
|
||
document.getElementById("passkey-add")?.addEventListener( | ||
"click", | ||
async (ev) => { | ||
ev.preventDefault(); | ||
|
||
try { | ||
await performRegistration(); | ||
} catch (error) { | ||
alert(`Could not perform Passkey registration: ${error}`); | ||
return; | ||
} | ||
}, | ||
); | ||
|
||
if (window.location.pathname === "/auth/login") { | ||
performPasswordlessLogin(); | ||
} | ||
|
||
async function performPasswordlessLogin() { | ||
if (!(await browserSupportsWebAuthnAutofill())) return; | ||
|
||
const resp = await fetch("/auth/webauthn/generate-authentication-options"); | ||
const opts = await resp.json(); | ||
|
||
try { | ||
const auth = await startAuthentication(opts, true); | ||
const data = await verifyAuthentication(auth); | ||
|
||
const location = new URL(data.redirect_url, window.location.href); | ||
window.location.replace(location); | ||
} catch (error) { | ||
alert("Could not perform Passkey sign in."); | ||
} | ||
} | ||
|
||
async function performRegistration() { | ||
const name = prompt("Please enter a name for this Passkey."); | ||
if (!name) return; | ||
|
||
const opts = await generateRegistrationOptions(); | ||
let attestation = await startRegistration(opts); | ||
|
||
try { | ||
await verifyRegistration(name, attestation); | ||
window.location.reload(); | ||
} catch { | ||
alert("Could not register Passkey, please try again later."); | ||
} | ||
} | ||
|
||
async function generateRegistrationOptions(): Promise< | ||
PublicKeyCredentialCreationOptionsJSON | ||
> { | ||
const resp = await fetch("/auth/webauthn/generate-registration-options"); | ||
return await resp.json(); | ||
} | ||
|
||
async function verifyRegistration( | ||
name: string, | ||
response: RegistrationResponseJSON, | ||
) { | ||
const resp = await fetch("/auth/webauthn/verify-registration", { | ||
method: "POST", | ||
headers: { | ||
"content-type": "application/json", | ||
"x-passkey-name": name, | ||
}, | ||
body: JSON.stringify(response), | ||
}); | ||
if (resp.status !== 204) { | ||
throw new Error( | ||
`Got unexpected status code verifying credential registration: ${resp.status}`, | ||
); | ||
} | ||
} | ||
|
||
interface LoginData { | ||
redirect_url: string; | ||
} | ||
|
||
async function verifyAuthentication( | ||
response: AuthenticationResponseJSON, | ||
): Promise<LoginData> { | ||
const resp = await fetch("/auth/webauthn/verify-authentication", { | ||
method: "POST", | ||
headers: { | ||
"content-type": "application/json", | ||
}, | ||
body: JSON.stringify(response), | ||
}); | ||
if (resp.status !== 200) { | ||
throw new Error( | ||
`Got unexpected status code verifying authentication: ${resp.status}`, | ||
); | ||
} | ||
return await resp.json(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import "./auth"; | ||
import "./account"; | ||
import "./bluesky"; | ||
import "./media"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP TABLE webauthn_credential; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
CREATE TABLE webauthn_credential ( | ||
id uuid PRIMARY KEY NOT NULL DEFAULT gen_random_uuid(), | ||
owner_id uuid NOT NULL REFERENCES user_account (id) ON DELETE CASCADE, | ||
created_at timestamp with time zone NOT NULL DEFAULT current_timestamp, | ||
last_used timestamp with time zone, | ||
credential_id bytea NOT NULL UNIQUE, | ||
name text NOT NULL, | ||
credential jsonb NOT NULL | ||
); | ||
|
||
CREATE INDEX webauthn_credential_owner_idx ON webauthn_credential (owner_id, last_used DESC); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
SELECT | ||
user_account.id, | ||
webauthn_credential.credential | ||
FROM | ||
user_account | ||
JOIN webauthn_credential ON webauthn_credential.owner_id = user_account.id | ||
WHERE | ||
webauthn_credential.credential_id = $1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
INSERT INTO | ||
webauthn_credential (owner_id, credential_id, name, credential) | ||
VALUES | ||
($1, $2, $3, $4) RETURNING id; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
SELECT | ||
owner_id, | ||
credential | ||
FROM | ||
webauthn_credential | ||
WHERE | ||
webauthn_credential.credential_id = $1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
UPDATE | ||
webauthn_credential | ||
SET | ||
last_used = current_timestamp | ||
WHERE | ||
credential_id = $1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
DELETE FROM | ||
webauthn_credential | ||
WHERE | ||
owner_id = $1 | ||
AND credential_id = $2; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
SELECT | ||
created_at, | ||
last_used, | ||
name, | ||
credential_id | ||
FROM | ||
webauthn_credential | ||
WHERE | ||
owner_id = $1 | ||
ORDER BY | ||
last_used DESC; |
Oops, something went wrong.