|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Base class for REST API endpoints. |
| 4 | + * |
| 5 | + * @package Progress_Planner |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Progress_Planner\Rest; |
| 9 | + |
| 10 | +/** |
| 11 | + * Base class for REST API endpoints. |
| 12 | + */ |
| 13 | +abstract class Base { |
| 14 | + |
| 15 | + /** |
| 16 | + * Get client IP address. |
| 17 | + * |
| 18 | + * @return string |
| 19 | + */ |
| 20 | + protected function get_client_ip() { |
| 21 | + $ip_keys = [ |
| 22 | + 'HTTP_CF_CONNECTING_IP', // Cloudflare. |
| 23 | + 'HTTP_X_REAL_IP', // Nginx proxy. |
| 24 | + 'HTTP_X_FORWARDED_FOR', // Standard proxy header. |
| 25 | + 'REMOTE_ADDR', // Direct connection. |
| 26 | + ]; |
| 27 | + |
| 28 | + foreach ( $ip_keys as $key ) { |
| 29 | + if ( isset( $_SERVER[ $key ] ) && ! empty( $_SERVER[ $key ] ) ) { |
| 30 | + $ip = \sanitize_text_field( \wp_unslash( $_SERVER[ $key ] ) ); |
| 31 | + // Handle X-Forwarded-For which may contain multiple IPs. |
| 32 | + if ( \strpos( $ip, ',' ) !== false ) { |
| 33 | + $ip = \trim( \explode( ',', $ip )[0] ); |
| 34 | + } |
| 35 | + // Validate IP address. |
| 36 | + if ( \filter_var( $ip, FILTER_VALIDATE_IP ) ) { |
| 37 | + return $ip; |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + return '0.0.0.0'; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Validate the token. |
| 47 | + * |
| 48 | + * @param string $token The token. |
| 49 | + * |
| 50 | + * @return bool |
| 51 | + */ |
| 52 | + public function validate_token( $token ) { |
| 53 | + // Rate limiting: Check for too many failed attempts. |
| 54 | + $ip_address = $this->get_client_ip(); |
| 55 | + $rate_limit_key = 'prpl_api_rate_limit_' . \md5( $ip_address ); |
| 56 | + $failed_attempts = (int) \get_transient( $rate_limit_key ); |
| 57 | + |
| 58 | + // Block if more than 10 failed attempts in the last hour. |
| 59 | + if ( $failed_attempts >= 10 ) { |
| 60 | + return false; |
| 61 | + } |
| 62 | + |
| 63 | + $token = \str_replace( 'token/', '', $token ); |
| 64 | + |
| 65 | + // Check test token first (use hash_equals to prevent timing attacks). |
| 66 | + $test_token = \get_option( 'progress_planner_test_token', '' ); |
| 67 | + if ( ! empty( $test_token ) && \hash_equals( $test_token, $token ) ) { |
| 68 | + // Clear failed attempts on successful authentication. |
| 69 | + \delete_transient( $rate_limit_key ); |
| 70 | + return true; |
| 71 | + } |
| 72 | + |
| 73 | + $license_key = \get_option( 'progress_planner_license_key', false ); |
| 74 | + if ( ! $license_key || 'no-license' === $license_key ) { |
| 75 | + // Increment failed attempts counter. |
| 76 | + \set_transient( $rate_limit_key, $failed_attempts + 1, HOUR_IN_SECONDS ); |
| 77 | + return false; |
| 78 | + } |
| 79 | + |
| 80 | + // Use hash_equals() to prevent timing attacks. |
| 81 | + $is_valid = \hash_equals( $license_key, $token ); |
| 82 | + |
| 83 | + if ( ! $is_valid ) { |
| 84 | + // Increment failed attempts counter. |
| 85 | + \set_transient( $rate_limit_key, $failed_attempts + 1, HOUR_IN_SECONDS ); |
| 86 | + } else { |
| 87 | + // Clear failed attempts on successful authentication. |
| 88 | + \delete_transient( $rate_limit_key ); |
| 89 | + } |
| 90 | + |
| 91 | + return $is_valid; |
| 92 | + } |
| 93 | +} |
0 commit comments