ConsumerTest.php 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  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_OpenId
  17. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * Zend_OpenId
  23. */
  24. require_once 'Zend/OpenId/Consumer.php';
  25. /**
  26. * PHPUnit test case
  27. */
  28. require_once 'PHPUnit/Framework.php';
  29. /**
  30. * Zend_OpenId_ResponseHelper
  31. */
  32. require_once 'Zend/OpenId/ResponseHelper.php';
  33. /**
  34. * Zend_OpenId_Consumer_Storage_File
  35. */
  36. require_once 'Zend/OpenId/Consumer/Storage/File.php';
  37. /**
  38. * Zend_OpenId_Extensions_Sreg
  39. */
  40. require_once 'Zend/OpenId/Extension/Sreg.php';
  41. /**
  42. * Zend_Http_Client_Adapter_Test
  43. */
  44. require_once 'Zend/Http/Client/Adapter/Test.php';
  45. /**
  46. * @package Zend_OpenId
  47. * @subpackage UnitTests
  48. */
  49. class Zend_OpenId_ConsumerTest extends PHPUnit_Framework_TestCase
  50. {
  51. const ID = "http://id.myopenid.com/";
  52. const REAL_ID = "http://real_id.myopenid.com/";
  53. const SERVER = "http://www.myopenid.com/";
  54. const HANDLE = "d41d8cd98f00b204e9800998ecf8427e";
  55. const MAC_FUNC = "sha256";
  56. const SECRET = "4fa03202081808bd19f92b667a291873";
  57. /**
  58. * testing login
  59. *
  60. */
  61. public function testLogin()
  62. {
  63. $expiresIn = time() + 600;
  64. $_SERVER['SCRIPT_URI'] = "http://www.zf-test.com/test.php";
  65. $storage = new Zend_OpenId_Consumer_Storage_File(dirname(__FILE__)."/_files/consumer");
  66. $storage->delDiscoveryInfo(self::ID);
  67. $this->assertTrue( $storage->addDiscoveryInfo(self::ID, self::REAL_ID, self::SERVER, 1.1, $expiresIn) );
  68. $storage->delAssociation(self::SERVER);
  69. $this->assertTrue( $storage->addAssociation(self::SERVER, self::HANDLE, self::MAC_FUNC, self::SECRET, $expiresIn) );
  70. $response = new Zend_OpenId_ResponseHelper(true);
  71. $consumer = new Zend_OpenId_Consumer($storage);
  72. $this->assertTrue( $consumer->login(self::ID, null, null, null, $response) );
  73. $headers = $response->getHeaders();
  74. $this->assertSame( '', $response->getBody() );
  75. $this->assertTrue( is_array($headers) );
  76. $this->assertSame( 1, count($headers) );
  77. $this->assertTrue( is_array($headers[0]) );
  78. $this->assertSame( 3, count($headers[0]) );
  79. $this->assertSame( 'Location', $headers[0]['name'] );
  80. $this->assertSame( true, $headers[0]['replace'] );
  81. $url = $headers[0]['value'];
  82. $url = parse_url($url);
  83. $this->assertSame( "http", $url['scheme'] );
  84. $this->assertSame( "www.myopenid.com", $url['host'] );
  85. $this->assertSame( "/", $url['path'] );
  86. $q = explode("&", $url['query']);
  87. $query = array();
  88. foreach($q as $var) {
  89. if (list($key, $val) = explode("=", $var, 2)) {
  90. $query[$key] = $val;
  91. }
  92. }
  93. $this->assertTrue( is_array($query) );
  94. $this->assertSame( 6, count($query) );
  95. $this->assertSame( 'checkid_setup', $query['openid.mode'] );
  96. $this->assertSame( 'http%3A%2F%2Freal_id.myopenid.com%2F', $query['openid.identity'] );
  97. $this->assertSame( 'http%3A%2F%2Fid.myopenid.com%2F', $query['openid.claimed_id'] );
  98. $this->assertSame( self::HANDLE, $query['openid.assoc_handle'] );
  99. $this->assertSame( 'http%3A%2F%2Fwww.zf-test.com%2Ftest.php', $query['openid.return_to'] );
  100. $this->assertSame( 'http%3A%2F%2Fwww.zf-test.com', $query['openid.trust_root'] );
  101. // Test user defined return_to and trust_root
  102. $response = new Zend_OpenId_ResponseHelper(true);
  103. $consumer = new Zend_OpenId_Consumer($storage);
  104. $this->assertTrue( $consumer->login(self::ID, "http://www.zf-test.com/return.php", "http://www.zf-test.com/trust.php", null, $response) );
  105. $headers = $response->getHeaders();
  106. $url = $headers[0]['value'];
  107. $url = parse_url($url);
  108. $q = explode("&", $url['query']);
  109. $query = array();
  110. foreach($q as $var) {
  111. if (list($key, $val) = explode("=", $var, 2)) {
  112. $query[$key] = $val;
  113. }
  114. }
  115. $this->assertTrue( is_array($query) );
  116. $this->assertSame( 6, count($query) );
  117. $this->assertSame( 'checkid_setup', $query['openid.mode'] );
  118. $this->assertSame( 'http%3A%2F%2Freal_id.myopenid.com%2F', $query['openid.identity'] );
  119. $this->assertSame( 'http%3A%2F%2Fid.myopenid.com%2F', $query['openid.claimed_id'] );
  120. $this->assertSame( self::HANDLE, $query['openid.assoc_handle'] );
  121. $this->assertSame( 'http%3A%2F%2Fwww.zf-test.com%2Freturn.php', $query['openid.return_to'] );
  122. $this->assertSame( 'http%3A%2F%2Fwww.zf-test.com%2Ftrust.php', $query['openid.trust_root'] );
  123. $storage->delDiscoveryInfo(self::ID);
  124. $this->assertTrue( $storage->addDiscoveryInfo(self::ID, self::REAL_ID, self::SERVER, 2.0, $expiresIn) );
  125. // Test login with OpenID 2.0
  126. $response = new Zend_OpenId_ResponseHelper(true);
  127. $consumer = new Zend_OpenId_Consumer($storage);
  128. $this->assertTrue( $consumer->login(self::ID, "http://www.zf-test.com/return.php", "http://www.zf-test.com/trust.php", null, $response) );
  129. $headers = $response->getHeaders();
  130. $url = $headers[0]['value'];
  131. $url = parse_url($url);
  132. $q = explode("&", $url['query']);
  133. $query = array();
  134. foreach($q as $var) {
  135. if (list($key, $val) = explode("=", $var, 2)) {
  136. $query[$key] = $val;
  137. }
  138. }
  139. $this->assertTrue( is_array($query) );
  140. $this->assertSame( 7, count($query) );
  141. $this->assertSame( 'http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0', $query['openid.ns'] );
  142. $this->assertSame( 'checkid_setup', $query['openid.mode'] );
  143. $this->assertSame( 'http%3A%2F%2Freal_id.myopenid.com%2F', $query['openid.identity'] );
  144. $this->assertSame( 'http%3A%2F%2Fid.myopenid.com%2F', $query['openid.claimed_id'] );
  145. $this->assertSame( self::HANDLE, $query['openid.assoc_handle'] );
  146. $this->assertSame( 'http%3A%2F%2Fwww.zf-test.com%2Freturn.php', $query['openid.return_to'] );
  147. $this->assertSame( 'http%3A%2F%2Fwww.zf-test.com%2Ftrust.php', $query['openid.realm'] );
  148. // Test login with SREG extension
  149. $ext = new Zend_OpenId_Extension_Sreg(array("nickname"=>true,"email"=>false));
  150. $response = new Zend_OpenId_ResponseHelper(true);
  151. $consumer = new Zend_OpenId_Consumer($storage);
  152. $this->assertTrue( $consumer->login(self::ID, "http://www.zf-test.com/return.php", "http://www.zf-test.com/trust.php", $ext, $response) );
  153. $headers = $response->getHeaders();
  154. $url = $headers[0]['value'];
  155. $url = parse_url($url);
  156. $q = explode("&", $url['query']);
  157. $query = array();
  158. foreach($q as $var) {
  159. if (list($key, $val) = explode("=", $var, 2)) {
  160. $query[$key] = $val;
  161. }
  162. }
  163. $this->assertTrue( is_array($query) );
  164. $this->assertSame( 9, count($query) );
  165. $this->assertSame( 'http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0', $query['openid.ns'] );
  166. $this->assertSame( 'checkid_setup', $query['openid.mode'] );
  167. $this->assertSame( 'http%3A%2F%2Freal_id.myopenid.com%2F', $query['openid.identity'] );
  168. $this->assertSame( 'http%3A%2F%2Fid.myopenid.com%2F', $query['openid.claimed_id'] );
  169. $this->assertSame( self::HANDLE, $query['openid.assoc_handle'] );
  170. $this->assertSame( 'http%3A%2F%2Fwww.zf-test.com%2Freturn.php', $query['openid.return_to'] );
  171. $this->assertSame( 'http%3A%2F%2Fwww.zf-test.com%2Ftrust.php', $query['openid.realm'] );
  172. $this->assertSame( 'nickname', $query['openid.sreg.required'] );
  173. $this->assertSame( 'email', $query['openid.sreg.optional'] );
  174. // Test login in dumb mode
  175. $storage->delAssociation(self::SERVER);
  176. $response = new Zend_OpenId_ResponseHelper(true);
  177. $consumer = new Zend_OpenId_Consumer($storage, true);
  178. $this->assertTrue( $consumer->login(self::ID, "http://www.zf-test.com/return.php", "http://www.zf-test.com/trust.php", null, $response) );
  179. $headers = $response->getHeaders();
  180. $url = $headers[0]['value'];
  181. $url = parse_url($url);
  182. $q = explode("&", $url['query']);
  183. $query = array();
  184. foreach($q as $var) {
  185. if (list($key, $val) = explode("=", $var, 2)) {
  186. $query[$key] = $val;
  187. }
  188. }
  189. $this->assertTrue( is_array($query) );
  190. $this->assertSame( 6, count($query) );
  191. $this->assertSame( 'http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0', $query['openid.ns'] );
  192. $this->assertSame( 'checkid_setup', $query['openid.mode'] );
  193. $this->assertSame( 'http%3A%2F%2Freal_id.myopenid.com%2F', $query['openid.identity'] );
  194. $this->assertSame( 'http%3A%2F%2Fid.myopenid.com%2F', $query['openid.claimed_id'] );
  195. $this->assertSame( 'http%3A%2F%2Fwww.zf-test.com%2Freturn.php', $query['openid.return_to'] );
  196. $this->assertSame( 'http%3A%2F%2Fwww.zf-test.com%2Ftrust.php', $query['openid.realm'] );
  197. $storage->delDiscoveryInfo(self::ID);
  198. }
  199. /**
  200. * testing check
  201. *
  202. */
  203. public function testCheck()
  204. {
  205. $expiresIn = time() + 600;
  206. $_SERVER['SCRIPT_URI'] = "http://www.zf-test.com/test.php";
  207. $storage = new Zend_OpenId_Consumer_Storage_File(dirname(__FILE__)."/_files/consumer");
  208. $storage->delDiscoveryInfo(self::ID);
  209. $this->assertTrue( $storage->addDiscoveryInfo(self::ID, self::REAL_ID, self::SERVER, 1.1, $expiresIn) );
  210. $storage->delAssociation(self::SERVER);
  211. $this->assertTrue( $storage->addAssociation(self::SERVER, self::HANDLE, self::MAC_FUNC, self::SECRET, $expiresIn) );
  212. $response = new Zend_OpenId_ResponseHelper(true);
  213. $consumer = new Zend_OpenId_Consumer($storage);
  214. $this->assertTrue( $consumer->check(self::ID, null, null, null, $response) );
  215. $headers = $response->getHeaders();
  216. $this->assertSame( '', $response->getBody() );
  217. $this->assertTrue( is_array($headers) );
  218. $this->assertSame( 1, count($headers) );
  219. $this->assertTrue( is_array($headers[0]) );
  220. $this->assertSame( 3, count($headers[0]) );
  221. $this->assertSame( 'Location', $headers[0]['name'] );
  222. $this->assertSame( true, $headers[0]['replace'] );
  223. $url = $headers[0]['value'];
  224. $url = parse_url($url);
  225. $this->assertSame( "http", $url['scheme'] );
  226. $this->assertSame( "www.myopenid.com", $url['host'] );
  227. $this->assertSame( "/", $url['path'] );
  228. $q = explode("&", $url['query']);
  229. $query = array();
  230. foreach($q as $var) {
  231. if (list($key, $val) = explode("=", $var, 2)) {
  232. $query[$key] = $val;
  233. }
  234. }
  235. $this->assertTrue( is_array($query) );
  236. $this->assertSame( 6, count($query) );
  237. $this->assertSame( 'checkid_immediate', $query['openid.mode'] );
  238. $this->assertSame( 'http%3A%2F%2Freal_id.myopenid.com%2F', $query['openid.identity'] );
  239. $this->assertSame( 'http%3A%2F%2Fid.myopenid.com%2F', $query['openid.claimed_id'] );
  240. $this->assertSame( self::HANDLE, $query['openid.assoc_handle'] );
  241. $this->assertSame( 'http%3A%2F%2Fwww.zf-test.com%2Ftest.php', $query['openid.return_to'] );
  242. $this->assertSame( 'http%3A%2F%2Fwww.zf-test.com', $query['openid.trust_root'] );
  243. $storage->delDiscoveryInfo(self::ID);
  244. $storage->delAssociation(self::SERVER);
  245. }
  246. /**
  247. * testing _getAssociation
  248. *
  249. */
  250. public function testGetAssociation()
  251. {
  252. $expiresIn = time() + 600;
  253. $storage = new Zend_OpenId_Consumer_Storage_File(dirname(__FILE__)."/_files/consumer");
  254. $storage->delAssociation(self::SERVER);
  255. $consumer = new Zend_OpenId_ConsumerHelper($storage);
  256. $this->assertFalse( $consumer->getAssociation(self::SERVER, $handle, $macFunc, $secret, $expires) );
  257. $this->assertTrue( $storage->addAssociation(self::SERVER, self::HANDLE, self::MAC_FUNC, self::SECRET, $expiresIn) );
  258. $this->assertTrue( $consumer->getAssociation(self::SERVER, $handle, $macFunc, $secret, $expires) );
  259. $this->assertSame( self::HANDLE, $handle );
  260. $this->assertSame( self::MAC_FUNC, $macFunc );
  261. $this->assertSame( self::SECRET, $secret );
  262. $this->assertSame( $expiresIn, $expires );
  263. $storage->delAssociation(self::SERVER);
  264. $this->assertTrue( $consumer->getAssociation(self::SERVER, $handle, $macFunc, $secret, $expires) );
  265. $this->assertSame( self::HANDLE, $handle );
  266. $this->assertSame( self::MAC_FUNC, $macFunc );
  267. $this->assertSame( self::SECRET, $secret );
  268. $this->assertSame( $expiresIn, $expires );
  269. }
  270. /**
  271. * testing _httpRequest
  272. *
  273. */
  274. public function testHttpRequest()
  275. {
  276. $consumer = new Zend_OpenId_ConsumerHelper(new Zend_OpenId_Consumer_Storage_File(dirname(__FILE__)."/_files/consumer"));
  277. $http = new Zend_Http_Client(null,
  278. array(
  279. 'maxredirects' => 4,
  280. 'timeout' => 15,
  281. 'useragent' => 'Zend_OpenId'
  282. ));
  283. $test = new Zend_Http_Client_Adapter_Test();
  284. $http->setAdapter($test);
  285. $consumer->SetHttpClient($http);
  286. $this->assertSame( $http, $consumer->GetHttpClient() );
  287. $this->assertFalse( $consumer->httpRequest(self::SERVER) );
  288. $test->setResponse("HTTP/1.1 200 OK\r\n\r\nok\n");
  289. // Test GET request without parameters
  290. $this->assertSame( "ok\n", $consumer->httpRequest(self::SERVER) );
  291. $this->assertSame( "GET / HTTP/1.1\r\n" .
  292. "Host: www.myopenid.com\r\n" .
  293. "Connection: close\r\n" .
  294. "Accept-encoding: gzip, deflate\r\n" .
  295. "User-Agent: Zend_OpenId\r\n\r\n",
  296. $http->getLastRequest() );
  297. // Test POST request without parameters
  298. $this->assertSame( "ok\n", $consumer->httpRequest(self::SERVER, 'POST') );
  299. $this->assertSame( "POST / HTTP/1.1\r\n" .
  300. "Host: www.myopenid.com\r\n" .
  301. "Connection: close\r\n" .
  302. "Accept-encoding: gzip, deflate\r\n" .
  303. "Content-Type: application/x-www-form-urlencoded\r\n" .
  304. "User-Agent: Zend_OpenId\r\n" .
  305. "Content-Length: 0\r\n\r\n",
  306. $http->getLastRequest() );
  307. // Test GET request with parameters
  308. $this->assertSame( "ok\n", $consumer->httpRequest(self::SERVER . 'test.php', 'GET', array('a'=>'b','c'=>'d')) );
  309. $this->assertSame( "GET /test.php?a=b&c=d HTTP/1.1\r\n" .
  310. "Host: www.myopenid.com\r\n" .
  311. "Connection: close\r\n" .
  312. "Accept-encoding: gzip, deflate\r\n" .
  313. "User-Agent: Zend_OpenId\r\n\r\n",
  314. $http->getLastRequest() );
  315. // Test POST request with parameters
  316. $this->assertSame( "ok\n", $consumer->httpRequest(self::SERVER . 'test.php', 'POST', array('a'=>'b','c'=>'d')) );
  317. $this->assertSame( "POST /test.php HTTP/1.1\r\n" .
  318. "Host: www.myopenid.com\r\n" .
  319. "Connection: close\r\n" .
  320. "Accept-encoding: gzip, deflate\r\n" .
  321. "User-Agent: Zend_OpenId\r\n" .
  322. "Content-Type: application/x-www-form-urlencoded\r\n" .
  323. "Content-Length: 7\r\n\r\n" .
  324. "a=b&c=d",
  325. $http->getLastRequest() );
  326. // Test GET parameters combination
  327. $this->assertSame( "ok\n", $consumer->httpRequest(self::SERVER . 'test.php?a=b', 'GET', array('c'=>'x y')) );
  328. $this->assertSame( "GET /test.php?a=b&c=x+y HTTP/1.1\r\n" .
  329. "Host: www.myopenid.com\r\n" .
  330. "Connection: close\r\n" .
  331. "Accept-encoding: gzip, deflate\r\n" .
  332. "User-Agent: Zend_OpenId\r\n\r\n",
  333. $http->getLastRequest() );
  334. // Test GET and POST parameters combination
  335. $this->assertSame( "ok\n", $consumer->httpRequest(self::SERVER . 'test.php?a=b', 'POST', array('c'=>'x y')) );
  336. $this->assertSame( "POST /test.php?a=b HTTP/1.1\r\n" .
  337. "Host: www.myopenid.com\r\n" .
  338. "Connection: close\r\n" .
  339. "Accept-encoding: gzip, deflate\r\n" .
  340. "User-Agent: Zend_OpenId\r\n" .
  341. "Content-Type: application/x-www-form-urlencoded\r\n" .
  342. "Content-Length: 5\r\n\r\n" .
  343. "c=x+y",
  344. $http->getLastRequest() );
  345. }
  346. /**
  347. * testing _associate
  348. *
  349. */
  350. public function testAssociate()
  351. {
  352. try {
  353. $storage = new Zend_OpenId_Consumer_Storage_File(dirname(__FILE__)."/_files/consumer");
  354. $storage->delAssociation(self::SERVER);
  355. $consumer = new Zend_OpenId_ConsumerHelper($storage);
  356. $http = new Zend_Http_Client(null,
  357. array(
  358. 'maxredirects' => 4,
  359. 'timeout' => 15,
  360. 'useragent' => 'Zend_OpenId'
  361. ));
  362. $test = new Zend_Http_Client_Adapter_Test();
  363. $http->setAdapter($test);
  364. $consumer->SetHttpClient($http);
  365. // Test OpenID 1.1 association request with DH-SHA1
  366. $consumer->clearAssociation();
  367. $this->assertFalse( $consumer->associate(self::SERVER, 1.1, pack("H*", "60017f7ebf0ef29ace27f0dfee2aaa6528d170e147b1260cc3987d7851cb67d49fbfdbb42c56494e61b1e1e39fa42315db0bf4f879787fcf1e807d0629d47cf05d3ac50602b1e7f6e73cd370320ddcdcf7f7aa86f35a3273d187de9c9efa959a02ce3a9c80f47dfcc83cfaad60b673e1806a764227344deae158ceec9ca4d60e")) );
  368. $this->assertSame( "POST / HTTP/1.1\r\n" .
  369. "Host: www.myopenid.com\r\n" .
  370. "Connection: close\r\n" .
  371. "Accept-encoding: gzip, deflate\r\n" .
  372. "User-Agent: Zend_OpenId\r\n" .
  373. "Content-Type: application/x-www-form-urlencoded\r\n" .
  374. "Content-Length: 510\r\n\r\n" .
  375. "openid.mode=associate&" .
  376. "openid.assoc_type=HMAC-SHA1&" .
  377. "openid.session_type=DH-SHA1&".
  378. "openid.dh_modulus=ANz5OguIOXLsDhmYmsWizjEOHTdxfo2Vcbt2I3MYZuYe91ouJ4mLBX%2BYkcLiemOcPym2CBRYHNOyyjmG0mg3BVd9RcLn5S3IHHoXGHblzqdLFEi%2F368Ygo79JRnxTkXjgmY0rxlJ5bU1zIKaSDuKdiI%2BXUkKJX8Fvf8W8vsixYOr&" .
  379. "openid.dh_gen=Ag%3D%3D&" .
  380. "openid.dh_consumer_public=GaLlROlBGgSopPzo1ewYISnnT4BUFBfIKlgDPoS9U41t5eQb8QYqgcw7%2BW3dSF1VlWcvJGR0UbZIEhJ3UrCs6p69q6sgl%2FOZ7P%2B17rme7OynqszA3pqD6MJoQVZ5Ht%2FR%2BjmMjK08ajcgYEZU1GG4U5k8eYbcFnje00%2FTGfjKY0I%3D",
  381. $http->getLastRequest() );
  382. // Test OpenID 2.0 association request with DH-SHA256
  383. $consumer->clearAssociation();
  384. $this->assertFalse( $consumer->associate(self::SERVER, 2.0, pack("H*", "60017f7ebf0ef29ace27f0dfee2aaa6528d170e147b1260cc3987d7851cb67d49fbfdbb42c56494e61b1e1e39fa42315db0bf4f879787fcf1e807d0629d47cf05d3ac50602b1e7f6e73cd370320ddcdcf7f7aa86f35a3273d187de9c9efa959a02ce3a9c80f47dfcc83cfaad60b673e1806a764227344deae158ceec9ca4d60e")) );
  385. $this->assertSame( "POST / HTTP/1.1\r\n" .
  386. "Host: www.myopenid.com\r\n" .
  387. "Connection: close\r\n" .
  388. "Accept-encoding: gzip, deflate\r\n" .
  389. "User-Agent: Zend_OpenId\r\n" .
  390. "Content-Type: application/x-www-form-urlencoded\r\n" .
  391. "Content-Length: 567\r\n\r\n" .
  392. "openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&" .
  393. "openid.mode=associate&" .
  394. "openid.assoc_type=HMAC-SHA256&" .
  395. "openid.session_type=DH-SHA256&".
  396. "openid.dh_modulus=ANz5OguIOXLsDhmYmsWizjEOHTdxfo2Vcbt2I3MYZuYe91ouJ4mLBX%2BYkcLiemOcPym2CBRYHNOyyjmG0mg3BVd9RcLn5S3IHHoXGHblzqdLFEi%2F368Ygo79JRnxTkXjgmY0rxlJ5bU1zIKaSDuKdiI%2BXUkKJX8Fvf8W8vsixYOr&" .
  397. "openid.dh_gen=Ag%3D%3D&" .
  398. "openid.dh_consumer_public=GaLlROlBGgSopPzo1ewYISnnT4BUFBfIKlgDPoS9U41t5eQb8QYqgcw7%2BW3dSF1VlWcvJGR0UbZIEhJ3UrCs6p69q6sgl%2FOZ7P%2B17rme7OynqszA3pqD6MJoQVZ5Ht%2FR%2BjmMjK08ajcgYEZU1GG4U5k8eYbcFnje00%2FTGfjKY0I%3D",
  399. $http->getLastRequest() );
  400. // Test OpenID 1.1 association response with DH-SHA1
  401. $consumer->clearAssociation();
  402. $test->setResponse("HTTP/1.1 200 OK\r\n\r\n" .
  403. "assoc_type:HMAC-SHA1\n" .
  404. "assoc_handle:0123456789absdef0123456789absdef\n" .
  405. "expires_in:3600\n" .
  406. "session_type:DH-SHA1\n".
  407. "dh_server_public:AIoP3d+ZTkd5vZj6G82XVIQ6KRAfSKmLz2Q3qVMzZ5tt7Z7St714GccipYXzCs5Tzgkc+Nt/uDE5xQ/f0Zn0uDS65CZHx3MOPqAANw/9YC/CafF1CD1MxW5TiN50GsjT/wGkcJFcpPXYVigQDOjIkHjKCysk53ktFvCoT60nFKGc\n".
  408. "enc_mac_key:ON+M6/X8uUcOfxw1HF4sw/0XYyw=\n");
  409. $this->assertTrue( $consumer->associate(self::SERVER, 1.1, pack("H*", "60017f7ebf0ef29ace27f0dfee2aaa6528d170e147b1260cc3987d7851cb67d49fbfdbb42c56494e61b1e1e39fa42315db0bf4f879787fcf1e807d0629d47cf05d3ac50602b1e7f6e73cd370320ddcdcf7f7aa86f35a3273d187de9c9efa959a02ce3a9c80f47dfcc83cfaad60b673e1806a764227344deae158ceec9ca4d60e")) );
  410. $this->assertTrue( $storage->getAssociation(self::SERVER, $handle, $macFunc, $secret, $expires) );
  411. $this->assertSame( "0123456789absdef0123456789absdef", $handle );
  412. $this->assertSame( "sha1", $macFunc );
  413. $this->assertSame( "e36624c686748f6b646648f12748ffd157e4d4dd", bin2hex($secret) );
  414. $this->assertTrue( $storage->delAssociation(self::SERVER) );
  415. // Wrong OpenID 2.0 association response (wrong ns)
  416. $consumer->clearAssociation();
  417. $test->setResponse("HTTP/1.1 200 OK\r\n\r\n" .
  418. "ns:http://specs.openid.net/auth/1.0\n" .
  419. "assoc_type:HMAC-SHA256\n" .
  420. "assoc_handle:0123456789absdef0123456789absdef\n" .
  421. "expires_in:3600\n" .
  422. "session_type:DH-SHA256\n".
  423. "dh_server_public:AIlflxF8rvxx1Xi4Oj/KdP+7fvczeIRvx8WScMQS9I27R6YKd3Nx++5tAAF0rHelKDSG2ZeFM/zLEu9ZmUFzF02OaehWqykCfmtLASwMZO0u2GwYiIu5BoeJb9HlXJes58u/M4ViPXWhn27w2ZTlZJuuK8sDiTSTj9TmFxOriH4X\n".
  424. "enc_mac_key:lvvCoTyvKy8oV6wnNHeroU0uLgBHiGV4BNkrXJe04JE=\n");
  425. $this->assertFalse( $consumer->associate(self::SERVER, 2.0, pack("H*", "60017f7ebf0ef29ace27f0dfee2aaa6528d170e147b1260cc3987d7851cb67d49fbfdbb42c56494e61b1e1e39fa42315db0bf4f879787fcf1e807d0629d47cf05d3ac50602b1e7f6e73cd370320ddcdcf7f7aa86f35a3273d187de9c9efa959a02ce3a9c80f47dfcc83cfaad60b673e1806a764227344deae158ceec9ca4d60e")) );
  426. // Wrong OpenID 2.0 association response (wrong assoc_type)
  427. $consumer->clearAssociation();
  428. $test->setResponse("HTTP/1.1 200 OK\r\n\r\n" .
  429. "ns:http://specs.openid.net/auth/2.0\n" .
  430. "assoc_type:HMAC-SHA1\n" .
  431. "assoc_handle:0123456789absdef0123456789absdef\n" .
  432. "expires_in:3600\n" .
  433. "session_type:DH-SHA256\n".
  434. "dh_server_public:AIlflxF8rvxx1Xi4Oj/KdP+7fvczeIRvx8WScMQS9I27R6YKd3Nx++5tAAF0rHelKDSG2ZeFM/zLEu9ZmUFzF02OaehWqykCfmtLASwMZO0u2GwYiIu5BoeJb9HlXJes58u/M4ViPXWhn27w2ZTlZJuuK8sDiTSTj9TmFxOriH4X\n".
  435. "enc_mac_key:lvvCoTyvKy8oV6wnNHeroU0uLgBHiGV4BNkrXJe04JE=\n");
  436. $this->assertFalse( $consumer->associate(self::SERVER, 2.0, pack("H*", "60017f7ebf0ef29ace27f0dfee2aaa6528d170e147b1260cc3987d7851cb67d49fbfdbb42c56494e61b1e1e39fa42315db0bf4f879787fcf1e807d0629d47cf05d3ac50602b1e7f6e73cd370320ddcdcf7f7aa86f35a3273d187de9c9efa959a02ce3a9c80f47dfcc83cfaad60b673e1806a764227344deae158ceec9ca4d60e")) );
  437. // Wrong OpenID 2.0 association response (wrong session_type)
  438. $consumer->clearAssociation();
  439. $test->setResponse("HTTP/1.1 200 OK\r\n\r\n" .
  440. "ns:http://specs.openid.net/auth/2.0\n" .
  441. "assoc_type:HMAC-SHA256\n" .
  442. "assoc_handle:0123456789absdef0123456789absdef\n" .
  443. "expires_in:3600\n" .
  444. "session_type:DH-SHA257\n".
  445. "dh_server_public:AIlflxF8rvxx1Xi4Oj/KdP+7fvczeIRvx8WScMQS9I27R6YKd3Nx++5tAAF0rHelKDSG2ZeFM/zLEu9ZmUFzF02OaehWqykCfmtLASwMZO0u2GwYiIu5BoeJb9HlXJes58u/M4ViPXWhn27w2ZTlZJuuK8sDiTSTj9TmFxOriH4X\n".
  446. "enc_mac_key:lvvCoTyvKy8oV6wnNHeroU0uLgBHiGV4BNkrXJe04JE=\n");
  447. $this->assertFalse( $consumer->associate(self::SERVER, 2.0, pack("H*", "60017f7ebf0ef29ace27f0dfee2aaa6528d170e147b1260cc3987d7851cb67d49fbfdbb42c56494e61b1e1e39fa42315db0bf4f879787fcf1e807d0629d47cf05d3ac50602b1e7f6e73cd370320ddcdcf7f7aa86f35a3273d187de9c9efa959a02ce3a9c80f47dfcc83cfaad60b673e1806a764227344deae158ceec9ca4d60e")) );
  448. // Test OpenID 2.0 association response with DH-SHA256
  449. $consumer->clearAssociation();
  450. $test->setResponse("HTTP/1.1 200 OK\r\n\r\n" .
  451. "ns:http://specs.openid.net/auth/2.0\n" .
  452. "assoc_type:HMAC-SHA256\n" .
  453. "assoc_handle:0123456789absdef0123456789absdef\n" .
  454. "expires_in:3600\n" .
  455. "session_type:DH-SHA256\n".
  456. "dh_server_public:AIlflxF8rvxx1Xi4Oj/KdP+7fvczeIRvx8WScMQS9I27R6YKd3Nx++5tAAF0rHelKDSG2ZeFM/zLEu9ZmUFzF02OaehWqykCfmtLASwMZO0u2GwYiIu5BoeJb9HlXJes58u/M4ViPXWhn27w2ZTlZJuuK8sDiTSTj9TmFxOriH4X\n".
  457. "enc_mac_key:lvvCoTyvKy8oV6wnNHeroU0uLgBHiGV4BNkrXJe04JE=\n");
  458. $this->assertTrue( $consumer->associate(self::SERVER, 2.0, pack("H*", "60017f7ebf0ef29ace27f0dfee2aaa6528d170e147b1260cc3987d7851cb67d49fbfdbb42c56494e61b1e1e39fa42315db0bf4f879787fcf1e807d0629d47cf05d3ac50602b1e7f6e73cd370320ddcdcf7f7aa86f35a3273d187de9c9efa959a02ce3a9c80f47dfcc83cfaad60b673e1806a764227344deae158ceec9ca4d60e")) );
  459. $this->assertTrue( $storage->getAssociation(self::SERVER, $handle, $macFunc, $secret, $expires) );
  460. $this->assertSame( "0123456789absdef0123456789absdef", $handle );
  461. $this->assertSame( "sha256", $macFunc );
  462. $this->assertSame( "ed901bc561c29fd7bb42862e5f09fa37e7944a7ee72142322f34a21bfe1384b8", bin2hex($secret) );
  463. $this->assertTrue( $storage->delAssociation(self::SERVER) );
  464. // Test OpenID 2.0 association response without encryption (missing session_type)
  465. $consumer->clearAssociation();
  466. $test->setResponse("HTTP/1.1 200 OK\r\n\r\n" .
  467. "ns:http://specs.openid.net/auth/2.0\n" .
  468. "assoc_type:HMAC-SHA256\n" .
  469. "assoc_handle:0123456789absdef0123456789absdef\n" .
  470. "expires_in:3600\n" .
  471. "mac_key:7ZAbxWHCn9e7QoYuXwn6N+eUSn7nIUIyLzSiG/4ThLg=\n");
  472. $this->assertTrue( $consumer->associate(self::SERVER, 2.0, pack("H*", "60017f7ebf0ef29ace27f0dfee2aaa6528d170e147b1260cc3987d7851cb67d49fbfdbb42c56494e61b1e1e39fa42315db0bf4f879787fcf1e807d0629d47cf05d3ac50602b1e7f6e73cd370320ddcdcf7f7aa86f35a3273d187de9c9efa959a02ce3a9c80f47dfcc83cfaad60b673e1806a764227344deae158ceec9ca4d60e")) );
  473. $this->assertTrue( $storage->getAssociation(self::SERVER, $handle, $macFunc, $secret, $expires) );
  474. $this->assertSame( "0123456789absdef0123456789absdef", $handle );
  475. $this->assertSame( "sha256", $macFunc );
  476. $this->assertSame( "ed901bc561c29fd7bb42862e5f09fa37e7944a7ee72142322f34a21bfe1384b8", bin2hex($secret) );
  477. $this->assertTrue( $storage->delAssociation(self::SERVER) );
  478. // Test OpenID 2.0 association response without encryption (blank session_type)
  479. $consumer->clearAssociation();
  480. $test->setResponse("HTTP/1.1 200 OK\r\n\r\n" .
  481. "ns:http://specs.openid.net/auth/2.0\n" .
  482. "assoc_type:HMAC-SHA256\n" .
  483. "assoc_handle:0123456789absdef0123456789absdef\n" .
  484. "expires_in:3600\n" .
  485. "session_type:\n".
  486. "mac_key:7ZAbxWHCn9e7QoYuXwn6N+eUSn7nIUIyLzSiG/4ThLg=\n");
  487. $this->assertTrue( $consumer->associate(self::SERVER, 2.0, pack("H*", "60017f7ebf0ef29ace27f0dfee2aaa6528d170e147b1260cc3987d7851cb67d49fbfdbb42c56494e61b1e1e39fa42315db0bf4f879787fcf1e807d0629d47cf05d3ac50602b1e7f6e73cd370320ddcdcf7f7aa86f35a3273d187de9c9efa959a02ce3a9c80f47dfcc83cfaad60b673e1806a764227344deae158ceec9ca4d60e")) );
  488. $this->assertTrue( $storage->getAssociation(self::SERVER, $handle, $macFunc, $secret, $expires) );
  489. $this->assertSame( "0123456789absdef0123456789absdef", $handle );
  490. $this->assertSame( "sha256", $macFunc );
  491. $this->assertSame( "ed901bc561c29fd7bb42862e5f09fa37e7944a7ee72142322f34a21bfe1384b8", bin2hex($secret) );
  492. $this->assertTrue( $storage->delAssociation(self::SERVER) );
  493. // Test OpenID 2.0 association response without encryption (blank session_type)
  494. $consumer->clearAssociation();
  495. $test->setResponse("HTTP/1.1 200 OK\r\n\r\n" .
  496. "ns:http://specs.openid.net/auth/2.0\n" .
  497. "assoc_type:HMAC-SHA256\n" .
  498. "assoc_handle:0123456789absdef0123456789absdef\n" .
  499. "expires_in:3600\n" .
  500. "session_type:no-encryption\n".
  501. "mac_key:7ZAbxWHCn9e7QoYuXwn6N+eUSn7nIUIyLzSiG/4ThLg=\n");
  502. $this->assertTrue( $consumer->associate(self::SERVER, 2.0, pack("H*", "60017f7ebf0ef29ace27f0dfee2aaa6528d170e147b1260cc3987d7851cb67d49fbfdbb42c56494e61b1e1e39fa42315db0bf4f879787fcf1e807d0629d47cf05d3ac50602b1e7f6e73cd370320ddcdcf7f7aa86f35a3273d187de9c9efa959a02ce3a9c80f47dfcc83cfaad60b673e1806a764227344deae158ceec9ca4d60e")) );
  503. $this->assertTrue( $storage->getAssociation(self::SERVER, $handle, $macFunc, $secret, $expires) );
  504. $this->assertSame( "0123456789absdef0123456789absdef", $handle );
  505. $this->assertSame( "sha256", $macFunc );
  506. $this->assertSame( "ed901bc561c29fd7bb42862e5f09fa37e7944a7ee72142322f34a21bfe1384b8", bin2hex($secret) );
  507. $this->assertTrue( $storage->delAssociation(self::SERVER) );
  508. } catch (Zend_OpenId_Exception $e) {
  509. $this->markTestSkipped($e->getMessage());
  510. }
  511. }
  512. /**
  513. * testing discovery
  514. *
  515. */
  516. public function testDiscovery()
  517. {
  518. $storage = new Zend_OpenId_Consumer_Storage_File(dirname(__FILE__)."/_files/consumer");
  519. $consumer = new Zend_OpenId_ConsumerHelper($storage);
  520. $http = new Zend_Http_Client(null,
  521. array(
  522. 'maxredirects' => 4,
  523. 'timeout' => 15,
  524. 'useragent' => 'Zend_OpenId'
  525. ));
  526. $test = new Zend_Http_Client_Adapter_Test();
  527. $http->setAdapter($test);
  528. $consumer->SetHttpClient($http);
  529. // Bad response
  530. $storage->delDiscoveryInfo(self::ID);
  531. $id = self::ID;
  532. $this->assertFalse( $consumer->discovery($id, $server, $version) );
  533. // Test HTML based discovery (OpenID 1.1)
  534. $storage->delDiscoveryInfo(self::ID);
  535. $test->setResponse("HTTP/1.1 200 OK\r\n\r\n" .
  536. "<html><head>\n" .
  537. "<link rel=\"openid.server\" href=\"" . self::SERVER . "\">\n" .
  538. "<link rel=\"openid.delegate\" href=\"" . self::REAL_ID . "\">\n" .
  539. "</head><body</body></html>\n");
  540. $id = self::ID;
  541. $this->assertTrue( $consumer->discovery($id, $server, $version) );
  542. $this->assertSame( self::REAL_ID, $id );
  543. $this->assertSame( self::SERVER, $server );
  544. $this->assertSame( 1.1, $version );
  545. // Test HTML based discovery (OpenID 1.1)
  546. $storage->delDiscoveryInfo(self::ID);
  547. $test->setResponse("HTTP/1.1 200 OK\r\n\r\n" .
  548. "<html><head>\n" .
  549. "<link href=\"" . self::SERVER . "\" rel=\"openid.server\">\n" .
  550. "<link href=\"" . self::REAL_ID . "\" rel=\"openid.delegate\">\n" .
  551. "</head><body</body></html>\n");
  552. $id = self::ID;
  553. $this->assertTrue( $consumer->discovery($id, $server, $version) );
  554. $this->assertSame( self::REAL_ID, $id );
  555. $this->assertSame( self::SERVER, $server );
  556. $this->assertSame( 1.1, $version );
  557. // Test HTML based discovery (OpenID 2.0)
  558. $storage->delDiscoveryInfo(self::ID);
  559. $test->setResponse("HTTP/1.1 200 OK\r\n\r\n" .
  560. "<html><head>\n" .
  561. "<link rel=\"openid2.provider\" href=\"" . self::SERVER . "\">\n" .
  562. "<link rel=\"openid2.local_id\" href=\"" . self::REAL_ID . "\">\n" .
  563. "</head><body</body></html>\n");
  564. $id = self::ID;
  565. $this->assertTrue( $consumer->discovery($id, $server, $version) );
  566. $this->assertSame( self::REAL_ID, $id );
  567. $this->assertSame( self::SERVER, $server );
  568. $this->assertSame( 2.0, $version );
  569. // Test HTML based discovery (OpenID 2.0)
  570. $storage->delDiscoveryInfo(self::ID);
  571. $test->setResponse("HTTP/1.1 200 OK\r\n\r\n" .
  572. "<html><head>\n" .
  573. "<link href=\"" . self::SERVER . "\" rel=\"openid2.provider\">\n" .
  574. "<link href=\"" . self::REAL_ID . "\" rel=\"openid2.local_id\">\n" .
  575. "</head><body</body></html>\n");
  576. $id = self::ID;
  577. $this->assertTrue( $consumer->discovery($id, $server, $version) );
  578. $this->assertSame( self::REAL_ID, $id );
  579. $this->assertSame( self::SERVER, $server );
  580. $this->assertSame( 2.0, $version );
  581. // Test HTML based discovery (OpenID 1.1 and 2.0)
  582. $storage->delDiscoveryInfo(self::ID);
  583. $test->setResponse("HTTP/1.1 200 OK\r\n\r\n" .
  584. "<html><head>\n" .
  585. "<link rel=\"openid2.provider\" href=\"" . self::SERVER . "\">\n" .
  586. "<link rel=\"openid2.local_id\" href=\"" . self::REAL_ID . "\">\n" .
  587. "<link rel=\"openid.server\" href=\"" . self::SERVER . "\">\n" .
  588. "<link rel=\"openid.delegate\" href=\"" . self::REAL_ID . "\">\n" .
  589. "</head><body</body></html>\n");
  590. $id = self::ID;
  591. $this->assertTrue( $consumer->discovery($id, $server, $version) );
  592. $this->assertSame( self::REAL_ID, $id );
  593. $this->assertSame( self::SERVER, $server );
  594. $this->assertSame( 2.0, $version );
  595. // Test HTML based discovery (OpenID 1.1) (single quotes)
  596. $storage->delDiscoveryInfo(self::ID);
  597. $test->setResponse("HTTP/1.1 200 OK\r\n\r\n" .
  598. "<html><head>\n" .
  599. "<link rel='openid.server' href='" . self::SERVER . "'>\n" .
  600. "<link rel='openid.delegate' href='" . self::REAL_ID . "'>\n" .
  601. "</head><body</body></html>\n");
  602. $id = self::ID;
  603. $this->assertTrue( $consumer->discovery($id, $server, $version) );
  604. $this->assertSame( self::REAL_ID, $id );
  605. $this->assertSame( self::SERVER, $server );
  606. $this->assertSame( 1.1, $version );
  607. // Test HTML based discovery (OpenID 1.1) (single quotes)
  608. $storage->delDiscoveryInfo(self::ID);
  609. $test->setResponse("HTTP/1.1 200 OK\r\n\r\n" .
  610. "<html><head>\n" .
  611. "<link href='" . self::SERVER . "' rel='openid.server'>\n" .
  612. "<link href='" . self::REAL_ID . "' rel='openid.delegate'>\n" .
  613. "</head><body</body></html>\n");
  614. $id = self::ID;
  615. $this->assertTrue( $consumer->discovery($id, $server, $version) );
  616. $this->assertSame( self::REAL_ID, $id );
  617. $this->assertSame( self::SERVER, $server );
  618. $this->assertSame( 1.1, $version );
  619. // Test HTML based discovery (OpenID 2.0) (single quotes)
  620. $storage->delDiscoveryInfo(self::ID);
  621. $test->setResponse("HTTP/1.1 200 OK\r\n\r\n" .
  622. "<html><head>\n" .
  623. "<link rel='openid2.provider' href='" . self::SERVER . "'>\n" .
  624. "<link rel='openid2.local_id' href='" . self::REAL_ID . "'>\n" .
  625. "</head><body</body></html>\n");
  626. $id = self::ID;
  627. $this->assertTrue( $consumer->discovery($id, $server, $version) );
  628. $this->assertSame( self::REAL_ID, $id );
  629. $this->assertSame( self::SERVER, $server );
  630. $this->assertSame( 2.0, $version );
  631. // Test HTML based discovery (OpenID 2.0) (single quotes)
  632. $storage->delDiscoveryInfo(self::ID);
  633. $test->setResponse("HTTP/1.1 200 OK\r\n\r\n" .
  634. "<html><head>\n" .
  635. "<link href='" . self::SERVER . "' rel='openid2.provider'>\n" .
  636. "<link href='" . self::REAL_ID . "' rel='openid2.local_id'>\n" .
  637. "</head><body</body></html>\n");
  638. $id = self::ID;
  639. $this->assertTrue( $consumer->discovery($id, $server, $version) );
  640. $this->assertSame( self::REAL_ID, $id );
  641. $this->assertSame( self::SERVER, $server );
  642. $this->assertSame( 2.0, $version );
  643. // Test HTML based discovery (OpenID 1.1 and 2.0) (single quotes)
  644. $storage->delDiscoveryInfo(self::ID);
  645. $test->setResponse("HTTP/1.1 200 OK\r\n\r\n" .
  646. "<html><head>\n" .
  647. "<link rel='openid2.provider' href='" . self::SERVER . "'>\n" .
  648. "<link rel='openid2.local_id' href='" . self::REAL_ID . "'>\n" .
  649. "<link rel='openid.server' href='" . self::SERVER . "'>\n" .
  650. "<link rel='openid.delegate' href='" . self::REAL_ID . "'>\n" .
  651. "</head><body</body></html>\n");
  652. $id = self::ID;
  653. $this->assertTrue( $consumer->discovery($id, $server, $version) );
  654. $this->assertSame( self::REAL_ID, $id );
  655. $this->assertSame( self::SERVER, $server );
  656. $this->assertSame( 2.0, $version );
  657. // Wrong HTML
  658. $storage->delDiscoveryInfo(self::ID);
  659. $test->setResponse("HTTP/1.1 200 OK\r\n\r\n" .
  660. "<html><head>\n" .
  661. "</head><body</body></html>\n");
  662. $id = self::ID;
  663. $this->assertFalse( $consumer->discovery($id, $server, $version) );
  664. // Test HTML based discovery with multivalue rel (OpenID 1.1)
  665. $storage->delDiscoveryInfo(self::ID);
  666. $test->setResponse("HTTP/1.1 200 OK\r\n\r\n" .
  667. "<html><head>\n" .
  668. "<link rel=\" aaa openid.server bbb \" href=\"" . self::SERVER . "\">\n" .
  669. "<link rel=\"aaa openid.delegate\" href=\"" . self::REAL_ID . "\">\n" .
  670. "</head><body</body></html>\n");
  671. $id = self::ID;
  672. $this->assertTrue( $consumer->discovery($id, $server, $version) );
  673. $this->assertSame( self::REAL_ID, $id );
  674. $this->assertSame( self::SERVER, $server );
  675. $this->assertSame( 1.1, $version );
  676. }
  677. /**
  678. * testing verify
  679. *
  680. */
  681. public function testVerify()
  682. {
  683. $expiresIn = time() + 600;
  684. $_SERVER['SCRIPT_URI'] = "http://www.zf-test.com/test.php";
  685. $storage = new Zend_OpenId_Consumer_Storage_File(dirname(__FILE__)."/_files/consumer");
  686. $consumer = new Zend_OpenId_ConsumerHelper($storage);
  687. $storage->addDiscoveryInfo(self::ID, self::REAL_ID, self::SERVER, 1.1, $expiresIn);
  688. // Wrong arguments
  689. $this->assertFalse( $consumer->verify(array()) );
  690. // HMAC-SHA1
  691. $consumer->clearAssociation();
  692. $params = array(
  693. "openid_return_to" => "http://www.zf-test.com/test.php",
  694. "openid_assoc_handle" => self::HANDLE,
  695. "openid_claimed_id" => self::ID,
  696. "openid_identity" => self::REAL_ID,
  697. "openid_response_nonce" => "2007-08-14T12:52:33Z46c1a59124ffe",
  698. "openid_mode" => "id_res",
  699. "openid_signed" => "assoc_handle,return_to,claimed_id,identity,response_nonce,mode,signed",
  700. "openid_sig" => "h/5AFD25NpzSok5tzHEGCVUkQSw="
  701. );
  702. $storage->delAssociation(self::SERVER);
  703. $storage->addAssociation(self::SERVER, self::HANDLE, "sha1", pack("H*", "8382aea922560ece833ba55fa53b7a975f597370"), $expiresIn);
  704. $storage->purgeNonces();
  705. $this->assertTrue( $consumer->verify($params) );
  706. $storage->delDiscoveryInfo(self::ID);
  707. $storage->addDiscoveryInfo(self::ID, self::REAL_ID, self::SERVER, 2.0, $expiresIn);
  708. // HMAC-SHA256
  709. $consumer->clearAssociation();
  710. $params = array(
  711. "openid_ns" => Zend_OpenId::NS_2_0,
  712. "openid_op_endpoint" => self::SERVER,
  713. "openid_return_to" => "http://www.zf-test.com/test.php",
  714. "openid_assoc_handle" => self::HANDLE,
  715. "openid_claimed_id" => self::ID,
  716. "openid_identity" => self::REAL_ID,
  717. "openid_response_nonce" => "2007-08-14T12:52:33Z46c1a59124ffe",
  718. "openid_mode" => "id_res",
  719. "openid_signed" => "assoc_handle,return_to,claimed_id,identity,response_nonce,mode,signed",
  720. "openid_sig" => "rMiVhEmHVcIHoY2uzPNb7udWqa4lruvjnwZfujct0TE="
  721. );
  722. $storage->delAssociation(self::SERVER);
  723. $storage->addAssociation(self::SERVER, self::HANDLE, "sha256", pack("H*", "ed901bc561c29fd7bb42862e5f09fa37e7944a7ee72142322f34a21bfe1384b8"), $expiresIn);
  724. $storage->purgeNonces();
  725. $this->assertTrue( $consumer->verify($params) );
  726. // HMAC-SHA256 (duplicate response_nonce)
  727. $consumer->clearAssociation();
  728. $params = array(
  729. "openid_ns" => Zend_OpenId::NS_2_0,
  730. "openid_op_endpoint" => self::SERVER,
  731. "openid_return_to" => "http://www.zf-test.com/test.php",
  732. "openid_assoc_handle" => self::HANDLE,
  733. "openid_claimed_id" => self::ID,
  734. "openid_identity" => self::REAL_ID,
  735. "openid_response_nonce" => "2007-08-14T12:52:33Z46c1a59124ffe",
  736. "openid_mode" => "id_res",
  737. "openid_signed" => "assoc_handle,return_to,claimed_id,identity,response_nonce,mode,signed",
  738. "openid_sig" => "rMiVhEmHVcIHoY2uzPNb7udWqa4lruvjnwZfujct0TE="
  739. );
  740. $storage->delAssociation(self::SERVER);
  741. $storage->addAssociation(self::SERVER, self::HANDLE, "sha256", pack("H*", "ed901bc561c29fd7bb42862e5f09fa37e7944a7ee72142322f34a21bfe1384b8"), $expiresIn);
  742. $this->assertFalse( $consumer->verify($params) );
  743. $storage->delDiscoveryInfo(self::ID);
  744. $storage->addDiscoveryInfo(self::ID, self::REAL_ID, self::SERVER, 1.1, $expiresIn);
  745. // wrong signature
  746. $consumer->clearAssociation();
  747. $params = array(
  748. "openid_return_to" => "http://www.zf-test.com/test.php",
  749. "openid_assoc_handle" => self::HANDLE,
  750. "openid_claimed_id" => self::ID,
  751. "openid_identity" => self::REAL_ID,
  752. "openid_response_nonce" => "2007-08-14T12:52:33Z46c1a59124fff",
  753. "openid_mode" => "id_res",
  754. "openid_signed" => "assoc_handle,return_to,claimed_id,identity,response_nonce,mode,signed",
  755. "openid_sig" => "h/5AFD25NpzSok5tzHEGCVUkQSw="
  756. );
  757. $storage->delAssociation(self::SERVER);
  758. $storage->addAssociation(self::SERVER, self::HANDLE, "sha1", pack("H*", "8382aea922560ece833ba55fa53b7a975f597370"), $expiresIn);
  759. $storage->purgeNonces();
  760. $this->assertFalse( $consumer->verify($params) );
  761. $this->assertFalse( $storage->getAssociation(self::SERVER, $handle, $func, $secret, $expires) );
  762. // openid_invalidate_handle
  763. $consumer->clearAssociation();
  764. $params = array(
  765. "openid_return_to" => "http://www.zf-test.com/test.php",
  766. "openid_invalidate_handle" => self::HANDLE."1",
  767. "openid_assoc_handle" => self::HANDLE,
  768. "openid_claimed_id" => self::ID,
  769. "openid_identity" => self::REAL_ID,
  770. "openid_response_nonce" => "2007-08-14T12:52:33Z46c1a59124ffe",
  771. "openid_mode" => "id_res",
  772. "openid_signed" => "assoc_handle,return_to,claimed_id,identity,response_nonce,mode,signed",
  773. "openid_sig" => "h/5AFD25NpzSok5tzHEGCVUkQSw="
  774. );
  775. $storage->delAssociation(self::SERVER);
  776. $storage->addAssociation(self::SERVER, self::HANDLE, "sha1", pack("H*", "8382aea922560ece833ba55fa53b7a975f597370"), $expiresIn);
  777. $storage->delAssociation(self::SERVER."1");
  778. $storage->addAssociation(self::SERVER."1", self::HANDLE."1", "sha1", pack("H*", "8382aea922560ece833ba55fa53b7a975f597370"), $expiresIn);
  779. $storage->purgeNonces();
  780. $this->assertTrue( $consumer->verify($params) );
  781. $this->assertFalse( $storage->getAssociation(self::SERVER."1", $handle, $func, $secret, $expires) );
  782. $storage->delDiscoveryInfo(self::ID);
  783. }
  784. /**
  785. * testing verify
  786. *
  787. */
  788. public function testVerifyDumb()
  789. {
  790. $expiresIn = time() + 600;
  791. $_SERVER['SCRIPT_URI'] = "http://www.zf-test.com/test.php";
  792. $storage = new Zend_OpenId_Consumer_Storage_File(dirname(__FILE__)."/_files/consumer");
  793. $consumer = new Zend_OpenId_ConsumerHelper($storage);
  794. $http = new Zend_Http_Client(null,
  795. array(
  796. 'maxredirects' => 4,
  797. 'timeout' => 15,
  798. 'useragent' => 'Zend_OpenId'
  799. ));
  800. $test = new Zend_Http_Client_Adapter_Test();
  801. $http->setAdapter($test);
  802. $consumer->SetHttpClient($http);
  803. $storage->delDiscoveryInfo(self::ID);
  804. $this->assertTrue( $storage->addDiscoveryInfo(self::ID, self::REAL_ID, self::SERVER, 1.1, $expiresIn) );
  805. $this->assertTrue( $storage->addDiscoveryInfo(self::REAL_ID, self::REAL_ID, self::SERVER, 1.1, $expiresIn) );
  806. // Wrong arguments (no identity)
  807. $test->setResponse("HTTP/1.1 200 OK\r\n\r\nis_valid:true");
  808. $consumer->clearAssociation();
  809. $storage->delAssociation(self::SERVER);
  810. $params = array(
  811. "openid_return_to" => "http://www.zf-test.com/test.php",
  812. "openid_assoc_handle" => self::HANDLE,
  813. "openid_response_nonce" => "2007-08-14T12:52:33Z46c1a59124ffe",
  814. "openid_mode" => "id_res",
  815. "openid_signed" => "assoc_handle,return_to,response_nonce,mode,signed",
  816. "openid_sig" => "h/5AFD25NpzSok5tzHEGCVUkQSw="
  817. );
  818. $storage->purgeNonces();
  819. $this->assertFalse( $consumer->verify($params) );
  820. $test->setResponse("HTTP/1.1 200 OK\r\n\r\nis_valid:false");
  821. $consumer->clearAssociation();
  822. $storage->delAssociation(self::SERVER);
  823. $params = array(
  824. "openid_return_to" => "http://www.zf-test.com/test.php",
  825. "openid_assoc_handle" => self::HANDLE,
  826. "openid_claimed_id" => self::ID,
  827. "openid_identity" => self::REAL_ID,
  828. "openid_response_nonce" => "2007-08-14T12:52:33Z46c1a59124ffe",
  829. "openid_mode" => "id_res",
  830. "openid_signed" => "assoc_handle,return_to,claimed_id,identity,response_nonce,mode,signed",
  831. "openid_sig" => "h/5AFD25NpzSok5tzHEGCVUkQSw="
  832. );
  833. $storage->purgeNonces();
  834. $this->assertFalse( $consumer->verify($params) );
  835. $this->assertSame( "POST / HTTP/1.1\r\n" .
  836. "Host: www.myopenid.com\r\n" .
  837. "Connection: close\r\n" .
  838. "Accept-encoding: gzip, deflate\r\n" .
  839. "User-Agent: Zend_OpenId\r\n" .
  840. "Content-Type: application/x-www-form-urlencoded\r\n" .
  841. "Content-Length: 445\r\n\r\n" .
  842. "openid.return_to=http%3A%2F%2Fwww.zf-test.com%2Ftest.php&" .
  843. "openid.assoc_handle=d41d8cd98f00b204e9800998ecf8427e&" .
  844. "openid.claimed_id=http%3A%2F%2Fid.myopenid.com%2F&" .
  845. "openid.identity=http%3A%2F%2Freal_id.myopenid.com%2F&" .
  846. "openid.response_nonce=2007-08-14T12%3A52%3A33Z46c1a59124ffe&" .
  847. "openid.mode=check_authentication&" .
  848. "openid.signed=assoc_handle%2Creturn_to%2Cclaimed_id%2Cidentity%2Cresponse_nonce%2Cmode%2Csigned&" .
  849. "openid.sig=h%2F5AFD25NpzSok5tzHEGCVUkQSw%3D",
  850. $http->getLastRequest() );
  851. $test->setResponse("HTTP/1.1 200 OK\r\n\r\nis_valid:true");
  852. $consumer->clearAssociation();
  853. $storage->delAssociation(self::SERVER);
  854. $params = array(
  855. "openid_return_to" => "http://www.zf-test.com/test.php",
  856. "openid_assoc_handle" => self::HANDLE,
  857. "openid_identity" => self::REAL_ID,
  858. "openid_response_nonce" => "2007-08-14T12:52:33Z46c1a59124ffe",
  859. "openid_mode" => "id_res",
  860. "openid_signed" => "assoc_handle,return_to,identity,response_nonce,mode,signed",
  861. "openid_sig" => "h/5AFD25NpzSok5tzHEGCVUkQSw="
  862. );
  863. $storage->purgeNonces();
  864. $this->assertTrue( $consumer->verify($params) );
  865. // SREG
  866. $this->assertTrue( $storage->delDiscoveryInfo(self::ID) );
  867. $this->assertTrue( $storage->addDiscoveryInfo(self::ID, self::REAL_ID, self::SERVER, 2.0, $expiresIn) );
  868. $test->setResponse("HTTP/1.1 200 OK\r\n\r\nis_valid:true");
  869. $consumer->clearAssociation();
  870. $storage->delAssociation(self::SERVER);
  871. $params = array(
  872. "openid_ns" => Zend_OpenId::NS_2_0,
  873. "openid_return_to" => "http://www.zf-test.com/test.php",
  874. "openid_assoc_handle" => self::HANDLE,
  875. "openid_claimed_id" => self::ID,
  876. "openid_identity" => self::REAL_ID,
  877. "openid_response_nonce" => "2007-08-14T12:52:33Z46c1a59124ffe",
  878. "openid_op_endpoint" => self::SERVER,
  879. "openid_mode" => "id_res",
  880. "openid_ns_sreg" => "http://openid.net/extensions/sreg/1.1",
  881. "openid_sreg_nickname" => "test",
  882. "openid_signed" => "ns,assoc_handle,return_to,claimed_id,identity,response_nonce,mode,ns.sreg,sreg.nickname,signed",
  883. "openid_sig" => "h/5AFD25NpzSok5tzHEGCVUkQSw="
  884. );
  885. $storage->purgeNonces();
  886. $this->assertTrue( $consumer->verify($params) );
  887. $this->assertSame( "POST / HTTP/1.1\r\n" .
  888. "Host: www.myopenid.com\r\n" .
  889. "Connection: close\r\n" .
  890. "Accept-encoding: gzip, deflate\r\n" .
  891. "User-Agent: Zend_OpenId\r\n" .
  892. "Content-Type: application/x-www-form-urlencoded\r\n" .
  893. "Content-Length: 672\r\n\r\n" .
  894. "openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&" .
  895. "openid.return_to=http%3A%2F%2Fwww.zf-test.com%2Ftest.php&" .
  896. "openid.assoc_handle=d41d8cd98f00b204e9800998ecf8427e&" .
  897. "openid.claimed_id=http%3A%2F%2Fid.myopenid.com%2F&" .
  898. "openid.identity=http%3A%2F%2Freal_id.myopenid.com%2F&" .
  899. "openid.response_nonce=2007-08-14T12%3A52%3A33Z46c1a59124ffe&" .
  900. "openid.op_endpoint=http%3A%2F%2Fwww.myopenid.com%2F&" .
  901. "openid.mode=check_authentication&" .
  902. "openid.ns.sreg=http%3A%2F%2Fopenid.net%2Fextensions%2Fsreg%2F1.1&" .
  903. "openid.sreg.nickname=test&" .
  904. "openid.signed=ns%2Cassoc_handle%2Creturn_to%2Cclaimed_id%2Cidentity%2Cresponse_nonce%2Cmode%2Cns.sreg%2Csreg.nickname%2Csigned&" .
  905. "openid.sig=h%2F5AFD25NpzSok5tzHEGCVUkQSw%3D",
  906. $http->getLastRequest() );
  907. // invalidate_handle
  908. $test->setResponse("HTTP/1.1 200 OK\r\n\r\nis_valid:false\ninvalidate_handle:".self::HANDLE."1"."\n");
  909. $consumer->clearAssociation();
  910. $params = array(
  911. "openid_ns" => Zend_OpenId::NS_2_0,
  912. "openid_return_to" => "http://www.zf-test.com/test.php",
  913. "openid_assoc_handle" => self::HANDLE,
  914. "openid_claimed_id" => self::ID,
  915. "openid_identity" => self::REAL_ID,
  916. "openid_response_nonce" => "2007-08-14T12:52:33Z46c1a59124ffe",
  917. "openid_op_endpoint" => self::SERVER,
  918. "openid_mode" => "id_res",
  919. "openid_signed" => "assoc_handle,return_to,claimed_id,identity,response_nonce,mode,signed",
  920. "openid_sig" => "h/5AFD25NpzSok5tzHEGCVUkQSw="
  921. );
  922. $storage->delAssociation(self::SERVER."1");
  923. $storage->addAssociation(self::SERVER."1", self::HANDLE."1", "sha1", pack("H*", "8382aea922560ece833ba55fa53b7a975f597370"), $expiresIn);
  924. $storage->purgeNonces();
  925. $this->assertFalse( $consumer->verify($params) );
  926. $this->assertFalse( $storage->getAssociation(self::SERVER."1", $handle, $func, $secret, $expires) );
  927. }
  928. }
  929. class Zend_OpenId_ConsumerHelper extends Zend_OpenId_Consumer {
  930. public function addAssociation($url, $handle, $macFunc, $secret, $expires)
  931. {
  932. return $this->_addAssociation($url, $handle, $macFunc, $secret, $expires);
  933. }
  934. public function getAssociation($url, &$handle, &$macFunc, &$secret, &$expires)
  935. {
  936. return $this->_getAssociation($url, $handle, $macFunc, $secret, $expires);
  937. }
  938. public function clearAssociation()
  939. {
  940. $this->_cache = array();
  941. }
  942. public function httpRequest($url, $method = 'GET', array $params = array())
  943. {
  944. return $this->_httpRequest($url, $method, $params);
  945. }
  946. public function associate($url, $version, $priv_key = null)
  947. {
  948. return $this->_associate($url, $version, $priv_key);
  949. }
  950. public function discovery(&$id, &$server, &$version)
  951. {
  952. return $this->_discovery($id, $server, $version);
  953. }
  954. }