Generate QrCode to enroll Authenticator App
This commit is contained in:
@ -1,10 +1,13 @@
|
||||
use std::ops::Deref;
|
||||
|
||||
use actix_web::{HttpResponse, Responder};
|
||||
use actix_web::{HttpResponse, Responder, web};
|
||||
use askama::Template;
|
||||
use qrcode_generator::QrCodeEcc;
|
||||
|
||||
use crate::controllers::settings_controller::BaseSettingsPage;
|
||||
use crate::data::app_config::AppConfig;
|
||||
use crate::data::current_user::CurrentUser;
|
||||
use crate::data::totp_key::TotpKey;
|
||||
use crate::data::user::User;
|
||||
|
||||
#[derive(Template)]
|
||||
@ -18,6 +21,9 @@ struct TwoFactorsPage<'a> {
|
||||
#[template(path = "settings/add_2fa_totp_page.html")]
|
||||
struct AddTotpPage {
|
||||
_p: BaseSettingsPage,
|
||||
qr_code: String,
|
||||
account_name: String,
|
||||
secret_key: String,
|
||||
}
|
||||
|
||||
|
||||
@ -36,7 +42,22 @@ pub async fn two_factors_route(user: CurrentUser) -> impl Responder {
|
||||
|
||||
|
||||
/// Configure a new TOTP authentication factor
|
||||
pub async fn add_totp_factor_route(user: CurrentUser) -> impl Responder {
|
||||
pub async fn add_totp_factor_route(user: CurrentUser, app_conf: web::Data<AppConfig>) -> impl Responder {
|
||||
let key = TotpKey::new_random();
|
||||
|
||||
let qr_code = qrcode_generator::to_png_to_vec(
|
||||
key.url_for_user(&user, &app_conf),
|
||||
QrCodeEcc::Low,
|
||||
1024,
|
||||
);
|
||||
let qr_code = match qr_code {
|
||||
Ok(q) => q,
|
||||
Err(e) => {
|
||||
log::error!("Failed to generate QrCode! {:?}", e);
|
||||
return HttpResponse::InternalServerError().body("Failed to generate QrCode!");
|
||||
}
|
||||
};
|
||||
|
||||
HttpResponse::Ok()
|
||||
.body(AddTotpPage {
|
||||
_p: BaseSettingsPage::get(
|
||||
@ -44,5 +65,8 @@ pub async fn add_totp_factor_route(user: CurrentUser) -> impl Responder {
|
||||
&user,
|
||||
None,
|
||||
None),
|
||||
qr_code: base64::encode(qr_code),
|
||||
account_name: key.account_name(&user, &app_conf),
|
||||
secret_key: key.get_secret(),
|
||||
}.render().unwrap())
|
||||
}
|
Reference in New Issue
Block a user