PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 3.1.0
Defender Security – Malware Scanner, Login Security & Firewall v3.1.0
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / src / component / webauthn.php

webauthn.php in Defender Security – Malware Scanner, Login Security & Firewall 3.1.0, at src/component/webauthn.php

128 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 return $this->findAllForUserEntityByType( $publicKeyCredentialUserEntity );
69 }
70
71 /**
72 * Get all credentials of a user by authenticator type.
73 *
74 * @param PublicKeyCredentialUserEntity $publicKeyCredentialUserEntity
75 * @param null|string $type
76 *
77 * @return array
78 * @since 3.1.0
79 */
80 public function findAllForUserEntityByType( PublicKeyCredentialUserEntity $publicKeyCredentialUserEntity, $type = null ): array {
81 $sources = [];
82 $user_data = $this->getCredentials();
83
84 if ( is_array( $user_data ) ) {
85 foreach ( $user_data as $data ) {
86 if ( ! empty( $type ) && ! empty( $data['authenticator_type'] ) && $type !== $data['authenticator_type'] ) {
87 continue;
88 }
89
90 if ( isset( $data['credential_source'] ) ) {
91 $source = PublicKeyCredentialSource::createFromArray( $data['credential_source'] );
92 if ( $source->getUserHandle() === $publicKeyCredentialUserEntity->getId() ) {
93 $sources[] = $source;
94 }
95 }
96 }
97 }
98
99 return $sources;
100 }
101
102 /**
103 * Store credential into database.
104 *
105 * @param PublicKeyCredentialSource $publicKeyCredentialSource
106 *
107 * @return void
108 */
109 public function saveCredentialSource( PublicKeyCredentialSource $publicKeyCredentialSource ): void {
110 $data = $this->getCredentials();
111 $key = base64_encode( $publicKeyCredentialSource->getPublicKeyCredentialId() );
112
113 if ( ! isset( $data[ $key ] ) ) {
114 $data[ $key ] = array(
115 'label' => ! empty( $_POST['name'] ) ? sanitize_text_field( $_POST['name'] ) : '',
116 'added' => time(),
117 'authenticator_type' => ! empty( $_POST['type'] ) ? sanitize_text_field( $_POST['type'] ) : '',
118 'user' => $publicKeyCredentialSource->getUserHandle(),
119 'credential_source' => $publicKeyCredentialSource,
120 );
121 } else {
122 $data[ $key ]['credential_source'] = $publicKeyCredentialSource;
123 }
124
125 $this->setCredentials( $data );
126 }
127 }
128