//! First-run setup. While no user exists, `GET /setup` shows the form that creates the first //! administrator. Once a user exists, both the page and its POST are a 404, so the route does not //! reveal that the instance was set up. use im_core::model::{AuditAction, amr}; use im_core::names::{normalize_email, parse_display_name, parse_email}; use im_core::password::{check_policy, hash_password_async}; use im_core::store::sessions::SignInState; use serde::Deserialize; use serde_json::Value; use topcoat::Result; use topcoat::context::{Cx, app_context}; use topcoat::router::content::Form; use topcoat::router::error::{forbidden, not_found, see_other}; use topcoat::router::page; use topcoat::view::{View, view}; use crate::audit::{Actor, record}; use crate::auth::{after_sign_in, check_pre_csrf, clear_pre_csrf, pre_csrf_token, start_session}; use crate::i18n::Lang; use crate::i18n::account::{Txt, policy_problem, text}; use crate::state::AppState; use crate::views::{alert, field, page_lang, shell}; /// The fields of the setup form. Every field defaults to empty, so a missing one is a failed check /// (403 for the token, a message for the rest), not a 400. #[derive(Deserialize, Default)] struct SetupForm { #[serde(default)] precsrf: String, #[serde(default)] name: String, #[serde(default)] email: String, #[serde(default)] password: String, #[serde(default)] password_again: String, } /// `GET /setup`. #[page("/setup")] async fn setup_form(cx: &Cx) -> Result { if app_context::(cx).store.user_count().await? > 0 { return Err(not_found().into()); } let csrf = pre_csrf_token(cx); let lang = page_lang(cx, None); Ok(setup_view( cx, lang, csrf, String::new(), String::new(), None, )) } /// `POST /setup`. Creates the first administrator, signs them in and sends them on. #[page(POST "/setup")] async fn setup_submit(cx: &Cx, Form(form): Form) -> Result { let state = app_context::(cx); if state.store.user_count().await? > 0 { return Err(not_found().into()); } if !check_pre_csrf(cx, &form.precsrf) { return Err(forbidden().into()); } let csrf = pre_csrf_token(cx); let lang = page_lang(cx, None); let typed_email = form.email.clone(); // A refused name is not shown back: it may hold control characters, which must not reach HTML. let name = match parse_display_name(&form.name) { Ok(name) => name, Err(_) => { let problem = text(lang, Txt::ErrNameInvalid).to_owned(); return Ok(setup_view( cx, lang, csrf, String::new(), typed_email, Some(problem), )); } }; let problem = match parse_email(&normalize_email(&form.email)) { Err(_) => Some(text(lang, Txt::ErrEmailInvalid).to_owned()), Ok(email) => match check_policy(&form.password, &email, &name) { Err(violation) => Some(policy_problem(lang, violation)), Ok(()) if form.password != form.password_again => { Some(text(lang, Txt::ErrPasswordsDiffer).to_owned()) } Ok(()) => None, }, }; if let Some(problem) = problem { return Ok(setup_view(cx, lang, csrf, name, typed_email, Some(problem))); } let email = parse_email(&normalize_email(&form.email))?; let hash = hash_password_async(form.password.clone()).await?; let user = match state.store.create_first_admin(&email, &name, &hash).await { Ok(user) => user, // Another setup finished first; the page no longer exists for this request either. Err(im_core::Error::Conflict(_)) => return Err(not_found().into()), Err(error) => return Err(error.into()), }; if !start_session(cx, &user, amr::PASSWORD, SignInState::of(&user)).await? { clear_pre_csrf(cx); return Err(see_other("/login").into()); } clear_pre_csrf(cx); record( cx, Actor::User(&user.id), AuditAction::SetupDone, Some(("user", &user.id)), Value::Null, ) .await; Err(see_other(after_sign_in(state, &user, "/")).into()) } /// The setup page. The typed name and email come back; passwords never do. fn setup_view( cx: &Cx, lang: Lang, csrf: String, name: String, email: String, problem: Option, ) -> impl View { view! { cx => shell(title: text(lang, Txt::SetupTitle), lang: lang, viewer: None,

(text(lang, Txt::SetupTitle))

(text(lang, Txt::SetupIntro))

if let Some(problem) = problem { alert(message: problem) }
field(label: text(lang, Txt::Name), name: "name", kind: "text", autocomplete: "name", value: name) field(label: text(lang, Txt::Email), name: "email", kind: "email", autocomplete: "username", value: email) field(label: text(lang, Txt::Password), name: "password", kind: "password", autocomplete: "new-password", value: String::new()) field(label: text(lang, Txt::NewPasswordAgain), name: "password_again", kind: "password", autocomplete: "new-password", value: String::new())

(text(lang, Txt::PasswordHint))

) } }