Skip to content

Commit d4ecc98

Browse files
committed
refactor: split the largest source files by area
security, passkey, admin/service, config and oidc/authorize in auth; events in common; the SDK's axum login and session; the ui crate's layout and its stylesheet, whose parts are concatenated at compile time into the same bytes. Public paths are re-exported unchanged, and config's and authorize's inline tests move to tests.rs.
1 parent 0034135 commit d4ecc98

71 files changed

Lines changed: 12545 additions & 11536 deletions

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: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ just build
2323
```
2424

2525
The executables are `target/release/auth-service` and `target/release/piquel-web`.
26-
Both embed the stylesheet (`crates/ui/assets/style.css`) and its typefaces
27-
(`crates/ui/assets/fonts`, under the SIL Open Font License); auth also embeds the
28-
passkey script (`apps/auth-service/assets/passkey.js`), and piquel-web the
29-
policies (`apps/web/assets/policies/`). Rebuild Rust after changing them.
26+
Both embed the stylesheet (`crates/ui/assets/style/`, its parts joined into one
27+
file) and its typefaces (`crates/ui/assets/fonts`, under the SIL Open Font
28+
License); auth also embeds the passkey script (`apps/auth-service/assets/passkey.js`),
29+
and piquel-web the policies (`apps/web/assets/policies/`). Rebuild Rust after
30+
changing them.
3031

3132
## Development
3233

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

Lines changed: 0 additions & 1089 deletions
This file was deleted.
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
//! Who may join and who administers: signup invitations and administrator
2+
//! rights.
3+
4+
use super::AdminService;
5+
use crate::{
6+
account::AccountService,
7+
admin::{AdminError, InviteExpiry, Operator},
8+
routes,
9+
};
10+
use chrono::Utc;
11+
use piquel_common::{
12+
AccountStatus, TokenId, UserId,
13+
admin::{Administrator, CreatedInvitation, Invitation},
14+
config::Origin,
15+
events::DomainEvent,
16+
};
17+
use piquel_crypto::Secret;
18+
use piquel_ui::{LoginQuery, Query};
19+
20+
impl AdminService {
21+
/// Lists invitations that have not been purged yet, newest first.
22+
pub(crate) async fn list_invitations(
23+
&self,
24+
operator: &Operator,
25+
) -> Result<Vec<Invitation>, AdminError> {
26+
operator.authorize_read(&self.database).await?;
27+
Ok(self.database.security().list_invitations().await?)
28+
}
29+
30+
/// Creates a single-use signup link to the registration page on `origin`, auth's
31+
/// own. Only the secret's digest is stored, so the link cannot be shown again.
32+
///
33+
/// # Errors
34+
/// Fails when the operator may not make changes, or secret generation or
35+
/// persistence fails.
36+
pub async fn create_invitation(
37+
&self,
38+
operator: &Operator,
39+
origin: &Origin,
40+
expires_in: InviteExpiry,
41+
) -> Result<CreatedInvitation, AdminError> {
42+
let secret = Secret::generate()?;
43+
let id = TokenId::new();
44+
let expires_at = Utc::now() + expires_in.duration();
45+
let mut tx = self.database.begin().await?;
46+
tx.security()
47+
.create_invitation(id, secret.digest().as_bytes(), expires_at)
48+
.await?;
49+
let created = DomainEvent::InvitationCreated { expires_at };
50+
Self::finish(tx, operator, None, vec![created]).await?;
51+
let url = LoginQuery {
52+
invitation: Some(secret.encoded()),
53+
..LoginQuery::default()
54+
}
55+
.url(origin.as_url(), routes::page::REGISTER)?;
56+
Ok(CreatedInvitation {
57+
id,
58+
url,
59+
expires_at,
60+
})
61+
}
62+
63+
/// Revokes an unredeemed invitation.
64+
pub(crate) async fn revoke_invitation(
65+
&self,
66+
operator: &Operator,
67+
invitation_id: TokenId,
68+
) -> Result<(), AdminError> {
69+
let mut tx = self.database.begin().await?;
70+
if !tx.security().revoke_invitation(invitation_id).await? {
71+
return Err(AdminError::InvitationNotFound);
72+
}
73+
let revoked = DomainEvent::InvitationRevoked { invitation_id };
74+
Self::finish(tx, operator, None, vec![revoked]).await
75+
}
76+
77+
/// Lists every administrator, oldest grant first.
78+
///
79+
/// # Errors
80+
/// Fails when the operator may not read, or the query fails.
81+
pub async fn list_administrators(
82+
&self,
83+
operator: &Operator,
84+
) -> Result<Vec<Administrator>, AdminError> {
85+
operator.authorize_read(&self.database).await?;
86+
Ok(self.database.administrators().list().await?)
87+
}
88+
89+
/// Makes the active account `user_id` an administrator. Administrators cannot
90+
/// grant themselves rights: one whose rights were revoked while the request
91+
/// waited would otherwise restore them. While a recovery of the account can
92+
/// still be undone, it is refused, by the CLI too: granted during a probation,
93+
/// the rights would go live for whoever used its link once it settles. The
94+
/// migration's `administrators_no_grant_mid_recovery` backs it for every
95+
/// writer.
96+
///
97+
/// # Errors
98+
/// Fails when the operator may not make changes, or the account is unknown,
99+
/// disabled, already an administrator, the caller's own, or has a recovery
100+
/// that can still be undone.
101+
pub(crate) async fn grant_administrator(
102+
&self,
103+
operator: &Operator,
104+
user_id: UserId,
105+
) -> Result<(), AdminError> {
106+
operator.refuse_own_rights(user_id)?;
107+
let mut tx = self.database.begin().await?;
108+
tx.administrators().lock().await?;
109+
let user = tx
110+
.users()
111+
.find_by_id_for_update(user_id)
112+
.await?
113+
.ok_or(AdminError::UserNotFound)?;
114+
if user.status != AccountStatus::Active {
115+
return Err(AdminError::AccountDisabled);
116+
}
117+
if AccountService::undoable(&mut tx, user_id).await?.is_some() {
118+
return Err(AdminError::GrantDuringRecovery);
119+
}
120+
if !tx.administrators().grant(user_id).await? {
121+
return Err(AdminError::AlreadyAdministrator);
122+
}
123+
let granted = DomainEvent::AdministratorGranted {};
124+
Self::finish(tx, operator, Some(user_id), vec![granted]).await
125+
}
126+
127+
/// Removes the administrator rights of `user_id`. Administrators cannot remove
128+
/// their own, and nobody can remove the last active administrator's: someone
129+
/// must be left to administer.
130+
///
131+
/// # Errors
132+
/// Fails when the operator may not make changes, the account is not an
133+
/// administrator, or either rule forbids it.
134+
pub async fn revoke_administrator(
135+
&self,
136+
operator: &Operator,
137+
user_id: UserId,
138+
) -> Result<(), AdminError> {
139+
operator.refuse_own_rights(user_id)?;
140+
let mut tx = self.database.begin().await?;
141+
let locked = tx.administrators().lock().await?;
142+
if tx.administrators().is_last_active(&locked, user_id).await? {
143+
return Err(AdminError::LastAdministrator);
144+
}
145+
if !tx.administrators().revoke(user_id).await? {
146+
return Err(AdminError::NotAnAdministrator);
147+
}
148+
let revoked = DomainEvent::AdministratorRevoked {};
149+
Self::finish(tx, operator, Some(user_id), vec![revoked]).await
150+
}
151+
}

0 commit comments

Comments
 (0)