使用OAuth对CakePHP 2.3进行身份验证 [英] Using OAuth for authentication with CakePHP 2.3
问题描述
我有一个CakePHP应用程式,我想让使用者能够使用OAuth登入。
I have a CakePHP app that I'd like my users to be able to use OAuth to log in to.
我似乎有OAuth对话工作正常,因为我得到用户信息的结束,并可以保存令牌给我的用户
表罚款。
I seem to have the OAuth conversation working correctly, as I am getting user info out of the end of it, and can save the tokens to my users
table fine.
我的问题可能是一个愚蠢的,但我想努力工作,当我需要使用我给予的令牌。我应该将用户的ID存储在cookie中,并且每当他们回到我的网站时,从数据库获取它们的令牌,然后我们重新检查他们的详细信息?
My question is possibly a silly one, but I'm trying to work out when I need to use the token I was given. Should I be storing the user's ID in a cookie, and whenever they 'come back' to my site grab their token from the DB and us it to re-check their details?
我没有为使用OAuth的用户获得任何密码,所以我应该绕过Auth这些人,或使用令牌作为CakePHP的密码?
I don't get any sort of password for the user with OAuth, so should I just bypass Auth for these people, or use one of the tokens as the password for CakePHP?
这里是我的UsersController的登录和oauth2callback部分:
Here are the login and oauth2callback parts of my UsersController:
<?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());
}
}
}
推荐答案
CakePHP向Auth组件添加自定义认证的方法是创建一个自定义认证对象(见 http://book.cakephp.org )。
The CakePHP way of adding custom authentication to the Auth component is to create a "Custom Authentication object" (see http://book.cakephp.org).
这篇关于使用OAuth对CakePHP 2.3进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!