# webauthn-client
A software WebAuthn authenticator and JSON/HTTP client for Haskell.
WebAuthn's client side is normally the browser plus a hardware or platform
authenticator. What a relying party actually verifies, though, is an ES256
signature by the public key it enrolled for the credential, over
```
authData || SHA-256(clientDataJSON)
```
`webauthn-client` performs that same operation with a P-256 keypair the caller
holds. That makes passkey-gated APIs usable from a headless program: a CLI, a
daemon, a test suite, or a service that cannot show a WebAuthn prompt.
## What it is — and is not
It is a **software authenticator**. The private key is generated locally,
enrolled once with the relying party, and stored by you (the library can write
it owner-only with `saveCredentialFile`).
It is **not** a way to use an existing platform passkey (Touch ID, iCloud
Keychain, Windows Hello) or a hardware security key. Those private keys never
leave their authenticator, and are bound to the app/origin that owns them.
Registration uses attestation format `none`: the authenticator makes no
attestation claims, which the WebAuthn specification explicitly permits. A
relying party that *requires* attestation, or that demands a particular AAGUID,
will reject these credentials.
## Quick start
```haskell
{-# LANGUAGE OverloadedStrings #-}
import WebAuthn.Client
import WebAuthn.Client.Http
main :: IO ()
main = do
let endpoints = exampleEndpoints "https://gateway.example.com:9090"
-- One-time: redeem an enrollment token for a software credential.
enrolled <- enroll endpoints [("X-Enroll-Token", token)]
cred <- case enrolled of
Left err -> error (show err)
Right cred -> pure cred
saveCredentialFile "gateway-credential.json" cred
-- Later: sign in and get the relying party's session cookie.
authenticated <- authenticateWithStore
endpoints
(\c -> saveCredentialFile "gateway-credential.json" c)
cred
case authenticated of
Left err -> error (show err)
Right (sessionToken, _updated) ->
putStrLn ("session: " <> show sessionToken)
```
## Lower-level API
If your relying party does not follow the conventional options/verify JSON
endpoints, use the pure ceremony functions directly:
```haskell
import WebAuthn.Client
-- registration
(cred, registrationResponseJson) <-
createRegistration "https://rp.example" "rp.example" challenge
-- authentication
(cred', assertionResponseJson) <- createAssertion cred challenge
```
The response values are ready to be JSON-encoded into the fields a browser
would send (`clientDataJSON`, `attestationObject`/`authenticatorData`,
`signature`, `rawId`), all base64url-encoded as WebAuthn requires.
`assertionParts` exposes the raw `(clientDataJSON, authenticatorData,
Signature)` triple for tests and custom verifiers.
## Credentials and the sign counter
`Credential` is `ToJSON`/`FromJSON`, with the private key as an integer. There
is no hardware sign counter, so the library increments a software counter for
each assertion and stores it in the credential. Persist the *returned*
credential: `authenticateWithStore` calls your store before sending the
assertion, so a lost response can never make the next attempt replay an older
counter.
Treat a stored credential like a password. `saveCredentialFile` writes it with
mode `0600`.
## Configuration
`Endpoints` describes the four URLs and the session-cookie name; there is no
standard for them, so they are configuration rather than a constant.
`exampleEndpoints` is the adapter this package ships and works as a template for
others.
## Development
A Nix flake provides a dev shell with GHC 9.12.4 (matching `tested-with`),
`cabal-install`, `hlint` and `ghcid`, and with this project's dependencies
already registered — so `cabal build` inside the shell uses the Nix-provided
packages instead of resolving against Hackage.
```console
$ nix develop
$ cabal build all
$ cabal test all
```
Without entering a shell:
```console
$ nix build # the library
$ nix build .#test # build and run the hspec suite
$ nix flake check # build the library and run the tests
```
The first invocation downloads a prebuilt GHC and dependency closure from
`cache.iog.io`. `nixConfig` in `flake.nix` requests that cache, but Nix only
honors it for flake configs you trust — either pass `--accept-flake-config`
once, or add the substituter and key to `~/.config/nix/nix.conf`.
## License
BSD-3-Clause.