Enabling OpenID to work when anonymous user sessions are disabled

Posted May 18, 2010 // 3 comments
Brad :

If you have a Drupal installation, such as Pressflow, that does not initialize sessions for anonymous users, you may have found that certain modules don’t work correctly. One of these modules, in Drupal 6, is the OpenID module.

The OpenID module relies on storing user data in an anonymous user’s session. If a session hasn’t been started, then the login fails because the data does not get stored.

Instead of patching the OpenID module, which is part of Core, you can use a hook in a module to restore the OpenID functionality.

First, we’ll alter the user_login form to add a validation function to the beginning of the form validate array if the OpenID form has been submitted. (Note: This hook_form_alter needs to run after the form_alter hook in the OpenID module. If your validation function is not being called, you may need to adjust the module weights.)

1
2
3
4
5
6
7
function module_form_alter(&$form, $form_state, $form_id) {
  if($form_id == 'user_login') {
    if (!empty($form_state['post']['openid_identifier'])) {
      array_unshift($form['#validate'],'module_openid_validate');
    }    
  }
}

In our validation function, we check to make sure the OpenID form has been submitted, and if so, explicitly start a session. This way, the OpenID module is able to store data in an anonymous user’s session.

1
2
3
4
5
function module_openid_validate($form, &$form_state) {
  if(!empty($form_state['values']['openid_identifier']) && !empty($form_state['values']['openid.return_to'])) {
    session_start();
  }
}  

The benefit of solving this problem through the hook instead of a Core patch is that there is nothing to keep track of during upgrades.

About Brad

Web Developer Brad Blake brings a wealth of expertise to our team and our clients whenever he creates software tools and websites on the LAMP platform. For more than five years, he has been using PHP to build cutting-edge technologies that ...

more >

Read Brad 's Blog

Comments

by Anonymous (not verified) on Tue, 05/18/2010 - 16:46

That comes handy! Just

That comes handy! Just another great post of you guys @phase2tech

another question:

When do you guys plan to release tattlerapp (if at all)?

I´m waiting in vain...the current version is a little buggy.. - the idea/concept is marvellous.

no - rly =)

by christefano (not verified) on Thu, 05/20/2010 - 05:00

Have you considered

Have you considered contributing this to Pressflow?

by Jay (not verified) on Tue, 05/25/2010 - 09:03

still getting "OpenID login failed"

i plugged in your fix, but still get the overly generic openid error

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • Allowed HTML tags: <a> <strong> <code> <p> <img> <ul> <ol> <li> <h2> <h3> <h4> <b> <u> <i>
  • You may insert videos with [video:URL]

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.