openid.test 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. <?php
  2. /**
  3. * @file
  4. * Tests for openid.module.
  5. */
  6. /**
  7. * Base class for OpenID tests.
  8. */
  9. abstract class OpenIDWebTestCase extends DrupalWebTestCase {
  10. /**
  11. * Initiates the login procedure using the specified User-supplied Identity.
  12. */
  13. function submitLoginForm($identity) {
  14. // Fill out and submit the login form.
  15. $edit = array('openid_identifier' => $identity);
  16. $this->drupalPost('', $edit, t('Log in'));
  17. // Check we are on the OpenID redirect form.
  18. $this->assertTitle(t('OpenID redirect'), 'OpenID redirect page was displayed.');
  19. // Submit form to the OpenID Provider Endpoint.
  20. $this->drupalPost(NULL, array(), t('Send'));
  21. }
  22. /**
  23. * Parses the last sent e-mail and returns the one-time login link URL.
  24. */
  25. function getPasswordResetURLFromMail() {
  26. $mails = $this->drupalGetMails();
  27. $mail = end($mails);
  28. preg_match('@.+user/reset/.+@', $mail['body'], $matches);
  29. return $matches[0];
  30. }
  31. }
  32. /**
  33. * Test discovery and login using OpenID
  34. */
  35. class OpenIDFunctionalTestCase extends OpenIDWebTestCase {
  36. protected $web_user;
  37. public static function getInfo() {
  38. return array(
  39. 'name' => 'OpenID discovery and login',
  40. 'description' => "Adds an identity to a user's profile and uses it to log in.",
  41. 'group' => 'OpenID'
  42. );
  43. }
  44. function setUp() {
  45. parent::setUp('openid', 'openid_test');
  46. // User doesn't need special permissions; only the ability to log in.
  47. $this->web_user = $this->drupalCreateUser(array());
  48. }
  49. /**
  50. * Test discovery of OpenID Provider Endpoint via Yadis and HTML.
  51. */
  52. function testDiscovery() {
  53. $this->drupalLogin($this->web_user);
  54. // The User-supplied Identifier entered by the user may indicate the URL of
  55. // the OpenID Provider Endpoint in various ways, as described in OpenID
  56. // Authentication 2.0 and Yadis Specification 1.0.
  57. // Note that all of the tested identifiers refer to the same endpoint, so
  58. // only the first will trigger an associate request in openid_association()
  59. // (association is only done the first time Drupal encounters a given
  60. // endpoint).
  61. // Yadis discovery (see Yadis Specification 1.0, section 6.2.5):
  62. // If the User-supplied Identifier is a URL, it may be a direct or indirect
  63. // reference to an XRDS document (a Yadis Resource Descriptor) that contains
  64. // the URL of the OpenID Provider Endpoint.
  65. // Identifier is the URL of an XRDS document.
  66. // On HTTP test environments, the URL scheme is stripped in order to test
  67. // that the supplied identifier is normalized in openid_begin().
  68. $identity = url('openid-test/yadis/xrds', array('absolute' => TRUE));
  69. $this->addIdentity(preg_replace('@^http://@', '', $identity), 2, 'http://example.com/xrds', $identity);
  70. $identity = url('openid-test/yadis/xrds/delegate', array('absolute' => TRUE));
  71. $this->addIdentity(preg_replace('@^http://@', '', $identity), 2, 'http://example.com/xrds-delegate', $identity);
  72. // Identifier is the URL of an XRDS document containing an OP Identifier
  73. // Element. The Relying Party sends the special value
  74. // "http://specs.openid.net/auth/2.0/identifier_select" as Claimed
  75. // Identifier. The OpenID Provider responds with the actual identifier
  76. // including the fragment.
  77. $identity = url('openid-test/yadis/xrds/dummy-user', array('absolute' => TRUE, 'fragment' => $this->randomName()));
  78. // Tell openid_test.module to respond with this identifier. If the fragment
  79. // part is present in the identifier, it should be retained.
  80. variable_set('openid_test_response', array('openid.claimed_id' => $identity));
  81. $this->addIdentity(url('openid-test/yadis/xrds/server', array('absolute' => TRUE)), 2, 'http://specs.openid.net/auth/2.0/identifier_select', $identity);
  82. variable_set('openid_test_response', array());
  83. // Identifier is the URL of an HTML page that is sent with an HTTP header
  84. // that contains the URL of an XRDS document.
  85. $this->addIdentity(url('openid-test/yadis/x-xrds-location', array('absolute' => TRUE)), 2);
  86. // Identifier is the URL of an HTML page containing a <meta http-equiv=...>
  87. // element that contains the URL of an XRDS document.
  88. $this->addIdentity(url('openid-test/yadis/http-equiv', array('absolute' => TRUE)), 2);
  89. // Identifier is an XRI. Resolve using our own dummy proxy resolver.
  90. variable_set('xri_proxy_resolver', url('openid-test/yadis/xrds/xri', array('absolute' => TRUE)) . '/');
  91. $this->addIdentity('@example*résumé;%25', 2, 'http://example.com/xrds', 'http://example.com/user');
  92. // Make sure that unverified CanonicalID are not trusted.
  93. variable_set('openid_test_canonical_id_status', 'bad value');
  94. $this->addIdentity('@example*résumé;%25', 2, FALSE, FALSE);
  95. // HTML-based discovery:
  96. // If the User-supplied Identifier is a URL of an HTML page, the page may
  97. // contain a <link rel=...> element containing the URL of the OpenID
  98. // Provider Endpoint. OpenID 1 and 2 describe slightly different formats.
  99. // OpenID Authentication 1.1, section 3.1:
  100. $this->addIdentity(url('openid-test/html/openid1', array('absolute' => TRUE)), 1, 'http://example.com/html-openid1');
  101. // OpenID Authentication 2.0, section 7.3.3:
  102. $this->addIdentity(url('openid-test/html/openid2', array('absolute' => TRUE)), 2, 'http://example.com/html-openid2');
  103. // OpenID Authentication 2.0, section 7.2.4:
  104. // URL Identifiers MUST then be further normalized by both (1) following
  105. // redirects when retrieving their content and finally (2) applying the
  106. // rules in Section 6 of RFC3986 to the final destination URL. This final
  107. // URL MUST be noted by the Relying Party as the Claimed Identifier and be
  108. // used when requesting authentication.
  109. // Single redirect.
  110. $identity = $expected_claimed_id = url('openid-test/redirected/yadis/xrds/1', array('absolute' => TRUE));
  111. $this->addRedirectedIdentity($identity, 2, 'http://example.com/xrds', $expected_claimed_id, 0);
  112. // Exact 3 redirects (default value for the 'max_redirects' option in
  113. // drupal_http_request()).
  114. $identity = $expected_claimed_id = url('openid-test/redirected/yadis/xrds/2', array('absolute' => TRUE));
  115. $this->addRedirectedIdentity($identity, 2, 'http://example.com/xrds', $expected_claimed_id, 2);
  116. // Fails because there are more than 3 redirects (default value for the
  117. // 'max_redirects' option in drupal_http_request()).
  118. $identity = url('openid-test/redirected/yadis/xrds/3', array('absolute' => TRUE));
  119. $expected_claimed_id = FALSE;
  120. $this->addRedirectedIdentity($identity, 2, 'http://example.com/xrds', $expected_claimed_id, 3);
  121. }
  122. /**
  123. * Test login using OpenID.
  124. */
  125. function testLogin() {
  126. $this->drupalLogin($this->web_user);
  127. // Use a User-supplied Identity that is the URL of an XRDS document.
  128. $identity = url('openid-test/yadis/xrds', array('absolute' => TRUE));
  129. $this->addIdentity($identity);
  130. $this->drupalLogout();
  131. // Test logging in via the login block on the front page.
  132. $this->submitLoginForm($identity);
  133. $this->assertLink(t('Log out'), 0, 'User was logged in.');
  134. $this->drupalLogout();
  135. // Test logging in via the user/login page.
  136. $edit = array('openid_identifier' => $identity);
  137. $this->drupalPost('user/login', $edit, t('Log in'));
  138. // Check we are on the OpenID redirect form.
  139. $this->assertTitle(t('OpenID redirect'), 'OpenID redirect page was displayed.');
  140. // Submit form to the OpenID Provider Endpoint.
  141. $this->drupalPost(NULL, array(), t('Send'));
  142. $this->assertLink(t('Log out'), 0, 'User was logged in.');
  143. // Verify user was redirected away from user/login to an accessible page.
  144. $this->assertResponse(200);
  145. $this->drupalLogout();
  146. // Use a User-supplied Identity that is the URL of an XRDS document.
  147. // Tell the test module to add a doctype. This should fail.
  148. $identity = url('openid-test/yadis/xrds', array('absolute' => TRUE, 'query' => array('doctype' => 1)));
  149. // Test logging in via the login block on the front page.
  150. $edit = array('openid_identifier' => $identity);
  151. $this->drupalPost('', $edit, t('Log in'));
  152. $this->assertRaw(t('Sorry, that is not a valid OpenID. Ensure you have spelled your ID correctly.'), 'XML with DOCTYPE was rejected.');
  153. }
  154. /**
  155. * Test login using OpenID during maintenance mode.
  156. */
  157. function testLoginMaintenanceMode() {
  158. $this->web_user = $this->drupalCreateUser(array('access site in maintenance mode'));
  159. $this->drupalLogin($this->web_user);
  160. // Use a User-supplied Identity that is the URL of an XRDS document.
  161. $identity = url('openid-test/yadis/xrds', array('absolute' => TRUE));
  162. $this->addIdentity($identity);
  163. $this->drupalLogout();
  164. // Enable maintenance mode.
  165. variable_set('maintenance_mode', 1);
  166. // Test logging in via the user/login page while the site is offline.
  167. $edit = array('openid_identifier' => $identity);
  168. $this->drupalPost('user/login', $edit, t('Log in'));
  169. // Check we are on the OpenID redirect form.
  170. $this->assertTitle(t('OpenID redirect'), 'OpenID redirect page was displayed.');
  171. // Submit form to the OpenID Provider Endpoint.
  172. $this->drupalPost(NULL, array(), t('Send'));
  173. $this->assertLink(t('Log out'), 0, 'User was logged in.');
  174. // Verify user was redirected away from user/login to an accessible page.
  175. $this->assertText(t('Operating in maintenance mode.'));
  176. $this->assertResponse(200);
  177. }
  178. /**
  179. * Test deleting an OpenID identity from a user's profile.
  180. */
  181. function testDelete() {
  182. $this->drupalLogin($this->web_user);
  183. // Add identity to user's profile.
  184. $identity = url('openid-test/yadis/xrds', array('absolute' => TRUE));
  185. $this->addIdentity($identity);
  186. $this->assertText($identity, 'Identity appears in list.');
  187. // Delete the newly added identity.
  188. $this->clickLink(t('Delete'));
  189. $this->drupalPost(NULL, array(), t('Confirm'));
  190. $this->assertText(t('OpenID deleted.'), 'Identity deleted');
  191. $this->assertNoText($identity, 'Identity no longer appears in list.');
  192. }
  193. /**
  194. * Test that a blocked user cannot log in.
  195. */
  196. function testBlockedUserLogin() {
  197. // Use a User-supplied Identity that is the URL of an XRDS document.
  198. $identity = url('openid-test/yadis/xrds', array('absolute' => TRUE));
  199. // Log in and add an OpenID Identity to the account.
  200. $this->drupalLogin($this->web_user);
  201. $this->addIdentity($identity);
  202. $this->drupalLogout();
  203. // Log in as an admin user and block the account.
  204. $admin_user = $this->drupalCreateUser(array('administer users'));
  205. $this->drupalLogin($admin_user);
  206. $this->drupalGet('admin/people');
  207. $edit = array(
  208. 'operation' => 'block',
  209. 'accounts[' . $this->web_user->uid . ']' => TRUE,
  210. );
  211. $this->drupalPost('admin/people', $edit, t('Update'));
  212. $this->assertRaw('The update has been performed.', 'Account was blocked.');
  213. $this->drupalLogout();
  214. $this->submitLoginForm($identity);
  215. $this->assertRaw(t('The username %name has not been activated or is blocked.', array('%name' => $this->web_user->name)), 'User login was blocked.');
  216. }
  217. /**
  218. * Add OpenID identity to user's profile.
  219. *
  220. * @param $identity
  221. * The User-supplied Identifier.
  222. * @param $version
  223. * The protocol version used by the service.
  224. * @param $local_id
  225. * The expected OP-Local Identifier found during discovery.
  226. * @param $claimed_id
  227. * The expected Claimed Identifier returned by the OpenID Provider, or FALSE
  228. * if the discovery is expected to fail.
  229. */
  230. function addIdentity($identity, $version = 2, $local_id = 'http://example.com/xrds', $claimed_id = NULL) {
  231. // Tell openid_test.module to only accept this OP-Local Identifier.
  232. variable_set('openid_test_identity', $local_id);
  233. $edit = array('openid_identifier' => $identity);
  234. $this->drupalPost('user/' . $this->web_user->uid . '/openid', $edit, t('Add an OpenID'));
  235. if ($claimed_id === FALSE) {
  236. $this->assertRaw(t('Sorry, that is not a valid OpenID. Ensure you have spelled your ID correctly.'), 'Invalid identity was rejected.');
  237. return;
  238. }
  239. // OpenID 1 used a HTTP redirect, OpenID 2 uses a HTML form that is submitted automatically using JavaScript.
  240. if ($version == 2) {
  241. // Check we are on the OpenID redirect form.
  242. $this->assertTitle(t('OpenID redirect'), 'OpenID redirect page was displayed.');
  243. // Submit form to the OpenID Provider Endpoint.
  244. $this->drupalPost(NULL, array(), t('Send'));
  245. }
  246. if (!isset($claimed_id)) {
  247. $claimed_id = $identity;
  248. }
  249. $this->assertRaw(t('Successfully added %identity', array('%identity' => $claimed_id)), format_string('Identity %identity was added.', array('%identity' => $identity)));
  250. }
  251. /**
  252. * Add OpenID identity, changed by the following redirects, to user's profile.
  253. *
  254. * According to OpenID Authentication 2.0, section 7.2.4, URL Identifiers MUST
  255. * be further normalized by following redirects when retrieving their content
  256. * and this final URL MUST be noted by the Relying Party as the Claimed
  257. * Identifier and be used when requesting authentication.
  258. *
  259. * @param $identity
  260. * The User-supplied Identifier.
  261. * @param $version
  262. * The protocol version used by the service.
  263. * @param $local_id
  264. * The expected OP-Local Identifier found during discovery.
  265. * @param $claimed_id
  266. * The expected Claimed Identifier returned by the OpenID Provider, or FALSE
  267. * if the discovery is expected to fail.
  268. * @param $redirects
  269. * The number of redirects.
  270. */
  271. function addRedirectedIdentity($identity, $version = 2, $local_id = 'http://example.com/xrds', $claimed_id = NULL, $redirects = 0) {
  272. // Set the final destination URL which is the same as the Claimed
  273. // Identifier, we insert the same identifier also to the provider response,
  274. // but provider could further change the Claimed ID actually (e.g. it could
  275. // add unique fragment).
  276. variable_set('openid_test_redirect_url', $identity);
  277. variable_set('openid_test_response', array('openid.claimed_id' => $identity));
  278. $this->addIdentity(url('openid-test/redirect/' . $redirects, array('absolute' => TRUE)), $version, $local_id, $claimed_id);
  279. // Clean up.
  280. variable_del('openid_test_redirect_url');
  281. variable_del('openid_test_response');
  282. }
  283. /**
  284. * Tests that openid.signed is verified.
  285. */
  286. function testSignatureValidation() {
  287. // Use a User-supplied Identity that is the URL of an XRDS document.
  288. $identity = url('openid-test/yadis/xrds', array('absolute' => TRUE));
  289. // Respond with an invalid signature.
  290. variable_set('openid_test_response', array('openid.sig' => 'this-is-an-invalid-signature'));
  291. $this->submitLoginForm($identity);
  292. $this->assertRaw('OpenID login failed.');
  293. // Do not sign the mandatory field openid.assoc_handle.
  294. variable_set('openid_test_response', array('openid.signed' => 'op_endpoint,claimed_id,identity,return_to,response_nonce'));
  295. $this->submitLoginForm($identity);
  296. $this->assertRaw('OpenID login failed.');
  297. // Sign all mandatory fields and a custom field.
  298. $keys_to_sign = array('op_endpoint', 'claimed_id', 'identity', 'return_to', 'response_nonce', 'assoc_handle', 'foo');
  299. $association = new stdClass();
  300. $association->mac_key = variable_get('mac_key');
  301. $response = array(
  302. 'openid.op_endpoint' => url('openid-test/endpoint', array('absolute' => TRUE)),
  303. 'openid.claimed_id' => $identity,
  304. 'openid.identity' => $identity,
  305. 'openid.return_to' => url('openid/authenticate', array('absolute' => TRUE)),
  306. 'openid.response_nonce' => _openid_nonce(),
  307. 'openid.assoc_handle' => 'openid-test',
  308. 'openid.foo' => 123,
  309. 'openid.signed' => implode(',', $keys_to_sign),
  310. );
  311. $response['openid.sig'] = _openid_signature($association, $response, $keys_to_sign);
  312. variable_set('openid_test_response', $response);
  313. $this->submitLoginForm($identity);
  314. $this->assertNoRaw('OpenID login failed.');
  315. $this->assertFieldByName('name', '', 'No username was supplied by provider.');
  316. $this->assertFieldByName('mail', '', 'No e-mail address was supplied by provider.');
  317. // Check that unsigned SREG fields are ignored.
  318. $response = array(
  319. 'openid.signed' => 'op_endpoint,claimed_id,identity,return_to,response_nonce,assoc_handle,sreg.nickname',
  320. 'openid.sreg.nickname' => 'john',
  321. 'openid.sreg.email' => 'john@example.com',
  322. );
  323. variable_set('openid_test_response', $response);
  324. $this->submitLoginForm($identity);
  325. $this->assertNoRaw('OpenID login failed.');
  326. $this->assertFieldByName('name', 'john', 'Username was supplied by provider.');
  327. $this->assertFieldByName('mail', '', 'E-mail address supplied by provider was ignored.');
  328. }
  329. }
  330. /**
  331. * Test account registration using Simple Registration and Attribute Exchange.
  332. */
  333. class OpenIDRegistrationTestCase extends OpenIDWebTestCase {
  334. public static function getInfo() {
  335. return array(
  336. 'name' => 'OpenID account registration',
  337. 'description' => 'Creates a user account using auto-registration.',
  338. 'group' => 'OpenID'
  339. );
  340. }
  341. function setUp() {
  342. parent::setUp('openid', 'openid_test');
  343. variable_set('user_register', USER_REGISTER_VISITORS);
  344. }
  345. /**
  346. * Test OpenID auto-registration with e-mail verification enabled.
  347. */
  348. function testRegisterUserWithEmailVerification() {
  349. variable_set('user_email_verification', TRUE);
  350. // Tell openid_test.module to respond with these SREG fields.
  351. variable_set('openid_test_response', array('openid.sreg.nickname' => 'john', 'openid.sreg.email' => 'john@example.com'));
  352. // Use a User-supplied Identity that is the URL of an XRDS document.
  353. $identity = url('openid-test/yadis/xrds', array('absolute' => TRUE));
  354. $this->submitLoginForm($identity);
  355. $this->assertRaw(t('Once you have verified your e-mail address, you may log in via OpenID.'), 'User was asked to verify e-mail address.');
  356. $this->assertRaw(t('A welcome message with further instructions has been sent to your e-mail address.'), 'A welcome message was sent to the user.');
  357. $reset_url = $this->getPasswordResetURLFromMail();
  358. $user = user_load_by_name('john');
  359. $this->assertTrue($user, 'User was registered with right username.');
  360. $this->assertEqual($user->mail, 'john@example.com', 'User was registered with right email address.');
  361. $this->assertFalse($user->data, 'No additional user info was saved.');
  362. $this->submitLoginForm($identity);
  363. $this->assertRaw(t('You must validate your email address for this account before logging in via OpenID.'));
  364. // Follow the one-time login that was sent in the welcome e-mail.
  365. $this->drupalGet($reset_url);
  366. $this->drupalPost(NULL, array(), t('Log in'));
  367. $this->drupalLogout();
  368. // Verify that the account was activated.
  369. $this->submitLoginForm($identity);
  370. $this->assertLink(t('Log out'), 0, 'User was logged in.');
  371. }
  372. /**
  373. * Test OpenID auto-registration with e-mail verification disabled.
  374. */
  375. function testRegisterUserWithoutEmailVerification() {
  376. variable_set('user_email_verification', FALSE);
  377. // Tell openid_test.module to respond with these SREG fields.
  378. variable_set('openid_test_response', array('openid.sreg.nickname' => 'john', 'openid.sreg.email' => 'john@example.com'));
  379. // Use a User-supplied Identity that is the URL of an XRDS document.
  380. $identity = url('openid-test/yadis/xrds', array('absolute' => TRUE));
  381. $this->submitLoginForm($identity);
  382. $this->assertLink(t('Log out'), 0, 'User was logged in.');
  383. $user = user_load_by_name('john');
  384. $this->assertTrue($user, 'User was registered with right username.');
  385. $this->assertEqual($user->mail, 'john@example.com', 'User was registered with right email address.');
  386. $this->assertFalse($user->data, 'No additional user info was saved.');
  387. $this->drupalLogout();
  388. $this->submitLoginForm($identity);
  389. $this->assertLink(t('Log out'), 0, 'User was logged in.');
  390. }
  391. /**
  392. * Test OpenID auto-registration with a provider that supplies invalid SREG
  393. * information (a username that is already taken, and no e-mail address).
  394. */
  395. function testRegisterUserWithInvalidSreg() {
  396. // Tell openid_test.module to respond with these SREG fields.
  397. $web_user = $this->drupalCreateUser(array());
  398. variable_set('openid_test_response', array('openid.sreg.nickname' => $web_user->name, 'openid.sreg.email' => 'mail@invalid#'));
  399. // Use a User-supplied Identity that is the URL of an XRDS document.
  400. $identity = url('openid-test/yadis/xrds', array('absolute' => TRUE));
  401. $this->submitLoginForm($identity);
  402. $this->assertRaw(t('Account registration using the information provided by your OpenID provider failed due to the reasons listed below. Complete the registration by filling out the form below. If you already have an account, you can <a href="@login">log in</a> now and add your OpenID under "My account".', array('@login' => url('user/login'))), 'User was asked to complete the registration process manually.');
  403. $this->assertRaw(t('The name %name is already taken.', array('%name' => $web_user->name)), 'Form validation error for username was displayed.');
  404. $this->assertRaw(t('The e-mail address %mail is not valid.', array('%mail' => 'mail@invalid#')), 'Form validation error for e-mail address was displayed.');
  405. // Enter username and e-mail address manually.
  406. $edit = array('name' => 'john', 'mail' => 'john@example.com');
  407. $this->drupalPost(NULL, $edit, t('Create new account'));
  408. $this->assertRaw(t('Once you have verified your e-mail address, you may log in via OpenID.'), 'User was asked to verify e-mail address.');
  409. $reset_url = $this->getPasswordResetURLFromMail();
  410. $user = user_load_by_name('john');
  411. $this->assertTrue($user, 'User was registered with right username.');
  412. $this->assertFalse($user->data, 'No additional user info was saved.');
  413. // Follow the one-time login that was sent in the welcome e-mail.
  414. $this->drupalGet($reset_url);
  415. $this->drupalPost(NULL, array(), t('Log in'));
  416. // The user is taken to user/%uid/edit.
  417. $this->assertFieldByName('mail', 'john@example.com', 'User was registered with right e-mail address.');
  418. $this->clickLink(t('OpenID identities'));
  419. $this->assertRaw($identity, 'OpenID identity was registered.');
  420. }
  421. /**
  422. * Test OpenID auto-registration with a provider that does not supply SREG
  423. * information (i.e. no username or e-mail address).
  424. */
  425. function testRegisterUserWithoutSreg() {
  426. // Load the front page to get the user login block.
  427. $this->drupalGet('');
  428. // Use a User-supplied Identity that is the URL of an XRDS document.
  429. $identity = url('openid-test/yadis/xrds', array('absolute' => TRUE));
  430. $this->submitLoginForm($identity);
  431. $this->assertRaw(t('Complete the registration by filling out the form below. If you already have an account, you can <a href="@login">log in</a> now and add your OpenID under "My account".', array('@login' => url('user/login'))), 'User was asked to complete the registration process manually.');
  432. $this->assertNoRaw(t('You must enter a username.'), 'Form validation error for username was not displayed.');
  433. $this->assertNoRaw(t('You must enter an e-mail address.'), 'Form validation error for e-mail address was not displayed.');
  434. // Enter username and e-mail address manually.
  435. $edit = array('name' => 'john', 'mail' => 'john@example.com');
  436. $this->drupalPost(NULL, $edit, t('Create new account'));
  437. $this->assertRaw(t('Once you have verified your e-mail address, you may log in via OpenID.'), 'User was asked to verify e-mail address.');
  438. $reset_url = $this->getPasswordResetURLFromMail();
  439. $user = user_load_by_name('john');
  440. $this->assertTrue($user, 'User was registered with right username.');
  441. $this->assertFalse($user->data, 'No additional user info was saved.');
  442. // Follow the one-time login that was sent in the welcome e-mail.
  443. $this->drupalGet($reset_url);
  444. $this->drupalPost(NULL, array(), t('Log in'));
  445. // The user is taken to user/%uid/edit.
  446. $this->assertFieldByName('mail', 'john@example.com', 'User was registered with right e-mail address.');
  447. $this->clickLink(t('OpenID identities'));
  448. $this->assertRaw($identity, 'OpenID identity was registered.');
  449. }
  450. /**
  451. * Test OpenID auto-registration with a provider that supplies AX information,
  452. * but no SREG.
  453. */
  454. function testRegisterUserWithAXButNoSREG() {
  455. variable_set('user_email_verification', FALSE);
  456. // Tell openid_test.module to respond with these AX fields.
  457. variable_set('openid_test_response', array(
  458. 'openid.ns.ext123' => 'http://openid.net/srv/ax/1.0',
  459. 'openid.ext123.type.mail456' => 'http://axschema.org/contact/email',
  460. 'openid.ext123.value.mail456' => 'john@example.com',
  461. 'openid.ext123.type.name789' => 'http://schema.openid.net/namePerson/friendly',
  462. 'openid.ext123.count.name789' => '1',
  463. 'openid.ext123.value.name789.1' => 'john',
  464. ));
  465. // Use a User-supplied Identity that is the URL of an XRDS document.
  466. $identity = url('openid-test/yadis/xrds', array('absolute' => TRUE));
  467. $this->submitLoginForm($identity);
  468. $this->assertLink(t('Log out'), 0, 'User was logged in.');
  469. $user = user_load_by_name('john');
  470. $this->assertTrue($user, 'User was registered with right username.');
  471. $this->assertEqual($user->mail, 'john@example.com', 'User was registered with right email address.');
  472. }
  473. }
  474. /**
  475. * Test account registration using Simple Registration and Attribute Exchange.
  476. */
  477. class OpenIDInvalidIdentifierTransitionTestCase extends OpenIDFunctionalTestCase {
  478. public static function getInfo() {
  479. return array(
  480. 'name' => 'OpenID account update',
  481. 'description' => 'Tries to correct OpenID identifiers attached to accounts if their identifiers were stripped.',
  482. 'group' => 'OpenID',
  483. );
  484. }
  485. function setUp() {
  486. parent::setUp('openid', 'openid_test');
  487. variable_set('user_register', USER_REGISTER_VISITORS);
  488. variable_set('openid_less_obtrusive_transition', TRUE);
  489. }
  490. /**
  491. * Test OpenID transition with e-mail mismatch.
  492. */
  493. function testStrippedFragmentAccountEmailMismatch() {
  494. $this->drupalLogin($this->web_user);
  495. // Use a User-supplied Identity that is the URL of an XRDS document.
  496. $identity = url('openid-test/yadis/xrds', array('absolute' => TRUE, 'fragment' => $this->randomName()));
  497. $identity_stripped = preg_replace('/#.*/', '', $identity);
  498. // Add invalid identifier to the authmap (identifier has stripped fragment).
  499. $this->addIdentity($identity_stripped);
  500. $this->drupalLogout();
  501. // Test logging in via the login form, provider will respond with full
  502. // identifier (including fragment) but with different email, so we can't
  503. // provide auto-update.
  504. variable_set('openid_test_response', array(
  505. 'openid.claimed_id' => $identity,
  506. 'openid.sreg.nickname' => $this->web_user->name,
  507. 'openid.sreg.email' => 'invalid-' . $this->web_user->mail));
  508. $edit = array('openid_identifier' => $identity_stripped);
  509. $this->submitLoginForm($identity_stripped);
  510. // Verify user was redirected away from user login to an accessible page.
  511. $this->assertResponse(200);
  512. // Verify the message.
  513. $this->assertRaw(t('There is already an existing account associated with the OpenID identifier that you have provided.'), 'Message that OpenID identifier must be updated manually was displayed.');
  514. }
  515. /**
  516. * Test OpenID auto transition with e-mail.
  517. */
  518. function testStrippedFragmentAccountAutoUpdateSreg() {
  519. $this->drupalLogin($this->web_user);
  520. // Use a User-supplied Identity that is the URL of an XRDS document.
  521. $identity = url('openid-test/yadis/xrds', array('absolute' => TRUE, 'fragment' => $this->randomName()));
  522. $identity_stripped = preg_replace('/#.*/', '', $identity);
  523. // Add invalid identifier to the authmap (identifier has stripped fragment).
  524. $this->addIdentity($identity_stripped);
  525. $this->drupalLogout();
  526. // Test logging in via the login form, provider will respond with full
  527. // identifier (including fragment) but with different email, so we can't
  528. // provide auto-update.
  529. variable_set('openid_test_response', array(
  530. 'openid.claimed_id' => $identity,
  531. 'openid.sreg.nickname' => $this->web_user->name,
  532. 'openid.sreg.email' => $this->web_user->mail));
  533. $this->submitLoginForm($identity_stripped);
  534. // Verify user was redirected away from user login to an accessible page.
  535. $this->assertResponse(200);
  536. // Verify the message.
  537. $this->assertRaw(t('New OpenID identifier %identity was added as a replacement for invalid identifier %invalid_identity.', array('%invalid_identity' => $identity_stripped, '%identity' => $identity)), 'Message that OpenID identifier was added automatically was displayed.');
  538. }
  539. }
  540. /**
  541. * Test internal helper functions.
  542. */
  543. class OpenIDTestCase extends DrupalWebTestCase {
  544. public static function getInfo() {
  545. return array(
  546. 'name' => 'OpenID helper functions',
  547. 'description' => 'Test OpenID helper functions.',
  548. 'group' => 'OpenID'
  549. );
  550. }
  551. function setUp() {
  552. parent::setUp('openid');
  553. module_load_include('inc', 'openid');
  554. }
  555. /**
  556. * Test _openid_dh_XXX_to_XXX() functions.
  557. */
  558. function testConversion() {
  559. $this->assertEqual(_openid_dh_long_to_base64('12345678901234567890123456789012345678901234567890'), 'CHJ/Y2mq+DyhUCZ0evjH8ZbOPwrS', '_openid_dh_long_to_base64() returned expected result.');
  560. $this->assertEqual(_openid_dh_base64_to_long('BsH/g8Nrpn2dtBSdu/sr1y8hxwyx'), '09876543210987654321098765432109876543210987654321', '_openid_dh_base64_to_long() returned expected result.');
  561. $this->assertEqual(_openid_dh_long_to_binary('12345678901234567890123456789012345678901234567890'), "\x08r\x7fci\xaa\xf8<\xa1P&tz\xf8\xc7\xf1\x96\xce?\x0a\xd2", '_openid_dh_long_to_binary() returned expected result.');
  562. $this->assertEqual(_openid_dh_binary_to_long("\x06\xc1\xff\x83\xc3k\xa6}\x9d\xb4\x14\x9d\xbb\xfb+\xd7/!\xc7\x0c\xb1"), '09876543210987654321098765432109876543210987654321', '_openid_dh_binary_to_long() returned expected result.');
  563. }
  564. /**
  565. * Test _openid_dh_xorsecret().
  566. */
  567. function testOpenidDhXorsecret() {
  568. $this->assertEqual(_openid_dh_xorsecret('123456790123456790123456790', "abc123ABC\x00\xFF"), "\xa4'\x06\xbe\xf1.\x00y\xff\xc2\xc1", '_openid_dh_xorsecret() returned expected result.');
  569. }
  570. /**
  571. * Test _openid_get_bytes().
  572. */
  573. function testOpenidGetBytes() {
  574. $this->assertEqual(strlen(_openid_get_bytes(20)), 20, '_openid_get_bytes() returned expected result.');
  575. }
  576. /**
  577. * Test _openid_signature().
  578. */
  579. function testOpenidSignature() {
  580. // Test that signature is calculated according to OpenID Authentication 2.0,
  581. // section 6.1. In the following array, only the two first entries should be
  582. // included in the calculation, because the substring following the period
  583. // is mentioned in the third argument for _openid_signature(). The last
  584. // entry should not be included, because it does not start with "openid.".
  585. $response = array(
  586. 'openid.foo' => 'abc1',
  587. 'openid.bar' => 'abc2',
  588. 'openid.baz' => 'abc3',
  589. 'foobar.foo' => 'abc4',
  590. );
  591. $association = new stdClass();
  592. $association->mac_key = "1234567890abcdefghij\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9";
  593. $this->assertEqual(_openid_signature($association, $response, array('foo', 'bar')), 'QnKZQzSFstT+GNiJDFOptdcZjrc=', 'Expected signature calculated.');
  594. }
  595. /**
  596. * Test _openid_is_xri().
  597. */
  598. function testOpenidXRITest() {
  599. // Test that the XRI test is according to OpenID Authentication 2.0,
  600. // section 7.2. If the user-supplied string starts with xri:// it should be
  601. // stripped and the resulting string should be treated as an XRI when it
  602. // starts with "=", "@", "+", "$", "!" or "(".
  603. $this->assertTrue(_openid_is_xri('xri://=foo'), '_openid_is_xri() returned expected result for an xri identifier with xri scheme.');
  604. $this->assertTrue(_openid_is_xri('xri://@foo'), '_openid_is_xri() returned expected result for an xri identifier with xri scheme.');
  605. $this->assertTrue(_openid_is_xri('xri://+foo'), '_openid_is_xri() returned expected result for an xri identifier with xri scheme.');
  606. $this->assertTrue(_openid_is_xri('xri://$foo'), '_openid_is_xri() returned expected result for an xri identifier with xri scheme.');
  607. $this->assertTrue(_openid_is_xri('xri://!foo'), '_openid_is_xri() returned expected result for an xri identifier with xri scheme..');
  608. $this->assertTrue(_openid_is_xri('xri://(foo'), '_openid_is_xri() returned expected result for an xri identifier with xri scheme..');
  609. $this->assertTrue(_openid_is_xri('=foo'), '_openid_is_xri() returned expected result for an xri identifier.');
  610. $this->assertTrue(_openid_is_xri('@foo'), '_openid_is_xri() returned expected result for an xri identifier.');
  611. $this->assertTrue(_openid_is_xri('+foo'), '_openid_is_xri() returned expected result for an xri identifier.');
  612. $this->assertTrue(_openid_is_xri('$foo'), '_openid_is_xri() returned expected result for an xri identifier.');
  613. $this->assertTrue(_openid_is_xri('!foo'), '_openid_is_xri() returned expected result for an xri identifier.');
  614. $this->assertTrue(_openid_is_xri('(foo'), '_openid_is_xri() returned expected result for an xri identifier.');
  615. $this->assertFalse(_openid_is_xri('foo'), '_openid_is_xri() returned expected result for an http URL.');
  616. $this->assertFalse(_openid_is_xri('xri://foo'), '_openid_is_xri() returned expected result for an http URL.');
  617. $this->assertFalse(_openid_is_xri('http://foo/'), '_openid_is_xri() returned expected result for an http URL.');
  618. $this->assertFalse(_openid_is_xri('http://example.com/'), '_openid_is_xri() returned expected result for an http URL.');
  619. $this->assertFalse(_openid_is_xri('user@example.com/'), '_openid_is_xri() returned expected result for an http URL.');
  620. $this->assertFalse(_openid_is_xri('http://user@example.com/'), '_openid_is_xri() returned expected result for an http URL.');
  621. }
  622. /**
  623. * Test openid_normalize().
  624. */
  625. function testOpenidNormalize() {
  626. // Test that the normalization is according to OpenID Authentication 2.0,
  627. // section 7.2 and 11.5.2.
  628. $this->assertEqual(openid_normalize('$foo'), '$foo', 'openid_normalize() correctly normalized an XRI.');
  629. $this->assertEqual(openid_normalize('xri://$foo'), '$foo', 'openid_normalize() correctly normalized an XRI with an xri:// scheme.');
  630. $this->assertEqual(openid_normalize('example.com/'), 'http://example.com/', 'openid_normalize() correctly normalized a URL with a missing scheme.');
  631. $this->assertEqual(openid_normalize('example.com'), 'http://example.com/', 'openid_normalize() correctly normalized a URL with a missing scheme and empty path.');
  632. $this->assertEqual(openid_normalize('http://example.com'), 'http://example.com/', 'openid_normalize() correctly normalized a URL with an empty path.');
  633. $this->assertEqual(openid_normalize('http://example.com/path'), 'http://example.com/path', 'openid_normalize() correctly normalized a URL with a path.');
  634. $this->assertEqual(openid_normalize('http://example.com/path#fragment'), 'http://example.com/path', 'openid_normalize() correctly normalized a URL with a fragment.');
  635. }
  636. /**
  637. * Test openid_extract_namespace().
  638. */
  639. function testOpenidExtractNamespace() {
  640. $response = array(
  641. 'openid.sreg.nickname' => 'john',
  642. 'openid.ns.ext1' => OPENID_NS_SREG,
  643. 'openid.ext1.nickname' => 'george',
  644. 'openid.ext1.email' => 'george@example.com',
  645. 'openid.ns.ext2' => 'http://example.com/ns/ext2',
  646. 'openid.ext2.foo' => '123',
  647. 'openid.ext2.bar' => '456',
  648. 'openid.signed' => 'sreg.nickname,ns.ext1,ext1.email,ext2.foo',
  649. );
  650. $values = openid_extract_namespace($response, 'http://example.com/ns/dummy', NULL, FALSE);
  651. $this->assertEqual($values, array(), 'Nothing found for unused namespace.');
  652. $values = openid_extract_namespace($response, 'http://example.com/ns/dummy', 'sreg', FALSE);
  653. $this->assertEqual($values, array('nickname' => 'john'), 'Value found for fallback prefix.');
  654. $values = openid_extract_namespace($response, OPENID_NS_SREG, 'sreg', FALSE);
  655. $this->assertEqual($values, array('nickname' => 'george', 'email' => 'george@example.com'), 'Namespace takes precedence over fallback prefix.');
  656. // ext1.email is signed, but ext1.nickname is not.
  657. $values = openid_extract_namespace($response, OPENID_NS_SREG, 'sreg', TRUE);
  658. $this->assertEqual($values, array('email' => 'george@example.com'), 'Unsigned namespaced fields ignored.');
  659. $values = openid_extract_namespace($response, 'http://example.com/ns/ext2', 'sreg', FALSE);
  660. $this->assertEqual($values, array('foo' => '123', 'bar' => '456'), 'Unsigned fields found.');
  661. // ext2.foo and ext2.bar are ignored, because ns.ext2 is not signed. The
  662. // fallback prefix is not used, because the namespace is specified.
  663. $values = openid_extract_namespace($response, 'http://example.com/ns/ext2', 'sreg', TRUE);
  664. $this->assertEqual($values, array(), 'Unsigned fields ignored.');
  665. }
  666. }