webauthn
webauthn runs WebAuthn passkey registration and authentication ceremonies over the Rust crate webauthn-rs, shipped as a prebuilt native module so applications need no Rust toolchain.
Every function is stateless: the library returns ceremony state as JSON that the server stores between the start and finish calls, and returns registered passkeys as JSON strings for your database.
Install #
The released version needs Agentica 0.40.0. Add it in a project with an agentica.toml:
agentica add webauthn agentica-lang/packages --tag webauthn-0.2.0 --subdir webauthnThat writes this line of agentica.toml, and the entry of agentica.lock:
[dependencies]
webauthn = { git = "https://github.com/agentica-lang/packages", tag = "webauthn-0.2.0", subdir = "webauthn" }Example #
import Webauthn from "pkg/webauthn/webauthn"
def main() -> Void
rp = Webauthn.RelyingParty(
id: "example.com",
origin: "https://example.com",
name: "Example",
)
start = Webauthn.start_registration(
rp,
OpenSSL.Random.uuid(),
"alice",
"Alice",
).unwrap!
puts(start.options_json)
endServer-side state #
Store state_json from start_registration and start_authentication on the server (such as in a database or server-side session) and delete it immediately before or when finish_registration or finish_authentication runs so a challenge can never be replayed.
When finish_authentication returns needs_update == true, call Webauthn.update_passkey(passkey_json, outcome) and persist the returned passkey JSON so cloned or replayed counters are rejected.
API #
Webauthn.RelyingParty #
Relying party identity: id (the domain, such as "example.com"), origin (full origin URL, such as "https://example.com"), and name (human-readable site name).
Webauthn.RegistrationStart #
Returned by start_registration: options_json (JSON for navigator.credentials.create) and state_json (single-use ceremony state to keep on the server).
Webauthn.AuthenticationStart #
Returned by start_authentication: options_json (JSON for navigator.credentials.get) and state_json (single-use ceremony state to keep on the server).
Webauthn.AuthenticationOutcome #
Returned by finish_authentication: credential_id (base64url credential ID), counter (signature counter), user_verified (Bool), backup_eligible (Bool), backup_state (Bool), needs_update (Bool), and result_json (serialized AuthenticationResult for update_passkey).
Webauthn.start_registration(rp, user_id, user_name, display_name, exclude_passkeys = []) -> Result[RegistrationStart] #
Starts passkey registration for user_id (a UUID string), optionally excluding already-registered exclude_passkeys JSON strings. Fails if rp or user_id is invalid.
Webauthn.finish_registration(rp, credential_json, state_json) -> Result[String] #
Verifies the browser's RegisterPublicKeyCredential JSON against state_json and returns the serialized passkey JSON to store for the user.
Webauthn.start_authentication(rp, passkeys) -> Result[AuthenticationStart] #
Starts passkey authentication for a non-empty list of stored passkeys JSON strings.
import Webauthn from "pkg/webauthn/webauthn"
def start_login(rp: Webauthn.RelyingParty, stored_passkeys: List[String]) -> Result[Webauthn.AuthenticationStart]
Webauthn.start_authentication(rp, stored_passkeys)
endWebauthn.finish_authentication(rp, credential_json, state_json) -> Result[AuthenticationOutcome] #
Verifies the browser's PublicKeyCredential assertion JSON against state_json and returns the verified AuthenticationOutcome.
Webauthn.update_passkey(passkey_json, outcome) -> Result[String] #
Applies counter and backup-flag updates from outcome to a stored passkey_json string and returns the updated passkey JSON. Fails if outcome belongs to a different credential ID.
import Webauthn from "pkg/webauthn/webauthn"
def save_updated(passkey_json: String, outcome: Webauthn.AuthenticationOutcome) -> Result[String]
return ok(passkey_json) if not outcome.needs_update
Webauthn.update_passkey(passkey_json, outcome)
endWebauthn.credential_id(passkey_json) -> Result[String] #
Extracts the base64url credential ID from a stored passkey_json string.