<?php namespace Google\Auth\CredentialSource; use Google\Auth\ExternalAccountCredentialSourceInterface; use InvalidArgumentException; use UnexpectedValueException; class FileSource implements ExternalAccountCredentialSourceInterface { private string $file; private ?string $format; private ?string $subjectTokenFieldName; public function __construct( string $file, ?string $format = null, ?string $subjectTokenFieldName = null ) { $this->file = $file; if ($format === 'json' && is_null($subjectTokenFieldName)) { throw new InvalidArgumentException( 'subject_token_field_name must be set when format is JSON' ); } $this->format = $format; $this->subjectTokenFieldName = $subjectTokenFieldName; } public function fetchSubjectToken(?callable $httpHandler = null): string { $contents = file_get_contents($this->file); if ($this->format === 'json') { if (!$json = json_decode((string) $contents, true)) { throw new UnexpectedValueException( 'Unable to decode JSON file' ); } if (!isset($json[$this->subjectTokenFieldName])) { throw new UnexpectedValueException( 'subject_token_field_name not found in JSON file' ); } $contents = $json[$this->subjectTokenFieldName]; } return $contents; } public function getCacheKey(): ?string { return $this->file; } }