VerifyDisco.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. <?php
  2. require_once "Tests/Auth/OpenID/TestUtil.php";
  3. require_once "Tests/Auth/OpenID/MemStore.php";
  4. require_once "Auth/OpenID/Message.php";
  5. require_once "Auth/OpenID/Consumer.php";
  6. class Tests_Auth_OpenID_VerifyDisco_1 extends Auth_OpenID_GenericConsumer {
  7. function _discoverAndVerify($claimed_id, $to_match_endpoints)
  8. {
  9. $this->test_case->assertEquals($this->endpoint->claimed_id, $claimed_id);
  10. return new Auth_OpenID_FailureResponse(null, $this->text);
  11. }
  12. }
  13. class __VerifiedError extends Auth_OpenID_FailureResponse {
  14. }
  15. class VerifyDisco_Consumer_verifiedError extends Auth_OpenID_GenericConsumer {
  16. function _discoverAndVerify($to_match)
  17. {
  18. return new __VerifiedError(null, 'verified error');
  19. }
  20. }
  21. class _DiscoverAndVerify extends OpenIDTestMixin {
  22. var $consumer_class = 'Auth_OpenID_GenericConsumer';
  23. function setUp()
  24. {
  25. $this->store = new Tests_Auth_OpenID_MemStore();
  26. $cl = $this->consumer_class;
  27. $this->consumer = new $cl($this->store);
  28. $this->return_to = "http://some.host/path";
  29. $this->endpoint = new Auth_OpenID_ServiceEndpoint();
  30. $this->server_id = "sirod";
  31. $this->server_url = "serlie";
  32. $this->consumer_id = "consu";
  33. $this->endpoint->claimed_id = $this->consumer_id;
  34. $this->endpoint->server_url = $this->server_url;
  35. $this->endpoint->local_id = $this->server_id;
  36. $this->endpoint->type_uris = array(Auth_OpenID_TYPE_1_1);
  37. }
  38. function failUnlessProtocolError($thing)
  39. {
  40. $this->assertTrue(Auth_OpenID::isFailure($thing));
  41. }
  42. }
  43. class _Tests_discoveryOverride {
  44. function _Tests_discoveryOverride($endpoint)
  45. {
  46. $this->endpoint = $endpoint;
  47. }
  48. function discover($unused_url)
  49. {
  50. return array($this->endpoint->claimed_id, array($this->endpoint));
  51. }
  52. }
  53. class Tests_openID1Fallback1_0 extends _DiscoverAndVerify {
  54. function test_openID1Fallback1_0()
  55. {
  56. $claimed_id = 'http://claimed.id/';
  57. $resp_msg = Auth_OpenID_Message::fromOpenIDArgs(
  58. array('ns' => Auth_OpenID_OPENID1_NS,
  59. 'identity' => $claimed_id));
  60. $resp_msg->setArg(Auth_OpenID_BARE_NS, 'openid1_claimed_id',
  61. $claimed_id);
  62. $expected_endpoint = new Auth_OpenID_ServiceEndpoint();
  63. $expected_endpoint->type_uris = array(Auth_OpenID_TYPE_1_0);
  64. $expected_endpoint->local_id = null;
  65. $expected_endpoint->claimed_id = $claimed_id;
  66. $discovery_override = new _Tests_discoveryOverride($expected_endpoint);
  67. $this->consumer->discoverMethod = array($discovery_override, 'discover');
  68. $actual_endpoint = $this->consumer->_verifyDiscoveryResults(
  69. $resp_msg, null);
  70. $this->assertTrue(is_a($actual_endpoint, "Auth_OpenID_ServiceEndpoint"));
  71. $this->assertEquals($expected_endpoint->local_id,
  72. $actual_endpoint->local_id);
  73. $this->assertEquals($expected_endpoint->server_url,
  74. $actual_endpoint->server_url);
  75. $this->assertEquals($expected_endpoint->type_uris,
  76. $actual_endpoint->type_uris);
  77. $this->assertEquals($expected_endpoint->claimed_id,
  78. $actual_endpoint->claimed_id);
  79. }
  80. }
  81. class Tests_Auth_OpenID_VerifyDisco extends _DiscoverAndVerify {
  82. function test_openID1NoLocalID()
  83. {
  84. $endpoint = new Auth_OpenID_ServiceEndpoint();
  85. $endpoint->claimed_id = 'bogus';
  86. $msg = Auth_OpenID_Message::fromOpenIDArgs(array());
  87. // 'Missing required field openid.identity'
  88. $this->failUnlessProtocolError($this->consumer->_verifyDiscoveryResults($msg, $endpoint));
  89. }
  90. function test_openID1NoEndpoint()
  91. {
  92. $msg = Auth_OpenID_Message::fromOpenIDArgs(array('identity' => 'snakes on a plane'));
  93. $this->failUnlessProtocolError($this->consumer->_verifyDiscoveryResults($msg));
  94. }
  95. function test_openID2NoOPEndpointArg()
  96. {
  97. $msg = Auth_OpenID_Message::fromOpenIDArgs(array('ns' => Auth_OpenID_OPENID2_NS));
  98. $this->failUnlessProtocolError($this->consumer->_verifyDiscoveryResults($msg, null));
  99. }
  100. function test_openID2LocalIDNoClaimed()
  101. {
  102. $msg = Auth_OpenID_Message::fromOpenIDArgs(array('ns' => Auth_OpenID_OPENID2_NS,
  103. 'op_endpoint' => 'Phone Home',
  104. 'identity' => 'Jose Lius Borges'));
  105. // 'openid.identity is present without',
  106. $this->failUnlessProtocolError($this->consumer->_verifyDiscoveryResults($msg));
  107. }
  108. function test_openID2NoLocalIDClaimed()
  109. {
  110. $msg = Auth_OpenID_Message::fromOpenIDArgs(array('ns' => Auth_OpenID_OPENID2_NS,
  111. 'op_endpoint' => 'Phone Home',
  112. 'claimed_id' => 'Manuel Noriega'));
  113. // 'openid.claimed_id is present without',
  114. $this->failUnlessProtocolError(
  115. $this->consumer->_verifyDiscoveryResults($msg));
  116. }
  117. function test_openID2NoIdentifiers()
  118. {
  119. $op_endpoint = 'Phone Home';
  120. $msg = Auth_OpenID_Message::fromOpenIDArgs(array('ns' => Auth_OpenID_OPENID2_NS,
  121. 'op_endpoint' => $op_endpoint));
  122. $result_endpoint = $this->consumer->_verifyDiscoveryResults($msg);
  123. $this->assertTrue($result_endpoint->isOPIdentifier());
  124. $this->assertEquals($op_endpoint, $result_endpoint->server_url);
  125. $this->assertEquals(null, $result_endpoint->claimed_id);
  126. }
  127. function test_openid2UsePreDiscovered()
  128. {
  129. $endpoint = new Auth_OpenID_ServiceEndpoint();
  130. $endpoint->local_id = 'my identity';
  131. $endpoint->claimed_id = 'i am sam';
  132. $endpoint->server_url = 'Phone Home';
  133. $endpoint->type_uris = array(Auth_OpenID_TYPE_2_0);
  134. $msg = Auth_OpenID_Message::fromOpenIDArgs(
  135. array('ns' => Auth_OpenID_OPENID2_NS,
  136. 'identity' => $endpoint->local_id,
  137. 'claimed_id' => $endpoint->claimed_id,
  138. 'op_endpoint' => $endpoint->server_url));
  139. $result = $this->consumer->_verifyDiscoveryResults($msg, $endpoint);
  140. $this->assertTrue($result === $endpoint);
  141. }
  142. function test_openid2UsePreDiscoveredWrongType()
  143. {
  144. $this->consumer = new Tests_Auth_OpenID_VerifyDisco_1($this->store);
  145. $this->consumer->test_case =& $this;
  146. $this->consumer->text = "verify failed";
  147. $endpoint = new Auth_OpenID_ServiceEndpoint();
  148. $endpoint->local_id = 'my identity';
  149. $endpoint->claimed_id = 'i am sam';
  150. $endpoint->server_url = 'Phone Home';
  151. $endpoint->type_uris = array(Auth_OpenID_TYPE_1_1);
  152. $this->consumer->endpoint =& $endpoint;
  153. $msg = Auth_OpenID_Message::fromOpenIDArgs(
  154. array('ns' => Auth_OpenID_OPENID2_NS,
  155. 'identity' => $endpoint->local_id,
  156. 'claimed_id' => $endpoint->claimed_id,
  157. 'op_endpoint' => $endpoint->server_url));
  158. $result = $this->consumer->_verifyDiscoveryResults($msg, $endpoint);
  159. $this->failUnlessProtocolError($result);
  160. $this->assertTrue($result->message == "verify failed");
  161. }
  162. function test_openid1UsePreDiscovered()
  163. {
  164. $endpoint = new Auth_OpenID_ServiceEndpoint();
  165. $endpoint->local_id = 'my identity';
  166. $endpoint->claimed_id = 'i am sam';
  167. $endpoint->server_url = 'Phone Home';
  168. $endpoint->type_uris = array(Auth_OpenID_TYPE_1_1);
  169. $msg = Auth_OpenID_Message::fromOpenIDArgs(
  170. array('ns' => Auth_OpenID_OPENID1_NS,
  171. 'identity' => $endpoint->local_id));
  172. $result = $this->consumer->_verifyDiscoveryResults($msg, $endpoint);
  173. $this->assertTrue($result == $endpoint);
  174. }
  175. function test_openid2Fragment()
  176. {
  177. $claimed_id = "http://unittest.invalid/";
  178. $claimed_id_frag = $claimed_id . "#fragment";
  179. $endpoint = new Auth_OpenID_ServiceEndpoint();
  180. $endpoint->local_id = 'my identity';
  181. $endpoint->claimed_id = $claimed_id;
  182. $endpoint->server_url = 'Phone Home';
  183. $endpoint->type_uris = array(Auth_OpenID_TYPE_2_0);
  184. $msg = Auth_OpenID_Message::fromOpenIDArgs(
  185. array('ns' => Auth_OpenID_OPENID2_NS,
  186. 'identity' => $endpoint->local_id,
  187. 'claimed_id' => $claimed_id_frag,
  188. 'op_endpoint' => $endpoint->server_url));
  189. $result = $this->consumer->_verifyDiscoveryResults($msg, $endpoint);
  190. $this->assertEquals($result->local_id, $endpoint->local_id);
  191. $this->assertEquals($result->server_url, $endpoint->server_url);
  192. $this->assertEquals($result->type_uris, $endpoint->type_uris);
  193. $this->assertEquals($result->claimed_id, $claimed_id_frag);
  194. }
  195. }
  196. class Tests_openid1UsePreDiscoveredWrongType extends _DiscoverAndVerify {
  197. var $consumer_class = 'VerifyDisco_Consumer_verifiedError';
  198. function test_openid1UsePreDiscoveredWrongType()
  199. {
  200. $endpoint = new Auth_OpenID_ServiceEndpoint();
  201. $endpoint->local_id = 'my identity';
  202. $endpoint->claimed_id = 'i am sam';
  203. $endpoint->server_url = 'Phone Home';
  204. $endpoint->type_uris = array(Auth_OpenID_TYPE_2_0);
  205. $msg = Auth_OpenID_Message::fromOpenIDArgs(
  206. array('ns' => Auth_OpenID_OPENID1_NS,
  207. 'identity' => $endpoint->local_id));
  208. $result = $this->consumer->_verifyDiscoveryResults($msg, $endpoint);
  209. $this->failUnlessProtocolError($result);
  210. $this->assertTrue(is_a($result, '__VerifiedError'));
  211. }
  212. }
  213. // XXX: test the implementation of _discoverAndVerify
  214. class Tests_openID2NoEndpointDoesDisco_sentinel extends Auth_OpenID_GenericConsumer {
  215. var $sentinel = 'blah';
  216. function _discoverAndVerify($to_match)
  217. {
  218. return $this->sentinel;
  219. }
  220. }
  221. class Tests_openID2NoEndpointDoesDisco_failure extends Auth_OpenID_GenericConsumer {
  222. var $failure_message = 'A fake failure response message';
  223. function _verifyDiscoverySingle($to_match)
  224. {
  225. return new Auth_OpenID_FailureResponse(null, $this->failure_message);
  226. }
  227. }
  228. class Tests_openID2NoEndpointDoesDisco extends Tests_Auth_OpenID_VerifyDisco {
  229. var $consumer_class = 'Tests_openID2NoEndpointDoesDisco_sentinel';
  230. function test_openID2NoEndpointDoesDisco()
  231. {
  232. $op_endpoint = 'Phone Home';
  233. $this->consumer->sentinel = new Auth_OpenID_ServiceEndpoint();
  234. $this->consumer->sentinel->claimed_id = 'monkeysoft';
  235. $msg = Auth_OpenID_Message::fromOpenIDArgs(
  236. array('ns' => Auth_OpenID_OPENID2_NS,
  237. 'identity' => 'sour grapes',
  238. 'claimed_id' => 'monkeysoft',
  239. 'op_endpoint' => $op_endpoint));
  240. $result = $this->consumer->_verifyDiscoveryResults($msg);
  241. $this->assertEquals($this->consumer->sentinel, $result);
  242. }
  243. }
  244. class Tests_openID2MismatchedDoesDisco extends Tests_Auth_OpenID_VerifyDisco {
  245. var $consumer_class = 'Tests_openID2NoEndpointDoesDisco_sentinel';
  246. function test_openID2MismatchedDoesDisco()
  247. {
  248. $mismatched = new Auth_OpenID_ServiceEndpoint();
  249. $mismatched->identity = 'nothing special, but different';
  250. $mismatched->local_id = 'green cheese';
  251. $sentinel = new Auth_OpenID_ServiceEndpoint();
  252. $sentinel->claimed_id = 'monkeysoft';
  253. $this->consumer->sentinel = $sentinel;
  254. $op_endpoint = 'Phone Home';
  255. $msg = Auth_OpenID_Message::fromOpenIDArgs(
  256. array('ns' => Auth_OpenID_OPENID2_NS,
  257. 'identity' => 'sour grapes',
  258. 'claimed_id' => 'monkeysoft',
  259. 'op_endpoint' => $op_endpoint));
  260. $result = $this->consumer->_verifyDiscoveryResults($msg, $mismatched);
  261. $this->assertEquals($this->consumer->sentinel, $result);
  262. }
  263. }
  264. class Tests_openID2MismatchedDoesDisco_failure extends PHPUnit_Framework_TestCase {
  265. var $consumer_class = 'Tests_openID2NoEndpointDoesDisco_failure';
  266. function setUp()
  267. {
  268. $this->store = new Tests_Auth_OpenID_MemStore();
  269. $cl = $this->consumer_class;
  270. $this->consumer = new $cl($this->store);
  271. $this->return_to = "http://some.host/path";
  272. $this->endpoint = new Auth_OpenID_ServiceEndpoint();
  273. $this->consumer->discoverMethod = array($this, "_getServices");
  274. $this->server_id = "sirod";
  275. $this->server_url = "serlie";
  276. $this->consumer_id = "consu";
  277. $this->endpoint->claimed_id = $this->consumer_id;
  278. $this->endpoint->server_url = $this->server_url;
  279. $this->endpoint->local_id = $this->server_id;
  280. $this->endpoint->type_uris = array(Auth_OpenID_TYPE_1_1);
  281. }
  282. function _getServices($claimed_id, $fetcher=null) {
  283. return array(null, array($this->endpoint));
  284. }
  285. function test_openID2MismatchedDoesDisco_failure()
  286. {
  287. $mismatched = new Auth_OpenID_ServiceEndpoint();
  288. $mismatched->identity = 'nothing special, but different';
  289. $mismatched->local_id = 'green cheese';
  290. $op_endpoint = 'Phone Home';
  291. $msg = Auth_OpenID_Message::fromOpenIDArgs(
  292. array('ns' => Auth_OpenID_OPENID2_NS,
  293. 'identity' => 'sour grapes',
  294. 'claimed_id' => 'monkeysoft',
  295. 'op_endpoint' => $op_endpoint));
  296. $result = $this->consumer->_verifyDiscoveryResults($msg, $mismatched);
  297. $this->assertTrue(Auth_OpenID::isFailure($result));
  298. }
  299. }
  300. class TestVerifyDiscoverySingle extends OpenIDTestMixin {
  301. var $consumer_class = 'Auth_OpenID_GenericConsumer';
  302. function setUp()
  303. {
  304. $this->store = new Tests_Auth_OpenID_MemStore();
  305. $cl = $this->consumer_class;
  306. $this->consumer = new $cl($this->store);
  307. $this->return_to = "http://some.host/path";
  308. $this->endpoint = new Auth_OpenID_ServiceEndpoint();
  309. $this->server_id = "sirod";
  310. $this->server_url = "serlie";
  311. $this->consumer_id = "consu";
  312. $this->endpoint->claimed_id = $this->consumer_id;
  313. $this->endpoint->server_url = $this->server_url;
  314. $this->endpoint->local_id = $this->server_id;
  315. $this->endpoint->type_uris = array(Auth_OpenID_TYPE_1_1);
  316. }
  317. function test_endpointWithoutLocalID()
  318. {
  319. // An endpoint like this with no local_id is generated as a
  320. // result of e.g. Yadis discovery with no LocalID tag.
  321. $endpoint = new Auth_OpenID_ServiceEndpoint();
  322. $endpoint->server_url = "http://localhost:8000/openidserver";
  323. $endpoint->claimed_id = "http://localhost:8000/id/id-jo";
  324. $to_match = new Auth_OpenID_ServiceEndpoint();
  325. $to_match->server_url = "http://localhost:8000/openidserver";
  326. $to_match->claimed_id = "http://localhost:8000/id/id-jo";
  327. $to_match->local_id = "http://localhost:8000/id/id-jo";
  328. $result = $this->consumer->_verifyDiscoverySingle($endpoint, $to_match);
  329. // result should always be None, raises exception on failure.
  330. $this->assertEquals($result, null);
  331. }
  332. }
  333. global $Tests_Auth_OpenID_VerifyDisco_other;
  334. $Tests_Auth_OpenID_VerifyDisco_other = array(
  335. new Tests_openID2MismatchedDoesDisco(),
  336. new Tests_openID2NoEndpointDoesDisco(),
  337. new Tests_openID2MismatchedDoesDisco_failure(),
  338. new Tests_openid1UsePreDiscoveredWrongType(),
  339. new Tests_openID1Fallback1_0(),
  340. );