Two-factor enrolment turns optional — invites and password logins land signed-in on the landing, which offers an Activate two-factor link beside the muted 2FA-off chip; /enroll now serves the signed-in, generating the secret on first visit, and the pending-enroll marker is gone
Browse files at this commit22061eb69124a4b9260a61fe922a4c72ef44a69bParent: 77568714 changed files +41 -62
- crates/im-web/src/auth.rs +16 -35
- crates/im-web/src/i18n.rs +6 -6
- crates/im-web/src/pages.rs +18 -18
- crates/im-web/src/server.rs +1 -3
crates/im-web/src/auth.rsmodified+16 -35
| @@ -101,17 +101,12 @@ async fn login(cx: &Cx, Form(input): Form<LoginForm>) -> Redirect { | |||
| 101 | see("/login/totp".to_string()) | 101 | see("/login/totp".to_string()) |
| 102 | } | 102 | } |
| 103 | Ok(user) => { | 103 | Ok(user) => { |
| 104 | - // An account without confirmed 2FA — everyone who migrated from | ||
| 105 | - // İzlek — enrols before any session exists: same bar as an | ||
| 106 | - // invited account. | ||
| 107 | - if im_core::totp::totp_secret(store, &user.id).await?.is_none() { | ||
| 108 | - let secret = im_core::totp::generate_secret(); | ||
| 109 | - im_core::totp::set_totp(store, &user.id, &secret).await?; | ||
| 110 | - } | ||
| 111 | - let sealed = server::mint_pending(cx, &user.id, PendingPurpose::Enroll, back).await; | ||
| 112 | - server::set_pending_cookie(cx, sealed).await; | ||
| 113 | let _ = accounts::clear_login_failures(store, &key).await; | 104 | let _ = accounts::clear_login_failures(store, &key).await; |
| 114 | - see("/enroll".to_string()) | 105 | + let token = |
| 106 | + im_core::sessions::create_session(store, &user.id, &session_meta(cx)).await?; | ||
| 107 | + server::set_session_cookie(cx, token.expose()); | ||
| 108 | + server::log_event(cx, "login_ok", Some(&user.email), None).await; | ||
| 109 | + see(back) | ||
| 115 | } | 110 | } |
| 116 | Err(_) => { | 111 | Err(_) => { |
| 117 | // The failure is logged against the address tried, never the | 112 | // The failure is logged against the address tried, never the |
| @@ -220,47 +215,33 @@ async fn invite(cx: &Cx, Form(input): Form<InviteForm>) -> Redirect { | |||
| 220 | return see(format!("/invite/{}?error={code}", input.token)); | 215 | return see(format!("/invite/{}?error={code}", input.token)); |
| 221 | } | 216 | } |
| 222 | }; | 217 | }; |
| 223 | - // TOTP enrolment is not optional: the account exists from here, and the | 218 | + let token = im_core::sessions::create_session(store, &user.id, &session_meta(cx)).await?; |
| 224 | - // enrolment page is the only way forward. | ||
| 225 | - let secret = im_core::totp::generate_secret(); | ||
| 226 | - im_core::totp::set_totp(store, &user.id, &secret).await?; | ||
| 227 | - let sealed = server::mint_pending(cx, &user.id, PendingPurpose::Enroll, "/".to_string()).await; | ||
| 228 | - server::set_pending_cookie(cx, sealed).await; | ||
| 229 | server::log_event(cx, "invite_accepted", Some(&user.email), None).await; | 219 | server::log_event(cx, "invite_accepted", Some(&user.email), None).await; |
| 230 | - see("/enroll".to_string()) | 220 | + server::set_session_cookie(cx, token.expose()); |
| 221 | + see("/".to_string()) | ||
| 231 | } | 222 | } |
| 232 | 223 | ||
| 233 | #[route(POST "/enroll")] | 224 | #[route(POST "/enroll")] |
| 234 | async fn enroll(cx: &Cx, Form(input): Form<TotpForm>) -> Redirect { | 225 | async fn enroll(cx: &Cx, Form(input): Form<TotpForm>) -> Redirect { |
| 235 | - let Some(pending) = server::opened_pending(cx) else { | 226 | + let Some(user) = server::current_user(cx).await else { |
| 236 | return see("/login".to_string()); | 227 | return see("/login".to_string()); |
| 237 | }; | 228 | }; |
| 238 | - if pending.purpose != PendingPurpose::Enroll { | 229 | + if user.totp_confirmed { |
| 239 | - return see("/login".to_string()); | 230 | + return see("/".to_string()); |
| 240 | } | 231 | } |
| 241 | let store = &server::app(cx).store; | 232 | let store = &server::app(cx).store; |
| 242 | - let user_id = UserId::from(pending.user); | 233 | + let Some((secret, _)) = im_core::totp::totp_secret(store, &user.id).await? else { |
| 243 | - let Some((secret, _)) = im_core::totp::totp_secret(store, &user_id).await? else { | 234 | + return see("/enroll".to_string()); |
| 244 | - return see("/login?error=enroll_first".to_string()); | ||
| 245 | }; | 235 | }; |
| 246 | if !im_core::totp::verify_totp(&secret, input.code.trim(), time::OffsetDateTime::now_utc()) { | 236 | if !im_core::totp::verify_totp(&secret, input.code.trim(), time::OffsetDateTime::now_utc()) { |
| 247 | return see("/enroll?error=bad_code".to_string()); | 237 | return see("/enroll?error=bad_code".to_string()); |
| 248 | } | 238 | } |
| 249 | - im_core::totp::confirm_totp(store, &user_id).await?; | 239 | + im_core::totp::confirm_totp(store, &user.id).await?; |
| 250 | - let token = im_core::sessions::create_session(store, &user_id, &session_meta(cx)).await?; | 240 | + let email = accounts::user_by_id(store, &user.id) |
| 251 | - let email = accounts::user_by_id(store, &user_id) | ||
| 252 | .await? | 241 | .await? |
| 253 | .map(|u| u.email); | 242 | .map(|u| u.email); |
| 254 | server::log_event(cx, "enrolled", email.as_deref(), None).await; | 243 | server::log_event(cx, "enrolled", email.as_deref(), None).await; |
| 255 | - server::clear_pending_cookie(cx); | 244 | + see("/?ok=enrolled".to_string()) |
| 256 | - server::set_session_cookie(cx, token.expose()); | ||
| 257 | - // A migrated user who was mid-`/authorize` continues to their app; a | ||
| 258 | - // fresh invite lands on the welcome banner. | ||
| 259 | - if pending.back == "/" { | ||
| 260 | - see("/?ok=enrolled".to_string()) | ||
| 261 | - } else { | ||
| 262 | - see(pending.back) | ||
| 263 | - } | ||
| 264 | } | 245 | } |
| 265 | 246 | ||
| 266 | /// "Sign out everywhere": the central session dies, and with it every | 247 | /// "Sign out everywhere": the central session dies, and with it every |
crates/im-web/src/i18n.rsmodified+6 -6
| @@ -53,7 +53,6 @@ pub enum Key { | |||
| 53 | ErrPasswordTooShort, | 53 | ErrPasswordTooShort, |
| 54 | ErrPasswordPersonal, | 54 | ErrPasswordPersonal, |
| 55 | ErrPasswordsDiffer, | 55 | ErrPasswordsDiffer, |
| 56 | - ErrEnrollFirst, | ||
| 57 | ErrSmtpTest, | 56 | ErrSmtpTest, |
| 58 | ErrPasswordWrong, | 57 | ErrPasswordWrong, |
| 59 | ErrPasswordSame, | 58 | ErrPasswordSame, |
| @@ -98,7 +97,7 @@ pub enum Key { | |||
| 98 | BackToSignIn, | 97 | BackToSignIn, |
| 99 | EnrollTitle, | 98 | EnrollTitle, |
| 100 | EnrollSub, | 99 | EnrollSub, |
| 101 | - ConfirmAndSignIn, | 100 | + ConfirmEnroll, |
| 102 | ForgotTitle, | 101 | ForgotTitle, |
| 103 | ForgotSub, | 102 | ForgotSub, |
| 104 | ForgotSentNote, | 103 | ForgotSentNote, |
| @@ -118,6 +117,7 @@ pub enum Key { | |||
| 118 | // Signed-in landing and person page, shared. | 117 | // Signed-in landing and person page, shared. |
| 119 | TwoFaOn, | 118 | TwoFaOn, |
| 120 | TwoFaOff, | 119 | TwoFaOff, |
| 120 | + Activate2fa, | ||
| 121 | AdminChip, | 121 | AdminChip, |
| 122 | MemberSinceLabel, | 122 | MemberSinceLabel, |
| 123 | PublicProfileLink, | 123 | PublicProfileLink, |
| @@ -304,8 +304,6 @@ pub fn t(lang: Lang, key: Key) -> &'static str { | |||
| 304 | (ErrPasswordPersonal, Tr) => "Parola adresini ya da adını içeremez.", | 304 | (ErrPasswordPersonal, Tr) => "Parola adresini ya da adını içeremez.", |
| 305 | (ErrPasswordsDiffer, En) => "The two passwords don't match.", | 305 | (ErrPasswordsDiffer, En) => "The two passwords don't match.", |
| 306 | (ErrPasswordsDiffer, Tr) => "İki parola eşleşmiyor.", | 306 | (ErrPasswordsDiffer, Tr) => "İki parola eşleşmiyor.", |
| 307 | - (ErrEnrollFirst, En) => "Set up your second factor first.", | ||
| 308 | - (ErrEnrollFirst, Tr) => "Önce ikinci faktörünü kur.", | ||
| 309 | (ErrSmtpTest, En) => "The test mail could not be sent.", | 307 | (ErrSmtpTest, En) => "The test mail could not be sent.", |
| 310 | (ErrSmtpTest, Tr) => "Test postası gönderilemedi.", | 308 | (ErrSmtpTest, Tr) => "Test postası gönderilemedi.", |
| 311 | (ErrPasswordWrong, En) => "That's not your current password.", | 309 | (ErrPasswordWrong, En) => "That's not your current password.", |
| @@ -390,8 +388,8 @@ pub fn t(lang: Lang, key: Key) -> &'static str { | |||
| 390 | (EnrollTitle, Tr) => "İki faktörlüyü kur", | 388 | (EnrollTitle, Tr) => "İki faktörlüyü kur", |
| 391 | (EnrollSub, En) => "Scan with your authenticator, then type the 6-digit code it shows.", | 389 | (EnrollSub, En) => "Scan with your authenticator, then type the 6-digit code it shows.", |
| 392 | (EnrollSub, Tr) => "Doğrulama uygulamanla tara, sonra gösterdiği 6 haneli kodu yaz.", | 390 | (EnrollSub, Tr) => "Doğrulama uygulamanla tara, sonra gösterdiği 6 haneli kodu yaz.", |
| 393 | - (ConfirmAndSignIn, En) => "Confirm and sign in", | 391 | + (ConfirmEnroll, En) => "Enable two-factor", |
| 394 | - (ConfirmAndSignIn, Tr) => "Onayla ve giriş yap", | 392 | + (ConfirmEnroll, Tr) => "İki faktörlüyü aç", |
| 395 | (ForgotTitle, En) => "Forgot it?", | 393 | (ForgotTitle, En) => "Forgot it?", |
| 396 | (ForgotTitle, Tr) => "Parolamı unuttum", | 394 | (ForgotTitle, Tr) => "Parolamı unuttum", |
| 397 | (ForgotSub, En) => { | 395 | (ForgotSub, En) => { |
| @@ -431,6 +429,8 @@ pub fn t(lang: Lang, key: Key) -> &'static str { | |||
| 431 | (TwoFaOn, Tr) => "2FA açık", | 429 | (TwoFaOn, Tr) => "2FA açık", |
| 432 | (TwoFaOff, En) => "2FA off", | 430 | (TwoFaOff, En) => "2FA off", |
| 433 | (TwoFaOff, Tr) => "2FA kapalı", | 431 | (TwoFaOff, Tr) => "2FA kapalı", |
| 432 | + (Activate2fa, En) => "Activate two-factor", | ||
| 433 | + (Activate2fa, Tr) => "İki faktörlüyü etkinleştir", | ||
| 434 | (AdminChip, En) => "Admin", | 434 | (AdminChip, En) => "Admin", |
| 435 | (AdminChip, Tr) => "Yönetici", | 435 | (AdminChip, Tr) => "Yönetici", |
| 436 | (MemberSinceLabel, En) => "Member since", | 436 | (MemberSinceLabel, En) => "Member since", |
crates/im-web/src/pages.rsmodified+18 -18
| @@ -30,7 +30,6 @@ pub fn error_text(code: &str, lang: Lang) -> &'static str { | |||
| 30 | "password_too_short" => t(lang, Key::ErrPasswordTooShort), | 30 | "password_too_short" => t(lang, Key::ErrPasswordTooShort), |
| 31 | "password_personal" => t(lang, Key::ErrPasswordPersonal), | 31 | "password_personal" => t(lang, Key::ErrPasswordPersonal), |
| 32 | "passwords_differ" => t(lang, Key::ErrPasswordsDiffer), | 32 | "passwords_differ" => t(lang, Key::ErrPasswordsDiffer), |
| 33 | - "enroll_first" => t(lang, Key::ErrEnrollFirst), | ||
| 34 | "smtp_test" => t(lang, Key::ErrSmtpTest), | 33 | "smtp_test" => t(lang, Key::ErrSmtpTest), |
| 35 | "password_wrong" => t(lang, Key::ErrPasswordWrong), | 34 | "password_wrong" => t(lang, Key::ErrPasswordWrong), |
| 36 | "password_same" => t(lang, Key::ErrPasswordSame), | 35 | "password_same" => t(lang, Key::ErrPasswordSame), |
| @@ -347,26 +346,24 @@ async fn invite(cx: &Cx) -> Result<Response> { | |||
| 347 | } | 346 | } |
| 348 | 347 | ||
| 349 | /// TOTP enrolment: the QR, the manual key, and the first code that proves | 348 | /// TOTP enrolment: the QR, the manual key, and the first code that proves |
| 350 | -/// the authenticator agrees. Reached with a pending-enroll cookie, set when | 349 | +/// the authenticator agrees. Reached from the landing's activation link by a |
| 351 | -/// the account was created. | 350 | +/// signed-in user without confirmed two-factor. |
| 352 | #[route(GET "/enroll")] | 351 | #[route(GET "/enroll")] |
| 353 | async fn enroll(cx: &Cx) -> Result<Response> { | 352 | async fn enroll(cx: &Cx) -> Result<Response> { |
| 354 | - let Some(pending) = server::opened_pending(cx) else { | 353 | + let Some(user) = server::current_user(cx).await else { |
| 355 | return see_other("/login").into_response(cx); | 354 | return see_other("/login").into_response(cx); |
| 356 | }; | 355 | }; |
| 357 | - if pending.purpose != PendingPurpose::Enroll { | 356 | + if user.totp_confirmed { |
| 358 | - return see_other("/login").into_response(cx); | 357 | + return see_other("/").into_response(cx); |
| 359 | } | 358 | } |
| 360 | - let user = im_core::accounts::user_by_id( | 359 | + let store = &server::app(cx).store; |
| 361 | - &server::app(cx).store, | 360 | + let secret = match im_core::totp::totp_secret(store, &user.id).await? { |
| 362 | - &im_core::model::UserId::from(pending.user.clone()), | 361 | + Some((secret, _confirmed)) => secret, |
| 363 | - ) | 362 | + None => { |
| 364 | - .await? | 363 | + let secret = im_core::totp::generate_secret(); |
| 365 | - .ok_or_else(|| topcoat::Error::from(std::io::Error::other("pending user is gone")))?; | 364 | + im_core::totp::set_totp(store, &user.id, &secret).await?; |
| 366 | - let Some((secret, _confirmed)) = | 365 | + secret |
| 367 | - im_core::totp::totp_secret(&server::app(cx).store, &user.id).await? | 366 | + } |
| 368 | - else { | ||
| 369 | - return see_other("/login?error=enroll_first").into_response(cx); | ||
| 370 | }; | 367 | }; |
| 371 | 368 | ||
| 372 | let issuer = &server::app(cx).config.issuer; | 369 | let issuer = &server::app(cx).config.issuer; |
| @@ -415,7 +412,7 @@ async fn enroll(cx: &Cx) -> Result<Response> { | |||
| 415 | > | 412 | > |
| 416 | </label> | 413 | </label> |
| 417 | <button class="auth-submit" type="submit"> | 414 | <button class="auth-submit" type="submit"> |
| 418 | - <span class="auth-submit-text">(t(lang, Key::ConfirmAndSignIn))</span> | 415 | + <span class="auth-submit-text">(t(lang, Key::ConfirmEnroll))</span> |
| 419 | </button> | 416 | </button> |
| 420 | </form> | 417 | </form> |
| 421 | </div> | 418 | </div> |
| @@ -423,7 +420,7 @@ async fn enroll(cx: &Cx) -> Result<Response> { | |||
| 423 | </div> | 420 | </div> |
| 424 | </main> | 421 | </main> |
| 425 | }; | 422 | }; |
| 426 | - shell(cx, t(lang, Key::TitleEnroll), None, stage) | 423 | + shell(cx, t(lang, Key::TitleEnroll), Some(&user), stage) |
| 427 | .await? | 424 | .await? |
| 428 | .into_response(cx) | 425 | .into_response(cx) |
| 429 | } | 426 | } |
| @@ -771,6 +768,9 @@ async fn signed_in(cx: &Cx, user: &im_core::model::User) -> Result { | |||
| 771 | </div> | 768 | </div> |
| 772 | </dl> | 769 | </dl> |
| 773 | <a class="auth-alt" href=(format!("/people/{}", user.id))>(t(lang, Key::PublicProfileLink))</a> | 770 | <a class="auth-alt" href=(format!("/people/{}", user.id))>(t(lang, Key::PublicProfileLink))</a> |
| 771 | + if !user.totp_confirmed { | ||
| 772 | + <a class="auth-alt" href="/enroll">(t(lang, Key::Activate2fa))</a> | ||
| 773 | + } | ||
| 774 | if user.admin { | 774 | if user.admin { |
| 775 | <a class="auth-alt" href="/admin">(t(lang, Key::AdminPanelLink))</a> | 775 | <a class="auth-alt" href="/admin">(t(lang, Key::AdminPanelLink))</a> |
| 776 | } | 776 | } |
crates/im-web/src/server.rsmodified+1 -3
| @@ -119,7 +119,7 @@ pub async fn current_user(cx: &Cx) -> Option<User> { | |||
| 119 | } | 119 | } |
| 120 | 120 | ||
| 121 | // --------------------------------------------------------------------------- | 121 | // --------------------------------------------------------------------------- |
| 122 | -// The pending marker: between password and TOTP, between invite and enrolment | 122 | +// The pending marker: between password and TOTP |
| 123 | // --------------------------------------------------------------------------- | 123 | // --------------------------------------------------------------------------- |
| 124 | 124 | ||
| 125 | #[derive(Debug, serde::Serialize, serde::Deserialize, PartialEq)] | 125 | #[derive(Debug, serde::Serialize, serde::Deserialize, PartialEq)] |
| @@ -127,8 +127,6 @@ pub async fn current_user(cx: &Cx) -> Option<User> { | |||
| 127 | pub enum PendingPurpose { | 127 | pub enum PendingPurpose { |
| 128 | /// Password verified, TOTP code still owed. | 128 | /// Password verified, TOTP code still owed. |
| 129 | Login, | 129 | Login, |
| 130 | - /// Account created, TOTP enrolment still owed. | ||
| 131 | - Enroll, | ||
| 132 | } | 130 | } |
| 133 | 131 | ||
| 134 | #[derive(Debug, serde::Serialize, serde::Deserialize)] | 132 | #[derive(Debug, serde::Serialize, serde::Deserialize)] |