Skip to content

Commit

Permalink
Automatically enable WebAuthn for users with U2F
Browse files Browse the repository at this point in the history
  • Loading branch information
sjinks committed Jan 23, 2022
1 parent 3ef3be1 commit 1af74fa
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions inc/class-webauthn-provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use MadWizard\WebAuthn\Server\Authentication\AuthenticationOptions;
use Throwable;
use Two_Factor_Provider;
use TwoFactor_Provider_WebAuthn;
use UnexpectedValueException;
use WP_User;

Expand All @@ -34,6 +35,8 @@ final protected function __construct() {
parent::__construct();

add_filter( 'load_script_translation_file', [ $this, 'load_script_translation_file' ], 10, 3 );
add_filter( 'two_factor_enabled_providers_for_user', [ $this, 'two_factor_enabled_providers_for_user' ] );
add_filter( 'two_factor_primary_provider_for_user', [ $this, 'two_factor_primary_provider_for_user' ] );
}

/**
Expand Down Expand Up @@ -173,4 +176,34 @@ private function apply_u2f_hack( WebAuthn_Credential_Store $repo, array $credent

return $credential;
}

/**
* Filter the enabled two-factor authentication providers for this user.
*
* @psalm-param class-string[] $enabled_providers
* @psalm-return class-string[]
*/
public function two_factor_enabled_providers_for_user( array $enabled_providers ): array {
if ( in_array( \Two_Factor_FIDO_U2F::class, $enabled_providers, true ) ) {
$enabled_providers[] = TwoFactor_Provider_WebAuthn::class;
}

return $enabled_providers;
}

/**
* Filter the two-factor authentication provider used for this user.
*
* @param string $provider
* @psalm-param class-string $provider
* @return string
* @psalm-return class-string
*/
public function two_factor_primary_provider_for_user( $provider ) {
if ( \Two_Factor_FIDO_U2F::class === $provider ) {
$provider = TwoFactor_Provider_WebAuthn::class;
}

return $provider;
}
}

0 comments on commit 1af74fa

Please sign in to comment.