PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.8
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.8
4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 All 139 releases
← All changes | inc/MCP/Auth/ApiKeyAuthenticator.php +5 -35 4.3.74.4.8 View file →
@@ -1,8 +1,9 @@
1 1 <?php
2 2
3 3 namespace LearnPress\MCP\Auth;
4 4
5 +use LearnPress\MCP\Support\Errors;
5 6 use LP_Helper;
6 7 use WP_Error;
7 8 use WP_REST_Request;
8 9
@@ -110,13 +111,9 @@
110 111 return $this->auth_error;
111 112 }
112 113
113 114 if ( ! AuthContext::is_api_key_auth() ) {
114 - return new WP_Error(
115 - 'learnpress_mcp_api_key_required',
116 - __( 'MCP API key authentication is required.', 'learnpress' ),
117 - array( 'status' => 401 )
118 - );
115 + return Errors::api_key_required();
119 116 }
120 117
121 118 return $error;
122 119 }
@@ -148,21 +145,21 @@
148 145 $consumer_key = $credentials['consumer_key'];
149 146 $consumer_secret = $credentials['consumer_secret'];
150 147
151 148 if ( '' === $consumer_key || '' === $consumer_secret ) {
152 - $this->auth_error = $this->invalid_credentials_error();
149 + $this->auth_error = Errors::invalid_api_credentials();
153 150 return 0;
154 151 }
155 152
156 153 $key = $this->keys_repository->find_by_consumer_key( $consumer_key );
157 154 if ( ! $key || empty( $key->consumer_secret ) || ! $this->keys_repository->verify_secret_hash( (string) $key->consumer_secret, $consumer_secret ) ) {
158 - $this->auth_error = $this->invalid_credentials_error();
155 + $this->auth_error = Errors::invalid_api_credentials();
159 156 return 0;
160 157 }
161 158
162 159 $resolved_user_id = absint( $key->user_id );
163 160 if ( $resolved_user_id <= 0 || ! get_user_by( 'id', $resolved_user_id ) ) {
164 - $this->auth_error = $this->invalid_credentials_error();
161 + $this->auth_error = Errors::invalid_api_credentials();
165 162 return 0;
166 163 }
167 164
168 165 AuthContext::set_api_key_auth(
@@ -209,22 +206,9 @@
209 206 *
210 207 * @return array<string, mixed>
211 208 */
212 209 protected function parse_credentials(): array {
213 - $consumer_key_present = isset( $_GET['consumer_key'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
214 - $consumer_secret_present = isset( $_GET['consumer_secret'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
215 210
216 - $consumer_key = $consumer_key_present ? LP_Helper::sanitize_params_submitted( $_GET['consumer_key'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
217 - $consumer_secret = $consumer_secret_present ? LP_Helper::sanitize_params_submitted( $_GET['consumer_secret'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
218 -
219 - if ( $consumer_key_present || $consumer_secret_present ) {
220 - return array(
221 - 'present' => true,
222 - 'consumer_key' => $consumer_key,
223 - 'consumer_secret' => $consumer_secret,
224 - );
225 - }
226 -
227 211 $has_php_auth_user = isset( $_SERVER['PHP_AUTH_USER'] );
228 212 $has_php_auth_pw = isset( $_SERVER['PHP_AUTH_PW'] );
229 213
230 214 if ( $has_php_auth_user || $has_php_auth_pw ) {
@@ -396,22 +380,8 @@
396 380 }
397 381
398 382 return false;
399 383 }
400 - /**
401 - * Standardized invalid credentials error.
402 - *
403 - * @return WP_Error
404 - */
405 - protected function invalid_credentials_error(): WP_Error {
406 -
407 - return new WP_Error(
408 - 'learnpress_mcp_invalid_api_key_credentials',
409 - __( 'Invalid MCP API credentials.', 'learnpress' ),
410 - array( 'status' => 401 )
411 - );
412 - }
413 -
414 384 /**
415 385 * Validate expected consumer key format.
416 386 *
417 387 * @param string $consumer_key Plaintext consumer key.