Search code examples
zend-frameworkauthenticationopenid

How to retrieve Profile data using OpenID authentication? ( Zend Framework )


I read some tutorials and came up with the following script to authenticate with OpenID. But I don't know how to retrieve profile data of the user logged in, like fullname, or email. can anyone help me with that?


$status='';
$auth=Zend_Auth::getInstance();
$post=array();
$get=$this->getRequest()->getParams();
if($this->getRequest()->isPost()){
    $post=$this->getRequest()->getPost();
}
$profile=new Zend_OpenId_Extension_Sreg(array(
    'nickname' => true,
    'email'    => true,
    'fullname' => true),null,1.1
);

if($auth->hasIdentity()){
    if(isset($get['openid_action']) && $get['openid_action']=='logout'){
        $auth->clearIdentity();
        $status="logged Out";
    }else{
        $status="logged in as ".$auth->getIdentity();   
    }
}else if(isset($post['openid_action']) && $post['openid_action']=='login' && $post['openid_identifier']){
    $result=$auth->authenticate(new Zend_Auth_Adapter_OpenId($post['openid_identifier']));
    $status='something went wrong';
}else if(isset($get['openid_mode'])){
    $result=$auth->authenticate(new Zend_Auth_Adapter_OpenId());
    if(!$result->isValid()){
        $auth->clearIdentity();
    }
    $status.= implode('
',$result->getMessages()); }else{ $status='You are not logged in'; } $this->view->status=$status;

Solution

  • Ok I found out how to do that:

    
    $status='';
    $auth=Zend_Auth::getInstance();
    $post=array();
    $get=$this->getRequest()->getParams();
    if($this->getRequest()->isPost()){
        $post=$this->getRequest()->getPost();
    }
    
    //changed nickname and fullname to false, so if provider didn't provide these, authentication won't fail.
    $profile=new Zend_OpenId_Extension_Sreg(array(
        'nickname' => false,
        'email'    => true,
        'fullname' => false),null,1.1
    );
    
    if($auth->hasIdentity()){
        if(isset($get['openid_action']) && $get['openid_action']=='logout'){
            $auth->clearIdentity();
            $status="logged Out";
        }else{
            $status="logged in as ".$auth->getIdentity();   
        }
    }else if(isset($post['openid_action']) && $post['openid_action']=='login' && $post['openid_identifier']){
        $result=$auth->authenticate(new Zend_Auth_Adapter_OpenId($post['openid_identifier'],null,null,null,$profile));
        $status='something went wrong';
    }else if(isset($get['openid_mode'])){
        $result=$auth->authenticate(new Zend_Auth_Adapter_OpenId(null,null,null,null,$profile));
        if(!$result->isValid()){
            $auth->clearIdentity();
        }else{
            //here you have the information you need.
            $info=$profile->getProperties();
        }
        $status.= implode('
    ',$result->getMessages());
    
    }else{
        $status='You are not logged in';
    }
    $this->view->status=$status;
    

    But if you want to use google or yahoo, or any OpenId 2.0 provider, you should use patches.