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

fix: Do not create user handles if they are not needed #480

Merged
merged 1 commit into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions inc/class-webauthn-credential-store.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,18 @@ public static function get_user_keys( WP_User $user ): array {
/** @var wpdb $wpdb */
global $wpdb;

$handle = WebAuthn_User::get_for( $user )->getUserHandle();
$wauser = WebAuthn_User::get_for( $user );
$handle = $wauser->get_user_handle_if_exists();
/** @psalm-var CredentialRow[] $modern */
$modern = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->webauthn_credentials} WHERE user_handle = %s", $handle->toString() ) );
$modern = $handle ? $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->webauthn_credentials} WHERE user_handle = %s", $handle->toString() ) ) : [];
if ( empty( $modern ) ) {
/** @var array */
$legacy = get_user_meta( $user->ID, self::REGISTERED_KEY_LEGACY_META );
if ( ! empty( $legacy ) ) {
if ( ! $handle ) {
$handle = $wauser->generate_and_save_handle();
}

Credential_Migrator::migrate( $user, $handle );
/** @psalm-var CredentialRow[] $modern */
$modern = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->webauthn_credentials} WHERE user_handle = %s", $handle->toString() ) );
Expand Down
57 changes: 37 additions & 20 deletions inc/class-webauthn-user.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use WP_User;
use WildWolf\WordPress\TwoFactorWebAuthn\Vendor\{
MadWizard\WebAuthn\Credential\UserHandle,
MadWizard\WebAuthn\Exception\NotAvailableException,
MadWizard\WebAuthn\Server\UserIdentityInterface,
};
use wpdb;
Expand All @@ -26,11 +25,45 @@ private function __construct( WP_User $user ) {
$this->user = $user;
}

public function getUserHandle(): UserHandle {
$handle = $this->get_user_handle_if_exists();
if ( null === $handle ) {
$handle = $this->generate_and_save_handle();
}

return $handle;
}

/**
* @throws NotAvailableException
* @global wpdb $wpdb
*/
public function getUserHandle(): UserHandle {
public function generate_and_save_handle(): UserHandle {
/** @var wpdb $wpdb */
global $wpdb;

$handle = UserHandle::random()->toString();
$result = $wpdb->insert(
$wpdb->webauthn_users,
[
'user_id' => $this->user->ID,
'user_handle' => $handle,
],
[ '%d', '%s' ]
);

if ( false === $result ) {
throw new UnexpectedValueException( __( 'Unable to save the user handle to the database.', 'two-factor-provider-webauthn' ) );
}

$key = sprintf( 'handle:%u', $this->user->ID );
wp_cache_set( $key, $handle, self::CACHE_GROUP_NAME, 3600 );
return UserHandle::fromString( $handle );
}

/**
* @global wpdb $wpdb
*/
public function get_user_handle_if_exists(): ?UserHandle {
/** @var wpdb $wpdb */
global $wpdb;

Expand All @@ -39,26 +72,10 @@ public function getUserHandle(): UserHandle {
$handle = wp_cache_get( $key, self::CACHE_GROUP_NAME );
if ( false === $handle || ! is_string( $handle ) ) {
$handle = $wpdb->get_var( $wpdb->prepare( "SELECT user_handle FROM {$wpdb->webauthn_users} WHERE user_id = %d", $this->user->ID ) );
if ( ! $handle ) {
$handle = UserHandle::random()->toString();
$result = $wpdb->insert(
$wpdb->webauthn_users,
[
'user_id' => $this->user->ID,
'user_handle' => $handle,
],
[ '%d', '%s' ]
);

if ( false === $result ) {
throw new UnexpectedValueException( __( 'Unable to save the user handle to the database.', 'two-factor-provider-webauthn' ) );
}
}

wp_cache_set( $key, $handle, self::CACHE_GROUP_NAME, 3600 );
}

return UserHandle::fromString( $handle );
return $handle ? UserHandle::fromString( $handle ) : null;
}

public function getUsername(): string {
Expand Down