diff --git a/crates/im-web/src/auth.rs b/crates/im-web/src/auth.rs index 78ab347..e34fbd9 100644 --- a/crates/im-web/src/auth.rs +++ b/crates/im-web/src/auth.rs @@ -101,17 +101,12 @@ async fn login(cx: &Cx, Form(input): Form) -> Redirect { see("/login/totp".to_string()) } Ok(user) => { - // An account without confirmed 2FA — everyone who migrated from - // İzlek — enrols before any session exists: same bar as an - // invited account. - if im_core::totp::totp_secret(store, &user.id).await?.is_none() { - let secret = im_core::totp::generate_secret(); - im_core::totp::set_totp(store, &user.id, &secret).await?; - } - let sealed = server::mint_pending(cx, &user.id, PendingPurpose::Enroll, back).await; - server::set_pending_cookie(cx, sealed).await; let _ = accounts::clear_login_failures(store, &key).await; - see("/enroll".to_string()) + let token = + im_core::sessions::create_session(store, &user.id, &session_meta(cx)).await?; + server::set_session_cookie(cx, token.expose()); + server::log_event(cx, "login_ok", Some(&user.email), None).await; + see(back) } Err(_) => { // The failure is logged against the address tried, never the @@ -220,47 +215,33 @@ async fn invite(cx: &Cx, Form(input): Form) -> Redirect { return see(format!("/invite/{}?error={code}", input.token)); } }; - // TOTP enrolment is not optional: the account exists from here, and the - // enrolment page is the only way forward. - let secret = im_core::totp::generate_secret(); - im_core::totp::set_totp(store, &user.id, &secret).await?; - let sealed = server::mint_pending(cx, &user.id, PendingPurpose::Enroll, "/".to_string()).await; - server::set_pending_cookie(cx, sealed).await; + let token = im_core::sessions::create_session(store, &user.id, &session_meta(cx)).await?; server::log_event(cx, "invite_accepted", Some(&user.email), None).await; - see("/enroll".to_string()) + server::set_session_cookie(cx, token.expose()); + see("/".to_string()) } #[route(POST "/enroll")] async fn enroll(cx: &Cx, Form(input): Form) -> Redirect { - let Some(pending) = server::opened_pending(cx) else { + let Some(user) = server::current_user(cx).await else { return see("/login".to_string()); }; - if pending.purpose != PendingPurpose::Enroll { - return see("/login".to_string()); + if user.totp_confirmed { + return see("/".to_string()); } let store = &server::app(cx).store; - let user_id = UserId::from(pending.user); - let Some((secret, _)) = im_core::totp::totp_secret(store, &user_id).await? else { - return see("/login?error=enroll_first".to_string()); + let Some((secret, _)) = im_core::totp::totp_secret(store, &user.id).await? else { + return see("/enroll".to_string()); }; if !im_core::totp::verify_totp(&secret, input.code.trim(), time::OffsetDateTime::now_utc()) { return see("/enroll?error=bad_code".to_string()); } - im_core::totp::confirm_totp(store, &user_id).await?; - let token = im_core::sessions::create_session(store, &user_id, &session_meta(cx)).await?; - let email = accounts::user_by_id(store, &user_id) + im_core::totp::confirm_totp(store, &user.id).await?; + let email = accounts::user_by_id(store, &user.id) .await? .map(|u| u.email); server::log_event(cx, "enrolled", email.as_deref(), None).await; - server::clear_pending_cookie(cx); - server::set_session_cookie(cx, token.expose()); - // A migrated user who was mid-`/authorize` continues to their app; a - // fresh invite lands on the welcome banner. - if pending.back == "/" { - see("/?ok=enrolled".to_string()) - } else { - see(pending.back) - } + see("/?ok=enrolled".to_string()) } /// "Sign out everywhere": the central session dies, and with it every diff --git a/crates/im-web/src/i18n.rs b/crates/im-web/src/i18n.rs index f378c58..3d8fef5 100644 --- a/crates/im-web/src/i18n.rs +++ b/crates/im-web/src/i18n.rs @@ -53,7 +53,6 @@ pub enum Key { ErrPasswordTooShort, ErrPasswordPersonal, ErrPasswordsDiffer, - ErrEnrollFirst, ErrSmtpTest, ErrPasswordWrong, ErrPasswordSame, @@ -98,7 +97,7 @@ pub enum Key { BackToSignIn, EnrollTitle, EnrollSub, - ConfirmAndSignIn, + ConfirmEnroll, ForgotTitle, ForgotSub, ForgotSentNote, @@ -118,6 +117,7 @@ pub enum Key { // Signed-in landing and person page, shared. TwoFaOn, TwoFaOff, + Activate2fa, AdminChip, MemberSinceLabel, PublicProfileLink, @@ -304,8 +304,6 @@ pub fn t(lang: Lang, key: Key) -> &'static str { (ErrPasswordPersonal, Tr) => "Parola adresini ya da adını içeremez.", (ErrPasswordsDiffer, En) => "The two passwords don't match.", (ErrPasswordsDiffer, Tr) => "İki parola eşleşmiyor.", - (ErrEnrollFirst, En) => "Set up your second factor first.", - (ErrEnrollFirst, Tr) => "Önce ikinci faktörünü kur.", (ErrSmtpTest, En) => "The test mail could not be sent.", (ErrSmtpTest, Tr) => "Test postası gönderilemedi.", (ErrPasswordWrong, En) => "That's not your current password.", @@ -390,8 +388,8 @@ pub fn t(lang: Lang, key: Key) -> &'static str { (EnrollTitle, Tr) => "İki faktörlüyü kur", (EnrollSub, En) => "Scan with your authenticator, then type the 6-digit code it shows.", (EnrollSub, Tr) => "Doğrulama uygulamanla tara, sonra gösterdiği 6 haneli kodu yaz.", - (ConfirmAndSignIn, En) => "Confirm and sign in", - (ConfirmAndSignIn, Tr) => "Onayla ve giriş yap", + (ConfirmEnroll, En) => "Enable two-factor", + (ConfirmEnroll, Tr) => "İki faktörlüyü aç", (ForgotTitle, En) => "Forgot it?", (ForgotTitle, Tr) => "Parolamı unuttum", (ForgotSub, En) => { @@ -431,6 +429,8 @@ pub fn t(lang: Lang, key: Key) -> &'static str { (TwoFaOn, Tr) => "2FA açık", (TwoFaOff, En) => "2FA off", (TwoFaOff, Tr) => "2FA kapalı", + (Activate2fa, En) => "Activate two-factor", + (Activate2fa, Tr) => "İki faktörlüyü etkinleştir", (AdminChip, En) => "Admin", (AdminChip, Tr) => "Yönetici", (MemberSinceLabel, En) => "Member since", diff --git a/crates/im-web/src/pages.rs b/crates/im-web/src/pages.rs index c7eec45..9b2785a 100644 --- a/crates/im-web/src/pages.rs +++ b/crates/im-web/src/pages.rs @@ -30,7 +30,6 @@ pub fn error_text(code: &str, lang: Lang) -> &'static str { "password_too_short" => t(lang, Key::ErrPasswordTooShort), "password_personal" => t(lang, Key::ErrPasswordPersonal), "passwords_differ" => t(lang, Key::ErrPasswordsDiffer), - "enroll_first" => t(lang, Key::ErrEnrollFirst), "smtp_test" => t(lang, Key::ErrSmtpTest), "password_wrong" => t(lang, Key::ErrPasswordWrong), "password_same" => t(lang, Key::ErrPasswordSame), @@ -347,26 +346,24 @@ async fn invite(cx: &Cx) -> Result { } /// TOTP enrolment: the QR, the manual key, and the first code that proves -/// the authenticator agrees. Reached with a pending-enroll cookie, set when -/// the account was created. +/// the authenticator agrees. Reached from the landing's activation link by a +/// signed-in user without confirmed two-factor. #[route(GET "/enroll")] async fn enroll(cx: &Cx) -> Result { - let Some(pending) = server::opened_pending(cx) else { + let Some(user) = server::current_user(cx).await else { return see_other("/login").into_response(cx); }; - if pending.purpose != PendingPurpose::Enroll { - return see_other("/login").into_response(cx); + if user.totp_confirmed { + return see_other("/").into_response(cx); } - let user = im_core::accounts::user_by_id( - &server::app(cx).store, - &im_core::model::UserId::from(pending.user.clone()), - ) - .await? - .ok_or_else(|| topcoat::Error::from(std::io::Error::other("pending user is gone")))?; - let Some((secret, _confirmed)) = - im_core::totp::totp_secret(&server::app(cx).store, &user.id).await? - else { - return see_other("/login?error=enroll_first").into_response(cx); + let store = &server::app(cx).store; + let secret = match im_core::totp::totp_secret(store, &user.id).await? { + Some((secret, _confirmed)) => secret, + None => { + let secret = im_core::totp::generate_secret(); + im_core::totp::set_totp(store, &user.id, &secret).await?; + secret + } }; let issuer = &server::app(cx).config.issuer; @@ -415,7 +412,7 @@ async fn enroll(cx: &Cx) -> Result { > @@ -423,7 +420,7 @@ async fn enroll(cx: &Cx) -> Result { }; - shell(cx, t(lang, Key::TitleEnroll), None, stage) + shell(cx, t(lang, Key::TitleEnroll), Some(&user), stage) .await? .into_response(cx) } @@ -771,6 +768,9 @@ async fn signed_in(cx: &Cx, user: &im_core::model::User) -> Result { (t(lang, Key::PublicProfileLink)) + if !user.totp_confirmed { + (t(lang, Key::Activate2fa)) + } if user.admin { (t(lang, Key::AdminPanelLink)) } diff --git a/crates/im-web/src/server.rs b/crates/im-web/src/server.rs index a7c5979..241257e 100644 --- a/crates/im-web/src/server.rs +++ b/crates/im-web/src/server.rs @@ -119,7 +119,7 @@ pub async fn current_user(cx: &Cx) -> Option { } // --------------------------------------------------------------------------- -// The pending marker: between password and TOTP, between invite and enrolment +// The pending marker: between password and TOTP // --------------------------------------------------------------------------- #[derive(Debug, serde::Serialize, serde::Deserialize, PartialEq)] @@ -127,8 +127,6 @@ pub async fn current_user(cx: &Cx) -> Option { pub enum PendingPurpose { /// Password verified, TOTP code still owed. Login, - /// Account created, TOTP enrolment still owed. - Enroll, } #[derive(Debug, serde::Serialize, serde::Deserialize)]