No Description

FetchAuthTokenTest.php 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /*
  3. * Copyright 2010 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\Credentials\AppIdentityCredentials;
  19. use Google\Auth\Credentials\GCECredentials;
  20. use Google\Auth\Credentials\ServiceAccountCredentials;
  21. use Google\Auth\Credentials\ServiceAccountJwtAccessCredentials;
  22. use Google\Auth\Credentials\UserRefreshCredentials;
  23. use Google\Auth\CredentialsLoader;
  24. use Google\Auth\FetchAuthTokenInterface;
  25. use Google\Auth\OAuth2;
  26. class FetchAuthTokenTest extends BaseTest
  27. {
  28. private $scopes = ['https://www.googleapis.com/auth/drive.readonly'];
  29. /** @dataProvider provideMakeHttpClient */
  30. public function testMakeHttpClient($fetcherClass)
  31. {
  32. $mockFetcher = $this->getMockBuilder($fetcherClass)
  33. ->disableOriginalConstructor()
  34. ->getMock();
  35. $mockFetcher
  36. ->expects($this->once())
  37. ->method('fetchAuthToken')
  38. ->will($this->returnCallback(function ($httpHandler) {
  39. return $httpHandler();
  40. }));
  41. $httpHandlerCalled = false;
  42. $httpHandler = function () use (&$httpHandlerCalled) {
  43. $httpHandlerCalled = true;
  44. return ['access_token' => 'xyz'];
  45. };
  46. $tokenCallbackCalled = false;
  47. $tokenCallback = function ($cacheKey, $accessToken) use (&$tokenCallbackCalled) {
  48. $tokenCallbackCalled = true;
  49. $this->assertEquals('xyz', $accessToken);
  50. };
  51. $client = CredentialsLoader::makeHttpClient(
  52. $mockFetcher,
  53. [
  54. 'base_url' => 'https://www.googleapis.com/books/v1/',
  55. 'base_uri' => 'https://www.googleapis.com/books/v1/',
  56. 'exceptions' => false,
  57. 'defaults' => ['exceptions' => false]
  58. ],
  59. $httpHandler,
  60. $tokenCallback
  61. );
  62. $response = $client->get(
  63. 'volumes?q=Henry+David+Thoreau&country=US'
  64. );
  65. $this->assertEquals(401, $response->getStatusCode());
  66. $this->assertTrue($httpHandlerCalled);
  67. $this->assertTrue($tokenCallbackCalled);
  68. }
  69. public function provideMakeHttpClient()
  70. {
  71. return [
  72. ['Google\Auth\Credentials\AppIdentityCredentials'],
  73. ['Google\Auth\Credentials\GCECredentials'],
  74. ['Google\Auth\Credentials\ServiceAccountCredentials'],
  75. ['Google\Auth\Credentials\ServiceAccountJwtAccessCredentials'],
  76. ['Google\Auth\Credentials\UserRefreshCredentials'],
  77. ['Google\Auth\OAuth2'],
  78. ];
  79. }
  80. public function testAppIdentityCredentialsGetLastReceivedToken()
  81. {
  82. $class = new \ReflectionClass(
  83. 'Google\Auth\Credentials\AppIdentityCredentials'
  84. );
  85. $property = $class->getProperty('lastReceivedToken');
  86. $property->setAccessible(true);
  87. $credentials = new AppIdentityCredentials();
  88. $property->setValue($credentials, [
  89. 'access_token' => 'xyz',
  90. 'expiration_time' => strtotime('2001'),
  91. ]);
  92. $this->assertGetLastReceivedToken($credentials);
  93. }
  94. public function testGCECredentialsGetLastReceivedToken()
  95. {
  96. $class = new \ReflectionClass(
  97. 'Google\Auth\Credentials\GCECredentials'
  98. );
  99. $property = $class->getProperty('lastReceivedToken');
  100. $property->setAccessible(true);
  101. $credentials = new GCECredentials();
  102. $property->setValue($credentials, [
  103. 'access_token' => 'xyz',
  104. 'expires_at' => strtotime('2001'),
  105. ]);
  106. $this->assertGetLastReceivedToken($credentials);
  107. }
  108. public function testServiceAccountCredentialsGetLastReceivedToken()
  109. {
  110. $jsonPath = sprintf(
  111. '%s/fixtures/.config/%s',
  112. __DIR__,
  113. CredentialsLoader::WELL_KNOWN_PATH
  114. );
  115. $class = new \ReflectionClass(
  116. 'Google\Auth\Credentials\ServiceAccountCredentials'
  117. );
  118. $property = $class->getProperty('auth');
  119. $property->setAccessible(true);
  120. $credentials = new ServiceAccountCredentials($this->scopes, $jsonPath);
  121. $property->setValue($credentials, $this->getOAuth2Mock());
  122. $this->assertGetLastReceivedToken($credentials);
  123. }
  124. public function testServiceAccountJwtAccessCredentialsGetLastReceivedToken()
  125. {
  126. $jsonPath = sprintf(
  127. '%s/fixtures/.config/%s',
  128. __DIR__,
  129. CredentialsLoader::WELL_KNOWN_PATH
  130. );
  131. $class = new \ReflectionClass(
  132. 'Google\Auth\Credentials\ServiceAccountJwtAccessCredentials'
  133. );
  134. $property = $class->getProperty('auth');
  135. $property->setAccessible(true);
  136. $credentials = new ServiceAccountJwtAccessCredentials($jsonPath);
  137. $property->setValue($credentials, $this->getOAuth2Mock());
  138. $this->assertGetLastReceivedToken($credentials);
  139. }
  140. public function testUserRefreshCredentialsGetLastReceivedToken()
  141. {
  142. $jsonPath = sprintf(
  143. '%s/fixtures2/.config/%s',
  144. __DIR__,
  145. CredentialsLoader::WELL_KNOWN_PATH
  146. );
  147. $class = new \ReflectionClass(
  148. 'Google\Auth\Credentials\UserRefreshCredentials'
  149. );
  150. $property = $class->getProperty('auth');
  151. $property->setAccessible(true);
  152. $credentials = new UserRefreshCredentials($this->scopes, $jsonPath);
  153. $property->setValue($credentials, $this->getOAuth2Mock());
  154. $this->assertGetLastReceivedToken($credentials);
  155. }
  156. private function getOAuth2()
  157. {
  158. $oauth = new OAuth2([
  159. 'access_token' => 'xyz',
  160. 'expires_at' => strtotime('2001'),
  161. ]);
  162. $this->assertGetLastReceivedToken($oauth);
  163. }
  164. private function getOAuth2Mock()
  165. {
  166. $mock = $this->getMockBuilder('Google\Auth\OAuth2')
  167. ->disableOriginalConstructor()
  168. ->getMock();
  169. $mock
  170. ->expects($this->once())
  171. ->method('getLastReceivedToken')
  172. ->will($this->returnValue([
  173. 'access_token' => 'xyz',
  174. 'expires_at' => strtotime('2001'),
  175. ]));
  176. return $mock;
  177. }
  178. private function assertGetLastReceivedToken(FetchAuthTokenInterface $fetcher)
  179. {
  180. $accessToken = $fetcher->getLastReceivedToken();
  181. $this->assertNotNull($accessToken);
  182. $this->assertArrayHasKey('access_token', $accessToken);
  183. $this->assertArrayHasKey('expires_at', $accessToken);
  184. $this->assertEquals('xyz', $accessToken['access_token']);
  185. $this->assertEquals(strtotime('2001'), $accessToken['expires_at']);
  186. }
  187. }