# Passkeys
Source: https://sovaria.in/docs/passkeys

Sign users in with a passkey (WebAuthn / FIDO2): Face ID, Touch ID, Windows Hello, Android or a security key, with no password. Project setup, registering a passkey, signing in, managing passkeys and how it is protected.

A passkey is a key pair created and kept by the user’s device or password manager. Signing in proves the user has that device, and, when the device asks for their fingerprint, face or PIN, that it is really them. Passkeys can’t be phished: the browser only uses them on your own domain.

Users add a passkey **after** signing in another way (password, magic link or a provider), then use it to sign in from then on.

## Set up passkeys for your project

Open **Users › Passkeys** and fill in all three fields, then turn on **Enable passkeys**. Until all three are set, passkey requests answer `403` “Passkeys are turned off for this project”.

| Setting | What to enter |
|---|---|
| Relying party ID | Your app’s domain, for example `example.com`. Passkeys work on that domain and its subdomains (`app.example.com`), and **can’t move to another domain later**, so use the domain you will keep. |
| Display name | The name the browser shows when someone creates a passkey, for example `Skolist`. |
| Allowed origins | Every address your sign-in page runs on, with scheme and port: `https://app.example.com`, and `http://localhost:5173` while developing. |

## Add a passkey

For a signed-in user, `register` asks Sovaria for options, lets the device create the passkey (Touch ID, Windows Hello, Android, a security key) and saves it. The name helps the user recognise it later.

JavaScript:
```js
if (sovaria.auth.passkeys.isSupported()) {
  await sovaria.auth.passkeys.register('MacBook')
}
```

Dart:
```dart
// Flutter has no built-in WebAuthn API: use a passkey plugin that takes and returns WebAuthn JSON
final options = await sovaria.auth.passkeys.startRegistration(name: 'Pixel');
final credential = await passkeyPlugin.register(options);
await sovaria.auth.passkeys.finishRegistration(credential, name: 'Pixel');
```

Swift:
```swift
// Build an ASAuthorizationPlatformPublicKeyCredentialRegistrationRequest from `options`,
// then send the result as WebAuthn JSON (base64url fields)
let options = try await sovaria.auth.passkeys.startRegistration(name: "iPhone")
let credential = try await passkeyController.register(options)
try await sovaria.auth.passkeys.finishRegistration(credential: credential, name: "iPhone")
```

Kotlin:
```kotlin
// Credential Manager takes and returns WebAuthn JSON
val options = sovaria.auth.passkeys.startRegistration("Pixel")
val result = credentialManager.createCredential(activity, CreatePublicKeyCredentialRequest(Json.encode(options)))
val json = (result as CreatePublicKeyCredentialResponse).registrationResponseJson
sovaria.auth.passkeys.finishRegistration(Json.decode(json).asMap(), "Pixel")
```

## Sign in with a passkey

Signing in needs no email: the device offers the passkeys it has for your domain. The SDK signs the user in like any other sign-in, keeping the refresh token and firing `SIGNED_IN`.

JavaScript:
```js
const tokens = await sovaria.auth.passkeys.signIn()
tokens.aal   // 'aal2' when the device checked the user's fingerprint, face or PIN
```

Dart:
```dart
final start = await sovaria.auth.passkeys.startSignIn();
final credential = await passkeyPlugin.authenticate(start.options);
final session = await sovaria.auth.passkeys.finishSignIn(start.challengeId, credential);
```

Swift:
```swift
let start = try await sovaria.auth.passkeys.startSignIn()
let credential = try await passkeyController.assert(start.options)   // ASAuthorization… assertion as JSON
let session = try await sovaria.auth.passkeys.finishSignIn(challengeId: start.challengeId, credential: credential)
```

Kotlin:
```kotlin
val start = sovaria.auth.passkeys.startSignIn()
val response = credentialManager.getCredential(activity,
    GetCredentialRequest(listOf(GetPublicKeyCredentialOption(Json.encode(start.options)))))
val json = (response.credential as PublicKeyCredential).authenticationResponseJson
val session = sovaria.auth.passkeys.finishSignIn(start.challengeId, Json.decode(json).asMap())
```

HTTP:
```bash
POST /api/v1/auth/passkeys/authenticate/start   {}                       → { challenge_id, options }
POST /api/v1/auth/passkeys/authenticate/finish  { challenge_id, credential } → { access_token, refresh_token, aal, … }
```

To offer only one user’s passkeys (for example after they typed their email), pass their user id: `signIn({ userId })` in JavaScript, `startSignIn(userId: …)` elsewhere. A user with no passkeys gets `404`.

## Passkeys and two-factor authentication

- When the device checked the user’s fingerprint, face or PIN, the session is **two-factor** (`aal: "aal2"`), like a password plus an authenticator code. It passes **Require MFA** and rules that ask for `aal2` ([Multi-factor authentication](https://sovaria.in/docs/mfa)).
- When the device didn’t (some security keys without a PIN), the session is `aal1`, and a user with MFA turned on still has to enter their code.

## List and remove passkeys

| Action | Request |
|---|---|
| List the signed-in user’s passkeys (name, created, last used) | `GET /api/v1/auth/passkeys` |
| Remove one | `DELETE /api/v1/auth/passkeys/{id}` |

JavaScript:
```js
const passkeys = await sovaria.auth.passkeys.list()   // [{ id, name, created_at, last_used_at }]
await sovaria.auth.passkeys.remove(passkeys[0].id)
```

Dart:
```dart
final passkeys = await sovaria.auth.passkeys.list();
await sovaria.auth.passkeys.remove(passkeys.first['id'] as String);
```

Swift:
```swift
let passkeys = try await sovaria.auth.passkeys.list()
try await sovaria.auth.passkeys.remove(passkeys[0]["id"] as! String)
```

Kotlin:
```kotlin
val passkeys = sovaria.auth.passkeys.list()
sovaria.auth.passkeys.remove(passkeys.first()["id"].toString())
```

Python:
```python
passkeys = await sovaria.list_passkeys()
await sovaria.delete_passkey(passkeys[0]["id"])
```

Only the owner can list or remove their passkeys. Show the list on your account page so people can remove a passkey from a lost device.

## How it is protected

- Each challenge is valid for **5 minutes** and works once.
- The browser only uses a passkey on the relying party ID’s domain, and Sovaria checks the origin against your allowed origins, so a look-alike site can’t use it.
- The authenticator’s signature counter is checked; a counter that goes backwards (a cloned key) is refused.
- A passkey can only sign in a user of the same project.

## Adding and removing passkeys safely

- Registering or removing a passkey needs a **recent sign-in** (the last 10 minutes), an `aal2` session, or a one-time code from `sovaria.auth.reauthenticate()` passed as `nonce`. With MFA on, only an `aal2` session will do. Otherwise the answer is `401 REAUTHENTICATION_REQUIRED`.
- Passkey sign-in is public (no session needed) and rate-limited like every sign-in; offering a `userId` never reveals whether that user has passkeys.
- Password managers show the account’s email (not an id). Adding or removing a passkey is recorded and emailed to the user.
