2010-06-03 124 views
2

我希望我的網站能夠做到Stackoverflow與openId完全相同的功能。使用OpenID與Zend框架

我正在梳理源代碼,我之前用facebook做過這個,但 在OpenID方面沒有取得很大進展。

我想要做的只是檢測是否有人登錄到谷歌, ,如果他們已經得到一些識別信息,並允許他們聯繫到我的網站。

任何人都可以提出任何教程或代碼片段,我應該使用Zends庫嗎?

-P

回答

3
/** 
* SM's code library 
* 
* @category  
* @package  
* @subpackage 
* @copyright Copyright (c) 2009 Pavel V Egorov 
* @author  Pavel V Egorov <[email protected]> 
* @link  http://epavel.ru/ 
* @since  29.04.2010 
*/ 

/* 
* в configs/application.ini 
* 
* ;php-openid library 
* includePaths[] = APPLICATION_PATH "/../include/php-openid-2.1.3" 
* autoloadernamespaces[] = Auth_ 
*/ 


define ('Auth_Yadis_CURL_OVERRIDE', true); 
define ('Auth_OpenID_RAND_SOURCE', null); 

class Smapp_Auth_Adapter_OpenId implements Zend_Auth_Adapter_Interface 
{ 
    /** 
    * The identity value being authenticated 
    * 
    * @var string 
    */ 
    protected $_id = null; 

    /** 
    * Reference to an implementation of a storage object 
    * 
    * @var Auth_OpenID_OpenIDStore 
    */ 
    protected $_storage = null; 

    /** 
    * The URL to redirect response from server to 
    * 
    * @var string 
    */ 
    protected $_returnTo = null; 

    /** 
    * The HTTP URL to identify consumer on server 
    * 
    * @var string 
    */ 
    protected $_root = null; 

    /** 
    * Extension object or array of extensions objects 
    * 
    * @var string 
    */ 
    protected $_extensions = null; 

    /** 
    * The response object to perform HTTP or HTML form redirection 
    * 
    * @var Zend_Controller_Response_Abstract 
    */ 
    protected $_response = null; 

    /** 
    * Enables or disables interaction with user during authentication on 
    * OpenID provider. 
    * 
    * @var bool 
    */ 
    protected $_check_immediate = false; 

    /** 
    * Constructor 
    * 
    * @param string $id the identity value 
    * @param Auth_OpenID_OpenIDStore $storage an optional implementation 
    *  of a storage object 
    * @param string $returnTo HTTP URL to redirect response from server to 
    * @param string $root HTTP URL to identify consumer on server 
    * @param mixed $extensions Auth_OpenID_Extension extension object or array of extensions objects 
    * @param Zend_Controller_Response_Abstract $response an optional response 
    *  object to perform HTTP or HTML form redirection 
    * @return void 
    */ 
    public function __construct($id = null, 
           Auth_OpenID_OpenIDStore $storage = null, 
           $returnTo = null, 
           $root = null, 
           $extensions = null, 
           Zend_Controller_Response_Abstract $response = null) { 
     $this->_id   = $id; 
     $this->_storage = $storage; 
     $this->_returnTo = $returnTo; 
     $this->_root  = $root; 
     $this->_extensions = $extensions; 
     $this->_response = $response; 

     //нужно потому что автозагрузчик зенда найти не может. там куча классов в одном файле 
     require_once 'Auth/OpenID/SReg.php'; 
     require_once 'Auth/OpenID/PAPE.php'; 
    } 

    /** 
    * 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; 
      } 
     } 
    } 

    /** 
    * @param Auth_OpenID_ConsumerResponse $response 
    * @return Zend_Auth_Result 
    */ 
    protected function _constructSuccessfulResult(Auth_OpenID_ConsumerResponse $response) 
    { 
     $identity = array(); 

     $identity['openid_identity'] = $response->getDisplayIdentifier(); 

     if ($response->endpoint->canonicalID) { 
      $identity['openid_op_endpoint'] = $response->endpoint->canonicalID;  
     } 
     if ($sreg = Auth_OpenID_SRegResponse::fromSuccessResponse($response)) { 
      $identity['sreg'] = $sreg->contents(); 
     } 
     if ($pape = Auth_OpenID_PAPE_Response::fromSuccessResponse($response)) { 
      $identity['pape'] = (array)$pape; 
     } 

     return new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, $identity, array("Authentication successful")); 
    } 

    /** 
    * Sets the storage implementation which will be use by OpenId 
    * 
    * @param Auth_OpenID_OpenIDStore $storage 
    * @return Smapp_Auth_Adapter_OpenId Provides a fluent interface 
    */ 
    public function setStorage(Auth_OpenID_OpenIDStore $storage) 
    { 
     $this->_storage = $storage; 
     return $this; 
    } 

    /** 
    * Sets the value to be used as the identity 
    * 
    * @param string $id the identity value 
    * @return Zend_Auth_Adapter_OpenId Provides a fluent interface 
    */ 
    public function setIdentity($id) 
    { 
     $this->_id = $id; 
     return $this; 
    } 

    /** 
    * Sets the HTTP URL to redirect response from server to 
    * 
    * @param string $returnTo 
    * @return Zend_Auth_Adapter_OpenId Provides a fluent interface 
    */ 
    public function setReturnTo($returnTo) 
    { 
     $this->_returnTo = $returnTo; 
     return $this; 
    } 

    /** 
    * Sets HTTP URL to identify consumer on server 
    * 
    * @param string $root 
    * @return Zend_Auth_Adapter_OpenId Provides a fluent interface 
    */ 
    public function setRoot($root) 
    { 
     $this->_root = $root; 
     return $this; 
    } 

    /** 
    * Sets OpenID extension(s) 
    * 
    * @param mixed $extensions 
    * @return Zend_Auth_Adapter_OpenId Provides a fluent interface 
    */ 
    public function setExtensions($extensions) 
    { 
     $this->_extensions = $extensions; 
     return $this; 
    } 

    /** 
    * Sets an optional response object to perform HTTP or HTML form redirection 
    * 
    * @param string $root 
    * @return Zend_Auth_Adapter_OpenId Provides a fluent interface 
    */ 
    public function setResponse($response) 
    { 
     $this->_response = $response; 
     return $this; 
    } 

    /** 
    * Enables or disables interaction with user during authentication on 
    * OpenID provider. 
    * 
    * @param bool $check_immediate 
    * @return Zend_Auth_Adapter_OpenId Provides a fluent interface 
    */ 
    public function setCheckImmediate($check_immediate) 
    { 
     $this->_check_immediate = $check_immediate; 
     return $this; 
    } 
} 

現實世界的例子 http://zonabarahla.ru/users/auth/login

來源:http://zonabarahla.ru/users/auth/login?show-me-the-truth

是使用http://openidenabled.com/files/php-openid/packages/php-openid-2.1.3.zip

class Users_View_Helper_UsersLinker extends Zend_View_Helper_Abstract 
{ 
    /** 
    * @var Zend_View_Helper_Url 
    */ 
    protected static $_urlHelper; 

    /** 
    * @return Users_View_Helper_UsersLinker 
    */ 
    public function usersLinker() 
    { 
     return $this; 
    } 


    public function viewProfile($id) 
    { 
     $options = array(
      'controller' => 'view', 
      'action'  => 'profile', 
      'id'   => (int)$id 
     ); 
     return $this->_url($options); 
    } 

    public function viewList() 
    {   
     $options = array(
      'controller' => 'view', 
      'action'  => 'list', 
     ); 
     return $this->_url($options); 
    } 

    public function crudUpdate($id) 
    { 
     $options = array(
      'controller' => 'crud', 
      'action'  => 'update', 
      'id'   => (int)$id 
     ); 
     return $this->_url($options); 
    } 


    public function login() 
    { 
     $options = array(
      'controller' => 'auth', 
      'action'  => 'login', 
     ); 
     $url = $this->_url($options); 
     return $url; 
    } 

    public function logout() 
    { 
     $options = array(
      'controller' => 'auth', 
      'action'  => 'logout' 
     ); 
     return $this->_url($options); 
    } 

    /** 
    * @return Zend_View_Helper_Url 
    */ 
    public function getUrlHelper() 
    { 
     if (null === self::$_urlHelper) { 
      self::$_urlHelper = new Zend_View_Helper_Url(); 
     } 
     return self::$_urlHelper; 
    } 

    protected function _url($options, $routeName = null, $reset = true) 
    { 
     $urlHelper = $this->getUrlHelper(); 
     if (empty($options['module'])) { 
      $options['module'] = $this->_getModuleName(); 
     } 

     return $urlHelper->url($options, is_null($routeName) ? 'default' : $routeName, $reset); 
    } 

    protected function _getModuleName() 
    { 
     $moduleName = substr(get_class($this), 0, strpos(get_class($this), '_')); 
     return strtolower($moduleName); 
    } 
} 
+0

感謝您的代碼 – user284503 2010-06-05 13:11:24

+0

歡迎您:) – SMka 2010-06-06 14:52:40

+0

@SM感謝您這段代碼。你會分享你的'userLinker'視圖助手嗎? – takeshin 2010-10-31 13:44:34