VerifyDisco.php 16 KB

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