Display form to enter OTP code
This commit is contained in:
@ -11,7 +11,7 @@ use crate::controllers::base_controller::{FatalErrorPage, redirect_user, redirec
|
||||
use crate::data::login_redirect_query::LoginRedirectQuery;
|
||||
use crate::data::remote_ip::RemoteIP;
|
||||
use crate::data::session_identity::{SessionIdentity, SessionStatus};
|
||||
use crate::data::user::{TwoFactor, User};
|
||||
use crate::data::user::{FactorID, TwoFactor, User};
|
||||
|
||||
struct BaseLoginPage {
|
||||
danger: Option<String>,
|
||||
@ -42,6 +42,13 @@ struct ChooseSecondFactorTemplate<'a> {
|
||||
factors: &'a [TwoFactor],
|
||||
}
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "login/opt_input.html")]
|
||||
struct LoginWithOTPTemplate<'a> {
|
||||
_p: BaseLoginPage,
|
||||
factor: &'a TwoFactor,
|
||||
}
|
||||
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
pub struct LoginRequestBody {
|
||||
@ -253,4 +260,40 @@ pub async fn choose_2fa_method(id: Identity, query: web::Query<ChooseSecondFacto
|
||||
.render()
|
||||
.unwrap(),
|
||||
)
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
pub struct LoginWithOTPQuery {
|
||||
#[serde(default)]
|
||||
redirect: LoginRedirectQuery,
|
||||
id: FactorID,
|
||||
}
|
||||
|
||||
|
||||
/// Login with OTP
|
||||
pub async fn login_with_otp(id: Identity, query: web::Query<LoginWithOTPQuery>,
|
||||
users: web::Data<Addr<UsersActor>>) -> impl Responder {
|
||||
if !SessionIdentity(&id).need_2fa_auth() {
|
||||
return redirect_user_for_login(query.redirect.get());
|
||||
}
|
||||
|
||||
let user: User = users.send(users_actor::GetUserRequest(SessionIdentity(&id).user_id()))
|
||||
.await.unwrap().0.expect("Could not find user!");
|
||||
|
||||
let factor = match user.find_factor(&query.id) {
|
||||
Some(f) => f,
|
||||
None => return HttpResponse::Ok()
|
||||
.body(FatalErrorPage { message: "Factor not found!" }.render().unwrap())
|
||||
};
|
||||
|
||||
HttpResponse::Ok().body(LoginWithOTPTemplate {
|
||||
_p: BaseLoginPage {
|
||||
danger: None,
|
||||
success: None,
|
||||
page_title: "Two-Factor Auth",
|
||||
app_name: APP_NAME,
|
||||
redirect_uri: query.redirect.get_encoded(),
|
||||
},
|
||||
factor,
|
||||
}.render().unwrap())
|
||||
}
|
@ -29,7 +29,7 @@ impl TwoFactor {
|
||||
|
||||
pub fn login_url(&self, redirect_uri: &str) -> String {
|
||||
match self.kind {
|
||||
TwoFactorType::TOTP(_) => format!("/2fa_totp?id={}&redirect_uri={}",
|
||||
TwoFactorType::TOTP(_) => format!("/2fa_otp?id={}&redirect_uri={}",
|
||||
self.id.0, redirect_uri)
|
||||
}
|
||||
}
|
||||
@ -83,6 +83,10 @@ impl User {
|
||||
pub fn remove_factor(&mut self, factor_id: FactorID) {
|
||||
self.two_factor.retain(|f| f.id != factor_id);
|
||||
}
|
||||
|
||||
pub fn find_factor(&self, factor_id: &FactorID) -> Option<&TwoFactor> {
|
||||
self.two_factor.iter().find(|f| f.id.eq(&factor_id))
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for User {
|
||||
|
@ -112,6 +112,8 @@ async fn main() -> std::io::Result<()> {
|
||||
.route("/reset_password", web::get().to(login_controller::reset_password_route))
|
||||
.route("/reset_password", web::post().to(login_controller::reset_password_route))
|
||||
.route("/2fa_auth", web::get().to(login_controller::choose_2fa_method))
|
||||
.route("/2fa_otp", web::get().to(login_controller::login_with_otp))
|
||||
.route("/2fa_otp", web::post().to(login_controller::login_with_otp))
|
||||
|
||||
// Logout page
|
||||
.route("/logout", web::get().to(login_controller::logout_route))
|
||||
|
Reference in New Issue
Block a user