← All changes
|
includes/sdk/google/google/auth/src/CredentialSource/UrlSource.php
+25
-14
1.2.12
→
1.4.1
View file →
| @@ -14,14 +14,14 @@ | ||
| 14 | 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | 15 | * See the License for the specific language governing permissions and |
| 16 | 16 | * limitations under the License. |
| 17 | 17 | */ |
| 18 | -namespace Dudlewebs\WPMCS\Google\Auth\CredentialSource; | |
| 18 | +namespace Dudlewebs\WPMCS\GCP\Google\Auth\CredentialSource; | |
| 19 | 19 | |
| 20 | -use Dudlewebs\WPMCS\Google\Auth\ExternalAccountCredentialSourceInterface; | |
| 21 | -use Dudlewebs\WPMCS\Google\Auth\HttpHandler\HttpClientCache; | |
| 22 | -use Dudlewebs\WPMCS\Google\Auth\HttpHandler\HttpHandlerFactory; | |
| 23 | -use Dudlewebs\WPMCS\GuzzleHttp\Psr7\Request; | |
| 20 | +use Dudlewebs\WPMCS\GCP\Google\Auth\ExternalAccountCredentialSourceInterface; | |
| 21 | +use Dudlewebs\WPMCS\GCP\Google\Auth\HttpHandler\HttpClientCache; | |
| 22 | +use Dudlewebs\WPMCS\GCP\Google\Auth\HttpHandler\HttpHandlerFactory; | |
| 23 | +use Dudlewebs\WPMCS\GCP\GuzzleHttp\Psr7\Request; | |
| 24 | 24 | use InvalidArgumentException; |
| 25 | 25 | use UnexpectedValueException; |
| 26 | 26 | /** |
| 27 | 27 | * Retrieve a token from a URL. |
| @@ -35,18 +35,18 @@ | ||
| 35 | 35 | * @var array<string, string|string[]> |
| 36 | 36 | */ |
| 37 | 37 | private ?array $headers; |
| 38 | 38 | /** |
| 39 | - * @param string $url The URL to fetch the subject token from. | |
| 40 | - * @param string $format The format of the token in the response. Can be null or "json". | |
| 41 | - * @param string $subjectTokenFieldName The name of the field containing the token in the response. This is required | |
| 39 | + * @param string $url The URL to fetch the subject token from. | |
| 40 | + * @param string|null $format The format of the token in the response. Can be null or "json". | |
| 41 | + * @param string|null $subjectTokenFieldName The name of the field containing the token in the response. This is required | |
| 42 | 42 | * when format is "json". |
| 43 | - * @param array<string, string|string[]> $headers Request headers to send in with the request to the URL. | |
| 43 | + * @param array<string, string|string[]>|null $headers Request headers to send in with the request to the URL. | |
| 44 | 44 | */ |
| 45 | - public function __construct(string $url, string $format = null, string $subjectTokenFieldName = null, array $headers = null) | |
| 45 | + public function __construct(string $url, ?string $format = null, ?string $subjectTokenFieldName = null, ?array $headers = null) | |
| 46 | 46 | { |
| 47 | 47 | $this->url = $url; |
| 48 | - if ($format === 'json' && is_null($subjectTokenFieldName)) { | |
| 48 | + if ($format === 'json' && \is_null($subjectTokenFieldName)) { | |
| 49 | 49 | throw new InvalidArgumentException('subject_token_field_name must be set when format is JSON'); |
| 50 | 50 | } |
| 51 | 51 | $this->format = $format; |
| 52 | 52 | $this->subjectTokenFieldName = $subjectTokenFieldName; |
| @@ -51,11 +51,11 @@ | ||
| 51 | 51 | $this->format = $format; |
| 52 | 52 | $this->subjectTokenFieldName = $subjectTokenFieldName; |
| 53 | 53 | $this->headers = $headers; |
| 54 | 54 | } |
| 55 | - public function fetchSubjectToken(callable $httpHandler = null): string | |
| 55 | + public function fetchSubjectToken(?callable $httpHandler = null) : string | |
| 56 | 56 | { |
| 57 | - if (is_null($httpHandler)) { | |
| 57 | + if (\is_null($httpHandler)) { | |
| 58 | 58 | $httpHandler = HttpHandlerFactory::build(HttpClientCache::getHttpClient()); |
| 59 | 59 | } |
| 60 | 60 | $request = new Request('GET', $this->url, $this->headers ?: []); |
| 61 | 61 | $response = $httpHandler($request); |
| @@ -60,9 +60,9 @@ | ||
| 60 | 60 | $request = new Request('GET', $this->url, $this->headers ?: []); |
| 61 | 61 | $response = $httpHandler($request); |
| 62 | 62 | $body = (string) $response->getBody(); |
| 63 | 63 | if ($this->format === 'json') { |
| 64 | - if (!$json = json_decode((string) $body, \true)) { | |
| 64 | + if (!($json = \json_decode((string) $body, \true))) { | |
| 65 | 65 | throw new UnexpectedValueException('Unable to decode JSON response'); |
| 66 | 66 | } |
| 67 | 67 | if (!isset($json[$this->subjectTokenFieldName])) { |
| 68 | 68 | throw new UnexpectedValueException('subject_token_field_name not found in JSON file'); |
| @@ -69,6 +69,17 @@ | ||
| 69 | 69 | } |
| 70 | 70 | $body = $json[$this->subjectTokenFieldName]; |
| 71 | 71 | } |
| 72 | 72 | return $body; |
| 73 | + } | |
| 74 | + /** | |
| 75 | + * Get the cache key for the credentials. | |
| 76 | + * The format for the cache key is: | |
| 77 | + * URL | |
| 78 | + * | |
| 79 | + * @return ?string | |
| 80 | + */ | |
| 81 | + public function getCacheKey() : ?string | |
| 82 | + { | |
| 83 | + return $this->url; | |
| 73 | 84 | } |
| 74 | 85 | } |