使用Yii-user和Opauth

时间:2013-01-20 14:15:00

标签: php oauth yii yii-extensions

我一直试图让Opauth以理智的方式将params传递给Yii-User。这是我正在使用的扩展git repos:

Opauth (via Yii Framework module)使用官方Opauth回购的更新库 Yii-User Module repo

在修改了Opauth的Callback控制器后,我从Opauth获得了正确的数组,直接在页面上显示了我的Google帐户信息。

这是Callback控制器中的代码:



    class CallbackController extends Controller {

        public $defaultAction = 'callback';

            public function actionCallBack() {
                if ( $this->module->getConfig() ) {

                    /**
                     * Instantiate Opauth with the loaded config but no run automatically
                     * Only create Opauth if one is not already there
                     */
                    if (!$this->module->getOpauth()) {
                        $this->module->setOpauth (new Opauth($this->module->getConfig(), false));
                        $Opauth = $this->module->getOpauth();
                    } else {
                        $Opauth = $this->module->getOpauth();
                    }


                    /**
                     * Fetch auth response, based on transport configuration for callback
                     */
                    $response = null;

                    switch($Opauth->env['callback_transport']) {
                        case 'session':
                            session_start();
                            $response = $_SESSION['opauth'];
                            unset($_SESSION['opauth']);
                            break;
                        case 'post':
                            $response = unserialize(base64_decode( $_POST['opauth'] ));
                            break;
                        case 'get':
                            $response = unserialize(base64_decode( $_GET['opauth'] ));
                            break;
                        default:
                            echo 'Error: Unsupported callback_transport.'."
\n"; break; } /** * Check if it's an error callback */ if (array_key_exists('error', $response)) { echo 'Authentication error: Opauth returns error auth response.'."
\n"; } /** * Auth response validation * * To validate that the auth response received is unaltered, especially auth response that * is sent through GET or POST. */ else{ if (empty($response['auth']) || empty($response['timestamp']) || empty($response['signature']) || empty($response['auth']['provider']) || empty($response['auth']['uid'])) { echo 'Invalid auth response: Missing key auth response components.'."
\n"; } elseif (!$Opauth->validate(sha1(print_r($response['auth'], true)), $response['timestamp'], $response['signature'], $reason)) { echo 'Invalid auth response: '.$reason.".
\n"; } else { echo 'OK: Auth response is validated.'."
\n"; /** * It's all good. Go ahead with your application-specific authentication logic */ $Opauth->response = $response; Yii::app()->getModule('opauth')->setOpauth($Opauth); $this->redirect(Yii::app()->getModule('user')->returnUrl); } } } else { echo 'No configuration loaded!'; } } }

我已经尝试将响应直接保存到模块(显示在控制器的末尾)但是这不起作用 - 我认为每次运行都会创建模块类。我不确定如何将响应直接移到yii-user模块中。

编辑:我在Callback控制器中尝试这样的事情:

$identity = new UserIdentity($response);
if ($identity->authenticate()) {
     Yii::app()->user->login($identity);
     $this->redirect('/');
} else {
     $this->redirect(Yii::app()->getModule('user')->returnUrl);
}

对于$ identity-> authenticate(),我有:

$identity = new UserIdentity($response);
if ($identity->authenticate()) {
     Yii::app()->user->login($identity);
     $this->redirect('/');
} else {
     $this->redirect(Yii::app()->getModule('user')->returnUrl);
}

1 个答案:

答案 0 :(得分:1)

以下是我为使这部分工作所做的工作:

$identity = new UserIdentity($response);
$identity->authenticate();

switch($identity->errorCode) {
    case $identity::ERROR_NONE:
        Yii::app()->user->login($identity);
        $this->redirect(array("page_after_login"));
        break;
     case $identity::ERROR_USERNAME_INVALID:
        $this->redirect(Yii::app()->user->registrationUrl);
        break;
     default:
        throw new CException ($identity->errorCode);
}