| @@ -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( |
| @@ -383,22 +380,8 @@ | ||
| 383 | 380 | } |
| 384 | 381 | |
| 385 | 382 | return false; |
| 386 | 383 | } |
| 387 | - /** | |
| 388 | - * Standardized invalid credentials error. | |
| 389 | - * | |
| 390 | - * @return WP_Error | |
| 391 | - */ | |
| 392 | - protected function invalid_credentials_error(): WP_Error { | |
| 393 | - | |
| 394 | - return new WP_Error( | |
| 395 | - 'learnpress_mcp_invalid_api_key_credentials', | |
| 396 | - __( 'Invalid MCP API credentials.', 'learnpress' ), | |
| 397 | - array( 'status' => 401 ) | |
| 398 | - ); | |
| 399 | - } | |
| 400 | - | |
| 401 | 384 | /** |
| 402 | 385 | * Validate expected consumer key format. |
| 403 | 386 | * |
| 404 | 387 | * @param string $consumer_key Plaintext consumer key. |