5

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

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

我的问题可能很愚蠢,但我正在努力解决何时需要使用给我的令牌。我是否应该将用户的 ID 存储在 cookie 中,并且每当他们“回到”我的网站时,从数据库中获取他们的令牌并让我们重新检查他们的详细信息?

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


这是我的 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());
        }
    }

}
4

1 回答 1

1

CakePHP 向 Auth 组件添加自定义身份验证的方法是创建一个“自定义身份验证对象”(请参阅​​ http://book.cakephp.org)。

于 2014-01-03T16:22:19.340 回答