Yii 的 OpenId 支持

发布于 2024-10-31 15:34:45 字数 780 浏览 1 评论 0原文

我想在 Yii 中使用 OpenID 支持。

在研究了可能的插件后,我找到了这两个。一种用于 OpenidSelector,一种用于 LightOpenId

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

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

是这些是在 Yii 中用于 OpenId 支持的正确扩展吗?还要别的吗? 如果这些扩展正确的话,我想获得一些关于如何处理这些扩展的指南。

这是我认为除了按照页面上的说明安装它们之外我还需要做的事情。

  1. 创建 OpenIdUserIdentity 扩展 CUserIdentity 并将authenticate() 代码放在那里
  2. 创建一个登录页面并将 simpleopenidselector 代码放入视图中。
  3. 在 siteController 中创建一个 actionOpenIdLogin 方法

,然后我有点迷失了,因为我不理解 Loid 中的用法示例,并且我不知道如何执行上面的(1)和(3)。

请让我知道我是否走在正确的道路上,并可能提供一些指导。谢谢。

I want to play with OpenID support in Yii.

After researching for possible plugins, I found these two. One for OpenidSelector and one for LightOpenId

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

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

Are these the right extensions to use in Yii for OpenId support? Anything else?
And I would like to get some guide line on what to do with these extensions if they are correct.

This is what I think I need to do beside installing them as per instructions on the page.

  1. Create OpenIdUserIdentity extends CUserIdentity and put the authenticate() code there
  2. Create a login page and put the simpleopenidselector code in a view.
  3. Create a actionOpenIdLogin methon in siteController

then I am kind of lost as I don't understand the Usage sample in Loid and I am not sure how to do (1) and (3) above.

Please let me know if I am on the right track and possibly provide some guidance. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

懵少女 2024-11-07 15:34:45

玩了一段时间后,我将回答我自己的问题。这就是我的工作方式,所以你可以根据你的需要更改它。

注意:我使用 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
    ),

b) 在 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) 将 openidProviders/views/main-en.php 中的操作 URL 更改为 Authenticate

更改

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

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

应该就是这样了。尚未测试失败案例,仅使用谷歌登录进行测试。

After playing with it for awhile, I am going to answer my own question. This is how I make it to work, so you can change it according to your needs.

Note: I use a userController instead of the siteController and please follow all the instructions in the respective extension page.

If you used the two plugins as indicated above, then what you need to do next to make it work are the followings: (this is a step by step guide)
But the most important steps are 2c and 3, they are the glue to both plugins

1) Have a login page that uses the OpenidSelector. Place it at views/user/login.php

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

2) Setup actions to handle the selection from the openidSelector. I put this in the userController.

a) In main config file.

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

b) In userController file, add login and authenticate actions

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

Code for action #1 actionLogin - this is to trigger the login view page.

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

c) Code for action #2 actionAuthenticate - code modified from the LOID instruction page, this is to handle when an OpenIDProvider is selected in the login page.

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) Change the action URL to Authenticate in the openidProviders/views/main-en.php

Change

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

to

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

That should be it. Haven't tested failure case, only tested with google login.

怎言笑 2024-11-07 15:34:45

在此处输入图像描述

YiiAuth 现在,它使用 HybridAuth 库。

enter image description here

There is YiiAuth now, which makes use of the HybridAuth library.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文