No Description

AuthTokenMiddlewareTest.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <?php
  2. /*
  3. * Copyright 2015 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. namespace Google\Auth\Tests;
  18. use Google\Auth\FetchAuthTokenCache;
  19. use Google\Auth\Middleware\AuthTokenMiddleware;
  20. use GuzzleHttp\Handler\MockHandler;
  21. use GuzzleHttp\Psr7\Response;
  22. class AuthTokenMiddlewareTest extends BaseTest
  23. {
  24. private $mockFetcher;
  25. private $mockCacheItem;
  26. private $mockCache;
  27. private $mockRequest;
  28. protected function setUp()
  29. {
  30. $this->onlyGuzzle6();
  31. $this->mockFetcher =
  32. $this
  33. ->getMockBuilder('Google\Auth\FetchAuthTokenInterface')
  34. ->getMock();
  35. $this->mockCacheItem =
  36. $this
  37. ->getMockBuilder('Psr\Cache\CacheItemInterface')
  38. ->getMock();
  39. $this->mockCache =
  40. $this
  41. ->getMockBuilder('Psr\Cache\CacheItemPoolInterface')
  42. ->getMock();
  43. $this->mockRequest =
  44. $this
  45. ->getMockBuilder('GuzzleHttp\Psr7\Request')
  46. ->disableOriginalConstructor()
  47. ->getMock();
  48. }
  49. public function testOnlyTouchesWhenAuthConfigScoped()
  50. {
  51. $this->mockFetcher
  52. ->expects($this->any())
  53. ->method('fetchAuthToken')
  54. ->will($this->returnValue([]));
  55. $this->mockRequest
  56. ->expects($this->never())
  57. ->method('withHeader');
  58. $middleware = new AuthTokenMiddleware($this->mockFetcher);
  59. $mock = new MockHandler([new Response(200)]);
  60. $callable = $middleware($mock);
  61. $callable($this->mockRequest, ['auth' => 'not_google_auth']);
  62. }
  63. public function testAddsTheTokenAsAnAuthorizationHeader()
  64. {
  65. $authResult = ['access_token' => '1/abcdef1234567890'];
  66. $this->mockFetcher
  67. ->expects($this->once())
  68. ->method('fetchAuthToken')
  69. ->will($this->returnValue($authResult));
  70. $this->mockRequest
  71. ->expects($this->once())
  72. ->method('withHeader')
  73. ->with('authorization', 'Bearer ' . $authResult['access_token'])
  74. ->will($this->returnValue($this->mockRequest));
  75. // Run the test.
  76. $middleware = new AuthTokenMiddleware($this->mockFetcher);
  77. $mock = new MockHandler([new Response(200)]);
  78. $callable = $middleware($mock);
  79. $callable($this->mockRequest, ['auth' => 'google_auth']);
  80. }
  81. public function testDoesNotAddAnAuthorizationHeaderOnNoAccessToken()
  82. {
  83. $authResult = ['not_access_token' => '1/abcdef1234567890'];
  84. $this->mockFetcher
  85. ->expects($this->once())
  86. ->method('fetchAuthToken')
  87. ->will($this->returnValue($authResult));
  88. $this->mockRequest
  89. ->expects($this->once())
  90. ->method('withHeader')
  91. ->with('authorization', 'Bearer ')
  92. ->will($this->returnValue($this->mockRequest));
  93. // Run the test.
  94. $middleware = new AuthTokenMiddleware($this->mockFetcher);
  95. $mock = new MockHandler([new Response(200)]);
  96. $callable = $middleware($mock);
  97. $callable($this->mockRequest, ['auth' => 'google_auth']);
  98. }
  99. public function testUsesCachedAuthToken()
  100. {
  101. $cacheKey = 'myKey';
  102. $cachedValue = '2/abcdef1234567890';
  103. $this->mockCacheItem
  104. ->expects($this->once())
  105. ->method('isHit')
  106. ->will($this->returnValue(true));
  107. $this->mockCacheItem
  108. ->expects($this->once())
  109. ->method('get')
  110. ->will($this->returnValue($cachedValue));
  111. $this->mockCache
  112. ->expects($this->once())
  113. ->method('getItem')
  114. ->with($this->equalTo($cacheKey))
  115. ->will($this->returnValue($this->mockCacheItem));
  116. $this->mockFetcher
  117. ->expects($this->never())
  118. ->method('fetchAuthToken');
  119. $this->mockFetcher
  120. ->expects($this->any())
  121. ->method('getCacheKey')
  122. ->will($this->returnValue($cacheKey));
  123. $this->mockRequest
  124. ->expects($this->once())
  125. ->method('withHeader')
  126. ->with('authorization', 'Bearer ' . $cachedValue)
  127. ->will($this->returnValue($this->mockRequest));
  128. // Run the test.
  129. $cachedFetcher = new FetchAuthTokenCache(
  130. $this->mockFetcher,
  131. null,
  132. $this->mockCache
  133. );
  134. $middleware = new AuthTokenMiddleware($cachedFetcher);
  135. $mock = new MockHandler([new Response(200)]);
  136. $callable = $middleware($mock);
  137. $callable($this->mockRequest, ['auth' => 'google_auth']);
  138. }
  139. public function testGetsCachedAuthTokenUsingCacheOptions()
  140. {
  141. $prefix = 'test_prefix_';
  142. $cacheKey = 'myKey';
  143. $cachedValue = '2/abcdef1234567890';
  144. $this->mockCacheItem
  145. ->expects($this->once())
  146. ->method('isHit')
  147. ->will($this->returnValue(true));
  148. $this->mockCacheItem
  149. ->expects($this->once())
  150. ->method('get')
  151. ->will($this->returnValue($cachedValue));
  152. $this->mockCache
  153. ->expects($this->once())
  154. ->method('getItem')
  155. ->with($this->equalTo($prefix . $cacheKey))
  156. ->will($this->returnValue($this->mockCacheItem));
  157. $this->mockFetcher
  158. ->expects($this->never())
  159. ->method('fetchAuthToken');
  160. $this->mockFetcher
  161. ->expects($this->any())
  162. ->method('getCacheKey')
  163. ->will($this->returnValue($cacheKey));
  164. $this->mockRequest
  165. ->expects($this->once())
  166. ->method('withHeader')
  167. ->with('authorization', 'Bearer ' . $cachedValue)
  168. ->will($this->returnValue($this->mockRequest));
  169. // Run the test.
  170. $cachedFetcher = new FetchAuthTokenCache(
  171. $this->mockFetcher,
  172. ['prefix' => $prefix],
  173. $this->mockCache
  174. );
  175. $middleware = new AuthTokenMiddleware($cachedFetcher);
  176. $mock = new MockHandler([new Response(200)]);
  177. $callable = $middleware($mock);
  178. $callable($this->mockRequest, ['auth' => 'google_auth']);
  179. }
  180. public function testShouldSaveValueInCacheWithSpecifiedPrefix()
  181. {
  182. $prefix = 'test_prefix_';
  183. $lifetime = '70707';
  184. $cacheKey = 'myKey';
  185. $token = '1/abcdef1234567890';
  186. $authResult = ['access_token' => $token];
  187. $this->mockCacheItem
  188. ->expects($this->any())
  189. ->method('get')
  190. ->will($this->returnValue(null));
  191. $this->mockCacheItem
  192. ->expects($this->once())
  193. ->method('set')
  194. ->with($this->equalTo($token))
  195. ->will($this->returnValue(false));
  196. $this->mockCacheItem
  197. ->expects($this->once())
  198. ->method('expiresAfter')
  199. ->with($this->equalTo($lifetime));
  200. $this->mockCache
  201. ->expects($this->any())
  202. ->method('getItem')
  203. ->with($this->equalTo($prefix . $cacheKey))
  204. ->will($this->returnValue($this->mockCacheItem));
  205. $this->mockFetcher
  206. ->expects($this->any())
  207. ->method('getCacheKey')
  208. ->will($this->returnValue($cacheKey));
  209. $this->mockFetcher
  210. ->expects($this->once())
  211. ->method('fetchAuthToken')
  212. ->will($this->returnValue($authResult));
  213. $this->mockRequest
  214. ->expects($this->once())
  215. ->method('withHeader')
  216. ->with('authorization', 'Bearer ' . $token)
  217. ->will($this->returnValue($this->mockRequest));
  218. // Run the test.
  219. $cachedFetcher = new FetchAuthTokenCache(
  220. $this->mockFetcher,
  221. ['prefix' => $prefix, 'lifetime' => $lifetime],
  222. $this->mockCache
  223. );
  224. $middleware = new AuthTokenMiddleware($cachedFetcher);
  225. $mock = new MockHandler([new Response(200)]);
  226. $callable = $middleware($mock);
  227. $callable($this->mockRequest, ['auth' => 'google_auth']);
  228. }
  229. /** @dataProvider provideShouldNotifyTokenCallback */
  230. public function testShouldNotifyTokenCallback(callable $tokenCallback)
  231. {
  232. $prefix = 'test_prefix_';
  233. $cacheKey = 'myKey';
  234. $token = '1/abcdef1234567890';
  235. $authResult = ['access_token' => $token];
  236. $this->mockCacheItem
  237. ->expects($this->any())
  238. ->method('get')
  239. ->will($this->returnValue(null));
  240. $this->mockCache
  241. ->expects($this->any())
  242. ->method('getItem')
  243. ->with($this->equalTo($prefix . $cacheKey))
  244. ->will($this->returnValue($this->mockCacheItem));
  245. $this->mockFetcher
  246. ->expects($this->any())
  247. ->method('getCacheKey')
  248. ->will($this->returnValue($cacheKey));
  249. $this->mockFetcher
  250. ->expects($this->once())
  251. ->method('fetchAuthToken')
  252. ->will($this->returnValue($authResult));
  253. $this->mockRequest
  254. ->expects($this->once())
  255. ->method('withHeader')
  256. ->will($this->returnValue($this->mockRequest));
  257. MiddlewareCallback::$expectedKey = $this->getValidKeyName($prefix . $cacheKey);
  258. MiddlewareCallback::$expectedValue = $token;
  259. MiddlewareCallback::$called = false;
  260. // Run the test.
  261. $cachedFetcher = new FetchAuthTokenCache(
  262. $this->mockFetcher,
  263. ['prefix' => $prefix],
  264. $this->mockCache
  265. );
  266. $middleware = new AuthTokenMiddleware(
  267. $cachedFetcher,
  268. null,
  269. $tokenCallback
  270. );
  271. $mock = new MockHandler([new Response(200)]);
  272. $callable = $middleware($mock);
  273. $callable($this->mockRequest, ['auth' => 'google_auth']);
  274. $this->assertTrue(MiddlewareCallback::$called);
  275. }
  276. public function provideShouldNotifyTokenCallback()
  277. {
  278. MiddlewareCallback::$phpunit = $this;
  279. $anonymousFunc = function ($key, $value) {
  280. MiddlewareCallback::staticInvoke($key, $value);
  281. };
  282. return [
  283. ['Google\Auth\Tests\MiddlewareCallbackFunction'],
  284. ['Google\Auth\Tests\MiddlewareCallback::staticInvoke'],
  285. [['Google\Auth\Tests\MiddlewareCallback', 'staticInvoke']],
  286. [$anonymousFunc],
  287. [[new MiddlewareCallback, 'staticInvoke']],
  288. [[new MiddlewareCallback, 'methodInvoke']],
  289. [new MiddlewareCallback],
  290. ];
  291. }
  292. }
  293. class MiddlewareCallback
  294. {
  295. public static $phpunit;
  296. public static $expectedKey;
  297. public static $expectedValue;
  298. public static $called = false;
  299. public function __invoke($key, $value)
  300. {
  301. self::$phpunit->assertEquals(self::$expectedKey, $key);
  302. self::$phpunit->assertEquals(self::$expectedValue, $value);
  303. self::$called = true;
  304. }
  305. public function methodInvoke($key, $value)
  306. {
  307. return $this($key, $value);
  308. }
  309. public static function staticInvoke($key, $value)
  310. {
  311. $instance = new self();
  312. return $instance($key, $value);
  313. }
  314. }
  315. function MiddlewareCallbackFunction($key, $value)
  316. {
  317. return MiddlewareCallback::staticInvoke($key, $value);
  318. }