2011-04-13 75 views
22

我想在Yii中使用OpenID支持。對Yii的OpenId支持

經過研究可能的插件,我發現這兩個。一個用於OpenidSelector,一個用於LightOpenId

http://www.yiiframework.com/extension/simpleopenidselector/

http://www.yiiframework.com/extension/loid

這些是在Yii中使用了對OpenID的支持正確的擴展?還要別的嗎? 我想得到一些關於如何處理這些擴展的指導,如果他們是正確的。

這是我認爲我需要做的除了按照網頁上的說明安裝它們。

  1. 創建OpenIdUserIdentity延伸CUserIdentity並把身份驗證()的代碼有
  2. 創建一個登錄頁面,並把simpleopenidselector代碼視圖。
  3. 在siteController

然後我有點失去了創建actionOpenIdLogin methon,因爲我不明白霧狀的用法樣品和我不知道該怎麼辦(3)上述(1)和。

請讓我知道,如果我在正確的軌道上,並可能提供一些指導。謝謝。

+0

你需要使用它們嗎? – BlueDolphin 2011-11-27 00:45:43

+0

是的,使用它們兩個 – Alocus 2012-02-20 08:06:35

回答

10

玩了一段時間後,我會回答我自己的問題。這就是我的工作方式,所以您可以根據自己的需求進行更改。

注意:我使用userController而不是siteController,請按照相應擴展頁面中的所有說明進行操作。

如果您使用了上述兩個插件,那麼您需要做的下一步工作如下:(這是一步一步的指南) 但最重要的步驟是2c和3,他們是這兩種插件的膠水

1)有一個使用OpenidSelector的登錄頁面。將它置於views/user/login.php

<?php 
$this->widget('application.extensions.openidProviders.openidProviders', 
array ('options' => array ('lang' => 'en', 
//  'demo' => 'js:true', 
    'cookie_expires' => 6*30, 
    )));?> 

2)設置操作以處理來自openidSelector的選擇。我把它放在userController中。

a)在主配置文件中。

'components'=>array(
    'user'=>array(
     // enable cookie-based authentication 
     'allowAutoLogin'=>true, 
     'loginUrl' => array('/user/login'), //change the default login page 
    ), 

二)在UserController的文件,添加登錄和驗證行動

array('allow', // allow all users to perform 'index' and 'view' actions 
    'actions'=>array('login', 'authenticate'), 

規範行動#1 actionLogin - 這是觸發登錄視圖頁面。

public function actionLogin() 
{  
    // display the login form 
    $this->render('login',array()); 
} 

c)代碼:動作#2 actionAuthenticate - 從LOID指令頁修改的碼,這是當在登錄頁面中選擇一個OpenIDProvider來處理。

public function actionAuthenticate() 
{ 
    // Put the Simple usage: code on 
    // http://www.yiiframework.com/extension/loid here: 

    // Code from loid Simple usage page. 
    // START HERE 
    $loid = Yii::app()->loid->load(); 
    if (!empty($_GET['openid_mode'])) { 
     if ($_GET['openid_mode'] == 'cancel') { 
     $err = Yii::t('core', 'Authorization cancelled'); 
     } else { 
     try { 
      echo $loid->validate() ? 'Logged in.' : 'Failed'; 
     } catch (Exception $e) { 
      $err = Yii::t('core', $e->getMessage()); 
     } 
    } 
    if(!empty($err)) echo $err; 
    } else { 
     // **NOTE:Comment out this line from the loid sample page** 
     // $loid->identity = "http://my.openid.identifier"; //Setting identifier 
     // this openid_identifier is need after you click the openselector 
     $loid->identity = $_GET['openid_identifier']; // CHANGE HERE 

     $loid->required = array('namePerson/friendly', 'contact/email'); //Try to get info from openid provider 
     $loid->realm  = (!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST']; 
     $loid->returnUrl = $loid->realm . $_SERVER['REQUEST_URI']; //getting return URL 
     if (empty($err)) { 
      try { 
       $url = $loid->authUrl(); 
       $this->redirect($url); 
      } catch (Exception $e) { 
       $err = Yii::t('core', $e->getMessage()); 
      } 
     } 
    } 
    // Code from loid Simple usage page. 
    // END HERE 
} 

3)變更動作URL在openidProviders /視圖/主en.php進行身份驗證

變化

form action="examples/consumer/try_auth.php" method="get" id="openid_form" 

form action="authenticate" method="get" id="openid_form" 

這應該是它。沒有測試失敗的案例,只用谷歌登錄測試。

+0

你能舉一個例子說明你在C部分做了些什麼來完成這項工作。我只是不確定在「打印」登錄時需要做什麼。';「部分。用於更改其他身份驗證機制的文檔涉及修改UserIdentity類。但是,這個擴展似乎並沒有這樣工作。或者如果你不想在這裏發佈你的代碼。你能寫信給[email protected]。謝謝。 – deltaray 2011-07-18 20:01:22

+0

與deltaray相同的請求。謝謝 – BlueDolphin 2011-11-27 01:56:51

+0

加上你的示例代碼,我看到了一個id提供者列表,比如google,yahoo,但是當我點擊其中的任何一個時,我就看不到404頁面。這是網址:http://testingenv.com/authenticate?action = verify&openid_identifier = http%3A%2F%2Fme.yahoo.com%2F任何建議? – BlueDolphin 2011-12-01 02:40:29

8

enter image description here

YiiAuth現在,這使得使用HybridAuth庫。

+0

剛剛瀏覽Yii身份驗證助手並發現此問題。 +1,因爲它真的幫助我:) – 2012-08-15 00:31:56