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

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

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.

@@ -1902,30 +1901,20 @@ public static function user_two_factor_options( $user ) {
* @return bool True if the provider was enabled, false otherwise.
*/
public static function enable_provider_for_user( $user_id, $new_provider ) {
$available_providers = self::get_providers();
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This method is very heavy as it will instantiate new set of provider classes on every call. That's why we introduced get_provider_classes() earlier which doesn't do any of that.

@@ -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.

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