| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | use im_core::model::{AuditAction, amr}; |
| 6 | use im_core::names::{normalize_email, parse_display_name, parse_email}; |
| 7 | use im_core::password::{check_policy, hash_password_async}; |
| 8 | use im_core::store::sessions::SignInState; |
| 9 | use serde::Deserialize; |
| 10 | use serde_json::Value; |
| 11 | use topcoat::Result; |
| 12 | use topcoat::context::{Cx, app_context}; |
| 13 | use topcoat::router::content::Form; |
| 14 | use topcoat::router::error::{forbidden, not_found, see_other}; |
| 15 | use topcoat::router::page; |
| 16 | use topcoat::view::{View, view}; |
| 17 | |
| 18 | use crate::audit::{Actor, record}; |
| 19 | use crate::auth::{after_sign_in, check_pre_csrf, clear_pre_csrf, pre_csrf_token, start_session}; |
| 20 | use crate::i18n::Lang; |
| 21 | use crate::i18n::account::{Txt, policy_problem, text}; |
| 22 | use crate::state::AppState; |
| 23 | use crate::views::{alert, field, page_lang, shell}; |
| 24 | |
| 25 | |
| 26 | |
| 27 | #[derive(Deserialize, Default)] |
| 28 | struct SetupForm { |
| 29 | #[serde(default)] |
| 30 | precsrf: String, |
| 31 | #[serde(default)] |
| 32 | name: String, |
| 33 | #[serde(default)] |
| 34 | email: String, |
| 35 | #[serde(default)] |
| 36 | password: String, |
| 37 | #[serde(default)] |
| 38 | password_again: String, |
| 39 | } |
| 40 | |
| 41 | |
| 42 | #[page("/setup")] |
| 43 | async fn setup_form(cx: &Cx) -> Result<impl View> { |
| 44 | if app_context::<AppState>(cx).store.user_count().await? > 0 { |
| 45 | return Err(not_found().into()); |
| 46 | } |
| 47 | let csrf = pre_csrf_token(cx); |
| 48 | let lang = page_lang(cx, None); |
| 49 | Ok(setup_view( |
| 50 | cx, |
| 51 | lang, |
| 52 | csrf, |
| 53 | String::new(), |
| 54 | String::new(), |
| 55 | None, |
| 56 | )) |
| 57 | } |
| 58 | |
| 59 | |
| 60 | #[page(POST "/setup")] |
| 61 | async fn setup_submit(cx: &Cx, Form(form): Form<SetupForm>) -> Result<impl View> { |
| 62 | let state = app_context::<AppState>(cx); |
| 63 | if state.store.user_count().await? > 0 { |
| 64 | return Err(not_found().into()); |
| 65 | } |
| 66 | if !check_pre_csrf(cx, &form.precsrf) { |
| 67 | return Err(forbidden().into()); |
| 68 | } |
| 69 | let csrf = pre_csrf_token(cx); |
| 70 | let lang = page_lang(cx, None); |
| 71 | let typed_email = form.email.clone(); |
| 72 | |
| 73 | |
| 74 | let name = match parse_display_name(&form.name) { |
| 75 | Ok(name) => name, |
| 76 | Err(_) => { |
| 77 | let problem = text(lang, Txt::ErrNameInvalid).to_owned(); |
| 78 | return Ok(setup_view( |
| 79 | cx, |
| 80 | lang, |
| 81 | csrf, |
| 82 | String::new(), |
| 83 | typed_email, |
| 84 | Some(problem), |
| 85 | )); |
| 86 | } |
| 87 | }; |
| 88 | let problem = match parse_email(&normalize_email(&form.email)) { |
| 89 | Err(_) => Some(text(lang, Txt::ErrEmailInvalid).to_owned()), |
| 90 | Ok(email) => match check_policy(&form.password, &email, &name) { |
| 91 | Err(violation) => Some(policy_problem(lang, violation)), |
| 92 | Ok(()) if form.password != form.password_again => { |
| 93 | Some(text(lang, Txt::ErrPasswordsDiffer).to_owned()) |
| 94 | } |
| 95 | Ok(()) => None, |
| 96 | }, |
| 97 | }; |
| 98 | if let Some(problem) = problem { |
| 99 | return Ok(setup_view(cx, lang, csrf, name, typed_email, Some(problem))); |
| 100 | } |
| 101 | |
| 102 | let email = parse_email(&normalize_email(&form.email))?; |
| 103 | let hash = hash_password_async(form.password.clone()).await?; |
| 104 | let user = match state.store.create_first_admin(&email, &name, &hash).await { |
| 105 | Ok(user) => user, |
| 106 | |
| 107 | Err(im_core::Error::Conflict(_)) => return Err(not_found().into()), |
| 108 | Err(error) => return Err(error.into()), |
| 109 | }; |
| 110 | if !start_session(cx, &user, amr::PASSWORD, SignInState::of(&user)).await? { |
| 111 | clear_pre_csrf(cx); |
| 112 | return Err(see_other("/login").into()); |
| 113 | } |
| 114 | clear_pre_csrf(cx); |
| 115 | record( |
| 116 | cx, |
| 117 | Actor::User(&user.id), |
| 118 | AuditAction::SetupDone, |
| 119 | Some(("user", &user.id)), |
| 120 | Value::Null, |
| 121 | ) |
| 122 | .await; |
| 123 | Err(see_other(after_sign_in(state, &user, "/")).into()) |
| 124 | } |
| 125 | |
| 126 | |
| 127 | fn setup_view( |
| 128 | cx: &Cx, |
| 129 | lang: Lang, |
| 130 | csrf: String, |
| 131 | name: String, |
| 132 | email: String, |
| 133 | problem: Option<String>, |
| 134 | ) -> impl View { |
| 135 | view! { cx => shell(title: text(lang, Txt::SetupTitle), lang: lang, viewer: None, |
| 136 | <section class="card narrow stack"> |
| 137 | <h1>(text(lang, Txt::SetupTitle))</h1> |
| 138 | <p class="muted">(text(lang, Txt::SetupIntro))</p> |
| 139 | if let Some(problem) = problem { |
| 140 | alert(message: problem) |
| 141 | } |
| 142 | <form method="post" action="/setup" class="stack"> |
| 143 | <input type="hidden" name="precsrf" value=(csrf)> |
| 144 | field(label: text(lang, Txt::Name), name: "name", kind: "text", autocomplete: "name", value: name) |
| 145 | field(label: text(lang, Txt::Email), name: "email", kind: "email", autocomplete: "username", value: email) |
| 146 | field(label: text(lang, Txt::Password), name: "password", kind: "password", autocomplete: "new-password", value: String::new()) |
| 147 | field(label: text(lang, Txt::NewPasswordAgain), name: "password_again", kind: "password", autocomplete: "new-password", value: String::new()) |
| 148 | <p class="hint">(text(lang, Txt::PasswordHint))</p> |
| 149 | <button type="submit" class="button primary">(text(lang, Txt::CreateAdmin))</button> |
| 150 | </form> |
| 151 | </section> |
| 152 | ) } |
| 153 | } |