#StackBounty: #7 #users #oauth How to use user_external_login_register() with user authenticated by token?

Bounty: 100

So, I’m working on an Oauth2 implementation, and I’m forced to use a custom module as the existing modules all have slightly differing functionalities than my use case, assume Drupal-to-Drupal implementation, or request the user credentials to be submitted to the remote server (which is not how Oauth should be implemented).

Using the standard authflow, the users are redirected and authenticate against a remote server, and are returned with an access code which is then submitted for an auth token, which is finally used to get user info from the remote API. Finally, I take the json response, format it into an array to be created into an user object, and this is where I’m stuck.

The examples presented on the User External Login Register API page seem to assume querying an external database to check for the user’s existence. In my case, I have the user info and name, and need to create a user and log them in with that information.
This is my current attempt which doesn’t seem to be working. I modified the API example use the user_load_by_name() function to see if the user exists. Otherwise, I’m not sure how I should approach this otherwise.

So, my question is: 1) How do I correctly use this function? the example uses it without a module prefix, but if I declare it as a regular function I get a fatal error about re-declaring functions.

  $cs_user = array(
        'name' => $user_info['data']['user']['lastname'] . "_psr",
        'mail' => $user_info['data']['user']['email'],
        'pass' => $user_info['data']['user']['lastname'] . "_user_pass",
        'init' => $user_info['data']['user']['lastname'] . "_psr",
        'signature_format' => 'full_html',
        'status' => 1,
        'roles' => array(DRUPAL_AUTHENTICATED_RID => 'authenticated user', 
                            22 => 'CS User External',),
        'field_first_name' => array(array('value' => $user_info['data']['user']['firstname'])),                    
        'field_last_name' => array(array('value' => $user_info['data']['user']['lastname'])),
        'field_chapter' => array(array('value' => $user_info['data']['user']['groups']['0']['title'])), 
       );
    //format the user info, feed it to external_user_login_register
    $name = $cs_user['name'];
    $module = 'chapterspot_signon';
    echo "<hr>";
    }
    function chapterspot_signon_user_external_login_register($name, $module){
        $account = user_load_by_name($name); 
$account = $name;
        if (!$account) {
        // Register this new user.
        $account = user_save(drupal_anonymous_user(), $cs_user);
        // Terminate if an error occurred during user_save().
        if (!$account) {
          drupal_set_message(t("Error saving user account."), 'error');
          return;
        }
        user_set_authmaps($account, array(
          "authname_{$module}" => $name,
        ));
      }
      // Log user in.
      $form_state['uid'] = $account->uid;
      user_login_submit(array(), $form_state);
      drupal_goto('/members');
    }


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.