PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.3.0
Defender Security – Malware Scanner, Login Security & Firewall v6.3.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 / traits / webauthn.php

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

215 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Helper functions for webauthn related tasks.
4 *
5 * @package WP_Defender\Traits
6 */
7
8 namespace WP_Defender\Traits;
9
10 use Throwable;
11
12 trait Webauthn {
13
14 /**
15 * Prefix used for option.
16 *
17 * @var string
18 */
19 public $option_prefix = 'wpdef_webauthn_';
20
21 /**
22 * Check if SSL is used.
23 *
24 * @return bool
25 */
26 public function is_ssl(): bool {
27 $server_data = defender_get_data_from_request( null, 's' );
28 if (
29 ( isset( $server_data['HTTPS'] ) && ( 'on' === strtolower( $server_data['HTTPS'] ) || '1' === $server_data['HTTPS'] ) ) ||
30 ( isset( $server_data['REQUEST_SCHEME'] ) && 'https' === $server_data['REQUEST_SCHEME'] ) ||
31 ( isset( $server_data['HTTP_X_FORWARDED_PROTO'] ) && 'https' === $server_data['HTTP_X_FORWARDED_PROTO'] ) ||
32 ( isset( $server_data['HTTP_X_FORWARDED_SSL'] ) && 'on' === $server_data['HTTP_X_FORWARDED_SSL'] )
33 ) {
34 return true;
35 }
36
37 return false;
38 }
39
40 /**
41 * Check if 'gmp' extension is enabled.
42 *
43 * @return bool
44 */
45 public function is_enabled_gmp(): bool {
46 return extension_loaded( 'gmp' );
47 }
48
49 /**
50 * Check if 'mbstring' extension is enabled.
51 *
52 * @return bool
53 */
54 public function is_enabled_mbstring(): bool {
55 return extension_loaded( 'mbstring' );
56 }
57
58 /**
59 * Check if 'sodium' extension is enabled.
60 *
61 * @return bool
62 */
63 public function is_enabled_sodium(): bool {
64 return extension_loaded( 'sodium' );
65 }
66
67 /**
68 * Check if server requirements are met.
69 *
70 * @return bool
71 */
72 public function check_webauthn_requirements(): bool {
73 return $this->is_ssl() &&
74 $this->is_enabled_gmp() &&
75 $this->is_enabled_mbstring() &&
76 $this->is_enabled_sodium();
77 }
78
79 /**
80 * Sets the value of a transient with a specific name, client ID, and expiration time.
81 *
82 * @param string $name The name of the transient.
83 * @param mixed $value The value to be serialized and stored in the transient.
84 * @param string $client_id The client ID associated with the transient.
85 * @param int $exp The expiration time in seconds. Default is 90.
86 *
87 * @return bool Returns true if the transient was successfully set, false otherwise.
88 */
89 public function set_trans_val( string $name, $value, string $client_id, int $exp = 90 ): bool {
90 $trans_name = $this->option_prefix . $name . '_' . $client_id;
91 $trans_val = wp_json_encode( $value );
92
93 return set_transient( $trans_name, $trans_val, $exp );
94 }
95
96 /**
97 * Retrieves the value of a transient with the specified name and client ID.
98 *
99 * @param string $name The name of the transient.
100 * @param string $client_id The client ID associated with the transient.
101 *
102 * @return mixed|false The deserialized value of the transient, or false if the transient does not exist.
103 */
104 public function get_trans_val( string $name, string $client_id ) {
105 $trans_name = $this->option_prefix . $name . '_' . $client_id;
106 $trans_val = get_transient( $trans_name );
107
108 return false !== $trans_val ? json_decode( $trans_val ) : false;
109 }
110
111 /**
112 * Delete transient.
113 *
114 * @param string $name The name of the transient.
115 * @param string $client_id The client ID associated with the transient.
116 *
117 * @return bool
118 */
119 public function delete_trans( string $name, string $client_id ): bool {
120 $trans_name = $this->option_prefix . $name . '_' . $client_id;
121
122 return delete_transient( $trans_name );
123 }
124
125 /**
126 * Update user meta.
127 *
128 * @param int $user_id User ID.
129 * @param string $name Metadata key.
130 * @param mixed $value Metadata value. Must be serializable if non-scalar.
131 *
132 * @return int|bool
133 */
134 public function update_user_meta( int $user_id, string $name, $value ) {
135 return update_user_meta( $user_id, $this->option_prefix . $name, addslashes( wp_json_encode( $value ) ) );
136 }
137
138 /**
139 * Get user meta.
140 *
141 * @param int $user_id User ID.
142 * @param string $name Metadata key.
143 *
144 * @return array
145 */
146 public function get_user_meta( int $user_id, string $name ): array {
147 $value = get_user_meta( $user_id, $this->option_prefix . $name, true );
148
149 if ( null === $value ) {
150 return array();
151 }
152
153 $data = json_decode( $value, true );
154
155 return is_array( $data ) ? $data : array();
156 }
157
158 /**
159 * Get website name.
160 *
161 * @return string
162 */
163 public function get_site_name(): string {
164 return get_bloginfo( 'name' );
165 }
166
167 /**
168 * Get website domain.
169 *
170 * @return string
171 */
172 public function get_site_domain(): string {
173 $site_url = get_bloginfo( 'url' );
174 $site_domain = preg_replace( '#^http(s)?:\/\/#', '', $site_url );
175 $site_domain = preg_replace( '#^www\.#', '', $site_domain );
176 $site_domain = explode( '/', $site_domain );
177
178 return $site_domain[0];
179 }
180
181 /**
182 * Get the hash of a user's username using SHA-256 algorithm.
183 *
184 * @param string $username The username of the user.
185 *
186 * @return string The hashed username.
187 */
188 public function get_user_hash( string $username ): string {
189 return hash( 'sha256', $username );
190 }
191
192 /**
193 * URL safe base64 encoding.
194 *
195 * @param string $data The data to encode.
196 *
197 * @return string
198 */
199 public function base64url_encode( string $data ): string {
200 return rtrim( strtr( base64_encode( $data ), '+/', '-_' ), '=' ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
201 }
202
203 /**
204 * Base64 decoding from URL safe base64 encoding.
205 *
206 * @param string $data The data to decode.
207 *
208 * @return string|bool
209 */
210 public function base64url_decode( string $data ) {
211 // No need to add '=' at the end.
212 return base64_decode( strtr( $data, '-_', '+/' ), true ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode
213 }
214 }
215