# Passkeys & Security Keys

Source: https://keyringauth.com/docs/0.x/go/passkeys

## Introduction

Keyring supports WebAuthn, the standard behind passkeys and security keys. Keyring for Go has the PHP package's passkey profile, on by default, with its settings in `Config.WebAuthn`:

```go
k := keyring.New[models.User](func(cfg *keyring.Config) {
    cfg.Factors.Passkeys = true
    cfg.WebAuthn.RelyingPartyID = "example.com"
    cfg.WebAuthn.Origins = []string{"https://example.com"}
})
```

The ceremonies are checked by Keyring's own `webauthn` package, on the standard library alone, which runs `web-auth/webauthn-lib`'s steps in its order with its readings of base64, client data and authenticator data, so what passes and what's refused matches the PHP package. A key's stored record is the PHP package's, so a key added on either side works on the other.

Set the `WebAuthn` fields one at a time: assigning the whole struct leaves `Algorithms` empty, which `Register` refuses while passkeys are on.

## The Two Profiles

The PHP package has two profiles, as its [docs](https://keyringauth.com/docs/0.x/passkeys#the-two-profiles) describe. Keyring for Go has passkeys alone so far:

| | Passkeys (`Factors.Passkeys`) | Security keys |
|---|---|---|
| Key | `passkey` | `security-key` |
| User verification | required, unless `WebAuthn.RequireUserVerification` is off | Not available yet |
| Can start a sign in | yes, without a username | |
| Priority | 100 | |

A user with a security key added on the PHP side is refused at sign in with the `unavailable` `422`, as one with any factor Keyring for Go can't check is, rather than let in on less. See [Passkeys and Attestation](https://keyringauth.com/docs/0.x/go/differences#passkeys-and-attestation).

Users add passkeys through the [authenticator routes](https://keyringauth.com/docs/0.x/go/authenticators). Keyring excludes credentials the user already has, and refuses any credential registered to any account. A posted credential whose `id` or `rawId` isn't the credential ID it attested is refused, stricter than webauthn-lib, which stores the attested ID whatever the browser posted. An empty credential ID, and an attestation object or authenticator data over 64 KiB, are refused too.

## What a Key Proves

As in the PHP package: every WebAuthn verification proves possession, and also inherence when the key reports that it verified the user. A key that verified the user is two factors, enough alone for [AAL2](https://keyringauth.com/docs/0.x/go/policies#assurance-levels). A passkey is always phishing resistant. Whether a key can sync comes from its backup-eligible flag, read on every sign in, and a key that can't is device bound. AAL3 needs a [verified attestation](https://keyringauth.com/docs/0.x/go/passkeys#requiring-attestation) too, which Keyring for Go can't give yet.

With `WebAuthn.RequireUserVerification` off, a passkey on the challenge page that didn't verify the user is one factor, though signing in with one alone always asks for it.

## Relying Party and Origins

| Setting | Default | Notes |
|---|---|---|
| `WebAuthn.RelyingPartyID` (`KEYRING_WEBAUTHN_RP_ID`) | `APP_URL`'s host, or `localhost` | The domain keys are bound to. Changing it later stops existing keys working. |
| `WebAuthn.Origins` | `APP_URL`'s origin | The exact origins allowed to complete a ceremony, as full URLs. |
| `WebAuthn.Timeout` | 60 seconds | How long the browser waits. |
| `WebAuthn.LoginOptionsFor` | 5 minutes | How long an answer to a usernameless challenge is accepted, never less than `Timeout`. |

An origin that isn't a full URL, or a relying party ID that isn't each origin's host or a parent domain of it, stops `Register`, where the PHP package refuses every response. A host with international characters is taken in its ASCII form, by the URL Standard's rules as browsers apply them, so a few hosts the PHP package takes stop `Register` here. WebAuthn only works on `https` and `localhost`, so in development `APP_URL` must match the address in your browser, such as `http://localhost:8000`.

## Signature Algorithms

`WebAuthn.Algorithms` lists the signature algorithms keys may use, in order of preference. It's `ES256` and `EdDSA` by default, as in the PHP package:

| Name | Algorithm | |
|---|---|---|
| `ES256` | ECDSA with P-256 and SHA-256 | Nearly every authenticator supports it. |
| `EdDSA` | Ed25519 | Supported. |
| `RS256` | RSASSA-PKCS1-v1_5 with SHA-256 | Off by default. See below. |

An unknown name, or an empty list, stops `Register`, where the PHP package throws at the first ceremony.

### RS256 and Windows Hello

RS256 is off for the reasons the [PHP docs](https://keyringauth.com/docs/0.x/passkeys#rs256-and-windows-hello) give: OWASP ASVS 5.0.0 doesn't approve it, but some Windows Hello passkeys only sign with it. To offer it, add it last:

```go
cfg.WebAuthn.Algorithms = []string{"ES256", "EdDSA", "RS256"}
```

With RS256 on, Keyring refuses RSA keys under `WebAuthn.MinimumRSABits` (2048) when a key is added and at every sign in, and never accepts under 2048 bits. Go's `crypto/rsa` refuses an RSA exponent over 2³¹−1, which OpenSSL takes. No authenticator is known to use one.

## Attestation and AAGUIDs

Keyring asks for `none` attestation and accepts any authenticator, including synced passkeys. Each key's AAGUID is stored, but without attestation that's only what the key claims.

### Requiring Attestation

Not available yet. The PHP package's `requireAttestation()`, the `packed` and `fido-u2f` formats and `attestation_roots` aren't ported, so a key whose browser sends any attestation but `none` can't be added. See [Passkeys and Attestation](https://keyringauth.com/docs/0.x/go/differences#passkeys-and-attestation).

### Attestation and AAL3

Without attestation, no key Keyring for Go adds is attested, so no session reaches AAL3 and `Requirement.AttestedKey()` can't be met. A key the PHP side added with a verified attestation, recorded in the `attestation` column, isn't counted as attested here either.

### Allowing Only Certain Models

Not available yet: `allowOnly()` needs attestation.

### Trusting Each Model Separately

Not available yet: there's no counterpart to the PHP package's `TrustsAuthenticators`.

### What Is Kept

A key is kept with `attestationType` `none`, an empty trust path and its AAGUID lowercased, as webauthn-lib writes it.

## Counters and Cloned Keys

As in the PHP package: a counter that didn't go up suggests a clone, so Keyring refuses that sign in and dispatches `CloneSuspected`, logged as a critical `clone_suspected` entry, before `FactorFailed`. Synced passkeys usually report zero, so this check doesn't apply to them. The `clone_suspected` [security email](https://keyringauth.com/docs/0.x/go/security-log-and-emails#security-emails) isn't sent yet.

## Signing In Without a Username

The routes and steps are the PHP package's: `GET /login/passkey/options`, then `POST /login/passkey` with `credential`. See [Signing In With a Passkey](https://keyringauth.com/docs/0.x/go/sign-in-flow#signing-in-with-a-passkey). Options are used once, right or wrong, in the session and in the cache, before anyone is looked up, so an answer sent again, or one to options another browser was given, is refused. So is one that isn't a `public-key` credential whose `id` is its `rawId`, a key nobody added, or a user handle that isn't the key's.

An answer from inside another page's frame is refused too: `crossOrigin` set, or any `topOrigin`, which webauthn-lib lets through. That's stricter than the PHP package.

These requests name no account, so only the per-address rate limit applies. Fetching options has its own limit of 60 a minute per address (`Limits.LoginOptions`). A passkey's failures on an account are logged as capped refusals, with `FactorFailed.Identifying` set, where the PHP package logs every one.

## Laravel's Passkeys Client

`@laravel/passkeys`' sign-in calls work against Keyring for Go's login routes, as they do against the PHP package's, since the routes answer the same JSON. Its add-a-passkey calls don't match Keyring's enrollment routes.

### Signing In With It

Pass Keyring's login routes to the client, as the [PHP docs](https://keyringauth.com/docs/0.x/passkeys#signing-in-with-it) show. If you set `Config.Prefix`, add it to both paths.

### Adding a Passkey and Step-Up

Use `@keyringjs/keyring`'s [`enroll`](https://keyringauth.com/docs/0.x/go/frontend#managing-authenticators) and [`useChallenge`](https://keyringauth.com/docs/0.x/go/frontend#the-challenge-page). Passkeys made with `laravel/passkeys` keep working once you [import them](https://keyringauth.com/docs/0.x/go/upgrading-from-fortify#importing-two-factor-data) with `keyring:import fortify`.

## Customising a Profile

Not available yet. The PHP package's `WebAuthn::securityKeys()->as()` adds more key factors under keys of their own, and a second passkey factor that signs in alone, `WebAuthn::passkeys()->as()`. Keyring for Go has the one `passkey` factor. A user with a key under another factor key, added on the PHP side, is refused at sign in.

## Stored Data

Each key is a row in `keyring_authenticators`, in the PHP package's form: web-auth/webauthn-lib's `CredentialRecord` JSON in `public_key`, its count in `sign_count`, its backup flags in `meta`, and the PHP package's columns for the rest, as its [docs](https://keyringauth.com/docs/0.x/passkeys#stored-data) list. Updates take a row lock.

The record is written back as webauthn-lib writes it on every sign in: its keys in its order, nulls left out, fields it doesn't know dropped, IDs as unpadded base64url and the AAGUID lowercased. A user handle's oldest key is found by `created_at`, then `id`.

The user handle is the PHP package's keyed hash of the user's type and ID, or the one the user's oldest key has. `k.UserHandle(ctx, user)` gives it. `keyring.PasskeyRecord` writes a record without an enrollment, for tests and test hooks, with `JSON()`; `webauthn.ES256Key(x, y)` makes its COSE public key. The contract app's [passkeys hook](https://github.com/keyringauth/keyring/blob/main/go/contract-app/hooks/passkeys.go) uses both. To test passkeys without a browser, see [Virtual Authenticators](https://keyringauth.com/docs/0.x/go/testing#virtual-authenticators).

## How It Works

Spending a challenge removes it from the session, and Keyring records in the default cache store that the answer was used, until a second after the challenge would have expired, as the PHP package's `SpentChallenges` does. An answer arriving more than `WebAuthn.LoginOptionsFor` after its challenge is refused. A second factor's or step-up's challenge ends with the pending login (`Config.PendingLogins`, 10 minutes), and a new key's with the enrollment (`Config.EnrollmentsFor`, 15 minutes).
