| 1 |
<?php |
| 2 |
/** |
| 3 |
* OAuth 2.1 Rewrite Rules. |
| 4 |
* |
| 5 |
* Registers the WordPress rewrite rules and query var used to route the |
| 6 |
* five OAuth endpoints (authorize, authorize-callback, token, consent, |
| 7 |
* revoke). |
| 8 |
*/ |
| 9 |
|
| 10 |
declare( strict_types=1 ); |
| 11 |
|
| 12 |
namespace WPMedia\MCP\OAuth\Auth; |
| 13 |
|
| 14 |
/** |
| 15 |
* Registers OAuth endpoint rewrite rules and the query var used to route them. |
| 16 |
*/ |
| 17 |
class Rewrite { |
| 18 |
|
| 19 |
/** |
| 20 |
* WordPress query var used to route OAuth endpoint requests. |
| 21 |
*/ |
| 22 |
const OAUTH_QUERY_VAR = 'mcp_oauth_endpoint'; |
| 23 |
|
| 24 |
/** |
| 25 |
* Canary rule key used to detect whether the OAuth rules are persisted. |
| 26 |
*/ |
| 27 |
const AUTHORIZE_RULE = '^oauth/authorize$'; |
| 28 |
|
| 29 |
/** |
| 30 |
* Register WordPress rewrite rules for all five OAuth endpoints. |
| 31 |
* |
| 32 |
* Called on the 'init' action (normal page load). |
| 33 |
* |
| 34 |
* @return void |
| 35 |
*/ |
| 36 |
public function register_oauth_rewrite_rules(): void { |
| 37 |
add_rewrite_rule( self::AUTHORIZE_RULE, 'index.php?' . self::OAUTH_QUERY_VAR . '=authorize', 'top' ); |
| 38 |
add_rewrite_rule( '^oauth/authorize-callback$', 'index.php?' . self::OAUTH_QUERY_VAR . '=authorize-callback', 'top' ); |
| 39 |
add_rewrite_rule( '^oauth/token$', 'index.php?' . self::OAUTH_QUERY_VAR . '=token', 'top' ); |
| 40 |
add_rewrite_rule( '^oauth/consent$', 'index.php?' . self::OAUTH_QUERY_VAR . '=consent', 'top' ); |
| 41 |
add_rewrite_rule( '^oauth/revoke$', 'index.php?' . self::OAUTH_QUERY_VAR . '=revoke', 'top' ); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Add the OAuth query var to WordPress's list of recognised vars. |
| 46 |
* |
| 47 |
* @param string[] $vars Existing query vars. |
| 48 |
* @return string[] Modified list. |
| 49 |
*/ |
| 50 |
public function add_oauth_query_vars( array $vars ): array { |
| 51 |
$vars[] = self::OAUTH_QUERY_VAR; |
| 52 |
|
| 53 |
return $vars; |
| 54 |
} |
| 55 |
} |
| 56 |
|