|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Azure\Identity\Credential; |
| 4 | + |
| 5 | +use Azure\Identity\EnvVar; |
| 6 | +use Azure\Identity\Exception\InvalidScopesException; |
| 7 | +use Azure\Identity\Token; |
| 8 | +use Azure\Identity\TokenInterface; |
| 9 | +use Psr\Log\LoggerInterface; |
| 10 | +use Symfony\Component\HttpClient\HttpClient; |
| 11 | +use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface; |
| 12 | +use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface; |
| 13 | +use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; |
| 14 | +use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 15 | + |
| 16 | +class AppServiceCredential implements AzureCredentialInterface |
| 17 | +{ |
| 18 | + private HttpClientInterface $httpClient; |
| 19 | + |
| 20 | + public function __construct(public ?string $clientId = null, ?HttpClientInterface $httpClient = null, private ?LoggerInterface $logger = null) |
| 21 | + { |
| 22 | + $this->httpClient = $httpClient ?? HttpClient::create(); |
| 23 | + } |
| 24 | + |
| 25 | + public function getToken(array $scopes, array $options = []): ?TokenInterface |
| 26 | + { |
| 27 | + $url = EnvVar::get('IDENTITY_ENDPOINT'); |
| 28 | + $secret = EnvVar::get('IDENTITY_HEADER'); |
| 29 | + |
| 30 | + if (!$url || !$secret) { |
| 31 | + $this->logger->warning('App Service managed identity configuration not found in environment'); |
| 32 | + |
| 33 | + return null; |
| 34 | + } |
| 35 | + |
| 36 | + $clientId = $this->clientId ?? EnvVar::get('AZURE_CLIENT_ID'); |
| 37 | + $params = [ |
| 38 | + 'api-version' => '2019-08-01', |
| 39 | + 'resource' => $this->scopesToResource($scopes), |
| 40 | + ]; |
| 41 | + |
| 42 | + if ($clientId) { |
| 43 | + $params['client_id'] = $clientId; |
| 44 | + } |
| 45 | + |
| 46 | + try { |
| 47 | + $response = $this->httpClient->request('GET', $url, [ |
| 48 | + 'query' => array_merge($params, $options), |
| 49 | + 'headers' => [ |
| 50 | + 'X-IDENTITY-HEADER' => $secret |
| 51 | + ] |
| 52 | + ]); |
| 53 | + |
| 54 | + $result = $response->toArray(); |
| 55 | + } catch (DecodingExceptionInterface $e) { |
| 56 | + $this->logger->info('Failed to decode token response.', ['exception' => $e]); |
| 57 | + |
| 58 | + return null; |
| 59 | + } catch (TransportExceptionInterface|HttpExceptionInterface $e) { |
| 60 | + $this->logger->info('Failed to fetch token.', ['exception' => $e]); |
| 61 | + |
| 62 | + return null; |
| 63 | + } |
| 64 | + |
| 65 | + // App service return expires_on instead expires_in |
| 66 | + $result['expires_in'] = floor($result['expires_on'] - time()); |
| 67 | + |
| 68 | + return Token::fromArray($result); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Convert an AADv2 scope to an AADv1 resource |
| 73 | + * @param array $scopes |
| 74 | + * @return string |
| 75 | + */ |
| 76 | + private function scopesToResource(array $scopes): string |
| 77 | + { |
| 78 | + if (count($scopes) !== 1) { |
| 79 | + throw new InvalidScopesException('AppServiceCredential requires exactly one scope per token request.'); |
| 80 | + } |
| 81 | + |
| 82 | + $resource = $scopes[0]; |
| 83 | + if (str_ends_with($resource, '/.default')) { |
| 84 | + $resource = substr($resource, 0, strlen('/.default') * -1); |
| 85 | + } |
| 86 | + |
| 87 | + return $resource; |
| 88 | + } |
| 89 | +} |
0 commit comments