ApiOAuth2Test

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.

Defined (1)

The class is defined in the following location(s).

/vendor/google/apiclient/tests/general/ApiOAuth2Test.php  
  1. class ApiOAuth2Test extends BaseTest 
  2.  
  3. public function testSign() 
  4. $client = $this->getClient(); 
  5. $oauth = new Google_Auth_OAuth2($client); 
  6.  
  7. $client->setClientId('clientId1'); 
  8. $client->setClientSecret('clientSecret1'); 
  9. $client->setRedirectUri('http://localhost'); 
  10. $client->setDeveloperKey('devKey'); 
  11. $client->setAccessType('offline'); 
  12. $client->setApprovalPrompt('force'); 
  13. $client->setRequestVisibleActions('http://foo'); 
  14.  
  15. $req = new Google_Http_Request('http://localhost'); 
  16. $req = $oauth->sign($req); 
  17.  
  18. $this->assertEquals('http://localhost?key=devKey', $req->getUrl()); 
  19.  
  20. // test accessToken 
  21. $oauth->setAccessToken( 
  22. json_encode( 
  23. array( 
  24. 'access_token' => 'ACCESS_TOKEN',  
  25. 'created' => time(),  
  26. 'expires_in' => '3600' 
  27. ); 
  28.  
  29. $req = $oauth->sign($req); 
  30. $auth = $req->getRequestHeader('authorization'); 
  31. $this->assertEquals('Bearer ACCESS_TOKEN', $auth); 
  32.  
  33. public function testRevokeAccess() 
  34. $accessToken = "ACCESS_TOKEN"; 
  35. $refreshToken = "REFRESH_TOKEN"; 
  36. $accessToken2 = "ACCESS_TOKEN_2"; 
  37. $token = ""; 
  38.  
  39. $client = $this->getClient(); 
  40. $response = $this->getMock("Google_Http_Request", array(), array('')); 
  41. $response->expects($this->any()) 
  42. ->method('getResponseHttpCode') 
  43. ->will($this->returnValue(200)); 
  44. $io = $this->getMock("Google_IO_Stream", array(), array($client)); 
  45. $io->expects($this->any()) 
  46. ->method('makeRequest') 
  47. ->will( 
  48. $this->returnCallback( 
  49. function ($request) use (&$token, $response) { 
  50. $elements = array(); 
  51. parse_str($request->getPostBody(), $elements); 
  52. $token = isset($elements['token']) ? $elements['token'] : null; 
  53. return $response; 
  54. ); 
  55. $client->setIo($io); 
  56.  
  57. // Test with access token. 
  58. $oauth = new Google_Auth_OAuth2($client); 
  59. $oauth->setAccessToken( 
  60. json_encode( 
  61. array( 
  62. 'access_token' => $accessToken,  
  63. 'created' => time(),  
  64. 'expires_in' => '3600' 
  65. ); 
  66. $this->assertTrue($oauth->revokeToken()); 
  67. $this->assertEquals($accessToken, $token); 
  68.  
  69. // Test with refresh token. 
  70. $oauth = new Google_Auth_OAuth2($client); 
  71. $oauth->setAccessToken( 
  72. json_encode( 
  73. array( 
  74. 'access_token' => $accessToken,  
  75. 'refresh_token' => $refreshToken,  
  76. 'created' => time(),  
  77. 'expires_in' => '3600' 
  78. ); 
  79. $this->assertTrue($oauth->revokeToken()); 
  80. $this->assertEquals($refreshToken, $token); 
  81.  
  82. // Test with passed in token. 
  83. $this->assertTrue($oauth->revokeToken($accessToken2)); 
  84. $this->assertEquals($accessToken2, $token); 
  85.  
  86. public function testCreateAuthUrl() 
  87. $client = $this->getClient(); 
  88. $oauth = new Google_Auth_OAuth2($client); 
  89.  
  90. $client->setClientId('clientId1'); 
  91. $client->setClientSecret('clientSecret1'); 
  92. $client->setRedirectUri('http://localhost'); 
  93. $client->setDeveloperKey('devKey'); 
  94. $client->setAccessType('offline'); 
  95. $client->setApprovalPrompt('force'); 
  96. $client->setRequestVisibleActions(array('http://foo')); 
  97. $client->setLoginHint("bob@example.org"); 
  98.  
  99. $authUrl = $oauth->createAuthUrl("http://googleapis.com/scope/foo"); 
  100. $expected = "https://accounts.google.com/o/oauth2/auth" 
  101. . "?response_type=code" 
  102. . "&redirect_uri=http%3A%2F%2Flocalhost" 
  103. . "&client_id=clientId1" 
  104. . "&scope=http%3A%2F%2Fgoogleapis.com%2Fscope%2Ffoo" 
  105. . "&access_type=offline" 
  106. . "&approval_prompt=force" 
  107. . "&login_hint=bob%40example.org"; 
  108. $this->assertEquals($expected, $authUrl); 
  109.  
  110. // Again with a blank login hint (should remove all traces from authUrl) 
  111. $client->setLoginHint(""); 
  112. $client->setHostedDomain("example.com"); 
  113. $client->setOpenidRealm("example.com"); 
  114. $client->setPrompt("select_account"); 
  115. $client->setIncludeGrantedScopes(true); 
  116. $authUrl = $oauth->createAuthUrl("http://googleapis.com/scope/foo"); 
  117. $expected = "https://accounts.google.com/o/oauth2/auth" 
  118. . "?response_type=code" 
  119. . "&redirect_uri=http%3A%2F%2Flocalhost" 
  120. . "&client_id=clientId1" 
  121. . "&scope=http%3A%2F%2Fgoogleapis.com%2Fscope%2Ffoo" 
  122. . "&access_type=offline" 
  123. . "&prompt=select_account" 
  124. . "&hd=example.com" 
  125. . "&openid.realm=example.com" 
  126. . "&include_granted_scopes=true"; 
  127. $this->assertEquals($expected, $authUrl); 
  128.  
  129. /** 
  130. * Most of the logic for ID token validation is in AuthTest - 
  131. * this is just a general check to ensure we verify a valid 
  132. * id token if one exists. 
  133. */ 
  134. public function testValidateIdToken() 
  135. if (!$this->checkToken()) { 
  136. return; 
  137.  
  138. $client = $this->getClient(); 
  139. $token = json_decode($client->getAccessToken()); 
  140. $segments = explode(".", $token->id_token); 
  141. $this->assertEquals(3, count($segments)); 
  142. // Extract the client ID in this case as it wont be set on the test client. 
  143. $data = json_decode(Google_Utils::urlSafeB64Decode($segments[1])); 
  144. $oauth = new Google_Auth_OAuth2($client); 
  145. $ticket = $oauth->verifyIdToken($token->id_token, $data->aud); 
  146. $this->assertInstanceOf( 
  147. "Google_Auth_LoginTicket",  
  148. $ticket 
  149. ); 
  150. $this->assertTrue(strlen($ticket->getUserId()) > 0); 
  151.  
  152. // TODO: Need to be smart about testing/disabling the 
  153. // caching for this test to make sense. Not sure how to do that 
  154. // at the moment. 
  155. $client = $this->getClient(); 
  156. $client->setIo(new Google_IO_Stream($client)); 
  157. $data = json_decode(Google_Utils::urlSafeB64Decode($segments[1])); 
  158. $oauth = new Google_Auth_OAuth2($client); 
  159. $this->assertInstanceOf( 
  160. "Google_Auth_LoginTicket",  
  161. $oauth->verifyIdToken($token->id_token, $data->aud) 
  162. ); 
  163.  
  164. /** 
  165. * Test for revoking token when none is opened 
  166. */ 
  167. public function testRevokeWhenNoTokenExists() 
  168. $client = new Google_Client(); 
  169. $this->assertFalse($client->revokeToken()); 
  170.  
  171. /** 
  172. * Test that the ID token is properly refreshed. 
  173. */ 
  174. public function testRefreshTokenSetsValues() 
  175. $client = new Google_Client(); 
  176. $response_data = json_encode( 
  177. array( 
  178. 'access_token' => "ACCESS_TOKEN",  
  179. 'id_token' => "ID_TOKEN",  
  180. 'expires_in' => "12345",  
  181. ); 
  182. $response = $this->getMock("Google_Http_Request", array(), array('')); 
  183. $response->expects($this->any()) 
  184. ->method('getResponseHttpCode') 
  185. ->will($this->returnValue(200)); 
  186. $response->expects($this->any()) 
  187. ->method('getResponseBody') 
  188. ->will($this->returnValue($response_data)); 
  189. $io = $this->getMock("Google_IO_Stream", array(), array($client)); 
  190. $io->expects($this->any()) 
  191. ->method('makeRequest') 
  192. ->will( 
  193. $this->returnCallback( 
  194. function ($request) use (&$token, $response) { 
  195. $elements = $request->getPostBody(); 
  196. PHPUnit_Framework_TestCase::assertEquals( 
  197. $elements['grant_type'],  
  198. "refresh_token" 
  199. ); 
  200. PHPUnit_Framework_TestCase::assertEquals( 
  201. $elements['refresh_token'],  
  202. "REFRESH_TOKEN" 
  203. ); 
  204. return $response; 
  205. ); 
  206. $client->setIo($io); 
  207. $oauth = new Google_Auth_OAuth2($client); 
  208. $oauth->refreshToken("REFRESH_TOKEN"); 
  209. $token = json_decode($oauth->getAccessToken(), true); 
  210. $this->assertEquals($token['id_token'], "ID_TOKEN");