Skip to content

Commit 6cc4fed

Browse files
authored
Merge branch 'main' into bump-auth-dep
2 parents 5e301fb + 31a9861 commit 6cc4fed

File tree

11 files changed

+138
-28
lines changed

11 files changed

+138
-28
lines changed

.github/sync-repo-settings.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ branchProtectionRules:
1010
- 'PHP 8.1 Unit Test'
1111
- 'PHP 8.2 Unit Test'
1212
- 'PHP 8.3 Unit Test'
13-
- 'PHP 8.3 --prefer-lowest Unit Test'
13+
- 'PHP 8.4'
14+
- 'PHP 8.4 --prefer-lowest Unit Test'
1415
- 'PHP Style Check'
1516
- 'cla/google'
1617
requiredApprovingReviewCount: 1

.github/workflows/tests.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ jobs:
1111
strategy:
1212
fail-fast: false
1313
matrix:
14-
php: [ "8.0", "8.1", "8.2", "8.3" ]
14+
php: [ "8.0", "8.1", "8.2", "8.3", "8.4" ]
1515
composer-flags: [""]
1616
include:
1717
- php: "8.0"
1818
composer-flags: "--prefer-lowest "
19-
- php: "8.3"
19+
- php: "8.4"
2020
composer-flags: "--prefer-lowest "
2121
name: PHP ${{ matrix.php }} ${{ matrix.composer-flags }}Unit Test
2222
steps:

CHANGELOG.md

+19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
# Changelog
22

3+
## [2.18.1](https://github.com/googleapis/google-api-php-client/compare/v2.18.0...v2.18.1) (2024-11-24)
4+
5+
6+
### Bug Fixes
7+
8+
* Implicitly marking parameter as nullable is deprecated ([#2638](https://github.com/googleapis/google-api-php-client/issues/2638)) ([de57db2](https://github.com/googleapis/google-api-php-client/commit/de57db2fdc0d56de1abbf778b28b77c3347eb3fd))
9+
10+
## [2.18.0](https://github.com/googleapis/google-api-php-client/compare/v2.17.0...v2.18.0) (2024-10-16)
11+
12+
13+
### Features
14+
15+
* **docs:** Use doctum shared workflow for reference docs ([#2618](https://github.com/googleapis/google-api-php-client/issues/2618)) ([242e2cb](https://github.com/googleapis/google-api-php-client/commit/242e2cb09ad5b25b047a862b4d521037e74cae29))
16+
17+
18+
### Bug Fixes
19+
20+
* Explicit token caching issue ([#2358](https://github.com/googleapis/google-api-php-client/issues/2358)) ([dc13e5e](https://github.com/googleapis/google-api-php-client/commit/dc13e5e3f517148d3c66d151a5ab133b7840d8fb))
21+
322
## [2.17.0](https://github.com/googleapis/google-api-php-client/compare/v2.16.0...v2.17.0) (2024-07-10)
423

524

src/AccessToken/Revoke.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Revoke
3939
* Instantiates the class, but does not initiate the login flow, leaving it
4040
* to the discretion of the caller.
4141
*/
42-
public function __construct(ClientInterface $http = null)
42+
public function __construct(?ClientInterface $http = null)
4343
{
4444
$this->http = $http;
4545
}

src/AccessToken/Verify.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,17 @@ class Verify
5959

6060
/**
6161
* @var \Firebase\JWT\JWT
62-
*/
62+
*/
6363
public $jwt;
6464

6565
/**
6666
* Instantiates the class, but does not initiate the login flow, leaving it
6767
* to the discretion of the caller.
6868
*/
6969
public function __construct(
70-
ClientInterface $http = null,
71-
CacheItemPoolInterface $cache = null,
72-
$jwt = null
70+
?ClientInterface $http = null,
71+
?CacheItemPoolInterface $cache = null,
72+
?JWT $jwt = null
7373
) {
7474
if (null === $http) {
7575
$http = new Client();
@@ -130,7 +130,7 @@ public function verifyIdToken($idToken, $audience = null)
130130
return false;
131131
}
132132

133-
return (array) $payload;
133+
return (array)$payload;
134134
} catch (ExpiredException $e) { // @phpstan-ignore-line
135135
return false;
136136
} catch (ExpiredExceptionV3 $e) {
@@ -154,17 +154,17 @@ private function getCache()
154154
* Retrieve and cache a certificates file.
155155
*
156156
* @param string $url location
157-
* @throws \Google\Exception
158157
* @return array certificates
158+
* @throws \Google\Exception
159159
*/
160160
private function retrieveCertsFromLocation($url)
161161
{
162162
// If we're retrieving a local file, just grab it.
163163
if (0 !== strpos($url, 'http')) {
164164
if (!$file = file_get_contents($url)) {
165165
throw new GoogleException(
166-
"Failed to retrieve verification certificates: '" .
167-
$url . "'."
166+
"Failed to retrieve verification certificates: '".
167+
$url."'."
168168
);
169169
}
170170

@@ -175,7 +175,7 @@ private function retrieveCertsFromLocation($url)
175175
$response = $this->http->get($url);
176176

177177
if ($response->getStatusCode() == 200) {
178-
return json_decode((string) $response->getBody(), true);
178+
return json_decode((string)$response->getBody(), true);
179179
}
180180
throw new GoogleException(
181181
sprintf(

src/AuthHandler/Guzzle6AuthHandler.php

+12-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Guzzle6AuthHandler
2020
protected $cache;
2121
protected $cacheConfig;
2222

23-
public function __construct(CacheItemPoolInterface $cache = null, array $cacheConfig = [])
23+
public function __construct(?CacheItemPoolInterface $cache = null, array $cacheConfig = [])
2424
{
2525
$this->cache = $cache;
2626
$this->cacheConfig = $cacheConfig;
@@ -29,7 +29,7 @@ public function __construct(CacheItemPoolInterface $cache = null, array $cacheCo
2929
public function attachCredentials(
3030
ClientInterface $http,
3131
CredentialsLoader $credentials,
32-
callable $tokenCallback = null
32+
?callable $tokenCallback = null
3333
) {
3434
// use the provided cache
3535
if ($this->cache) {
@@ -46,7 +46,7 @@ public function attachCredentials(
4646
public function attachCredentialsCache(
4747
ClientInterface $http,
4848
FetchAuthTokenCache $credentials,
49-
callable $tokenCallback = null
49+
?callable $tokenCallback = null
5050
) {
5151
// if we end up needing to make an HTTP request to retrieve credentials, we
5252
// can use our existing one, but we need to throw exceptions so the error
@@ -74,10 +74,18 @@ public function attachToken(ClientInterface $http, array $token, array $scopes)
7474
return $token['access_token'];
7575
};
7676

77+
// Derive a cache prefix from the token, to ensure setting a new token
78+
// results in a cache-miss.
79+
// Note: Supplying a custom "prefix" will bust this behavior.
80+
$cacheConfig = $this->cacheConfig;
81+
if (!isset($cacheConfig['prefix']) && isset($token['access_token'])) {
82+
$cacheConfig['prefix'] = substr(sha1($token['access_token']), -10);
83+
}
84+
7785
$middleware = new ScopedAccessTokenMiddleware(
7886
$tokenFunc,
7987
$scopes,
80-
$this->cacheConfig,
88+
$cacheConfig,
8189
$this->cache
8290
);
8391

src/Client.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ public function refreshTokenWithAssertion()
321321
* @param ClientInterface $authHttp optional.
322322
* @return array access token
323323
*/
324-
public function fetchAccessTokenWithAssertion(ClientInterface $authHttp = null)
324+
public function fetchAccessTokenWithAssertion(?ClientInterface $authHttp = null)
325325
{
326326
if (!$this->isUsingApplicationDefaultCredentials()) {
327327
throw new DomainException(
@@ -454,7 +454,7 @@ public function createAuthUrl($scope = null, array $queryParams = [])
454454
* @param ClientInterface $http the http client object.
455455
* @return ClientInterface the http client object
456456
*/
457-
public function authorize(ClientInterface $http = null)
457+
public function authorize(?ClientInterface $http = null)
458458
{
459459
$http = $http ?: $this->getHttpClient();
460460
$authHandler = $this->getAuthHandler();

src/Http/REST.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static function execute(
5454
) {
5555
$runner = new Runner(
5656
$config,
57-
sprintf('%s %s', $request->getMethod(), (string) $request->getUri()),
57+
sprintf('%s %s', $request->getMethod(), (string)$request->getUri()),
5858
[self::class, 'doExecute'],
5959
[$client, $request, $expectedClass]
6060
);
@@ -120,15 +120,15 @@ interface_exists('\GuzzleHttp\Message\ResponseInterface')
120120
*/
121121
public static function decodeHttpResponse(
122122
ResponseInterface $response,
123-
RequestInterface $request = null,
123+
?RequestInterface $request = null,
124124
$expectedClass = null
125125
) {
126126
$code = $response->getStatusCode();
127127

128128
// retry strategy
129129
if (intVal($code) >= 400) {
130130
// if we errored out, it should be safe to grab the response body
131-
$body = (string) $response->getBody();
131+
$body = (string)$response->getBody();
132132

133133
// Check if we received errors, and add those to the Exception for convenience
134134
throw new GoogleServiceException($body, $code, null, self::getResponseErrors($body));
@@ -147,17 +147,17 @@ public static function decodeHttpResponse(
147147
return $response;
148148
}
149149

150-
private static function decodeBody(ResponseInterface $response, RequestInterface $request = null)
150+
private static function decodeBody(ResponseInterface $response, ?RequestInterface $request = null)
151151
{
152152
if (self::isAltMedia($request)) {
153153
// don't decode the body, it's probably a really long string
154154
return '';
155155
}
156156

157-
return (string) $response->getBody();
157+
return (string)$response->getBody();
158158
}
159159

160-
private static function determineExpectedClass($expectedClass, RequestInterface $request = null)
160+
private static function determineExpectedClass($expectedClass, ?RequestInterface $request = null)
161161
{
162162
// "false" is used to explicitly prevent an expected class from being returned
163163
if (false === $expectedClass) {
@@ -184,7 +184,7 @@ private static function getResponseErrors($body)
184184
return null;
185185
}
186186

187-
private static function isAltMedia(RequestInterface $request = null)
187+
private static function isAltMedia(?RequestInterface $request = null)
188188
{
189189
if ($request && $qs = $request->getUri()->getQuery()) {
190190
parse_str($qs, $query);

src/Service/Exception.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Exception extends GoogleException
3939
public function __construct(
4040
$message,
4141
$code = 0,
42-
Exception $previous = null,
42+
?Exception $previous = null,
4343
$errors = []
4444
) {
4545
if (version_compare(PHP_VERSION, '5.3.0') >= 0) {

src/Task/Composer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Composer
3030
*/
3131
public static function cleanup(
3232
Event $event,
33-
Filesystem $filesystem = null
33+
?Filesystem $filesystem = null
3434
) {
3535
$composer = $event->getComposer();
3636
$extra = $composer->getPackage()->getExtra();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
/**
3+
* Copyright 2022 Google LLC
4+
*
5+
* Licensed to the Apache Software Foundation (ASF) under one
6+
* or more contributor license agreements. See the NOTICE file
7+
* distributed with this work for additional information
8+
* regarding copyright ownership. The ASF licenses this file
9+
* to you under the Apache License, Version 2.0 (the
10+
* "License"); you may not use this file except in compliance
11+
* with the License. You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing,
16+
* software distributed under the License is distributed on an
17+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18+
* KIND, either express or implied. See the License for the
19+
* specific language governing permissions and limitations
20+
* under the License.
21+
*/
22+
23+
namespace Google\Tests\AuthHandler;
24+
25+
use Google\AuthHandler\AuthHandlerFactory;
26+
use Google\Auth\Cache\MemoryCacheItemPool;
27+
use GuzzleHttp\Client;
28+
use GuzzleHttp\Psr7\Request;
29+
use Google\Tests\BaseTest;
30+
31+
class AuthHandlerTest extends BaseTest
32+
{
33+
public function testSetAccessTokenResultsInAuthCacheMiss()
34+
{
35+
$client = new Client();
36+
$cache = new MemoryCacheItemPool();
37+
$authHandler = AuthHandlerFactory::build($cache);
38+
$scopes = ['scope1', 'scope2'];
39+
40+
// Attach the first token to the HTTP client
41+
$http1 = $authHandler->attachToken(
42+
$client,
43+
['access_token' => '1234'],
44+
$scopes
45+
);
46+
47+
// Call our middleware and verify the token is set
48+
$scopedMiddleware = $this->getGoogleAuthMiddleware($http1);
49+
$request = $scopedMiddleware(new Request('GET', '/'), ['auth' => 'scoped']);
50+
$this->assertEquals(['Bearer 1234'], $request->getHeader('Authorization'));
51+
52+
// Attach a new token to the HTTP client
53+
$http2 = $authHandler->attachToken(
54+
$client,
55+
['access_token' => '5678'],
56+
$scopes
57+
);
58+
59+
// Call our middleware and verify the NEW token is set
60+
$scopedMiddleware = $this->getGoogleAuthMiddleware($http2);
61+
$request = $scopedMiddleware(new Request('GET', '/'), ['auth' => 'scoped']);
62+
$this->assertEquals(['Bearer 5678'], $request->getHeader('Authorization'));
63+
}
64+
65+
private function getGoogleAuthMiddleware(Client $http)
66+
{
67+
// All sorts of horrible reflection to get at our middleware
68+
$handler = $http->getConfig()['handler'];
69+
$reflectionMethod = new \ReflectionMethod($handler, 'findByName');
70+
$reflectionMethod->setAccessible(true);
71+
$authMiddlewareIdx = $reflectionMethod->invoke($handler, 'google_auth');
72+
73+
$reflectionProperty = new \ReflectionProperty($handler, 'stack');
74+
$reflectionProperty->setAccessible(true);
75+
$stack = $reflectionProperty->getValue($handler);
76+
77+
$callable = $stack[$authMiddlewareIdx][0];
78+
return $callable(function ($request) {
79+
return $request;
80+
});
81+
}
82+
}

0 commit comments

Comments
 (0)