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 / traits / webauthn.php

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

207 lines 4.3 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\Traits;
4
5 use Throwable;
6
7 trait Webauthn {
8 /**
9 * Prefix used for option.
10 *
11 * @var string
12 */
13 public $option_prefix = 'wpdef_webauthn_';
14
15 /**
16 * Check if SSL is used.
17 *
18 * @return bool
19 */
20 public function is_ssl(): bool {
21 if (
22 ( ! empty( $_SERVER['HTTPS'] ) && ( 'on' === strtolower( $_SERVER['HTTPS'] ) || '1' === $_SERVER['HTTPS'] ) ) ||
23 ( ! empty( $_SERVER['REQUEST_SCHEME'] ) && 'https' === $_SERVER['REQUEST_SCHEME'] ) ||
24 ( ! empty( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) && 'https' === $_SERVER['HTTP_X_FORWARDED_PROTO'] ) ||
25 ( ! empty( $_SERVER['HTTP_X_FORWARDED_SSL'] ) && 'on' === $_SERVER['HTTP_X_FORWARDED_SSL'] )
26 ) {
27 return true;
28 }
29
30 return false;
31 }
32
33 /**
34 * Check if 'gmp' extension is enabled.
35 *
36 * @return bool
37 */
38 public function is_enabled_gmp(): bool {
39 return extension_loaded( 'gmp' );
40 }
41
42 /**
43 * Check if 'mbstring' extension is enabled.
44 *
45 * @return bool
46 */
47 public function is_enabled_mbstring(): bool {
48 return extension_loaded( 'mbstring' );
49 }
50
51 /**
52 * Check if 'sodium' extension is enabled.
53 *
54 * @return bool
55 */
56 public function is_enabled_sodium(): bool {
57 return extension_loaded( 'sodium' );
58 }
59
60 /**
61 * Check if server requirements are met.
62 *
63 * @return bool
64 */
65 public function check_webauthn_requirements(): bool {
66 return $this->is_ssl() &&
67 $this->is_enabled_gmp() &&
68 $this->is_enabled_mbstring() &&
69 $this->is_enabled_sodium();
70 }
71
72 /**
73 * Set transient.
74 *
75 * @param string $name
76 * @param mixed $value
77 * @param string $client_id
78 * @param int $exp
79 *
80 * @return bool
81 */
82 public function set_trans_val( string $name, $value, string $client_id, int $exp = 90 ): bool {
83 $trans_name = $this->option_prefix . $name . '_' . $client_id;
84 $trans_val = serialize( $value );
85
86 return set_transient( $trans_name, $trans_val, $exp );
87 }
88
89 /**
90 * Get transient.
91 *
92 * @param string $name
93 * @param string $client_id
94 *
95 * @return mixed
96 */
97 public function get_trans_val( string $name, string $client_id ) {
98 $trans_name = $this->option_prefix . $name . '_' . $client_id;
99 $trans_val = get_transient( $trans_name );
100
101 return false !== $trans_val ? unserialize( $trans_val ) : false;
102 }
103
104 /**
105 * Delete transient.
106 *
107 * @param string $name
108 * @param string $client_id
109 *
110 * @return bool
111 */
112 public function delete_trans( string $name, string $client_id ): bool {
113 $trans_name = $this->option_prefix . $name . '_' . $client_id;
114
115 return delete_transient( $trans_name );
116 }
117
118 /**
119 * Update option.
120 *
121 * @param string $name
122 * @param mixed $value
123 *
124 * @return bool
125 */
126 public function update_option( string $name, $value ): bool {
127 return update_option( $this->option_prefix . $name, json_encode( $value ) );
128 }
129
130 /**
131 * Get option.
132 *
133 * @param string $name
134 *
135 * @return mixed
136 */
137 public function get_option( string $name ) {
138 $value = get_option( $this->option_prefix . $name );
139 if ( null !== $value ) {
140 try {
141 return json_decode( $value, true );
142 } catch ( Throwable $exception ) {
143 return [];
144 }
145 }
146 return [];
147 }
148
149 /**
150 * Get website name.
151 *
152 * @return string
153 */
154 public function get_site_name(): string {
155 return get_bloginfo( 'name' );
156 }
157
158 /**
159 * Get website domain.
160 *
161 * @return string
162 */
163 public function get_site_domain(): string {
164 $site_url = get_bloginfo( 'url' );
165 $site_domain = preg_replace( '#^http(s)?:\/\/#', '', $site_url );
166 $site_domain = preg_replace( '#^www\.#', '', $site_domain );
167 $site_domain = explode( '/', $site_domain );
168
169 return $site_domain[0] ?? $site_domain;
170 }
171
172 /**
173 * Get user hash.
174 *
175 * @param string $username
176 * @param string $display_name
177 *
178 * @return string
179 */
180 public function get_user_hash( string $username, string $display_name ): string {
181 return hash( 'sha256', $username . '-' . $display_name . '-' . AUTH_SALT );
182 }
183
184 /**
185 * URL safe base64 encoding.
186 *
187 * @param string $data
188 *
189 * @return string
190 */
191 public function base64url_encode( string $data ): string {
192 return rtrim( strtr( base64_encode( $data ), '+/', '-_' ), '=' );
193 }
194
195 /**
196 * Base64 decoding from URL safe base64 encoding.
197 *
198 * @param string $data
199 *
200 * @return string|bool
201 */
202 public function base64url_decode( string $data ) {
203 // No need to add '=' at the end.
204 return base64_decode( strtr( $data, '-_', '+/' ) );
205 }
206 }
207