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

Enable TOTP method when method is configured #643

Merged
merged 15 commits into from
Jan 9, 2025

Conversation

kasparsd
Copy link
Collaborator

@kasparsd kasparsd commented Sep 19, 2024

What?

Users expect a method to be enabled if they chose to configure it.

Why?

Fixes #556.

How?

Testing Instructions

Setup workflow:

  1. Configure the TOTP method by submitting the generated code.
  2. Confirm the TOTP method is now enabled in the table.

Reset workflow:

  1. Click on RESET TOTP method button.
  2. Confirm the TOTP method is now disabled in the table.

Screenshots or screencast

totp-actions.mp4

Changelog Entry

Added - New feature.
Changed - Existing functionality.
Deprecated - Soon-to-be removed feature.
Removed - Feature.
Fixed - Bug fix.
Security - Vulnerability.

@kasparsd kasparsd marked this pull request as ready for review September 19, 2024 12:22
@jeffpaul jeffpaul added this to the 0.10.0 milestone Sep 19, 2024
@@ -818,52 +818,52 @@ public function test_show_password_reset_error() {
public function test_enable_disable_provider_for_user() {
$user = self::factory()->user->create_and_get();
$enabled_providers = Two_Factor_Core::get_enabled_providers_for_user( $user->ID );
$this->assertEmpty( $enabled_providers );
$this->assertEmpty( $enabled_providers, 'No providers are enabled by default' );
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

These help us identify failing tests instead of just showing the test method name.

@@ -151,6 +151,10 @@ public function rest_delete_totp( $request ) {

$this->delete_user_totp_key( $user_id );

if ( ! Two_Factor_Core::disable_provider_for_user( $user_id, 'Two_Factor_Totp' ) ) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Deleting the TOTP key now also disables the method because it is unusable without the key.

if ( in_array( $new_provider, $enabled_providers ) ) {
return true;
}

$enabled_providers[] = $new_provider;
$enabled = update_user_meta( $user_id, self::ENABLED_PROVIDERS_USER_META_KEY, $enabled_providers );

// Primary provider must be enabled.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The primary provider is determined during get_primary_provider_for_user() so there is no need update it here. If no primary is configured, we always default to the first enabled.

class-two-factor-core.php Show resolved Hide resolved
@@ -386,8 +391,10 @@ public function user_two_factor_options( $user ) {

$error.find('p').text( errorMessage );

$( '#enabled-Two_Factor_Totp' ).prop( 'checked', false );
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We now enable the TOTP method when the key has been registered by default.

@@ -375,6 +379,7 @@ public function user_two_factor_options( $user ) {
user_id: <?php echo wp_json_encode( $user->ID ); ?>,
key: key,
code: code,
enable_provider: true,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Instruct the validator to also enable the provider, if everything is a pass.

@kasparsd kasparsd merged commit 4c733e4 into master Jan 9, 2025
48 checks passed
@kasparsd kasparsd deleted the 556-enable-totp-on-submit branch January 9, 2025 11:36
@kasparsd kasparsd mentioned this pull request Jan 9, 2025
@jeffpaul jeffpaul modified the milestones: 0.12.0, 0.11.0 Jan 9, 2025
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.

TOTP: After successful set up, JS should check the "Enabled" checkbox for the TOTP provider
2 participants