| 1 |
<?php |
| 2 |
|
| 3 |
namespace LearnPress\MCP\Auth; |
| 4 |
|
| 5 |
use LP_Helper; |
| 6 |
|
| 7 |
defined( 'ABSPATH' ) || exit; |
| 8 |
|
| 9 |
/** |
| 10 |
* Request-scoped authentication context for MCP API key auth. |
| 11 |
*/ |
| 12 |
class AuthContext { |
| 13 |
/** |
| 14 |
* @var bool |
| 15 |
*/ |
| 16 |
protected static $is_api_key_auth = false; |
| 17 |
|
| 18 |
/** |
| 19 |
* @var int |
| 20 |
*/ |
| 21 |
protected static $key_id = 0; |
| 22 |
|
| 23 |
/** |
| 24 |
* @var int |
| 25 |
*/ |
| 26 |
protected static $user_id = 0; |
| 27 |
|
| 28 |
/** |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
protected static $permissions = ''; |
| 32 |
|
| 33 |
/** |
| 34 |
* @var bool |
| 35 |
*/ |
| 36 |
protected static $usage_touched = false; |
| 37 |
|
| 38 |
/** |
| 39 |
* Reset state for current request. |
| 40 |
* |
| 41 |
* @return void |
| 42 |
*/ |
| 43 |
public static function reset(): void { |
| 44 |
|
| 45 |
self::$is_api_key_auth = false; |
| 46 |
self::$key_id = 0; |
| 47 |
self::$user_id = 0; |
| 48 |
self::$permissions = ''; |
| 49 |
self::$usage_touched = false; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Store successful API key auth context. |
| 54 |
* |
| 55 |
* @param int $key_id API key ID. |
| 56 |
* @param int $user_id Authenticated user ID. |
| 57 |
* @param string $permissions Granted permissions scope. |
| 58 |
* |
| 59 |
* @return void |
| 60 |
*/ |
| 61 |
public static function set_api_key_auth( int $key_id, int $user_id, string $permissions ): void { |
| 62 |
|
| 63 |
self::$is_api_key_auth = true; |
| 64 |
self::$key_id = absint( $key_id ); |
| 65 |
self::$user_id = absint( $user_id ); |
| 66 |
self::$permissions = LP_Helper::sanitize_params_submitted( $permissions, 'key', false ); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Whether request is authenticated by MCP API key. |
| 71 |
* |
| 72 |
* @return bool |
| 73 |
*/ |
| 74 |
public static function is_api_key_auth(): bool { |
| 75 |
|
| 76 |
return self::$is_api_key_auth; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Get API key ID from context. |
| 81 |
* |
| 82 |
* @return int |
| 83 |
*/ |
| 84 |
public static function get_key_id(): int { |
| 85 |
|
| 86 |
return self::$key_id; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Get user ID from context. |
| 91 |
* |
| 92 |
* @return int |
| 93 |
*/ |
| 94 |
public static function get_user_id(): int { |
| 95 |
|
| 96 |
return self::$user_id; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Get granted key permissions. |
| 101 |
* |
| 102 |
* @return string |
| 103 |
*/ |
| 104 |
public static function get_permissions(): string { |
| 105 |
|
| 106 |
return self::$permissions; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Mark usage metrics as already touched. |
| 111 |
* |
| 112 |
* @return void |
| 113 |
*/ |
| 114 |
public static function mark_usage_touched(): void { |
| 115 |
|
| 116 |
self::$usage_touched = true; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Whether usage metrics were already touched in this request. |
| 121 |
* |
| 122 |
* @return bool |
| 123 |
*/ |
| 124 |
public static function is_usage_touched(): bool { |
| 125 |
|
| 126 |
return self::$usage_touched; |
| 127 |
} |
| 128 |
} |
| 129 |
|