AuthRequest.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. require_once 'Tests/Auth/OpenID/TestUtil.php';
  3. require_once 'Auth/OpenID/Association.php';
  4. require_once 'Auth/OpenID/Consumer.php';
  5. class AuthRequest_DummyEndpoint {
  6. var $preferred_namespace = null;
  7. var $local_id = null;
  8. var $server_url = null;
  9. var $is_op_identifier = false;
  10. function preferredNamespace()
  11. {
  12. return $this->preferred_namespace;
  13. }
  14. function getLocalID()
  15. {
  16. return $this->local_id;
  17. }
  18. function isOPIdentifier()
  19. {
  20. return $this->is_op_identifier;
  21. }
  22. }
  23. class AuthRequest_DummyAssoc {
  24. var $handle = "assoc-handle";
  25. }
  26. /**
  27. * Base for AuthRequest tests for OpenID 1 and 2.
  28. */
  29. class TestAuthRequestMixin extends OpenIDTestMixin {
  30. var $preferred_namespace = null;
  31. var $immediate = false;
  32. var $expected_mode = 'checkid_setup';
  33. function setUp()
  34. {
  35. $this->endpoint = new AuthRequest_DummyEndpoint();
  36. $this->endpoint->local_id = 'http://server.unittest/joe';
  37. $this->endpoint->claimed_id = 'http://joe.vanity.example/';
  38. $this->endpoint->server_url = 'http://server.unittest/';
  39. $this->endpoint->preferred_namespace = $this->preferred_namespace;
  40. $this->realm = 'http://example/';
  41. $this->return_to = 'http://example/return/';
  42. $this->assoc = new AuthRequest_DummyAssoc();
  43. $this->authreq = new Auth_OpenID_AuthRequest($this->endpoint, $this->assoc);
  44. }
  45. function failUnlessAnonymous($msg)
  46. {
  47. foreach (array('claimed_id', 'identity') as $key) {
  48. $this->failIfOpenIDKeyExists($msg, $key);
  49. }
  50. }
  51. function failUnlessHasRequiredFields($msg)
  52. {
  53. $this->assertEquals($this->preferred_namespace,
  54. $this->authreq->message->getOpenIDNamespace());
  55. $this->assertEquals($this->preferred_namespace,
  56. $msg->getOpenIDNamespace());
  57. $this->failUnlessOpenIDValueEquals($msg, 'mode',
  58. $this->expected_mode);
  59. // Implement these in subclasses because they depend on
  60. // protocol differences!
  61. $this->failUnlessHasRealm($msg);
  62. $this->failUnlessIdentifiersPresent($msg);
  63. }
  64. // TESTS
  65. function test_checkNoAssocHandle()
  66. {
  67. $this->authreq->assoc = null;
  68. $msg = $this->authreq->getMessage($this->realm, $this->return_to,
  69. $this->immediate);
  70. $this->failIfOpenIDKeyExists($msg, 'assoc_handle');
  71. }
  72. function test_checkWithAssocHandle()
  73. {
  74. $msg = $this->authreq->getMessage($this->realm, $this->return_to,
  75. $this->immediate);
  76. $this->failUnlessOpenIDValueEquals($msg, 'assoc_handle',
  77. $this->assoc->handle);
  78. }
  79. function test_addExtensionArg()
  80. {
  81. $this->authreq->addExtensionArg('bag:', 'color', 'brown');
  82. $this->authreq->addExtensionArg('bag:', 'material', 'paper');
  83. $this->assertTrue($this->authreq->message->namespaces->contains('bag:'));
  84. $this->assertEquals($this->authreq->message->getArgs('bag:'),
  85. array('color' => 'brown',
  86. 'material' => 'paper'));
  87. $msg = $this->authreq->getMessage($this->realm, $this->return_to,
  88. $this->immediate);
  89. // XXX: this depends on the way that Message assigns
  90. // namespaces. Really it doesn't care that it has alias "0",
  91. // but that is tested anyway
  92. $post_args = $msg->toPostArgs();
  93. $this->assertEquals('brown', $post_args['openid.ext0.color']);
  94. $this->assertEquals('paper', $post_args['openid.ext0.material']);
  95. }
  96. function test_standard()
  97. {
  98. $msg = $this->authreq->getMessage($this->realm, $this->return_to,
  99. $this->immediate);
  100. $this->failUnlessHasIdentifiers(
  101. $msg, $this->endpoint->local_id,
  102. $this->endpoint->claimed_id);
  103. }
  104. }
  105. class TestAuthRequestOpenID2 extends TestAuthRequestMixin {
  106. var $preferred_namespace = Auth_OpenID_OPENID2_NS;
  107. function failUnlessHasRealm($msg)
  108. {
  109. // check presence of proper realm key and absence of the wrong
  110. // one.
  111. $this->failUnlessOpenIDValueEquals($msg, 'realm', $this->realm);
  112. $this->failIfOpenIDKeyExists($msg, 'trust_root');
  113. }
  114. function failUnlessIdentifiersPresent($msg)
  115. {
  116. $identity_present = $msg->hasKey(Auth_OpenID_OPENID_NS, 'identity');
  117. $claimed_present = $msg->hasKey(Auth_OpenID_OPENID_NS, 'claimed_id');
  118. $this->assertEquals($claimed_present, $identity_present);
  119. }
  120. function failUnlessHasIdentifiers($msg, $op_specific_id, $claimed_id)
  121. {
  122. $this->failUnlessOpenIDValueEquals($msg, 'identity', $op_specific_id);
  123. $this->failUnlessOpenIDValueEquals($msg, 'claimed_id', $claimed_id);
  124. }
  125. // TESTS
  126. function test_markup_checkidImmediate()
  127. {
  128. $result = $this->authreq->formMarkup($this->realm,
  129. null, true);
  130. $this->assertTrue(Auth_OpenID::isFailure($result));
  131. }
  132. function test_markup_returnToArgs()
  133. {
  134. $this->authreq->return_to_args = array('extra' => 'args');
  135. $result = $this->authreq->formMarkup($this->realm,
  136. null, false);
  137. $this->assertTrue(Auth_OpenID::isFailure($result));
  138. }
  139. function test_setAnonymousWorksForOpenID2()
  140. {
  141. // OpenID AuthRequests should be able to set 'anonymous' to true.
  142. $this->assertTrue($this->authreq->message->isOpenID2());
  143. $this->assertTrue($this->authreq->setAnonymous(true));
  144. $this->assertTrue($this->authreq->setAnonymous(false));
  145. }
  146. function test_userAnonymousIgnoresIdentfier()
  147. {
  148. $this->authreq->setAnonymous(true);
  149. $msg = $this->authreq->getMessage($this->realm, $this->return_to,
  150. $this->immediate);
  151. $this->failUnlessHasRequiredFields($msg);
  152. $this->failUnlessAnonymous($msg);
  153. }
  154. function test_opAnonymousIgnoresIdentifier()
  155. {
  156. $this->endpoint->is_op_identifier = true;
  157. $this->authreq->setAnonymous(true);
  158. $msg = $this->authreq->getMessage($this->realm, $this->return_to,
  159. $this->immediate);
  160. $this->failUnlessHasRequiredFields($msg);
  161. $this->failUnlessAnonymous($msg);
  162. }
  163. function test_opIdentifierSendsIdentifierSelect()
  164. {
  165. $this->endpoint->is_op_identifier = true;
  166. $msg = $this->authreq->getMessage($this->realm, $this->return_to,
  167. $this->immediate);
  168. $this->failUnlessHasRequiredFields($msg);
  169. $this->failUnlessHasIdentifiers($msg,
  170. Auth_OpenID_IDENTIFIER_SELECT,
  171. Auth_OpenID_IDENTIFIER_SELECT);
  172. }
  173. }
  174. class TestAuthRequestOpenID1 extends TestAuthRequestMixin {
  175. var $preferred_namespace = Auth_OpenID_OPENID1_NS;
  176. function setUpEndpoint()
  177. {
  178. parent::setUpEndpoint();
  179. $this->endpoint->preferred_namespace = Auth_OpenID_OPENID1_NS;
  180. }
  181. function failUnlessHasIdentifiers($msg, $op_specific_id, $claimed_id)
  182. {
  183. // Make sure claimed_is is *absent* in request.
  184. $this->failUnlessOpenIDValueEquals($msg, 'identity', $op_specific_id);
  185. $this->failIfOpenIDKeyExists($msg, 'claimed_id');
  186. }
  187. function failUnlessIdentifiersPresent($msg)
  188. {
  189. $this->failIfOpenIDKeyExists($msg, 'claimed_id');
  190. $this->assertTrue($msg->hasKey(Auth_OpenID_OPENID_NS, 'identity'));
  191. }
  192. function failUnlessHasRealm($msg)
  193. {
  194. // check presence of proper realm key and absence of the wrong
  195. // one.
  196. $this->failUnlessOpenIDValueEquals($msg, 'trust_root', $this->realm);
  197. $this->failIfOpenIDKeyExists($msg, 'realm');
  198. }
  199. // TESTS
  200. function test_markup_missingReturnTo()
  201. {
  202. $result = $this->authreq->formMarkup($this->realm,
  203. null, false);
  204. $this->assertTrue(Auth_OpenID::isFailure($result));
  205. }
  206. function test_setAnonymousFailsForOpenID1()
  207. {
  208. // OpenID 1 requests MUST NOT be able to set anonymous to True
  209. $this->assertTrue($this->authreq->message->isOpenID1());
  210. $this->assertFalse($this->authreq->setAnonymous(true));
  211. $this->assertTrue($this->authreq->setAnonymous(false));
  212. }
  213. function test_identifierSelect()
  214. {
  215. // Identfier select SHOULD NOT be sent, but this pathway is in
  216. // here in case some special discovery stuff is done to
  217. // trigger it with OpenID 1. If it is triggered, it will send
  218. // identifier_select just like OpenID 2.
  219. $this->endpoint->is_op_identifier = true;
  220. $msg = $this->authreq->getMessage($this->realm, $this->return_to,
  221. $this->immediate);
  222. $this->failUnlessHasRequiredFields($msg);
  223. $this->assertEquals(Auth_OpenID_IDENTIFIER_SELECT,
  224. $msg->getArg(Auth_OpenID_OPENID1_NS,
  225. 'identity'));
  226. }
  227. }
  228. class TestAuthRequestOpenID1Immediate extends TestAuthRequestOpenID1 {
  229. var $immediate = true;
  230. var $expected_mode = 'checkid_immediate';
  231. }
  232. class TestAuthRequestOpenID2Immediate extends TestAuthRequestOpenID2 {
  233. var $immediate = true;
  234. var $expected_mode = 'checkid_immediate';
  235. }
  236. class Tests_Auth_OpenID_AuthRequest extends PHPUnit_Framework_TestSuite {
  237. function getName()
  238. {
  239. return "Tests_Auth_OpenID_AuthRequest";
  240. }
  241. function Tests_Auth_OpenID_AuthRequest()
  242. {
  243. $this->addTestSuite('TestAuthRequestOpenID1');
  244. $this->addTestSuite('TestAuthRequestOpenID1Immediate');
  245. $this->addTestSuite('TestAuthRequestOpenID2');
  246. $this->addTestSuite('TestAuthRequestOpenID2Immediate');
  247. }
  248. }