Skip to content

Commit 6fcabb0

Browse files
authored
refactor(auth)!: one session-revocation service, and the duplicate session revoke commands are gone (#606)
Part of #544. Builds on #562, now merged. Signing an account's sessions out was implemented twice. The admin dashboard and `auth-service session revoke|revoke-all` went through `AdminService::revoke_session` and `revoke_user_sessions`. The settings and `auth-service account <user> sessions revoke|revoke-all` went through `AccountService::revoke_session` and `revoke_other_sessions`. The CLI offered both pairs of commands. Now the account service is the only one. The dashboard's **Revoke** and **Revoke all** pages call it with an operator `Subject`, as the CLI's `account` commands already do. The removed code: - `AdminService::revoke_session` and `revoke_user_sessions`. - `AdminOperation::RevokeSession` and `RevokeUserSessions`. The dashboard now mounts its two session pages directly, next to the other settings pages it shares with owners. - `AdminError::SessionNotFound`. - `cli/session.rs` and its `session revoke` and `session revoke-all` commands. The owner decided this behaviour. The account path's behaviour is the one kept. ### Visible changes - **Removed commands:** `auth-service session revoke` and `auth-service session revoke-all`. Use `auth-service account <user> sessions revoke <session>` and `auth-service account <user> sessions revoke-all`. They take the same `--reason`, and `revoke-all` the same `--yes`. On success the account commands print their own messages: "Signed session … of … out." and "Signed … out of N sessions.". The CLI reference is regenerated and loses only the `session` sections. - **Refusal for an unknown session:** - Dashboard: the form said "no active session has this ID". It now says "You have no such session; it may already be signed out.". The status is still 404. - CLI: `session revoke` printed "no active session has this ID". `account … sessions revoke` prints "session not found". - **Which error wins:** when a dashboard revocation of one session finds no active session, the administrator's rights, session and passkey confirmation are now checked first. If they lapsed, the page hands off to sign-in instead of refusing with "not found". Before, "not found" won without that check. - **Expired sessions:** revoking one expired session from the dashboard now commits its refresh grants as revoked (#562's intent), then refuses with "not found". Before, the admin path rolled that back. Expired grants no longer refresh either way, so only the stored `revoked_at` differs. - **Dropped log lines:** the dashboard no longer logs `admin dashboard request refused` for these two refusals. The responses are the same. - `admin.failure=session_not_found`, when **Revoke** names no active session of the account. - `admin.failure=database`, when the database fails while beginning, revoking or committing during **Revoke** or **Revoke all**. A database failure in the operator's own checks is still logged. The dashboard's other shared settings pages already refuse account-service errors without this line. Unchanged: the domain events (`SessionRevoked` per session, with `RevocationReason::Admin`, attributed to the operator with their reason), the lock order (`Operator::lock`, then the revocation, then `Operator::authorize`), one transaction per revocation, the dashboard's pages, their markup and their paths. ### How I checked the rest is unchanged - Read both paths side by side on #562. Revoke-all made the same calls in the same order: `Operator::lock`, then `revoke_sessions(operator.context(reason), user, All, Admin)`, then `authorize`, then commit. - Diffed `docs/dev/operations/cli-commands.md` before and after: it only loses the `session` sections. - `every_change_to_an_account_shows_its_reason_to_the_owner` now revokes through `AccountService` with an operator `Subject`, and still checks that the owner's history shows both reasons. **Proof:** `just db test` passed with only `keys::` skipped. The 13 `keys::` tests passed alone; one had timed out under load, which is #590. Then the dashboard, `cli::tests` and account-reason tests ran against a scratch database, 50 of 50, including the new dashboard test. CI is green. ### Tests - Changed `admin::tests::accounts::every_change_to_an_account_shows_its_reason_to_the_owner`: it revokes through the account service, and its assertions are unchanged. - Added `web::admin::tests::sessions_are_revoked_from_the_dashboard`: from an account's dashboard page, **Revoke** signs one session out and **Revoke all** the rest. Each goes back to the account's page, and the active sessions go from 2 to 1 to 0. Nothing covered the dashboard's successful revocations before. - Removed the `RevokeSession` and `RevokeUserSessions` arms from the operation tables in `cli/tests.rs` and `admin/tests/reads.rs`, since those operations no longer exist. `account … sessions revoke|revoke-all` remain covered by the `AccountOperation` tables. ### Docs - Updated `architecture/sessions.md`, `architecture/surfaces.md`, `operations/administration.md` and `operations/incidents.md`. #553 already corrected the threat-model sentence on `main`, so this PR leaves it alone. - `surfaces.md` says the account page's **Revoke** and **Revoke all** sign sessions out through the account service, from the dashboard's reason page. - The `just auth session` recipe is removed with the commands. No files were edited outside the revocation paths. ## Council review Rounds: 2 | Delegate | Round 1 | Round 2 | | --- | --- | --- | | GPT 6.1 Sol | 9/10 | 9/10 | | Claude Opus 5.5 | 9/10 | 9.5/10 | | Claude Fable 5.2 | 9/10 | 9/10 | Round 1 found the unlisted `session_not_found` log line, `make` wrapping admin errors in `AccountError`, and two docs sentences, all fixed. A shared helper for the two `Subject::operator` lines was declined as out of scope. Round 2's nits are fixed: the route comment and the database log wording above. The threat-model sentence is left to #553's version. > [!NOTE] > Written by Claude Opus 5.5 in Claude Code, reviewed by GPT 6.1 Sol, Claude Opus 5.5 and Claude Fable 5.2 (via T3 Code) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> **Admin service and error model (`apps/auth-service/src/admin/`)** - Removes `AccountService`-adjacent admin methods `revoke_user_sessions` and `revoke_session` from `service/accounts.rs`. Removes the `SessionId` and `SessionScope` imports. - Removes `AdminOperation::RevokeUserSessions` and `AdminOperation::RevokeSession` from `operation.rs`. - Removes `AdminError::SessionNotFound` from `error.rs`, including its log-reason mapping and its not-found `ApiError` classification. - Removes the two operations from `AdminOperation::read` in `tests/reads.rs`. **CLI (`apps/auth-service/src/cli/`)** - Deletes `cli/session.rs`, which held the `session revoke` and `session revoke-all` commands. - Removes the `session` module and command from `cli/mod.rs`. - Removes the matching invocation mappings from `cli/tests.rs`. The `account <user> sessions` mappings remain. **Web dashboard (`apps/auth-service/src/web/admin/`)** - `mod.rs` registers `users::revoke_sessions` and `users::revoke_session` as explicit routes. It removes both operations from the generic account-action routing and the operation-to-page mapping. - `users.rs` changes `AccountChange::make` to return `Result<(), Refusal>`. Session revocation now runs through `AccountService` with an operator `Subject`. Other changes still use the admin service, and their errors become refusals. **Tests** - `admin/tests/accounts.rs` builds an `AccountService` directly for session-revocation cases. It passes an operator `Subject` that contains the account ID, administrator, and reason. - `web/admin/tests.rs` adds `sessions_are_revoked_from_the_dashboard`, an ignored database-backed test. It revokes one session, then the rest, and checks the redirects and the active-session count (2, 1, 0). **Documentation (`docs/dev/`)** - `architecture/sessions.md` removes the `revoke_session`, `revoke_user_sessions`, and `session revoke*` callers. It adds notification-email sign-out links to the `Session::end_all` cases. - `architecture/surfaces.md` names the CLI command `account <user> sessions revoke` for the cross-account not-found case. It states that the dashboard's **Revoke** and **Revoke all** actions use the dashboard reason page. - `operations/administration.md` changes the documented commands to the `account <handle> sessions` group. - `operations/incidents.md` changes the runbook command to `auth-service account <handle> sessions revoke-all`. - `operations/cli-commands.md` removes the generated `auth-service session` reference. **Build recipes (`just/auth.just`)** - Removes the `session *ARGS` recipe and its example comment. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 915cff4 commit 6fcabb0

17 files changed

Lines changed: 86 additions & 251 deletions

File tree

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ pub enum AdminError {
3131
/// No account has this ID or handle, or no active one this verified email.
3232
#[error("no account has this ID, handle or verified email")]
3333
UserNotFound,
34-
/// No active session has this ID.
35-
#[error("no active session has this ID")]
36-
SessionNotFound,
3734
/// No unredeemed, unrevoked invitation has this ID.
3835
#[error("no unredeemed, unrevoked invitation has this ID")]
3936
InvitationNotFound,
@@ -233,7 +230,6 @@ impl AdminError {
233230
Self::StepUpRequired(_) => "step_up_required",
234231
Self::InvalidHandoff => "invalid_handoff",
235232
Self::UserNotFound => "user_not_found",
236-
Self::SessionNotFound => "session_not_found",
237233
Self::InvitationNotFound => "invitation_not_found",
238234
Self::ClientNotFound => "client_not_found",
239235
Self::DeliveryNotFound { .. } => "delivery_not_found",
@@ -329,7 +325,6 @@ impl From<AdminError> for ApiError {
329325
| AdminError::OwnRights
330326
| AdminError::LastAdministrator => Self::forbidden(message),
331327
AdminError::UserNotFound
332-
| AdminError::SessionNotFound
333328
| AdminError::InvitationNotFound
334329
| AdminError::ClientNotFound
335330
| AdminError::DeliveryNotFound { .. }

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ operations! {
2828
RestoreUser,
2929
/// Delete an account at once, without a grace period.
3030
PurgeUser,
31-
/// End every session of an account.
32-
RevokeUserSessions,
33-
/// End one session.
34-
RevokeSession,
3531
/// Send one account a message: it lands in the account's inbox, and is
3632
/// emailed to its primary verified address.
3733
MessageUser,

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

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ use crate::{
99
security::Session,
1010
};
1111
use sourceorigin_common::{
12-
AccountStatus, Handle, HandleError, Personal, SessionId, UserId,
12+
AccountStatus, Handle, HandleError, Personal, UserId,
1313
admin::{
1414
DashboardSession, Email, Paging, ReleasedHandle, ReservedHandle, ScheduledDeletion,
1515
UserDetail, UserPage,
1616
},
1717
email::NormalizedEmail,
1818
events::{DomainEvent, RevocationReason},
1919
};
20-
use sourceorigin_repository::{DatabaseError, DatabaseTransaction, SessionScope, StoredUser};
20+
use sourceorigin_repository::{DatabaseError, DatabaseTransaction, StoredUser};
2121

2222
/// Accounts per search page.
2323
const USER_PAGE_SIZE: u16 = 50;
@@ -319,47 +319,6 @@ impl AdminService {
319319
Self::finish(tx, operator, &reason, Some(user_id), vec![]).await
320320
}
321321

322-
/// Ends every session and refresh grant of an account, for `reason`.
323-
pub(crate) async fn revoke_user_sessions(
324-
&self,
325-
operator: &Operator,
326-
user_id: UserId,
327-
reason: Reason,
328-
) -> Result<(), AdminError> {
329-
let mut tx = self.database.begin().await?;
330-
operator.lock(&mut tx, user_id).await?;
331-
let context = operator.context(&reason);
332-
Session::end_all(&mut tx, &context, user_id, RevocationReason::Admin).await?;
333-
Self::finish(tx, operator, &reason, Some(user_id), vec![]).await
334-
}
335-
336-
/// Ends `user_id`'s active session `session_id` and the refresh grants issued
337-
/// from it, for `reason`. Another account's session is not found: the caller
338-
/// names the account it means to sign out, as its page or command shows it.
339-
pub(crate) async fn revoke_session(
340-
&self,
341-
operator: &Operator,
342-
user_id: UserId,
343-
session_id: SessionId,
344-
reason: Reason,
345-
) -> Result<(), AdminError> {
346-
let mut tx = self.database.begin().await?;
347-
operator.lock(&mut tx, user_id).await?;
348-
if tx
349-
.revoke_sessions(
350-
&operator.context(&reason),
351-
user_id,
352-
SessionScope::One(session_id),
353-
&RevocationReason::Admin,
354-
)
355-
.await?
356-
.is_empty()
357-
{
358-
return Err(AdminError::SessionNotFound);
359-
}
360-
Self::finish(tx, operator, &reason, Some(user_id), vec![]).await
361-
}
362-
363322
/// Lists the accounts whose current handle is reserved, by the handle rules or
364323
/// an administrator, by handle: ones an operator assigned on purpose, or that a
365324
/// later release or reservation reserved.

‎apps/auth-service/src/admin/tests/accounts.rs‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Disabling and enabling an account, and reading its event history.
22
33
use super::*;
4+
use crate::account::{AccountService, Subject};
45
use axum::http::{Method, StatusCode};
56
use serde_json::json;
67
use sourceorigin_common::events::RevocationReason;
@@ -136,17 +137,14 @@ async fn every_change_to_an_account_shows_its_reason_to_the_owner() {
136137
.expect("enable");
137138
let laptop = Browser::sign_in(&harness.database, id).await;
138139
Browser::sign_in(&harness.database, id).await;
139-
service
140-
.revoke_session(
141-
&administrator,
142-
id,
143-
laptop.session_id,
144-
reason("Laptop stolen"),
145-
)
140+
let account = AccountService::new(harness.database.clone());
141+
let signing_out = |why| Subject::operator(id, administrator.clone(), reason(why));
142+
account
143+
.revoke_session(&signing_out("Laptop stolen"), laptop.session_id)
146144
.await
147145
.expect("revoke one");
148-
service
149-
.revoke_user_sessions(&administrator, id, reason("Phone stolen too"))
146+
account
147+
.revoke_other_sessions(&signing_out("Phone stolen too"))
150148
.await
151149
.expect("revoke all");
152150
service

‎apps/auth-service/src/admin/tests/reads.rs‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,6 @@ impl AdminOperation {
116116
| Self::ScheduleDeletion
117117
| Self::RestoreUser
118118
| Self::PurgeUser
119-
| Self::RevokeUserSessions
120-
| Self::RevokeSession
121119
| Self::MessageUser
122120
| Self::CreateInvitation
123121
| Self::ResendInvitation

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ mod output;
3636
mod reference;
3737
/// Serving the public and admin listeners.
3838
mod serve;
39-
/// Ending sessions.
40-
mod session;
4139
/// Finding, showing, disabling and reactivating accounts.
4240
mod user;
4341

@@ -60,7 +58,6 @@ use handle::HandleCommand;
6058
use invite::InviteCommand;
6159
use keys::KeysCommand;
6260
use output::Output;
63-
use session::SessionCommand;
6461
use sourceorigin_common::ErrorChain;
6562
use sourceorigin_repository::Database;
6663
use sourceorigin_telemetry::{Export, Telemetry};
@@ -116,9 +113,6 @@ enum Administration {
116113
/// Search, show, disable, reactivate and recover accounts.
117114
#[command(subcommand)]
118115
User(UserCommand),
119-
/// End an account's sessions.
120-
#[command(subcommand)]
121-
Session(SessionCommand),
122116
/// Create, list, resend and revoke signup invitations.
123117
#[command(subcommand)]
124118
Invite(InviteCommand),
@@ -229,10 +223,6 @@ impl Administration {
229223
} => Op::PurgeUser,
230224
UserCommand::Restore { .. } => Op::RestoreUser,
231225
},
232-
Self::Session(command) => match command {
233-
SessionCommand::Revoke { .. } => Op::RevokeSession,
234-
SessionCommand::RevokeAll { .. } => Op::RevokeUserSessions,
235-
},
236226
Self::Invite(command) => match command {
237227
InviteCommand::Create { .. } => Op::CreateInvitation,
238228
InviteCommand::List => Op::ListInvitations,
@@ -304,7 +294,6 @@ impl Administration {
304294
async fn dispatch(self, console: &Console) -> Result<(), CliError> {
305295
match self {
306296
Self::User(command) => command.run(console).await,
307-
Self::Session(command) => command.run(console).await,
308297
Self::Invite(command) => command.run(console).await,
309298
Self::Client(command) => command.run(console).await,
310299
Self::Audit(command) => command.run(console).await,

‎apps/auth-service/src/cli/session.rs‎

Lines changed: 0 additions & 71 deletions
This file was deleted.

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,6 @@ impl AdminOperation {
4040
"--reason",
4141
"Spam account",
4242
],
43-
Self::RevokeUserSessions => {
44-
&["session", "revoke-all", "alice", "--reason", "Phone stolen"]
45-
}
46-
Self::RevokeSession => &[
47-
"session",
48-
"revoke",
49-
"alice",
50-
AN_ID,
51-
"--reason",
52-
"Phone stolen",
53-
],
5443
Self::MessageUser => &[
5544
"account",
5645
"alice",

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,9 @@ impl AdminApplication {
383383
.route(settings::history)
384384
.route(settings::notifications)
385385
.route(settings::display_name)
386+
// The account page's session revocations, the account service's too.
387+
.route(users::revoke_sessions)
388+
.route(users::revoke_session)
386389
// `ListAlerts` lists the alerts that cleared on a page of their own.
387390
.route(alerts::previous);
388391
let pages = AdminOperation::ALL
@@ -448,8 +451,6 @@ impl AdminApplication {
448451
| Op::UndoRecovery
449452
| Op::ScheduleDeletion
450453
| Op::RestoreUser
451-
| Op::RevokeUserSessions
452-
| Op::RevokeSession
453454
| Op::MessageUser
454455
| Op::RenameHandle
455456
| Op::RevokeAdministrator
@@ -518,8 +519,6 @@ impl AdminApplication {
518519
Op::EnableUser => &users::enable,
519520
Op::ScheduleDeletion => &users::delete,
520521
Op::RestoreUser => &users::restore,
521-
Op::RevokeUserSessions => &users::revoke_sessions,
522-
Op::RevokeSession => &users::revoke_session,
523522
Op::MessageUser => &users::message,
524523
Op::RenameHandle => &settings::handle,
525524
Op::RecoverAccount => &users::recover,

‎apps/auth-service/src/web/admin/tests.rs‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2206,6 +2206,41 @@ async fn a_session_is_revoked_only_from_its_own_account() {
22062206
assert!(session.is_some(), "still signed in");
22072207
}
22082208

2209+
/// An account's page signs one of its sessions out, then every one, each with a
2210+
/// reason, and goes back to the account's page.
2211+
#[tokio::test]
2212+
#[ignore = "requires TEST_DATABASE_URL"]
2213+
async fn sessions_are_revoked_from_the_dashboard() {
2214+
let harness = Harness::new().await;
2215+
let admin = harness.admin().await;
2216+
let owner = Browser::new_account(&harness.database).await;
2217+
Browser::sign_in(&harness.database, owner.user_id).await;
2218+
let account = format!("/users/{}", owner.user_id);
2219+
let one = format!("{account}/sessions/{}/revoke", owner.session_id);
2220+
let all = format!("{account}/sessions/revoke");
2221+
let dashboard = harness.confirmed(&admin, &one, Utc::now()).await;
2222+
let post = async |path: &str| {
2223+
let url = format!("{DASHBOARD}{path}");
2224+
dashboard
2225+
.submit(&harness.dashboard, &url, &[REASON], OWN_PAGE)
2226+
.await
2227+
};
2228+
let active = async || {
2229+
harness
2230+
.database
2231+
.sessions()
2232+
.list_active_for_user(owner.user_id)
2233+
.await
2234+
.expect("sessions")
2235+
.len()
2236+
};
2237+
assert_eq!(active().await, 2);
2238+
assert_eq!(redirect(&post(&one).await), account);
2239+
assert_eq!(active().await, 1);
2240+
assert_eq!(redirect(&post(&all).await), account);
2241+
assert_eq!(active().await, 0);
2242+
}
2243+
22092244
/// The Alerts page shows an open alert as needing attention, with posting an
22102245
/// update on it, and the overview's banner leads there. An update is recorded
22112246
/// as the administrator's, and the alert then shows its status and note. The

0 commit comments

Comments
 (0)