Skip to content

Commit dc13e5e

Browse files
authored
fix: explicit token caching issue (#2358)
1 parent 76c312e commit dc13e5e

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed

src/AuthHandler/Guzzle6AuthHandler.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 82 additions & 0 deletions
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)