| 1 |
<?php |
| 2 |
|
| 3 |
namespace WP_Defender\Component; |
| 4 |
|
| 5 |
use Webauthn\PublicKeyCredentialUserEntity; |
| 6 |
use Webauthn\PublicKeyCredentialSourceRepository; |
| 7 |
use Webauthn\PublicKeyCredentialSource; |
| 8 |
use WP_Defender\Traits\Webauthn as Webauthn_Trait; |
| 9 |
|
| 10 |
/** |
| 11 |
* Class Webauthn. |
| 12 |
* |
| 13 |
* @since 3.0.0 |
| 14 |
*/ |
| 15 |
class Webauthn implements PublicKeyCredentialSourceRepository { |
| 16 |
use Webauthn_Trait; |
| 17 |
|
| 18 |
/** |
| 19 |
* Option key for storing user credentials. |
| 20 |
* |
| 21 |
* @type string |
| 22 |
*/ |
| 23 |
public const CREDENTIAL_OPTION_KEY = 'user_credentials'; |
| 24 |
|
| 25 |
/** |
| 26 |
* Get user credentials. |
| 27 |
* |
| 28 |
* @return mixed|null |
| 29 |
*/ |
| 30 |
public function getCredentials() { |
| 31 |
return $this->get_option( self::CREDENTIAL_OPTION_KEY ); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Set user credentials. |
| 36 |
* |
| 37 |
* @param array $data |
| 38 |
* |
| 39 |
* @return bool |
| 40 |
*/ |
| 41 |
public function setCredentials( array $data ): bool { |
| 42 |
return $this->update_option( self::CREDENTIAL_OPTION_KEY, $data ); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Get one credential by credential ID. |
| 47 |
* |
| 48 |
* @param string $publicKeyCredentialId |
| 49 |
* |
| 50 |
* @return PublicKeyCredentialSource|null |
| 51 |
*/ |
| 52 |
public function findOneByCredentialId( string $publicKeyCredentialId ): ?PublicKeyCredentialSource { |
| 53 |
$data = $this->getCredentials(); |
| 54 |
if ( isset( $data[ base64_encode( $publicKeyCredentialId ) ]['credential_source'] ) ) { |
| 55 |
return PublicKeyCredentialSource::createFromArray( $data[ base64_encode( $publicKeyCredentialId ) ]['credential_source'] ); |
| 56 |
} |
| 57 |
return null; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Get all credentials of a user |
| 62 |
* |
| 63 |
* @param PublicKeyCredentialUserEntity $publicKeyCredentialUserEntity |
| 64 |
* |
| 65 |
* @return array |
| 66 |
*/ |
| 67 |
public function findAllForUserEntity( PublicKeyCredentialUserEntity $publicKeyCredentialUserEntity ): array { |
| 68 |
$sources = array(); |
| 69 |
$user_data = $this->getCredentials(); |
| 70 |
foreach ( $user_data as $data ) { |
| 71 |
if ( isset( $data['credential_source'] ) ) { |
| 72 |
$source = PublicKeyCredentialSource::createFromArray( $data['credential_source'] ); |
| 73 |
if ( $source->getUserHandle() === $publicKeyCredentialUserEntity->getId() ) { |
| 74 |
$sources[] = $source; |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
return $sources; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Store credential into database. |
| 83 |
* |
| 84 |
* @param PublicKeyCredentialSource $publicKeyCredentialSource |
| 85 |
* |
| 86 |
* @return void |
| 87 |
*/ |
| 88 |
public function saveCredentialSource( PublicKeyCredentialSource $publicKeyCredentialSource ): void { |
| 89 |
$data = $this->getCredentials(); |
| 90 |
$key = base64_encode( $publicKeyCredentialSource->getPublicKeyCredentialId() ); |
| 91 |
|
| 92 |
if ( ! isset( $data[ $key ] ) ) { |
| 93 |
$data[ $key ] = array( |
| 94 |
'label' => ! empty( $_POST['name'] ) ? sanitize_text_field( $_POST['name'] ) : '', |
| 95 |
'added' => time(), |
| 96 |
'authenticator_type' => ! empty( $_POST['type'] ) ? sanitize_text_field( $_POST['type'] ) : '', |
| 97 |
'user' => $publicKeyCredentialSource->getUserHandle(), |
| 98 |
'credential_source' => $publicKeyCredentialSource, |
| 99 |
); |
| 100 |
} else { |
| 101 |
$data[ $key ]['credential_source'] = $publicKeyCredentialSource; |
| 102 |
} |
| 103 |
|
| 104 |
$this->setCredentials( $data ); |
| 105 |
} |
| 106 |
} |
| 107 |
|