Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions apps/auth-service/src/web/admin/administrators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ pub(super) async fn list(cx: &Cx) -> Result<Response> {
/// What [`grant`] takes: by `GET`, the search for the account, from its query;
/// by `POST`, the account to grant and why, from its form. One type, since the
/// extractor reads a `GET`'s query as its form.
///
/// Not an [`Administered`](super::Administered): flattened fields reach their
/// type as text, so `page=2`, the pager's own link, and an empty `user_id` would
/// fail to decode. Its own `reason` defaults the same way.
#[derive(Clone, Deserialize)]
pub(super) struct GrantForm {
/// The account to grant rights, which the button posts.
Expand Down
21 changes: 6 additions & 15 deletions apps/auth-service/src/web/admin/clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
//! page at `/clients/new`, whatever the client's ID.

use super::{
AdminApplication as App, ReasonForm, ReasonPage, Refusal,
AdminApplication as App, Administered, ReasonForm, ReasonPage, Refusal,
origins::{self, OriginQuery},
};
use crate::{
Expand All @@ -25,7 +25,6 @@ use crate::{
security::ApiError,
};
use chrono::Utc;
use serde::Deserialize;
use sourceorigin_common::{
ClientId,
clients::{ClientKind, ClientRegistration},
Expand Down Expand Up @@ -232,7 +231,10 @@ pub(super) async fn edit(cx: &Cx, form: Option<Form<OperatorDraft>>) -> Result<R
/// `/clients/{client}/secret`: a confidential client's secrets, and rotating
/// them. A rotation comes back here, which shows the new secret once.
#[route([GET, POST] "/clients/{client}/secret")]
pub(super) async fn rotate(cx: &Cx, form: Option<Form<Rotation>>) -> Result<Response> {
pub(super) async fn rotate(
cx: &Cx,
form: Option<Form<Administered<SecretsChange>>>,
) -> Result<Response> {
let admin = App::admin(cx).await?;
// The page showing a new secret needs no confirmation: sending the browser
// to confirm would lose the secret.
Expand All @@ -247,7 +249,7 @@ pub(super) async fn rotate(cx: &Cx, form: Option<Form<Rotation>>) -> Result<Resp
return Err(AdminError::from(ClientRegistryError::Public(id)).into());
}
let here = href!(rotate, Client(id.clone())).resolve(cx);
let changed = App::change(cx, form, async |rotation: Rotation| {
let changed = App::change(cx, form, async |rotation| {
let SecretsChange::Rotate { grace } = rotation.change;
let reason = Reason::parse(rotation.reason)?;
service
Expand Down Expand Up @@ -313,17 +315,6 @@ pub(super) async fn rotate(cx: &Cx, form: Option<Form<Rotation>>) -> Result<Resp
Box::pin(App::render(cx, StatusCode::OK, "Client secrets", page)).await
}

/// The form rotating a client's secret: the developer console's, with why.
#[derive(Clone, Deserialize)]
pub(super) struct Rotation {
/// The rotation, as the developer console posts it.
#[serde(flatten)]
change: SecretsChange,
/// Why, as typed.
#[serde(default)]
reason: String,
}

/// A change to a client that only needs confirming, as its `client` command
/// makes it, each on its own page.
#[derive(Clone, Copy)]
Expand Down
20 changes: 11 additions & 9 deletions apps/auth-service/src/web/admin/handles.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! Handles: the reserved list, the accounts holding a reserved handle, and the
//! released handles, with reserving, unreserving and releasing one.

use super::{AdminApplication as App, ReasonForm, ReasonPage, settings, users::Account};
use super::{
AdminApplication as App, Administered, ReasonForm, ReasonPage, settings, users::Account,
};
use crate::{Reason, handles::HandlesError};
use serde::{Deserialize, Serialize};
use sourceorigin_common::{Handle, handle::RESERVED};
Expand All @@ -25,14 +27,11 @@ struct HandleQuery {

impl Query for HandleQuery {}

/// The form reserving a handle.
/// The form reserving a handle, whose reason says who or what it is kept for.
#[derive(Clone, Deserialize)]
struct Reservation {
/// The handle to reserve.
handle: String,
/// Who or what it is kept for.
#[serde(default)]
reason: String,
}

/// `/handles`: the reserved list, with the built-in names; the accounts whose
Expand Down Expand Up @@ -98,20 +97,23 @@ pub(super) async fn check(cx: &Cx) -> Result<Response> {

/// `/handles/reserve`: reserves a handle, saying who or what it is kept for.
#[route([GET, POST] "/handles/reserve")]
pub(super) async fn reserve(cx: &Cx, form: Option<Form<Reservation>>) -> Result<Response> {
pub(super) async fn reserve(
cx: &Cx,
form: Option<Form<Administered<Reservation>>>,
) -> Result<Response> {
let admin = App::confirmed(cx).await?;
let service = &App::of(cx).service;
let back = href!(check).resolve(cx);
let refused = App::submit(cx, form, back.clone(), |input: Reservation| async move {
let handle = Handle::parse(&input.handle).map_err(HandlesError::from)?;
let refused = App::submit(cx, form, back.clone(), |input| async move {
let handle = Handle::parse(&input.change.handle).map_err(HandlesError::from)?;
let reason = Reason::parse(input.reason)?;
service
.reserve_handle(&admin.operator, handle, reason)
.await
})
.await?;
let (typed, reason) = refused.as_ref().map_or(("", ""), |refused| {
(refused.form.handle.as_str(), refused.form.reason.as_str())
(refused.form.change.handle.as_str(), refused.reason())
});
let refusal = refused.as_deref();
let page = view! { cx =>
Expand Down
22 changes: 11 additions & 11 deletions apps/auth-service/src/web/admin/keys.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Signing keys: listing, rotating, revoking and rewrapping them.

use super::{AdminApplication as App, ReasonForm, ReasonPage};
use crate::Reason;
use super::{AdminApplication as App, Administered, ReasonForm, ReasonPage, Refused};
use serde::Deserialize;
use sourceorigin_common::protocol::jwt::KeyId;
use sourceorigin_ui::{Action, Change, Checkbox, SigningKeyRow, Target};
Expand All @@ -20,9 +19,6 @@ struct Rotation {
/// Whether the new key signs at once, skipping its day of waiting.
#[serde(default)]
now: Checkbox,
/// Why.
#[serde(default)]
reason: String,
}

/// `/keys`: every signing key, oldest first, each with the change revoking it.
Expand Down Expand Up @@ -57,20 +53,24 @@ pub(super) async fn list(cx: &Cx) -> Result<Response> {

/// `/keys/rotate`: publishes a new signing key, or makes it sign at once.
#[route([GET, POST] "/keys/rotate")]
pub(super) async fn rotate(cx: &Cx, form: Option<Form<Rotation>>) -> Result<Response> {
pub(super) async fn rotate(
cx: &Cx,
form: Option<Form<Administered<Rotation>>>,
) -> Result<Response> {
let admin = App::confirmed(cx).await?;
let app = App::of(cx);
let back = href!(list).resolve(cx);
let refused = App::submit(cx, form, back.clone(), |input: Rotation| async move {
let reason = Reason::parse(input.reason)?;
let refused = App::submit_reason(cx, form, back.clone(), |rotation, reason| async move {
app.service
.rotate_signing_key(&admin.operator, &app.keys, input.now.0, reason)
.rotate_signing_key(&admin.operator, &app.keys, rotation.now.0, reason)
.await
.map(drop)
})
.await?;
let now = refused.as_ref().is_some_and(|refused| refused.form.now.0);
let reason = refused.as_ref().map(|refused| refused.form.reason.as_str());
let now = refused
.as_ref()
.is_some_and(|refused| refused.form.change.now.0);
let reason = refused.as_ref().map(Refused::reason);
let (back, refused) = (&back, refused.as_deref());
let page = view! { cx =>
sourceorigin_ui::signing_key_rotation(back: back.as_str(), refused: refused, now: now, reason: reason)
Expand Down
46 changes: 32 additions & 14 deletions apps/auth-service/src/web/admin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -732,22 +732,25 @@ impl AdminApplication {
.map(Body::new)
}

/// [`Self::submit`] for a change whose form posts only why: parses the
/// reason and makes the change with it.
async fn submit_reason<F, Fut, E>(
/// [`Self::submit`] for a change that checks its reason first: parses the
/// reason, then makes the change with it.
async fn submit_reason<C, F, Fut, E>(
cx: &Cx,
form: Option<Form<ReasonForm>>,
form: Option<Form<Administered<C>>>,
done: String,
call: F,
) -> Result<Option<Refused<ReasonForm>>>
) -> Result<Option<Refused<Administered<C>>>>
where
C: Clone,
E: Into<Refusal>,
F: FnOnce(Reason) -> Fut,
F: FnOnce(C, Reason) -> Fut,
Fut: Future<Output = Result<(), E>>,
{
Self::submit(cx, form, done, async |input: ReasonForm| {
Self::submit(cx, form, done, async |input: Administered<C>| {
let reason = Reason::parse(input.reason)?;
call(reason).await.map_err(Into::<Refusal>::into)
call(input.change, reason)
.await
.map_err(Into::<Refusal>::into)
})
.await
}
Expand All @@ -767,7 +770,8 @@ impl AdminApplication {
F: FnOnce(Reason) -> Fut,
Fut: Future<Output = Result<(), E>>,
{
let refused = Self::submit_reason(cx, form, done, call).await?;
let refused =
Self::submit_reason(cx, form, done, async move |(), reason| call(reason).await).await?;
Self::reason_page(cx, page, refused, ()).await
}

Expand Down Expand Up @@ -856,16 +860,30 @@ struct Refused<I> {
form: I,
}

/// The form of a change that asks only why: its reason, recorded with it, which
/// the owner of an account it changes sees in their history. A `GET`'s query,
/// which the extractor reads as its form, has none.
/// The form of a dashboard change: the change, and why, recorded with it, which
/// the owner of an account it changes sees in their history. The reason
/// defaults to empty, so a form posted without one decodes and is refused on its
/// page, as a blank one is, and a `GET`'s query, which the extractor reads as
/// its form, needs none.
///
/// `C`'s fields reach it flattened, as text, past the form's own parsing: each
/// must decode from a string, as strings, IDs, `Checkbox` and text enums do.
/// A number, or an `Option` that reads an empty value as `None`, fails to
/// decode, so a form with one, such as the administrator grant's, keeps its
/// own `reason` field instead.
#[derive(Clone, Deserialize)]
struct ReasonForm {
struct Administered<C> {
/// The change, as its form names it.
#[serde(flatten)]
change: C,
/// Why, as typed.
#[serde(default)]
reason: String,
}

/// The form of a change that asks only why.
type ReasonForm = Administered<()>;

/// The page of a change whose form asks only why, which
/// [`AdminApplication::reason_change`] serves.
struct ReasonPage<'a> {
Expand Down Expand Up @@ -893,7 +911,7 @@ impl<'a> ReasonPage<'a> {
}
}

impl Refused<ReasonForm> {
impl<C> Refused<Administered<C>> {
/// The reason as it was typed, which the page shows again.
fn reason(&self) -> &str {
&self.form.reason
Expand Down
Loading