← Back
Editing: AuthHttpClient.php
<?php namespace GoDaddy\Auth; class AuthHttpClient implements AuthHttpClientInterface { private const PACKAGE_NAME = 'auth-contrib/php-auth-library'; private $httpClient; public function __construct(HttpClientInterface $httpClient = null, string $appCode) { if ($httpClient === null) { // Use the secure HTTP client with same-origin redirect policy $httpClient = new SecureCurlHttpClient(); } $this->httpClient = $httpClient; $this->httpClient->setAgent($this->buildAgent($appCode)); } public function getJwt(string $host, array $parameters = []): AuthResult { try { $response = $this->httpClient->post('https://'.$host.self::TOKEN_SERVICE_URI, $parameters); return new AuthResult($response); } catch (\Exception $e) { return new AuthResult(null, new AuthException($e)); } } public function getPublicKey(string $host, string $publicKeyId): AuthResult { try { // Validate kid to prevent path traversal attacks $validatedKid = KidValidator::validate($publicKeyId); // Validate host to prevent injection $parsedUrl = parse_url("https://{$host}"); if (!$parsedUrl || !isset($parsedUrl['host']) || (isset($parsedUrl['path']) && $parsedUrl['path'] !== '/') || isset($parsedUrl['query']) || isset($parsedUrl['fragment']) || isset($parsedUrl['user']) || isset($parsedUrl['pass'])) { throw new AuthException('Invalid SSO host'); } // Build safe URL with proper encoding $safeUrl = sprintf( 'https://%s%s%s', $parsedUrl['host'] . (isset($parsedUrl['port']) ? ':' . $parsedUrl['port'] : ''), self::KEY_SERVICE_URI, rawurlencode($validatedKid) ); $response = $this->httpClient->get($safeUrl); return new AuthResult($response); } catch (\Exception $e) { return new AuthResult(null, new AuthException($e)); } } public function setAppCode(string $appCode) { $this->appCode = $appCode; } private function buildAgent(string $appCode): string { $libVersion = $this->getLibraryVersion(); return 'PHP/' . phpversion() . ' PhpAuthLibrary/' . $libVersion . ' App/' . $appCode; } private function getLibraryVersion(): string { $libVersion = 'Unknown'; $packageDef = 'vendor/composer/installed.json'; if(file_exists($packageDef)) { $packageDef = file_get_contents($packageDef); $packages = json_decode($packageDef); if (is_object($packages) && isset($packages->packages) && is_array($packages->packages)) { $packages = $packages->packages; } foreach($packages as $package) { if($package->name == self::PACKAGE_NAME) { $libVersion = $package->version; } } } return $libVersion; } }
Save File
Cancel