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

Ensure that doesn't 'fail open' if existing providers poof. #586

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
26 changes: 24 additions & 2 deletions class-two-factor-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,15 +413,37 @@ public static function get_enabled_providers_for_user( $user = null ) {
if ( empty( $enabled_providers ) ) {
$enabled_providers = array();
}
$enabled_providers = array_intersect( $enabled_providers, array_keys( $providers ) );
$enabled_existing_providers = array_intersect( $enabled_providers, array_keys( $providers ) );
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
$enabled_existing_providers = array_intersect( $enabled_providers, array_keys( $providers ) );
$enabled_registered_providers = array_intersect( $enabled_providers, array_keys( $providers ) );

Would this be more clear that we are talking about enabled providers that are registered?


/**
* If the user had enabled providers, but none of them exist currently,
* if emailed codes is available force it to be on, so that deprecated
* or removed providers don't result in the two-factor requirement being
* removed and 'failing open'.
*/
if ( $enabled_providers && empty( $enabled_existing_providers ) ) {
if ( isset( $providers['Two_Factor_Email'] ) ) {
// Force Emailed codes to 'on'.
$enabled_existing_providers[] = 'Two_Factor_Email';
} else {
return new WP_Error(
'no_available_2fa_methods',
__( 'Error: User has Two Factor method(s) enabled, but provider(s) no longer exist,', 'two-factor' ),
array(
'enabled_providers' => $enabled_providers,
'available_providers' => array_keys( $providers ),
)
);
}
}

/**
* Filter the enabled two-factor authentication providers for this user.
*
* @param array $enabled_providers The enabled providers.
* @param int $user_id The user ID.
*/
return apply_filters( 'two_factor_enabled_providers_for_user', $enabled_providers, $user->ID );
return apply_filters( 'two_factor_enabled_providers_for_user', $enabled_existing_providers, $user->ID );
}

/**
Expand Down