Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework the way 2FA is enabled/disabled (fixes #3309) #3959

Merged
merged 8 commits into from
Sep 20, 2023
Merged

Conversation

Nutomic
Copy link
Member

@Nutomic Nutomic commented Sep 11, 2023

Currently 2FA is enabled in a single step which makes it easy to lock yourself out. This is fixed by using a two-step process, where the secret is generated first, and then 2FA is enabled by passing a valid 2FA token. Also fixes the problem where 2FA can be disabled without passing any 2FA token.

This PR will disable 2fa for all users who currently have it enabled. This allows users who are locked out to get into their account again. Once the js client is updated I will also add an api test to ensure that it works as expected.

#[cfg_attr(feature = "full", derive(TS))]
#[cfg_attr(feature = "full", ts(export))]
pub struct ToggleTotp {
pub totp_totp_token: String,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a comment about how to disable totp also (maybe sending an empty or invalid string)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added enabled param

.map_err(|_| LemmyErrorType::CouldntParseTotpSecret)?;

TOTP::new(
totp_rs::Algorithm::SHA1,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see you got this one, good.

crates/api/src/local_user/toggle_totp.rs Outdated Show resolved Hide resolved
#[cfg_attr(feature = "full", derive(TS))]
#[cfg_attr(feature = "full", ts(export))]
pub struct ToggleTotp {
pub totp_totp_token: String,
Copy link
Member

@dessalines dessalines Sep 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably also have a response, probably a simple success: boolean or enabled: boolean

Because right now the responses for enabling / disabling TOTP are the same.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

crates/api/src/local_user/toggle_totp.rs Outdated Show resolved Hide resolved
crates/utils/src/utils/validation.rs Show resolved Hide resolved
@phiresky
Copy link
Collaborator

I want to mention that this since this two-step process is not relevant for security and purely done for UX purposes, it's possible to do it completely client side without any server code or DB storage needed.

In a different project I generate the secret client side, ask the user to input one one-time token, verifiy it client side, and the activation api just sends the client generated secret to the server.

The advantage of that is simplicity since the server doesn't have to know about the incomplete non-activated secret at all.

Just saying this as as note.

@Nutomic
Copy link
Member Author

Nutomic commented Sep 18, 2023

@phiresky That makes sense. However this way seems easier for clients, as they wont have to pull in any 2fa library and figure out the correct parameters to validate the token.

crates/api/src/local_user/update_totp.rs Outdated Show resolved Hide resolved
crates/api/src/local_user/update_totp.rs Outdated Show resolved Hide resolved
Copy link
Member

@dessalines dessalines left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Nutomic
Copy link
Member Author

Nutomic commented Sep 20, 2023

Ready to merge, api tests are passing locally.

@dessalines dessalines merged commit 22608ae into main Sep 20, 2023
0 of 2 checks passed
@Nutomic Nutomic mentioned this pull request Sep 28, 2023
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants