|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace ExtraChill\ClaudeCodeAiProvider\Provider; |
| 6 | + |
| 7 | +use RuntimeException; |
| 8 | + |
| 9 | +/** |
| 10 | + * Refreshes Claude Code OAuth access tokens. |
| 11 | + */ |
| 12 | +class ClaudeCodeOAuthClient |
| 13 | +{ |
| 14 | + private const TOKEN_URL = 'https://platform.claude.com/v1/oauth/token'; |
| 15 | + private const CLIENT_ID = '9d1c250a-e61b-44d9-88ed-5944d1962f5e'; |
| 16 | + |
| 17 | + /** |
| 18 | + * @var ClaudeCodeTokenStore Token store. |
| 19 | + */ |
| 20 | + private ClaudeCodeTokenStore $tokenStore; |
| 21 | + |
| 22 | + public function __construct(ClaudeCodeTokenStore $tokenStore) |
| 23 | + { |
| 24 | + $this->tokenStore = $tokenStore; |
| 25 | + } |
| 26 | + |
| 27 | + public function getAccessToken(): string |
| 28 | + { |
| 29 | + $accessToken = $this->tokenStore->getAccessToken(); |
| 30 | + if ($accessToken !== null) { |
| 31 | + return $accessToken; |
| 32 | + } |
| 33 | + |
| 34 | + $tokens = $this->tokenStore->getTokens(); |
| 35 | + $refreshToken = $tokens['refresh_token'] ?? ''; |
| 36 | + if ($refreshToken === '') { |
| 37 | + throw new RuntimeException('Claude Code OAuth refresh token is not configured.'); |
| 38 | + } |
| 39 | + |
| 40 | + $data = $this->refreshAccessToken($refreshToken); |
| 41 | + if (empty($data['access_token']) || !is_scalar($data['access_token'])) { |
| 42 | + throw new RuntimeException('Claude Code OAuth refresh returned an invalid response.'); |
| 43 | + } |
| 44 | + |
| 45 | + $updated = array_merge( |
| 46 | + $tokens, |
| 47 | + [ |
| 48 | + 'access_token' => (string) $data['access_token'], |
| 49 | + 'expires_at' => time() + $this->getIntegerValue($data['expires_in'] ?? null, 3600), |
| 50 | + ] |
| 51 | + ); |
| 52 | + |
| 53 | + if (!empty($data['refresh_token']) && is_scalar($data['refresh_token'])) { |
| 54 | + $updated['refresh_token'] = (string) $data['refresh_token']; |
| 55 | + } |
| 56 | + |
| 57 | + $this->tokenStore->updateTokens($updated); |
| 58 | + return (string) $data['access_token']; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @return array<string, mixed> |
| 63 | + */ |
| 64 | + private function refreshAccessToken(string $refreshToken): array |
| 65 | + { |
| 66 | + $body = [ |
| 67 | + 'grant_type' => 'refresh_token', |
| 68 | + 'client_id' => self::CLIENT_ID, |
| 69 | + 'refresh_token' => $refreshToken, |
| 70 | + ]; |
| 71 | + |
| 72 | + if (function_exists('wp_remote_post')) { |
| 73 | + return $this->refreshAccessTokenWithWordPress($body); |
| 74 | + } |
| 75 | + |
| 76 | + $context = stream_context_create( |
| 77 | + [ |
| 78 | + 'http' => [ |
| 79 | + 'method' => 'POST', |
| 80 | + 'header' => "Content-Type: application/json\r\n", |
| 81 | + 'content' => json_encode($body), |
| 82 | + 'ignore_errors' => true, |
| 83 | + 'timeout' => 20, |
| 84 | + ], |
| 85 | + ] |
| 86 | + ); |
| 87 | + $responseBody = file_get_contents(self::TOKEN_URL, false, $context); |
| 88 | + if ($responseBody === false) { |
| 89 | + throw new RuntimeException('Claude Code OAuth refresh failed.'); |
| 90 | + } |
| 91 | + |
| 92 | + $data = json_decode($responseBody, true); |
| 93 | + if (!is_array($data)) { |
| 94 | + throw new RuntimeException('Claude Code OAuth refresh returned an invalid response.'); |
| 95 | + } |
| 96 | + |
| 97 | + /** @var array<string, mixed> $data */ |
| 98 | + return $data; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @param array<string, string> $body Request body. |
| 103 | + * @return array<string, mixed> |
| 104 | + */ |
| 105 | + private function refreshAccessTokenWithWordPress(array $body): array |
| 106 | + { |
| 107 | + $wpRemotePost = 'wp_remote_post'; |
| 108 | + // @phpstan-ignore-next-line WordPress HTTP API is available at runtime when function_exists() passes. |
| 109 | + $response = $wpRemotePost(self::TOKEN_URL, [ |
| 110 | + 'body' => wp_json_encode($body), |
| 111 | + 'headers' => ['Content-Type' => 'application/json'], |
| 112 | + 'timeout' => 20, |
| 113 | + ]); |
| 114 | + |
| 115 | + $isWpError = 'is_wp_error'; |
| 116 | + // @phpstan-ignore-next-line WordPress error helper is available at runtime when function_exists() passes. |
| 117 | + if (function_exists('is_wp_error') && $isWpError($response)) { |
| 118 | + throw new RuntimeException('Claude Code OAuth refresh failed.'); |
| 119 | + } |
| 120 | + |
| 121 | + $wpRemoteRetrieveResponseCode = 'wp_remote_retrieve_response_code'; |
| 122 | + $rawStatusCode = 0; |
| 123 | + if (function_exists('wp_remote_retrieve_response_code')) { |
| 124 | + // @phpstan-ignore-next-line WordPress HTTP helper is available at runtime when function_exists() passes. |
| 125 | + $rawStatusCode = $wpRemoteRetrieveResponseCode($response); |
| 126 | + } |
| 127 | + $statusCode = is_numeric($rawStatusCode) ? (int) $rawStatusCode : 0; |
| 128 | + if ($statusCode < 200 || $statusCode >= 300) { |
| 129 | + throw new RuntimeException('Claude Code OAuth refresh failed.'); |
| 130 | + } |
| 131 | + |
| 132 | + $wpRemoteRetrieveBody = 'wp_remote_retrieve_body'; |
| 133 | + $rawResponseBody = ''; |
| 134 | + if (function_exists('wp_remote_retrieve_body')) { |
| 135 | + // @phpstan-ignore-next-line WordPress HTTP helper is available at runtime when function_exists() passes. |
| 136 | + $rawResponseBody = $wpRemoteRetrieveBody($response); |
| 137 | + } |
| 138 | + $responseBody = is_scalar($rawResponseBody) ? (string) $rawResponseBody : ''; |
| 139 | + $data = json_decode($responseBody, true); |
| 140 | + if (!is_array($data)) { |
| 141 | + throw new RuntimeException('Claude Code OAuth refresh returned an invalid response.'); |
| 142 | + } |
| 143 | + |
| 144 | + /** @var array<string, mixed> $data */ |
| 145 | + return $data; |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * @param mixed $value Raw value. |
| 150 | + */ |
| 151 | + private function getIntegerValue($value, int $fallback): int |
| 152 | + { |
| 153 | + return is_numeric($value) ? (int) $value : $fallback; |
| 154 | + } |
| 155 | +} |
0 commit comments