1

When I sign in to StackOverflow using Google, I get the following message:

Stackoverflow.com is asking for some information from your Google Account example@gmail.com

• Email address: example@gmail.com

However, on my own site, when I log in with OpenID I can't ask for the e-mail address. Instead, I get this message:

You are signing in to example.com with your Google Account example@gmail.com

I am also finding it difficult to understand at what step I need to request the e-mail address. Here is the code that I think the step should be built into:

/**
 * Authenticates the given OpenId identity.
 * Defined by Zend_Auth_Adapter_Interface.
 *
 * @throws Zend_Auth_Adapter_Exception If answering the authentication query is impossible
 * @return Zend_Auth_Result
 */
public function authenticate() {
    $id = $this->_id;
    $consumer = new Auth_OpenID_Consumer($this->_storage);
    
    if (!empty($id)) {
        $authRequest = $consumer->begin($id);
        
        if (is_null($authRequest)) {
            return new Zend_Auth_Result(
                Zend_Auth_Result::FAILURE,
                $id,
                array("Authentication failed", 'Unknown error')
            );
        }
        
        if (Auth_OpenID::isFailure($authRequest)) {
            return new Zend_Auth_Result(
                Zend_Auth_Result::FAILURE,
                $id,
                array("Authentication failed", "Could not redirect to server: " . $authRequest->message)
            );
        }
        
        $redirectUrl = $authRequest->redirectUrl($this->_root, $this->_returnTo);
        
        if (Auth_OpenID::isFailure($redirectUrl)) {
            return new Zend_Auth_Result(
                Zend_Auth_Result::FAILURE,
                $id,
                array("Authentication failed", $redirectUrl->message)
            );
        }
        
        Zend_OpenId::redirect($redirectUrl);
    } else {
        $response = $consumer->complete(Zend_OpenId::selfUrl());

        switch($response->status) {
            case Auth_OpenID_CANCEL:
            case Auth_OpenID_FAILURE:
                return new Zend_Auth_Result(
                    Zend_Auth_Result::FAILURE,
                    null,
                    array("Authentication failed. " . @$response->message)
                );
                break;
            case Auth_OpenID_SUCCESS:
                return $this->_constructSuccessfulResult($response);
                break;
        }
    }
}

This seems like it should be so obvious... but I'm having a hard time Googling it and combing through the code to figure it out. Any help would be greatly appreciated!

Community
  • 1
  • 1
Moak
  • 12,596
  • 27
  • 111
  • 166

1 Answers1

1

You can ask for an email address using Zend Simple Registration Extension

$sreg = new Zend_OpenId_Extension_Sreg(array(
    'nickname'=>true,
    'email'=>false,
    'fullname'=>false), null, 1.1);
$consumer = new Zend_OpenId_Consumer();
if (!$consumer->login($_POST['openid_identifier'],
                      'example-6_3.php',
                      null,
                      $sreg)) {
    die("OpenID login failed.");
}

If you want to use Janrain library, you can add extensions to the request like this:

$sreg_request = Auth_OpenID_SRegRequest::build(
                                 // Required
                                 array('nickname'),
                                 // Optional
                                 array('email'));

if ($sreg_request) {
    $authRequest->addExtension($sreg_request);
}

Have a look at the consumer example: https://github.com/openid/php-openid/blob/master/examples/consumer/

bsrykt
  • 1,275
  • 9
  • 9