Skip to content

Commit d1eff00

Browse files
committed
refactor(auth): pages read what an account holds under one authorization
The history, the overview, a notification and the dashboard's history each read an account's passkeys, addresses and identities with three reads, each authorizing again, and mapped them for the timeline themselves. AccountService::holdings reads all three under one authorization, through readers that take the Authorized proof only Viewer::authorize builds, and HeldRows maps them once.
1 parent e99cc0e commit d1eff00

11 files changed

Lines changed: 153 additions & 91 deletions

File tree

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ mod error;
2525
mod subject;
2626

2727
pub(crate) use error::AccountError;
28-
pub(crate) use subject::{ConfirmedOwner, ConfirmedSubject, Owner, Subject, Viewer};
28+
pub(crate) use subject::{Authorized, ConfirmedOwner, ConfirmedSubject, Owner, Subject, Viewer};
2929

3030
use crate::{
3131
emails::EmailView,
@@ -178,6 +178,28 @@ impl AccountService {
178178
}
179179
subject.commit(tx).await
180180
}
181+
182+
/// What the account holds, which its security history and notifications
183+
/// name, under one authorization: its passkeys, addresses and linked
184+
/// identities, each read as its own reader does.
185+
pub(crate) async fn holdings(&self, viewer: &Viewer) -> Result<Holdings, AccountError> {
186+
let read = viewer.authorize(&self.database).await?;
187+
Ok(Holdings {
188+
passkeys: self.authorized_passkeys(&read).await?,
189+
emails: self.authorized_emails(&read).await?,
190+
identities: self.authorized_identities(&read).await?,
191+
})
192+
}
193+
}
194+
195+
/// What an account holds, as [`AccountService::holdings`] reads it.
196+
pub(crate) struct Holdings {
197+
/// Its active passkeys.
198+
pub(crate) passkeys: Vec<Passkey>,
199+
/// Its addresses, primary first.
200+
pub(crate) emails: Vec<EmailView>,
201+
/// Its active social identities.
202+
pub(crate) identities: Vec<Identity>,
181203
}
182204

183205
/// The account as auth's pages show it to the browser signed in to it: in the

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,17 @@ pub(crate) struct Viewer {
6767
by: Reading,
6868
}
6969

70+
/// A [`Viewer`]'s read of their account, once [`Viewer::authorize`] allowed it.
71+
/// Only `authorize` builds one, so a reader that takes it, such as
72+
/// [`AccountService::holdings`]'s, reads only what was authorized, and several
73+
/// reads share one authorization.
74+
///
75+
/// [`AccountService::holdings`]: crate::account::AccountService::holdings
76+
pub(crate) struct Authorized {
77+
/// The account.
78+
user: UserId,
79+
}
80+
7081
/// An [`Owner`] whose session the request's cookie showed confirmed with a passkey
7182
/// recently, for routes whose operation needs it. Checked when extracted, before
7283
/// the path and body, so such a route answers `step_up_required` before any other
@@ -338,14 +349,21 @@ impl Viewer {
338349
/// data: once an hour ([`Operator::record_view`]), so a page reading several
339350
/// settings records one. The extractor already checked an owner's session,
340351
/// and an owner's reads of their own are not recorded.
341-
pub(crate) async fn authorize(&self, database: &Database) -> Result<(), AccountError> {
352+
pub(crate) async fn authorize(&self, database: &Database) -> Result<Authorized, AccountError> {
342353
if let Reading::Operator(operator) = &self.by {
343354
operator.authorize_read(database).await?;
344355
let mut tx = database.begin().await?;
345356
operator.record_view(&mut tx, self.user).await?;
346357
tx.commit().await?;
347358
}
348-
Ok(())
359+
Ok(Authorized { user: self.user })
360+
}
361+
}
362+
363+
impl Authorized {
364+
/// The account.
365+
pub(crate) const fn user(&self) -> UserId {
366+
self.user
349367
}
350368
}
351369

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//! derived from the address's ID with [`VerificationKeys`], once here to store its
1111
//! digest, and again by the email sender. See `docs/dev/architecture/delivery-queue.md`.
1212
use crate::{
13-
account::{AccountError, AccountService, ConfirmedOwner, Owner, Subject, Viewer},
13+
account::{AccountError, AccountService, Authorized, ConfirmedOwner, Owner, Subject, Viewer},
1414
config::{EmailConfig, LinkKeys},
1515
routes,
1616
security::{ApiError, RequestContext},
@@ -453,8 +453,16 @@ impl Added<'_> {
453453
impl AccountService {
454454
/// Lists the account's addresses, primary first.
455455
pub(crate) async fn emails(&self, viewer: &Viewer) -> Result<Vec<EmailView>, AccountError> {
456-
viewer.authorize(&self.database).await?;
457-
let emails = self.database.emails().list(viewer.user()).await?;
456+
let read = viewer.authorize(&self.database).await?;
457+
self.authorized_emails(&read).await
458+
}
459+
460+
/// [`Self::emails`], for a read already authorized.
461+
pub(crate) async fn authorized_emails(
462+
&self,
463+
read: &Authorized,
464+
) -> Result<Vec<EmailView>, AccountError> {
465+
let emails = self.database.emails().list(read.user()).await?;
458466
Ok(emails.into_iter().map(EmailView::from).collect())
459467
}
460468

‎apps/auth-service/src/federation/identities.rs‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Pending and linked social identities: signup, linking, listing and unlinking.
22
33
use super::{FederationApplication, FederationError, Flow, FlowCookie, Provider, ProviderPicture};
4-
use crate::account::{AccountError, AccountService, Owner, Subject, Viewer};
4+
use crate::account::{AccountError, AccountService, Authorized, Owner, Subject, Viewer};
55
use crate::config::EmailConfig;
66
use crate::emails::EmailApplication;
77
use crate::security::BrowserCookie;
@@ -215,12 +215,16 @@ impl AccountService {
215215

216216
/// Lists the account's active social identities.
217217
pub(crate) async fn identities(&self, viewer: &Viewer) -> Result<Vec<Identity>, AccountError> {
218-
viewer.authorize(&self.database).await?;
219-
let identities = self
220-
.database
221-
.federation()
222-
.list_active(viewer.user())
223-
.await?;
218+
let read = viewer.authorize(&self.database).await?;
219+
self.authorized_identities(&read).await
220+
}
221+
222+
/// [`Self::identities`], for a read already authorized.
223+
pub(crate) async fn authorized_identities(
224+
&self,
225+
read: &Authorized,
226+
) -> Result<Vec<Identity>, AccountError> {
227+
let identities = self.database.federation().list_active(read.user()).await?;
224228
Ok(identities.into_iter().map(Into::into).collect())
225229
}
226230

‎apps/auth-service/src/passkey/settings.rs‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//! administrator may also list, rename and remove them.
33
44
use super::{PasskeyApplication, PasskeyError, PasskeyName};
5-
use crate::account::{AccountError, AccountService, Owner, Subject, Viewer};
5+
use crate::account::{AccountError, AccountService, Authorized, Owner, Subject, Viewer};
66
use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD};
77
use sourceorigin_common::{
88
PasskeyId,
@@ -43,8 +43,16 @@ impl AccountService {
4343
&self,
4444
viewer: &Viewer,
4545
) -> Result<Vec<api::Passkey>, AccountError> {
46-
viewer.authorize(&self.database).await?;
47-
let passkeys = self.database.passkeys().list_active(viewer.user()).await?;
46+
let read = viewer.authorize(&self.database).await?;
47+
self.authorized_passkeys(&read).await
48+
}
49+
50+
/// [`Self::passkeys`], for a read already authorized.
51+
pub(crate) async fn authorized_passkeys(
52+
&self,
53+
read: &Authorized,
54+
) -> Result<Vec<api::Passkey>, AccountError> {
55+
let passkeys = self.database.passkeys().list_active(read.user()).await?;
4856
Ok(passkeys.into_iter().map(Into::into).collect())
4957
}
5058

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

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ use crate::{
1818
security::ApiError,
1919
web::{
2020
account::AccountPage,
21-
settings::{Addresses, ConnectedApps, SignInMethods},
21+
settings::{Addresses, ConnectedApps, HeldRows, SignInMethods},
2222
},
2323
};
2424
use serde::{Deserialize, de::DeserializeOwned};
2525
use sourceorigin_common::{EventId, Handle, UserId};
2626
use sourceorigin_ui::{
2727
Audience, Change, Checkbox, ConnectedAppChange, ConnectedAppRow, EmailChange, EmailRow,
28-
HistoryPage, HistoryState, Holdings, InboxPage, InboxRow, InboxState, ProfileChange,
29-
ProfileDialog, ProfilePages, ProfileState, ProviderRow, Recovery, SignInChange, Tab, Who,
28+
HistoryPage, HistoryState, InboxPage, InboxRow, InboxState, ProfileChange, ProfileDialog,
29+
ProfilePages, ProfileState, ProviderRow, Recovery, SignInChange, Tab, Who,
3030
};
3131
use topcoat::{
3232
Result,
@@ -282,23 +282,11 @@ pub(super) async fn history(cx: &Cx) -> Result<Response> {
282282
let viewer = App::viewer(user, admin);
283283
let account = &App::of(cx).account;
284284
let history = account.history(&viewer, before).await?;
285-
let passkeys = account.passkeys(&viewer).await?;
286-
let addresses = account.emails(&viewer).await?;
287-
let linked: Vec<_> = account
288-
.identities(&viewer)
289-
.await?
290-
.into_iter()
291-
.map(|identity| identity.provider)
292-
.collect();
285+
let holdings = account.holdings(&viewer).await?;
293286
let recovery = account.recovery_notice(&viewer).await?.map(Recovery::from);
294-
let email_rows: Vec<_> = addresses.iter().map(EmailRow::from).collect();
287+
let held = HeldRows::from(&holdings);
295288
let rows = history.rows();
296-
let holds = Holdings {
297-
passkeys: &passkeys,
298-
emails: &email_rows,
299-
linked: &linked,
300-
recovery: recovery.as_ref(),
301-
};
289+
let holds = held.holds(recovery.as_ref());
302290
let links = [
303291
App::here(cx),
304292
href!(sign_in, Account(user)).resolve(cx),

‎apps/auth-service/src/web/settings/history.rs‎

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ use crate::{
77
web::{
88
Failure, WebApplication,
99
account::{AccountPage, Applied, Reader},
10+
settings::HeldRows,
1011
},
1112
};
12-
use sourceorigin_ui::{
13-
Audience, EmailRow, HistoryChange, HistoryPage, HistoryState, Holdings, Messages, Section, Tab,
14-
};
13+
use sourceorigin_ui::{Audience, HistoryChange, HistoryPage, HistoryState, Messages, Section, Tab};
1514
use topcoat::{
1615
context::Cx,
1716
router::{StatusCode, response::Response},
@@ -87,27 +86,14 @@ impl AccountPage for History {
8786
) -> Result<Response, Failure> {
8887
let viewer = reader.viewer();
8988
let history = web.account.history(&viewer, shown.before).await?;
90-
let passkeys = web.account.passkeys(&viewer).await?;
91-
let emails = web.account.emails(&viewer).await?;
92-
let linked: Vec<_> = web
93-
.account
94-
.identities(&viewer)
95-
.await?
96-
.into_iter()
97-
.map(|identity| identity.provider)
98-
.collect();
89+
let holdings = web.account.holdings(&viewer).await?;
9990
let reported = match shown.reported {
10091
Some(event_id) => web.account.reported(&viewer, event_id).await?,
10192
None => None,
10293
};
103-
let email_rows: Vec<_> = emails.iter().map(EmailRow::from).collect();
94+
let held = HeldRows::from(&holdings);
95+
let holds = held.holds(reader.recovery());
10496
let rows = history.rows();
105-
let holds = Holdings {
106-
passkeys: &passkeys,
107-
emails: &email_rows,
108-
linked: &linked,
109-
recovery: reader.recovery(),
110-
};
11197
let page = HistoryPage {
11298
page: settings::HISTORY,
11399
sign_in: settings::SIGN_IN,

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

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ use super::{
4040
account::{Framed, Reader},
4141
};
4242
use crate::{
43+
account::Holdings,
4344
emails::EmailView,
4445
federation::FederationApplication,
4546
grants::ConnectedApp,
@@ -48,9 +49,13 @@ use crate::{
4849
routes::page::settings,
4950
sessions::SessionItem,
5051
};
51-
use sourceorigin_common::protocol::api::Identity;
52+
use sourceorigin_common::{
53+
Provider,
54+
protocol::api::{Identity, Passkey},
55+
};
5256
use sourceorigin_ui::{
53-
ConnectedAppRow, DeviceRow, EmailRow, HistoryRow, InboxRow, Messages, ProviderRow, Section, Tab,
57+
ConnectedAppRow, DeviceRow, EmailRow, HistoryRow, InboxRow, Messages, ProviderRow, Recovery,
58+
Section, Tab,
5459
};
5560
use topcoat::{
5661
context::Cx,
@@ -72,6 +77,49 @@ pub(super) const TABS: [(Tab, &str); 9] = [
7277
(Tab::Data, settings::DATA),
7378
];
7479

80+
/// What an account holds, as its security history and notifications name it:
81+
/// the owner's pages and the admin dashboard's show it through
82+
/// [`Self::holds`].
83+
pub(super) struct HeldRows<'a> {
84+
/// The account's passkeys.
85+
passkeys: &'a [Passkey],
86+
/// Its addresses.
87+
emails: Vec<EmailRow<'a>>,
88+
/// The providers linked to it.
89+
linked: Vec<Provider>,
90+
}
91+
92+
impl<'a> From<&'a Holdings> for HeldRows<'a> {
93+
/// The holdings as the pages show them.
94+
fn from(holdings: &'a Holdings) -> Self {
95+
Self {
96+
passkeys: &holdings.passkeys,
97+
emails: holdings.emails.iter().map(EmailRow::from).collect(),
98+
linked: holdings
99+
.identities
100+
.iter()
101+
.map(|identity| identity.provider)
102+
.collect(),
103+
}
104+
}
105+
}
106+
107+
impl HeldRows<'_> {
108+
/// What the history's entries and the notifications name, with the
109+
/// account's `recovery` that can still be undone, if any.
110+
pub(super) fn holds<'a>(
111+
&'a self,
112+
recovery: Option<&'a Recovery>,
113+
) -> sourceorigin_ui::Holdings<'a> {
114+
sourceorigin_ui::Holdings {
115+
passkeys: self.passkeys,
116+
emails: &self.emails,
117+
linked: &self.linked,
118+
recovery,
119+
}
120+
}
121+
}
122+
75123
impl<'a> From<&'a EmailView> for EmailRow<'a> {
76124
/// The address as a page shows it.
77125
fn from(email: &'a EmailView) -> Self {

‎apps/auth-service/src/web/settings/notifications.rs‎

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@ use crate::{
1616
web::{
1717
Failure, WebApplication,
1818
account::{AccountPage, Applied, Framed, Reader, no_change},
19+
settings::HeldRows,
1920
},
2021
};
2122
use sourceorigin_common::{
2223
NotificationId,
2324
notifications::{NotificationAction, NotificationPreference},
2425
};
2526
use sourceorigin_ui::{
26-
Audience, Crumb, EmailRow, HistoryRow, Holdings, InboxChange, InboxPage, InboxRow, InboxState,
27-
Messages, NotificationState, Section, SignOutStep, Tab, Title, UnsubscribeStep,
27+
Audience, Crumb, HistoryRow, InboxChange, InboxPage, InboxRow, InboxState, Messages,
28+
NotificationState, Section, SignOutStep, Tab, Title, UnsubscribeStep,
2829
};
2930
use topcoat::{
3031
context::{Cx, app_context},
@@ -180,16 +181,8 @@ impl AccountPage for NotificationPage {
180181
}
181182
let viewer = reader.viewer();
182183
let view = web.account.notification(&viewer, id).await?;
183-
let passkeys = web.account.passkeys(&viewer).await?;
184-
let emails = web.account.emails(&viewer).await?;
185-
let linked: Vec<_> = web
186-
.account
187-
.identities(&viewer)
188-
.await?
189-
.into_iter()
190-
.map(|identity| identity.provider)
191-
.collect();
192-
let email_rows: Vec<_> = emails.iter().map(EmailRow::from).collect();
184+
let holdings = web.account.holdings(&viewer).await?;
185+
let held = HeldRows::from(&holdings);
193186
let entry = view.history.as_ref().map(HistoryRow::from);
194187
let here = uri(cx).path().to_owned();
195188
// What it offers, while it can be done: undoing a recovery the account
@@ -214,12 +207,7 @@ impl AccountPage for NotificationPage {
214207
.as_ref()
215208
.map(|(action, href)| (*action, href.as_str())),
216209
entry: entry.as_ref(),
217-
holds: Holdings {
218-
passkeys: &passkeys,
219-
emails: &email_rows,
220-
linked: &linked,
221-
recovery: reader.recovery(),
222-
},
210+
holds: held.holds(reader.recovery()),
223211
shown,
224212
};
225213
let row = InboxRow::from(&view.item);

0 commit comments

Comments
 (0)