Index: modules/openid/openid.module =================================================================== RCS file: /cvs/drupal/drupal/modules/openid/openid.module,v retrieving revision 1.52 diff -u -8 -p -r1.52 openid.module --- modules/openid/openid.module 12 Aug 2009 12:36:04 -0000 1.52 +++ modules/openid/openid.module 16 Aug 2009 11:22:55 -0000 @@ -58,18 +58,19 @@ function openid_help($path, $arg) { /** * Implement hook_user_insert(). */ function openid_user_insert(&$edit, $account, $category) { if (isset($_SESSION['openid']['values'])) { // The user has registered after trying to login via OpenID. if (variable_get('user_email_verification', TRUE)) { - drupal_set_message(t('Once you have verified your email address, you may log in via OpenID.')); + drupal_set_message(t('Once you have verified your e-mail address, you may log in via OpenID.')); } + user_set_authmaps($account, array('authname_openid' => $_SESSION['openid']['values']['response']['openid.claimed_id'])); unset($_SESSION['openid']); } } /** * Implement hook_form_FORM_ID_alter(). */ function openid_form_user_login_block_alter(&$form, &$form_state) { @@ -123,25 +124,31 @@ function _openid_user_login_form_alter(& /** * Implement hook_form_alter(). Adds OpenID login to the login forms. */ function openid_form_user_register_alter(&$form, &$form_state) { if (isset($_SESSION['openid']['values'])) { // We were unable to auto-register a new user. Prefill the registration // form with the values we have. - $form['name']['#default_value'] = $_SESSION['openid']['values']['name']; - $form['mail']['#default_value'] = $_SESSION['openid']['values']['mail']; + $form['account']['name']['#default_value'] = $_SESSION['openid']['values']['name']; + $form['account']['mail']['#default_value'] = $_SESSION['openid']['values']['mail']; + // If user_email_verification is off, hide the password field and just fill // with random password to avoid confusion. if (!variable_get('user_email_verification', TRUE)) { $form['pass']['#type'] = 'hidden'; $form['pass']['#value'] = user_password(); } - $form['auth_openid'] = array('#type' => 'hidden', '#value' => $_SESSION['openid']['values']['auth_openid']); + $form['openid_display'] = array( + '#type' => 'item', + '#title' => t('Your OpenID'), + '#description' => t('This OpenID will be attached to your account after registration.'), + '#markup' => check_plain($_SESSION['openid']['values']['response']['openid.claimed_id']), + ); } } /** * Login form _validate hook */ function openid_login_validate($form, &$form_state) { $return_to = $form_state['values']['openid.return_to']; @@ -420,28 +427,28 @@ function openid_authentication($response else { drupal_set_message(t('You must validate your email address for this account before logging in via OpenID')); } } elseif (variable_get('user_register', 1)) { // Register new user $form_state['args'] = array(); $form_state['redirect'] = NULL; - $form_state['values']['name'] = (empty($response['openid.sreg.nickname'])) ? $identity : $response['openid.sreg.nickname']; - $form_state['values']['mail'] = (empty($response['openid.sreg.email'])) ? '' : $response['openid.sreg.email']; + $form_state['values']['name'] = !empty($response['openid.sreg.nickname']) ? $response['openid.sreg.nickname'] : ''; + $form_state['values']['mail'] = !empty($response['openid.sreg.email']) ? $response['openid.sreg.email'] : ''; $form_state['values']['pass'] = user_password(); $form_state['values']['status'] = variable_get('user_register', 1) == 1; $form_state['values']['response'] = $response; $form = drupal_retrieve_form('user_register', $form_state); drupal_prepare_form('user_register', $form, $form_state); drupal_validate_form('user_register', $form, $form_state); if (form_get_errors()) { // We were unable to register a valid new user, redirect to standard // user/register and prefill with the values we received. - drupal_set_message(t('OpenID registration failed for the reasons listed. You may register now, or if you already have an account you can log in now and add your OpenID under "My Account"', array('@login' => url('user/login'))), 'error'); + drupal_set_message(t('OpenID registration failed for the reasons listed, possibly because your OpenID provider did not provide a valid username and e-mail-address. You may register manually, or if you already have an account you can log in now and add your OpenID under "My Account".', array('@login' => url('user/login'))), 'warning'); $_SESSION['openid']['values'] = $form_state['values']; // We'll want to redirect back to the same place. $destination = drupal_get_destination(); unset($_REQUEST['destination']); drupal_goto('user/register', $destination); } else { unset($form_state['values']['response']); Index: modules/openid/openid.test =================================================================== RCS file: /cvs/drupal/drupal/modules/openid/openid.test,v retrieving revision 1.3 diff -u -8 -p -r1.3 openid.test --- modules/openid/openid.test 13 Jul 2009 21:51:10 -0000 1.3 +++ modules/openid/openid.test 16 Aug 2009 11:22:55 -0000 @@ -82,17 +82,16 @@ class OpenIDFunctionalTest extends Drupa $edit = array('openid_identifier' => $identity); $this->drupalPost(NULL, $edit, t('Log in')); // Check we are on the OpenID redirect form. $this->assertTitle(t('OpenID redirect'), t('OpenID redirect page was displayed.')); // Submit form to the OpenID Provider Endpoint. $this->drupalPost(NULL, array(), t('Send')); - $this->assertText(t('My account'), t('User was logged in.')); } /** * Test deleting an OpenID identity from a user's profile. */ function testDelete() { $this->drupalLogin($this->web_user); @@ -124,42 +123,86 @@ class OpenIDFunctionalTest extends Drupa $this->assertRaw('', t('JavaScript form submission found.')); $this->drupalPost(NULL, array(), t('Send')); } $this->assertRaw(t('Successfully added %identity', array('%identity' => $identity)), t('Identity %identity was added.', array('%identity' => $identity))); } /** - * Test openID auto-registration with e-mail verification disabled. + * Test OpenID auto-registration with e-mail verification disabled. */ function testRegisterUserWithoutEmailVerification() { variable_set('user_email_verification', FALSE); // Load the front page to get the user login block. $this->drupalGet(''); // Use a User-supplied Identity that is the URL of an XRDS document. $identity = url('openid-test/yadis/xrds', array('absolute' => TRUE)); + // Tell openid_test.module to respond with these SREG fields. + variable_set('openid_test_response', array('openid.sreg.nickname' => 'john', 'openid.sreg.email' => 'john@example.com')); + // Fill out and submit the login form. $edit = array('openid_identifier' => $identity); $this->drupalPost(NULL, $edit, t('Log in')); - // The OpenID module responds with an HTML form that is to be submitted - // to the OpenID Provider Endpoint. This is usually done automatically - // using JavaScript, but the SimpleTest browser does not support JavaScript, - // so the form is submitted manually instead. - $this->assertRaw('', t('JavaScript form submission found.')); + // Check we are on the OpenID redirect form. + $this->assertTitle(t('OpenID redirect'), t('OpenID redirect page was displayed.')); + + // Submit form to the OpenID Provider Endpoint. $this->drupalPost(NULL, array(), t('Send')); $this->assertText(t('My account'), t('User was logged in.')); - $user = user_load_by_name('johndoe'); - $this->assertTrue($user, t('User was found.')); - $this->assertEqual($user->mail, 'johndoe@example.com', t('User was registered with right email address.')); + $user = user_load_by_name('john'); + $this->assertTrue($user, t('User was registered with right username.')); + $this->assertEqual($user->mail, 'john@example.com', t('User was registered with right email address.')); + } + + /** + * Test OpenID auto-registration with a provider that does not supply SREG + * information (i.e. username and e-mail address). + */ + function testRegisterUserWithoutSreg() { + // Load the front page to get the user login block. + $this->drupalGet(''); + + // Use a User-supplied Identity that is the URL of an XRDS document. + $identity = url('openid-test/yadis/xrds', array('absolute' => TRUE)); + + // Fill out and submit the login form. + $edit = array('openid_identifier' => $identity); + $this->drupalPost(NULL, $edit, t('Log in')); + + // Check we are on the OpenID redirect form. + $this->assertTitle(t('OpenID redirect'), t('OpenID redirect page was displayed.')); + + // Submit form to the OpenID Provider Endpoint. + $this->drupalPost(NULL, array(), t('Send')); + + $this->assertRaw(t('OpenID registration failed for the reasons listed, possibly because your OpenID provider did not provide a valid username and e-mail-address. You may register manually, or if you already have an account you can log in now and add your OpenID under "My Account".', array('@login' => url('user/login'))), t('User was asked to complete the registration process manually.')); + + // Enter username and e-mail address manually. + $edit = array('name' => 'john', 'mail' => 'john@example.com'); + $this->drupalPost(NULL, $edit, t('Create new account')); + $this->assertRaw(t('Once you have verified your e-mail address, you may log in via OpenID.'), t('User was asked to verify e-mail address.')); + + $user = user_load_by_name('john'); + $this->assertTrue($user, t('User was registered with right username.')); + + // Follow the one-time login that was sent in the confirmation e-mail. + $this->drupalGet(user_pass_reset_url($user)); + $this->drupalPost(NULL, array(), t('Log in')); + + // The user is taken to user/%uid/edit. + $this->assertFieldByName('mail', 'john@example.com', t('User was registered with right e-mail address.')); + + $this->clickLink(t('OpenID identities')); + $this->assertRaw($identity, t('OpenID identity was registered.')); } } /** * Test internal helper functions. */ class OpenIDUnitTest extends DrupalWebTestCase { public static function getInfo() { Index: modules/openid/tests/openid_test.module =================================================================== RCS file: /cvs/drupal/drupal/modules/openid/tests/openid_test.module,v retrieving revision 1.3 diff -u -8 -p -r1.3 openid_test.module --- modules/openid/tests/openid_test.module 10 Jun 2009 20:13:20 -0000 1.3 +++ modules/openid/tests/openid_test.module 16 Aug 2009 11:22:55 -0000 @@ -200,28 +200,26 @@ function _openid_test_endpoint_authentic module_load_include('inc', 'openid'); // Generate unique identifier for this authentication. $nonce = _openid_nonce(); // Generate response containing the user's identity. The openid.sreg.xxx // entries contain profile data stored by the OpenID Provider (see OpenID // Simple Registration Extension 1.0). - $response = array( + $response = variable_get('openid_test_response', array()) + array( 'openid.ns' => OPENID_NS_2_0, 'openid.mode' => 'id_res', 'openid.op_endpoint' => $base_url . url('openid/provider'), // openid.claimed_id is not sent by OpenID 1 clients. 'openid.claimed_id' => isset($_REQUEST['openid_claimed_id']) ? $_REQUEST['openid_claimed_id'] : '', 'openid.identity' => $_REQUEST['openid_identity'], 'openid.return_to' => $_REQUEST['openid_return_to'], 'openid.response_nonce' => $nonce, 'openid.assoc_handle' => 'openid-test', - 'openid.sreg.email' => 'johndoe@example.com', - 'openid.sreg.nickname' => 'johndoe', 'openid.signed' => 'op_endpoint,claimed_id,identity,return_to,response_nonce,assoc_handle', ); // Sign the message using the MAC key that was exchanged during association. $association = new stdClass; $association->mac_key = variable_get('mac_key'); $keys_to_sign = explode(',', $response['openid.signed']); $response['openid.sig'] = _openid_signature($association, $response, $keys_to_sign);