combining cakephp's auth with hybridauth (oauth)


combining cakephp's auth with hybridauth (oauth)

我想我只是在寻找一些关于如何使用cakehp2.0在我的网站上设置我的用户+身份验证区域的指导。

我在UserController.php中设置了一个基本的登录和注册功能,其中还包含查找和显示其他用户的功能。

我还想使用hybridauth插件,允许用户使用他们的社交媒体/oauth帐户登录/注册(例如,允许数据库中每个用户有一个以上的社交帐户,这样同一用户就可以使用他们的推特或脸书详细信息)。目前我只有3个提供者,所以我计划在我的用户数据库中只创建6列,令牌和outhid在同一用户行中。这在另一个控制器HybridauthController.php中。

我已经设置了hybridauth,这样它就可以根据帐户详细信息创建它的"hybridauth"对象,这很好,但我希望能够将整个"身份验证"合并在一起,这样我的cakepp会话就包含了hybridauth会话数据(对象),创建我可以使用的通用"当前用户"数组,并在其中设置一些通用变量,取决于他们是否来自oauth。

我目前没有将会话存储在数据库中,理想情况下,我希望允许所有人都使用持久会话,无论他们是否使用oauth帐户。我真的不明白持久会话应该如何使用hybridauth,因为在这个例子中,当用户第二天返回时,如何填写$current_user_id?当然不是通过饼干?

http://hybridauth.sourceforge.net/userguide/HybridAuth_Sessions.html

总之,我正在寻找一个简单的解决方案,将我的所有"会话"/"身份验证"/"用户"数据组合到用户会话中的一个简单数组中。希望这一切都有意义!

感谢

回答晚了,但希望它能帮助您或其他人进行搜索。

我很难将HybridAuth集成到先前存在的CakePHP(1.3)应用程序中,该应用程序已经在使用CakeSessions和Auth组件。我最初尝试使用从HybridAuth网站下载的Cake示例,但这对我不起作用

我们预先存在的用户控制器中已经有一堆逻辑,包括一个beforeFilter方法。这就是我插入session_start()的地方,而不是HybridAuth Cake示例中的类声明:

class UsersController extends AppController {
    ...
    var $components = array('Session','Auth','Email');
    ...
    public function beforeFilter(){
        // following two lines may not be necessary for your setup
        $this->Auth->allow('oauth', 'oauthLogout');
        $excludeBeforeFilter = array('oauth', 'oauthLogout');
        if ( ! in_array($this->action, $excludeBeforeFilter) ) {
            // preexisting Auth logic -- I had to bypass it because
            // calling `session_start` was messing up Cake's Auth
            ...
        } else {
            /* THIS IS NECCESSARY FOR HYBRIDAUTH (OAUTH) */
            /* setting the session ID made it so users could NOT log
               in from a 3rd party and our site */
            session_id($_COOKIE['CAKEPHP']);
            session_start();
        }
    }
    ...
    public function oauth($provider){
        $this->autoRender = false;
        // include HybridAuth
        require_once( WWW_ROOT . 'hybridauth/Hybrid/Auth.php' );
        try {
            // I stored my provider config in a Cake config file
            // It's essentially a copy from the site/Cake example
            $hybridAuth  = new Hybrid_Auth(Configure::read('HAuth.config'));
            $adapter     = $hybridAuth->authenticate($provider);
            $userProfile = $adapter->getUserProfile();
            // method on the User model to find or create the 3rd party user
            $user        = $this->User->findOrCreate($userProfile, $provider);
            if ( ! $user ) {
                echo 'Unable to find/create user';
            } else {
                // configure your fields as necessary
                $this->Auth->fields = array(...);
                $this->Auth->login($user);
            }
        } catch (Exception $e) {
            echo $e->getMessage();
        }
    }
}

此时,第三方(HybridAuth)用户可以在Auth组件中访问。通过这种方式,用户只登录了一个第三方服务或先前存在的登录(但不能同时登录)。

我在注销时也遇到了问题。为了安全起见,我基本上销毁了所有会话/cookie。在UsersController:的oauthLogout方法中

// probably overkill/paranoia
$this->Auth->logout();
$this->Session->destroy();
session_destroy($_COOKIE['CAKEPHP']);
/* these two seemed to make the difference 
   especially the 4th parameter */
setcookie( 'PHPSESSID', '', 1, '/' ); 
setcookie( 'CAKEPHP', '', 1, '/' );

看起来很生气——很抱歉。希望它能有所帮助!