-
-
Notifications
You must be signed in to change notification settings - Fork 889
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework the way 2FA is enabled/disabled (fixes #3309)
- Loading branch information
Showing
18 changed files
with
215 additions
and
100 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use crate::{build_totp_2fa, generate_totp_2fa_secret}; | ||
use activitypub_federation::config::Data; | ||
use actix_web::web::Json; | ||
use lemmy_api_common::{ | ||
context::LemmyContext, | ||
person::GenerateTotpSecretResponse, | ||
sensitive::Sensitive, | ||
}; | ||
use lemmy_db_schema::{ | ||
source::local_user::{LocalUser, LocalUserUpdateForm}, | ||
traits::Crud, | ||
}; | ||
use lemmy_db_views::structs::{LocalUserView, SiteView}; | ||
use lemmy_utils::error::{LemmyError, LemmyErrorType}; | ||
|
||
/// Generate a new secret for two-factor-authentication. Afterwards you need to call [toggle_totp] | ||
/// to enable it. This can only be called if 2FA is currently disabled. | ||
#[tracing::instrument(skip(context))] | ||
pub async fn generate_totp_secret( | ||
local_user_view: LocalUserView, | ||
context: Data<LemmyContext>, | ||
) -> Result<Json<GenerateTotpSecretResponse>, LemmyError> { | ||
let site_view = SiteView::read_local(&mut context.pool()).await?; | ||
|
||
if local_user_view.local_user.totp_2fa_enabled { | ||
return Err(LemmyErrorType::TotpAlreadyEnabled)?; | ||
} | ||
|
||
let secret = generate_totp_2fa_secret(); | ||
let secret_url = | ||
build_totp_2fa(&site_view.site.name, &local_user_view.person.name, &secret)?.get_url(); | ||
|
||
let local_user_form = LocalUserUpdateForm { | ||
totp_2fa_secret: Some(Some(secret)), | ||
..Default::default() | ||
}; | ||
LocalUser::update( | ||
&mut context.pool(), | ||
local_user_view.local_user.id, | ||
&local_user_form, | ||
) | ||
.await?; | ||
|
||
Ok(Json(GenerateTotpSecretResponse { | ||
totp_secret_url: Sensitive::new(secret_url), | ||
})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use crate::check_totp_2fa_valid; | ||
use actix_web::{ | ||
web::{Data, Json}, | ||
HttpResponse, | ||
}; | ||
use lemmy_api_common::{context::LemmyContext, person::ToggleTotp}; | ||
use lemmy_db_schema::{ | ||
source::local_user::{LocalUser, LocalUserUpdateForm}, | ||
traits::Crud, | ||
}; | ||
use lemmy_db_views::structs::{LocalUserView, SiteView}; | ||
use lemmy_utils::error::{LemmyError, LemmyErrorType}; | ||
|
||
/// Enable or disable two-factor-authentication. The current setting is determined from | ||
/// [LocalUser.totp_2fa_enabled]. | ||
/// | ||
/// To enable, you need to first call [generate_totp_secret] and then pass a valid token to this | ||
/// function. | ||
/// | ||
/// Disabling is only possible if 2FA was previously enabled. Again it is necessary to pass a valid | ||
/// token. | ||
#[tracing::instrument(skip(context))] | ||
pub async fn toggle_totp( | ||
data: Json<ToggleTotp>, | ||
local_user_view: LocalUserView, | ||
context: Data<LemmyContext>, | ||
) -> Result<HttpResponse, LemmyError> { | ||
let site_view = SiteView::read_local(&mut context.pool()).await?; | ||
|
||
// require valid 2fa token to enable or disable 2fa | ||
if local_user_view.local_user.totp_2fa_secret.is_none() { | ||
return Err(LemmyErrorType::MissingTotpToken.into()); | ||
} | ||
check_totp_2fa_valid( | ||
&local_user_view.local_user, | ||
&Some(data.totp_totp_token.clone()), | ||
&site_view.site.name, | ||
&local_user_view.person.name, | ||
)?; | ||
|
||
// toggle the 2fa setting | ||
let new_totp_state = !local_user_view.local_user.totp_2fa_enabled; | ||
let mut local_user_form = LocalUserUpdateForm { | ||
totp_2fa_enabled: Some(new_totp_state), | ||
..Default::default() | ||
}; | ||
|
||
// clear totp secret if 2fa is being disabled | ||
if !new_totp_state { | ||
local_user_form.totp_2fa_secret = None; | ||
} | ||
|
||
LocalUser::update( | ||
&mut context.pool(), | ||
local_user_view.local_user.id, | ||
&local_user_form, | ||
) | ||
.await?; | ||
|
||
Ok(HttpResponse::Ok().finish()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.