AuthSubTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Gdata
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id $
  21. */
  22. require_once 'Zend/Gdata/AuthSub.php';
  23. require_once 'Zend/Gdata/HttpClient.php';
  24. /**
  25. * @category Zend
  26. * @package Zend_Gdata
  27. * @subpackage UnitTests
  28. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. * @group Zend_Gdata
  31. * @group Zend_Gdata_AuthSub
  32. */
  33. class Zend_Gdata_AuthSubTest extends PHPUnit_Framework_TestCase
  34. {
  35. /**
  36. * Dummy token used during testing
  37. * @var type string
  38. */
  39. protected $token = 'DQAAFPHOW7DCTN';
  40. public function setUp()
  41. {
  42. }
  43. public function testNormalGetAuthSubTokenUri()
  44. {
  45. $uri = Zend_Gdata_AuthSub::getAuthSubTokenUri(
  46. 'http://www.example.com/foo.php', //next
  47. 'http://www.google.com/calendar/feeds', //scope
  48. 0, //secure
  49. 1); //session
  50. // Note: the scope here is not encoded. It should be encoded,
  51. // but the method getAuthSubTokenUri calls urldecode($scope).
  52. // This currently works (no reported bugs) as web browsers will
  53. // handle the encoding in most cases.
  54. $this->assertEquals('https://www.google.com/accounts/AuthSubRequest?next=http%3A%2F%2Fwww.example.com%2Ffoo.php&scope=http://www.google.com/calendar/feeds&secure=0&session=1', $uri);
  55. }
  56. public function testGetAuthSubTokenUriModifiedBase()
  57. {
  58. $uri = Zend_Gdata_AuthSub::getAuthSubTokenUri(
  59. 'http://www.example.com/foo.php', //next
  60. 'http://www.google.com/calendar/feeds', //scope
  61. 0, //secure
  62. 1, //session
  63. 'http://www.otherauthservice.com/accounts/AuthSubRequest');
  64. // Note: the scope here is not encoded. It should be encoded,
  65. // but the method getAuthSubTokenUri calls urldecode($scope).
  66. // This currently works (no reported bugs) as web browsers will
  67. // handle the encoding in most cases.
  68. $this->assertEquals('http://www.otherauthservice.com/accounts/AuthSubRequest?next=http%3A%2F%2Fwww.example.com%2Ffoo.php&scope=http://www.google.com/calendar/feeds&secure=0&session=1', $uri);
  69. }
  70. public function testSecureAuthSubSigning()
  71. {
  72. if (!extension_loaded('openssl')) {
  73. $this->markTestSkipped('The openssl extension is not available');
  74. } else {
  75. $c = new Zend_Gdata_HttpClient();
  76. $c->setAuthSubPrivateKeyFile("Zend/Gdata/_files/RsaKey.pem",
  77. null, true);
  78. $c->setAuthSubToken('abcdefg');
  79. $requestData = $c->filterHttpRequest('POST',
  80. 'http://www.example.com/feed',
  81. array(),
  82. 'foo bar',
  83. 'text/plain');
  84. $authHeaderCheckPassed = false;
  85. $headers = $requestData['headers'];
  86. foreach ($headers as $headerName => $headerValue) {
  87. if (strtolower($headerName) == 'authorization') {
  88. preg_match('/data="([^"]*)"/', $headerValue, $matches);
  89. $dataToSign = $matches[1];
  90. preg_match('/sig="([^"]*)"/', $headerValue, $matches);
  91. $sig = $matches[1];
  92. if (function_exists('openssl_verify')) {
  93. $fp = fopen('Zend/Gdata/_files/RsaCert.pem', 'r', true);
  94. $cert = '';
  95. while (!feof($fp)) {
  96. $cert .= fread($fp, 8192);
  97. }
  98. fclose($fp);
  99. $pubkeyid = openssl_get_publickey($cert);
  100. $verified = openssl_verify($dataToSign,
  101. base64_decode($sig), $pubkeyid);
  102. $this->assertEquals(
  103. 1, $verified,
  104. 'The generated signature was unable ' .
  105. 'to be verified.');
  106. $authHeaderCheckPassed = true;
  107. }
  108. }
  109. }
  110. $this->assertEquals(true, $authHeaderCheckPassed,
  111. 'Auth header not found for sig verification.');
  112. }
  113. }
  114. public function testPrivateKeyNotFound()
  115. {
  116. $this->setExpectedException('Zend_Gdata_App_InvalidArgumentException');
  117. if (!extension_loaded('openssl')) {
  118. $this->markTestSkipped('The openssl extension is not available');
  119. } else {
  120. $c = new Zend_Gdata_HttpClient();
  121. $c->setAuthSubPrivateKeyFile("zendauthsubfilenotfound", null, true);
  122. }
  123. }
  124. public function testAuthSubSessionTokenReceivesSuccessfulResult()
  125. {
  126. $adapter = new Zend_Http_Client_Adapter_Test();
  127. $adapter->setResponse("HTTP/1.1 200 OK\r\n\r\nToken={$this->token}\r\nExpiration=20201004T123456Z");
  128. $client = new Zend_Gdata_HttpClient();
  129. $client->setUri('http://example.com/AuthSub');
  130. $client->setAdapter($adapter);
  131. $respToken = Zend_Gdata_AuthSub::getAuthSubSessionToken($this->token, $client);
  132. $this->assertEquals($this->token, $respToken);
  133. }
  134. /**
  135. * @expectedException Zend_Gdata_App_AuthException
  136. */
  137. public function testAuthSubSessionTokenCatchesFailedResult()
  138. {
  139. $adapter = new Zend_Http_Client_Adapter_Test();
  140. $adapter->setResponse("HTTP/1.1 500 Internal Server Error\r\n\r\nInternal Server Error");
  141. $client = new Zend_Gdata_HttpClient();
  142. $client->setUri('http://example.com/AuthSub');
  143. $client->setAdapter($adapter);
  144. $newtok = Zend_Gdata_AuthSub::getAuthSubSessionToken($this->token, $client);
  145. }
  146. /**
  147. * @expectedException Zend_Gdata_App_HttpException
  148. */
  149. public function testAuthSubSessionTokenCatchesHttpClientException()
  150. {
  151. $adapter = new Zend_Http_Client_Adapter_Test();
  152. $adapter->setNextRequestWillFail(true);
  153. $client = new Zend_Gdata_HttpClient();
  154. $client->setUri('http://example.com/AuthSub');
  155. $client->setAdapter($adapter);
  156. $newtok = Zend_Gdata_AuthSub::getAuthSubSessionToken($this->token, $client);
  157. }
  158. public function testAuthSubRevokeTokenReceivesSuccessfulResult()
  159. {
  160. $adapter = new Zend_Http_Client_Adapter_Test();
  161. $adapter->setResponse("HTTP/1.1 200 OK");
  162. $client = new Zend_Gdata_HttpClient();
  163. $client->setUri('http://example.com/AuthSub');
  164. $client->setAdapter($adapter);
  165. $revoked = Zend_Gdata_AuthSub::AuthSubRevokeToken($this->token, $client);
  166. $this->assertTrue($revoked);
  167. }
  168. public function testAuthSubRevokeTokenCatchesFailedResult()
  169. {
  170. $adapter = new Zend_Http_Client_Adapter_Test();
  171. $adapter->setResponse("HTTP/1.1 500 Not Successful");
  172. $client = new Zend_Gdata_HttpClient();
  173. $client->setUri('http://example.com/AuthSub');
  174. $client->setAdapter($adapter);
  175. $revoked = Zend_Gdata_AuthSub::AuthSubRevokeToken($this->token, $client);
  176. $this->assertFalse($revoked);
  177. }
  178. /**
  179. * @expectedException Zend_Gdata_App_HttpException
  180. */
  181. public function testAuthSubRevokeTokenCatchesHttpClientException()
  182. {
  183. $adapter = new Zend_Http_Client_Adapter_Test();
  184. $adapter->setNextRequestWillFail(true);
  185. $client = new Zend_Gdata_HttpClient();
  186. $client->setUri('http://example.com/AuthSub');
  187. $client->setAdapter($adapter);
  188. $revoked = Zend_Gdata_AuthSub::AuthSubRevokeToken($this->token, $client);
  189. }
  190. public function testGetAuthSubTokenInfoReceivesSuccessfulResult()
  191. {
  192. $adapter = new Zend_Http_Client_Adapter_Test();
  193. $response = "HTTP/1.1 200 OK\r\n\r\nTarget=http://example.com\nScope=http://example.com\nSecure=false";
  194. $adapter->setResponse($response);
  195. $client = new Zend_Gdata_HttpClient();
  196. $client->setUri('http://example.com/AuthSub');
  197. $client->setAdapter($adapter);
  198. $respBody = Zend_Gdata_AuthSub::getAuthSubTokenInfo($this->token, $client);
  199. $this->assertContains("Target=http://example.com", $respBody);
  200. $this->assertContains("Scope=http://example.com", $respBody);
  201. $this->assertContains("Secure=false", $respBody);
  202. }
  203. /**
  204. * @expectedException Zend_Gdata_App_HttpException
  205. */
  206. public function testGetAuthSubTokenInfoCatchesHttpClientException()
  207. {
  208. $adapter = new Zend_Http_Client_Adapter_Test();
  209. $adapter->setNextRequestWillFail(true);
  210. $client = new Zend_Gdata_HttpClient();
  211. $client->setUri('http://example.com/AuthSub');
  212. $client->setAdapter($adapter);
  213. $revoked = Zend_Gdata_AuthSub::getAuthSubTokenInfo($this->token, $client);
  214. }
  215. public function testGetHttpClientProvidesNewClientWhenNullPassed()
  216. {
  217. $client = Zend_Gdata_AuthSub::getHttpClient($this->token);
  218. $this->assertTrue($client instanceof Zend_Gdata_HttpClient );
  219. $this->assertEquals($this->token, $client->getAuthSubToken());
  220. }
  221. /**
  222. * @group ZF-11351
  223. * @expectedException Zend_Gdata_App_HttpException
  224. */
  225. public function testAuthSubGetHttpClientShouldThrowExceptionOnVanillaHttpClient()
  226. {
  227. $client = new Zend_Http_Client();
  228. $client->setUri('http://example.com/AuthSub');
  229. $gdclient = Zend_Gdata_AuthSub::getHttpClient('FakeToken', $client);
  230. $this->fail('Expected exception Zend_Gdata_App_HttpException not raised!');
  231. }
  232. }