Skip to content

Commit e959fbe

Browse files
PiquelChipsclaude
andcommitted
feat(auth): recover an account whose owner lost every passkey
An owner who loses every passkey can't get back in: adding a passkey needs a passkey confirmation. An operator now starts a recovery (`auth-service user recover`): it signs the account out everywhere, removes its unverified addresses, and mails the primary verified address a link that enrolls one passkey, once, within a day. Only an operator at the terminal sees the link. For 15 days after the link is used, the recovery is in probation: its passkey signs in like a social login and never confirms the session (Credential::Probation, decided where the passkey is accepted). Every step-up gate refuses it (account changes, owned clients, the admin API, relying parties that ask for phr), so whoever used the link can use the account but add no way into it. - Undo uses any passkey but the recovery's own, needs no session and works on a disabled account. It takes back a fixed list: the recovery's passkey, every session and refresh grant, and every consent. Support can undo too (`user undo-recovery`). - Until it settles, nobody removes a passkey but the recovery's own, and nobody is made an administrator. Administrators are never recovered. - One recovery can be undone at a time: a start is refused during a probation and cancels an unused link. - `user recover --revoke [--keep]` is for an account that was in someone else's hands: it takes back every way in but the kept passkeys and only prints the link; every verified address is told at once. - Triggers in the one migration (0019) hold the probation, the passkey rule and the grant rule for every writer, older releases included. - Authorize answers unmet_authentication_requirements instead of looping a probation user through sign-in. docs/security/account-recovery.md states the policy as built, as invariants that each name their test, and support's evidence policy. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent c86be52 commit e959fbe

78 files changed

Lines changed: 6183 additions & 310 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎README.md‎

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ work from devices on the tailnet. Run the same command after changing code.
5252
| `AUTH_PUBLIC_LISTENER` | Listener address; defaults to `127.0.0.1:8080` |
5353
| `AUTH_ADMIN_LISTENER` | Optional address of the private admin API listener, e.g. `0.0.0.0:8081`. No ingress route may reach it. Without it, auth serves no admin API |
5454
| `AUTH_KEY_ENCRYPTION_KEY` | Required env/file reference to the key that seals [signing keys](#signing-keys) in the database: 32 bytes as unpadded base64url text. A trailing newline is ignored |
55-
| `AUTH_TOKEN_DIGEST_KEY` | Required env/file reference to the key under which refresh tokens, authorization codes and pending logins are stored as HMAC digests. Its raw bytes are the key, at least 32 of them. Passkey decoys and email verification links derive from it too |
55+
| `AUTH_TOKEN_DIGEST_KEY` | Required env/file reference to the key under which refresh tokens, authorization codes and pending logins are stored as HMAC digests. Its raw bytes are the key, at least 32 of them. Passkey decoys, email verification links and recovery links derive from it too |
5656
| `AUTH_TOKEN_DIGEST_KEY_ACCEPTED` | Optional env/file reference to another token digest key, accepted but never used for new digests: the next key, or the old one (see [changing the token digest key](#changing-the-token-digest-key)) |
5757
| `AUTH_INVITE_REQUIRED` | `true` by default; set `false` to allow public registration |
5858
| `AUTH_GITHUB_CLIENT_ID`, `AUTH_GITHUB_CLIENT_SECRET` | Both required to enable GitHub; secret is an env/file reference |
@@ -194,7 +194,8 @@ admin API (`AdminService` in `apps/auth-service/src/admin`), and so does every
194194
setting administrators may change on another account (`AccountService` in
195195
`apps/auth-service/src/account`). Commands need only
196196
`AUTH_DATABASE_URL` (`invite create` also needs `AUTH_PUBLIC_BASE_URL`, to build its
197-
link), so they work while auth is down: they are the break-glass path when the
197+
link, and `user recover` needs it and `AUTH_TOKEN_DIGEST_KEY`, to derive its link), so
198+
they work while auth is down: they are the break-glass path when the
198199
dashboard or auth's frontends are unreachable. In deployed environments,
199200
run them with `piquelctl app exec piquel-fr auth --env ENV -- auth-service …` (see
200201
[infra/README.md](infra/README.md)).
@@ -207,6 +208,9 @@ just auth user search ali # by ID, or part of a username, name o
207208
just auth user show alice # recorded, since it shows personal data
208209
just auth user disable alice --reason "Spam"
209210
just auth user enable alice
211+
just auth user recover alice --reason "Lost every passkey" # not usable yet, see below
212+
just auth user recover alice --reason "Phone stolen" --revoke --keep <passkey-id>
213+
just auth user undo-recovery alice --reason "Owner confirmed by phone"
210214
just auth session revoke <session-id> # one session, as `user show` lists it
211215
just auth session revoke-all alice # every session of the account
212216
just auth audit --user alice --kind session_revoked
@@ -219,7 +223,8 @@ just auth account alice emails remove <email-id> --reason "Asked by email"
219223
- `--output json` prints the admin API's body for `user search` (`UserPage`), `user
220224
show` (`UserDetail`), `audit` (`EventPage`), `client list`, `invite list` and
221225
`admin list`, as defined in `crates/admin-api`. It changes only as the API does.
222-
- `user disable`, `session revoke-all`, `admin revoke`, `client
226+
- `user disable`, `user recover`, `user undo-recovery`, `session revoke-all`,
227+
`admin revoke`, `client
223228
disable|suspend|delete`, and `account … passkeys revoke`, `identities unlink` and
224229
`sessions revoke-all` describe what they will change and ask for confirmation.
225230
Without a terminal they refuse unless given `--yes`.
@@ -230,8 +235,36 @@ just auth account alice emails remove <email-id> --reason "Asked by email"
230235
reads are recorded as views. See
231236
[surfaces.md](docs/architecture/surfaces.md#changing-an-account-on-its-owners-behalf)
232237
for what is shared.
238+
- `user recover` is not usable yet: the page its link opens comes with the
239+
server-rendered pages. It is for an owner who lost every passkey. It signs the
240+
account out everywhere, removes its unverified addresses, and emails its
241+
primary verified address a link that enrolls one passkey, once, within a day;
242+
it prints the link too, for an owner without an address. For 15 days after,
243+
that passkey signs in but confirms no change, and any other passkey of the
244+
account can undo the recovery, which takes back its passkey, every session and
245+
every app consent. Until then, nobody removes a passkey but the recovery's
246+
own.
247+
- It first lists the account's recoveries of the last 15 days. While one is in
248+
probation, it refuses: find out who used it, and undo it with `user
249+
undo-recovery` first. An unused link is just replaced.
250+
- `--revoke [--keep <passkey-id>…]` is for an account that was in someone
251+
else's hands: it also revokes every passkey not kept, unlinks every social
252+
identity and withdraws every consent, and only prints the link. Remove the
253+
addresses the owner doesn't recognize afterwards, before handing the link
254+
over.
255+
- `user undo-recovery` undoes the account's recovery for an owner with no other
256+
passkey, or whose account is disabled, once support has confirmed them.
257+
- Before running either, follow the evidence policy in
258+
[account recovery](docs/security/account-recovery.md#supports-evidence-policy).
259+
Every reason shows in the account's history: write what was asked, never the
260+
evidence.
261+
- It never recovers an account while it holds administrator rights: revoke them
262+
with `admin revoke` first (grant another administrator first if it is the
263+
last). `admin grant` refuses while a recovery of the account can still be
264+
undone; grant the rights again once it settles.
233265
- Each change is recorded as a domain event with the operator: `$USER` and the host
234-
name. Unlike the admin API, the CLI may act on administrators' accounts.
266+
name. Unlike the admin API, the CLI may act on administrators' accounts, except
267+
to recover one.
235268
- Failures print their causes and exit with 1; usage errors exit with 2.
236269

237270
Administrators manage accounts, sessions, invitations and other administrators
@@ -436,16 +469,19 @@ authenticated:
436469

437470
| Claim | Values |
438471
| --- | --- |
439-
| `acr` | `phr` once a passkey signed in or confirmed the session: phishing-resistant multi-factor authentication ([OpenID Connect EAP ACR Values](https://openid.net/specs/openid-connect-eap-acr-values-1_0.html)). `urn:piquel:acr:social` for a session signed in with GitHub or Google only: the provider's authentication, whatever it was |
440-
| `amr` | Every method the session used (RFC 8176): `pop`, `mfa` and `user` for a passkey, which proves possession of a key and requires user verification; `fed` for a provider sign-in. RFC 8176 registers no value for federation, so `fed` is Piquel's own |
472+
| `acr` | `phr` once a passkey confirmed the session, which a passkey sign-in does: phishing-resistant multi-factor authentication ([OpenID Connect EAP ACR Values](https://openid.net/specs/openid-connect-eap-acr-values-1_0.html)). `urn:piquel:acr:social` for a session signed in with GitHub or Google only, the provider's authentication, whatever it was; and for one signed in with a recovered passkey until its recovery settles |
473+
| `amr` | Every method the session used (RFC 8176): `pop`, `mfa` and `user` for a passkey, which proves possession of a key and requires user verification; `fed` for a provider sign-in. RFC 8176 registers no value for federation, so `fed` is Piquel's own. It says how, not how strongly: gate on `acr` |
441474
| `auth_time` | The latest authentication at the session's `acr`: its last passkey confirmation, or else its sign-in |
442475

443476
`phr` satisfies a `urn:piquel:acr:social` requirement, not the reverse. Both values
444477
are listed in discovery's `acr_values_supported`. An authorization request may send
445478
`acr_values` in order of preference: the first known value becomes its target, and
446479
unknown values are ignored. A session below the target signs in again, on a login
447480
page offering only passkeys for `phr`; with `prompt=none` the request fails with
448-
`login_required`. With no known value, the tokens report what the session reached.
481+
`login_required`. If a passkey sign-in for the request still falls short, as a
482+
recovered passkey does, the request fails with `unmet_authentication_requirements`
483+
rather than asking again. With no known value, the tokens report what the session
484+
reached.
449485

450486
Signing in again (`prompt=login`, `max_age`, a target) re-authenticates the
451487
browser's session rather than starting another, so `sid` stays the same: a passkey
@@ -499,7 +535,11 @@ explains how to choose a requirement and handle the challenge.
499535
identity so another account can link it; account data and credentials never move.
500536
Settings keep listing identities of disabled providers so they can be unlinked.
501537
- The last passkey cannot be removed. Removing a passkey/provider revokes other
502-
central sessions. There is no recovery if every passkey is lost.
538+
central sessions. An owner who lost every passkey is recovered with a link that
539+
enrolls one new passkey; for 15 days after, it confirms no change, and another
540+
passkey of the account can undo the recovery (see
541+
[surfaces.md](docs/architecture/surfaces.md#recovering-an-account)). Until its
542+
page ships, no page uses the link.
503543
- Sessions last at most 30 days. Access tokens last five minutes. Refresh rotation
504544
detects reuse and stops when its central session expires or is revoked. Ending a
505545
session tells the clients that registered a back-channel logout URI, which end

‎apps/auth-service/src/account/error.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::{
77
federation::FederationError,
88
history::HistoryError,
99
passkey::PasskeyError,
10+
recovery::RecoveryError,
1011
security::{ApiError, SessionError},
1112
sessions::SessionsError,
1213
};
@@ -35,6 +36,9 @@ pub(crate) enum AccountError {
3536
/// One of the owner's clients could not be registered or changed.
3637
#[error(transparent)]
3738
Client(#[from] OwnedClientError),
39+
/// A recovery link was refused, or a recovery could not be undone.
40+
#[error(transparent)]
41+
Recovery(#[from] RecoveryError),
3842
/// The owner's session ended, or lacks a recent enough passkey confirmation.
3943
#[error(transparent)]
4044
Owner(#[from] SessionError),
@@ -56,6 +60,7 @@ impl From<AccountError> for ApiError {
5660
AccountError::Session(error) => error.into(),
5761
AccountError::History(error) => error.into(),
5862
AccountError::Client(error) => error.into(),
63+
AccountError::Recovery(error) => error.into(),
5964
AccountError::Owner(error) => error.into(),
6065
AccountError::Operator(error) => error.into(),
6166
AccountError::Database(error) => error.into(),

‎apps/auth-service/src/account/tests.rs‎

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::{
1313
security::{BrowserCookie, CONFIRMATION_MAX_AGE},
1414
tests::{
1515
AUTH_ORIGIN, Browser, Replicas, change, pending_sign_in, registration, sign_out,
16-
until_blocked, unusable_passkey,
16+
until_blocked, unusable_passkey, verified_address,
1717
},
1818
};
1919
use axum::{
@@ -30,8 +30,8 @@ use piquel_common::{
3030
protocol::step_up::StepUpRequirement,
3131
};
3232
use piquel_crypto::Secret;
33+
use piquel_repository::PendingFederation;
3334
use piquel_repository::{InsertExternalIdentity, InsertPasskey};
34-
use piquel_repository::{InsertVerifiedEmail, PendingFederation};
3535
use std::{fmt::Debug, mem::discriminant};
3636

3737
/// The reason the scenarios give.
@@ -597,23 +597,11 @@ impl Confirmation {
597597
let database = &fixture.harness.database;
598598
let (account, passkeys) = fixture.target(2).await;
599599
let user = account.user_id;
600-
let mut tx = database.begin().await.expect("transaction");
601600
let mut addresses = Vec::new();
602601
for (index, is_primary) in [(1, true), (2, false)] {
603-
let email = tx
604-
.emails()
605-
.insert_verified(&InsertVerifiedEmail {
606-
id: EmailId::new(),
607-
user_id: user,
608-
normalized_email: format!("{user}-{index}@example.com"),
609-
is_primary,
610-
verified_at: Utc::now(),
611-
})
612-
.await
613-
.expect("address");
614-
addresses.push(email.id);
602+
let address = format!("{user}-{index}@example.com");
603+
addresses.push(verified_address(database, user, &address, is_primary).await);
615604
}
616-
tx.commit().await.expect("commit");
617605
let client = registration(ClientKind::Confidential, "https://notes.example");
618606
change(database, async |registry| {
619607
registry

‎apps/auth-service/src/admin/error.rs‎

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use axum::{
66
response::{IntoResponse, Response},
77
};
88
use piquel_common::{
9-
DeliveryId, HandleError,
9+
DeliveryId, HandleError, PasskeyId,
1010
protocol::{
1111
api::ErrorCode,
1212
challenge::{BearerChallenge, BearerError},
@@ -80,6 +80,39 @@ pub enum AdminError {
8080
/// The invitation lifetime is not 1 hour to 365 days.
8181
#[error("expires_in {0}")]
8282
InvalidInviteExpiry(#[from] super::InvalidLifetime),
83+
/// Only an active account can be recovered.
84+
#[error("the account is disabled; enable it before recovering it")]
85+
RecoveryOfDisabledAccount,
86+
/// An administrator never sees a recovery link, so only a recovery that mails
87+
/// it to a primary verified address is theirs to start.
88+
#[error(
89+
"the recovery link would not be mailed (no primary verified address, or --revoke); only an operator at the terminal can recover this account, with `auth-service user recover`"
90+
)]
91+
RecoveryNeedsEmail,
92+
/// Whoever used an administrator's recovery link could use its rights before
93+
/// the owner undoes it, so an administrator is never recovered.
94+
#[error(
95+
"the account is an administrator: revoke its rights with `auth-service admin revoke` (grant another administrator first if it is the last), recover it, then grant them again once its owner is confirmed"
96+
)]
97+
RecoveryOfAdministrator,
98+
/// A recovery of the account is in probation: support finds out who used it,
99+
/// and undoes it first.
100+
#[error(
101+
"the account has a recovery in probation: find out who used it, and undo it with `auth-service user undo-recovery` before starting another"
102+
)]
103+
RecoveryInProbation,
104+
/// A passkey `--keep` named is not an active passkey of the account.
105+
#[error("passkey {0} is not an active passkey of the account")]
106+
UnknownPasskey(PasskeyId),
107+
/// A recovery of the account can still be undone: rights granted now would go
108+
/// live for whoever used its link once it settles.
109+
#[error(
110+
"the account has a recovery that can still be undone: grant the rights once it settles, or after undoing it"
111+
)]
112+
GrantDuringRecovery,
113+
/// The account has no recovery that can still be undone.
114+
#[error("the account has no recovery that can still be undone")]
115+
NoRecoveryToUndo,
83116
/// Only active accounts can become administrators.
84117
#[error("the account is disabled; enable it before granting administrator rights")]
85118
AccountDisabled,
@@ -153,6 +186,13 @@ impl AdminError {
153186
Self::EmailClaimed(_) => "email_claimed",
154187
Self::InvalidReason => "invalid_reason",
155188
Self::InvalidInviteExpiry(_) => "invalid_invite_expiry",
189+
Self::RecoveryOfDisabledAccount => "recovery_of_disabled_account",
190+
Self::RecoveryNeedsEmail => "recovery_needs_email",
191+
Self::RecoveryOfAdministrator => "recovery_of_administrator",
192+
Self::RecoveryInProbation => "recovery_in_probation",
193+
Self::UnknownPasskey(_) => "unknown_passkey",
194+
Self::NoRecoveryToUndo => "no_recovery_to_undo",
195+
Self::GrantDuringRecovery => "grant_during_recovery",
156196
Self::AccountDisabled => "account_disabled",
157197
Self::AlreadyAdministrator => "already_administrator",
158198
Self::NotAnAdministrator => "not_an_administrator",
@@ -192,6 +232,13 @@ impl AdminError {
192232
| Self::EmailClaimed(_)
193233
| Self::InvalidReason
194234
| Self::InvalidInviteExpiry(_)
235+
| Self::RecoveryOfDisabledAccount
236+
| Self::RecoveryNeedsEmail
237+
| Self::RecoveryOfAdministrator
238+
| Self::RecoveryInProbation
239+
| Self::UnknownPasskey(_)
240+
| Self::NoRecoveryToUndo
241+
| Self::GrantDuringRecovery
195242
| Self::AccountDisabled
196243
| Self::AlreadyAdministrator
197244
| Self::NotAnAdministrator
@@ -234,6 +281,13 @@ impl From<AdminError> for ApiError {
234281
AdminError::AlreadyDisabled
235282
| AdminError::AlreadyActive
236283
| AdminError::EmailClaimed(_)
284+
| AdminError::RecoveryOfDisabledAccount
285+
| AdminError::RecoveryNeedsEmail
286+
| AdminError::RecoveryOfAdministrator
287+
| AdminError::RecoveryInProbation
288+
| AdminError::UnknownPasskey(_)
289+
| AdminError::NoRecoveryToUndo
290+
| AdminError::GrantDuringRecovery
237291
| AdminError::AccountDisabled
238292
| AdminError::AlreadyAdministrator
239293
| AdminError::NotAnAdministrator

‎apps/auth-service/src/admin/mod.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,13 @@ impl AdminApplication {
112112
Op::ListAdministrators => (admin::ADMINISTRATORS, get(Self::list_administrators)),
113113
Op::GrantAdministrator => (admin::ADMINISTRATORS, post(Self::grant_administrator)),
114114
Op::RevokeAdministrator => (admin::ADMINISTRATOR, delete(Self::revoke_administrator)),
115-
// Client changes, handles and the delivery queue are CLI-only, for now.
115+
// Client changes, handles, recovery and the delivery queue are CLI-only,
116+
// for now.
116117
Op::ListInvitations
117118
| Op::CreateInvitation
118119
| Op::RevokeInvitation
120+
| Op::RecoverAccount
121+
| Op::UndoRecovery
119122
| Op::CreateClient
120123
| Op::ApplyClient
121124
| Op::UpdateClient

‎apps/auth-service/src/admin/operation.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ operations! {
1717
DisableUser,
1818
/// Reactivate a disabled account.
1919
EnableUser,
20+
/// Start recovering an account whose owner lost every passkey.
21+
RecoverAccount,
22+
/// Undo an account's recovery for its owner, once they are confirmed.
23+
UndoRecovery,
2024
/// End every session of an account.
2125
RevokeUserSessions,
2226
/// End one session.

0 commit comments

Comments
 (0)