|
| 1 | +
|
| 2 | +/* |
| 3 | + * Copyright 2026 Google Inc. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +namespace Google\Auth\Credentials; |
| 19 | + |
| 20 | +use Google\Auth\FetchAuthTokenInterface; |
| 21 | +use InvalidArgumentException; |
| 22 | + |
| 23 | +/** |
| 24 | + * Provides a set of credentials that always returns a pre-issued access token. |
| 25 | + * |
| 26 | + * No token exchange or HTTP request is ever performed; the supplied token is |
| 27 | + * returned as-is. This is useful for development and testing, or when an access |
| 28 | + * token has already been obtained through some other means and simply needs to |
| 29 | + * be handed to the auth middleware. |
| 30 | + * |
| 31 | + * ``` |
| 32 | + * use Google\Auth\Credentials\StaticCredentials; |
| 33 | + * use Google\Auth\Middleware\AuthTokenMiddleware; |
| 34 | + * |
| 35 | + * $creds = new StaticCredentials('ya29.my-access-token'); |
| 36 | + * $middleware = new AuthTokenMiddleware($creds); |
| 37 | + * ``` |
| 38 | + */ |
| 39 | +class StaticCredentials implements FetchAuthTokenInterface |
| 40 | +{ |
| 41 | + /** |
| 42 | + * @var array{access_token: string, expires_at: int} |
| 43 | + */ |
| 44 | + private array $token; |
| 45 | + |
| 46 | + /** |
| 47 | + * @param string $accessToken The pre-issued OAuth2 access token to use. |
| 48 | + * @param int $expiresIn [optional] The lifetime of the token in seconds, used |
| 49 | + * to populate its expiration. Defaults to 3600 (one hour). |
| 50 | + */ |
| 51 | + public function __construct(string $accessToken, int $expiresIn = 3600) |
| 52 | + { |
| 53 | + if ($accessToken === '') { |
| 54 | + throw new InvalidArgumentException('The access token must not be empty'); |
| 55 | + } |
| 56 | + |
| 57 | + $this->token = [ |
| 58 | + 'access_token' => $accessToken, |
| 59 | + 'expires_at' => time() + $expiresIn, |
| 60 | + ]; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Returns the static access token supplied to the constructor. |
| 65 | + * |
| 66 | + * @param callable|null $httpHandler Unused; present for interface compatibility. |
| 67 | + * @return array{access_token: string, expires_at: int} |
| 68 | + */ |
| 69 | + public function fetchAuthToken(?callable $httpHandler = null) |
| 70 | + { |
| 71 | + return $this->token; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Returns a cache key derived from the supplied token. Caching a static token |
| 76 | + * has no real benefit, but a stable, token-specific key ensures a shared cache |
| 77 | + * pool never returns another credential's token. |
| 78 | + * |
| 79 | + * @return string |
| 80 | + */ |
| 81 | + public function getCacheKey() |
| 82 | + { |
| 83 | + return 'static_credentials_' . md5($this->token['access_token']); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @return array{access_token: string, expires_at: int} |
| 88 | + */ |
| 89 | + public function getLastReceivedToken() |
| 90 | + { |
| 91 | + return $this->token; |
| 92 | + } |
| 93 | +} |
0 commit comments