使用OAuth与CakePHP 2.3进行身份验证

使用OAuth与CakePHP 2.3进行身份验证,cakephp,oauth-2.0,cakephp-2.3,Cakephp,Oauth 2.0,Cakephp 2.3,我有一个CakePHP应用程序,我希望我的用户能够使用OAuth登录 我似乎让OAuth对话正常工作,因为我从它的末尾获取了用户信息,并且可以将令牌保存到我的用户表中 我的问题可能很傻,但我正在努力确定何时需要使用别人给我的代币。我是否应该将用户的ID存储在cookie中,并且每当他们“返回”我的站点时,从DB获取他们的令牌,然后让我们重新检查他们的详细信息 我没有为使用OAuth的用户获取任何类型的密码,所以我应该为这些人绕过Auth,还是使用其中一个令牌作为CakePHP的密码 以下是my

我有一个CakePHP应用程序,我希望我的用户能够使用OAuth登录

我似乎让OAuth对话正常工作,因为我从它的末尾获取了用户信息,并且可以将令牌保存到我的
用户
表中

我的问题可能很傻,但我正在努力确定何时需要使用别人给我的代币。我是否应该将用户的ID存储在cookie中,并且每当他们“返回”我的站点时,从DB获取他们的令牌,然后让我们重新检查他们的详细信息

我没有为使用OAuth的用户获取任何类型的密码,所以我应该为这些人绕过Auth,还是使用其中一个令牌作为CakePHP的密码


以下是my UsersController的登录和oauth2callback部分:

<?php
class UsersController extends AppController {

    public function login() {
        if ($this->request->is('post')) {
            if ($this->Auth->login()) {
                $this->redirect($this->Auth->redirect());
            } else {
                $this->Session->setFlash(__('Invalid username or password'));
            }
        } else {
            $client = $this->getGoogleClient();
            $authUrl = $client->createAuthUrl();
            $this->set(array('GoogleAuthUrl' => $authUrl));
        }
    }

    public function oauth2callback() {
        $client = $this->getGoogleClient();

        if (isset($this->request->query['code'])) {
            $client->authenticate($this->request->query['code']);
            $this->Session->write('token', $client->getAccessToken());
            $this->redirect('oauth2callback');
            return;
        }

        if ($this->Session->read('token')) {
            $client->setAccessToken($this->Session->read('token'));
        }

        $accessToken = $client->getAccessToken();
        if ($accessToken) {
            $oauth2  = new Google_Oauth2Service($client);
            $user = $oauth2->userinfo->get();

            $token = json_decode($accessToken);
            debug($token);
            debug($user);
            // We now have a user from Google. Either log them in, or create a new user
            $id = $this->User->field('id', array('email' => $user['email'], 'oauth_id' => $user['id']));
            if (empty($id)) {
                $new_user = $this->User->create();
                $new_user['User']['username'] = $user['email'];
                $new_user['User']['email'] = $user['email'];
                $new_user['User']['oauth_id'] = $user['id'];
                $new_user['User']['oauth_token'] = $token->access_token;
                $new_user['User']['oauth_expires'] = time() + $token->expires_in;
                $new_user['User']['oauth_id_token'] = $token->id_token;
                $new_user['User']['oauth_refresh_token'] = $token->refresh_token;
                $new_user['User']['oauth_created'] = $token->created;
                if ($this->User->save($new_user)) {
                    $new_user['User']['id'] = $this->User->id;
                    debug($new_user);
                    $this->Session->setFlash(__('Registration complete!'));
                    if ($this->Auth->login($new_user)) {
                     //   return $this->redirect($this->Auth->redirectUrl());
                    }
                    //$this->redirect(array('action' => 'index'));
                } else {
                    $this->Session->setFlash(__('There was a problem with your registration. Please, try again.'));
                }

            }

            // The access token may have been updated lazily.
            $this->Session->write('token', $client->getAccessToken());
        }
    }

}

向Auth组件添加自定义身份验证的CakePHP方法是创建一个“自定义身份验证对象”(请参阅)