| 1 |
<?php |
| 2 |
/** |
| 3 |
* OAuth Request Body Parser. |
| 4 |
* |
| 5 |
* Shared logic for parsing application/json or application/x-www-form-urlencoded |
| 6 |
* request bodies in OAuth endpoints (TokenEndpoint, RevokeEndpoint). |
| 7 |
*/ |
| 8 |
|
| 9 |
declare(strict_types=1); |
| 10 |
|
| 11 |
namespace WPMedia\MCP\OAuth\Auth; |
| 12 |
|
| 13 |
/** |
| 14 |
* Provides parse_body() to OAuth endpoint classes. |
| 15 |
*/ |
| 16 |
trait ParseBodyTrait { |
| 17 |
|
| 18 |
/** |
| 19 |
* Parse the request body from either JSON or form-encoded content. |
| 20 |
* |
| 21 |
* JSON bodies are capped at 32 KB to prevent memory exhaustion from |
| 22 |
* oversized payloads; form-encoded bodies are read from $_POST which |
| 23 |
* PHP already limits via the post_max_size ini setting. |
| 24 |
* |
| 25 |
* @return array<string, mixed> Associative array of body parameters. |
| 26 |
*/ |
| 27 |
private function parse_body(): array { |
| 28 |
$content_type = isset( $_SERVER['CONTENT_TYPE'] ) ? sanitize_text_field( wp_unslash( $_SERVER['CONTENT_TYPE'] ) ) : ''; |
| 29 |
|
| 30 |
if ( false !== strpos( $content_type, 'application/json' ) ) { |
| 31 |
$raw = substr( (string) file_get_contents( 'php://input' ), 0, 32768 ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 32 |
$body = json_decode( '' !== $raw ? $raw : '{}', true ); |
| 33 |
return is_array( $body ) ? $body : []; |
| 34 |
} |
| 35 |
|
| 36 |
// Form-encoded (application/x-www-form-urlencoded). |
| 37 |
// phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 38 |
return array_map( 'sanitize_text_field', wp_unslash( $_POST ) ); |
| 39 |
} |
| 40 |
} |
| 41 |
|