【发布时间】:2017-07-23 02:47:17
【问题描述】:
首先,感谢任何愿意和我一起研究这个问题的人。让我说我是一个 CakePHP 初学者。虽然我的网站是功能性的,但它相对简单,因此我没有从开发它的框架中获得太多的知识。比方说,我是一个基本用户,有一个不太基本的问题......!
所以,我目前正在使用 AngularJS 和 CakePHP 3 开发一个网站。CakePHP 部分是一个 REST API,当然还有网站的客户端 Angular。
某些页面只能由注册/登录的用户访问,或者至少是电子邮件与@mydomain.com 匹配的用户(然后应该注册)。
最初,API/站点被设计为通过 HTTP 基本身份验证来处理这个问题,但两天前我被要求通过 Google OAuth2 身份验证来处理它。
所以我试着环顾四周,是否有人在 CakePHP3 上做过,没有插件(有人向我提到 CakeDC/users 插件,但文档太差了,我没有去那里......) .我找到了这些:
http://caketuts.key-conseil.fr/index.php/2015/05/22/integrer-lapi-oauth2-de-google-avec-cakephp-v3/(法语,抱歉,但代码很清楚)
http://blog.jainsiddharth21.com/2013/04/29/login-with-google-in-cakephp/(这是可以理解的,但不是我真正选择的做法,但仍然有用且接近我的代码)
虽然我的代码几乎 90% 都像第一个链接,但我似乎无法让身份验证按预期的方式工作。
这是我的代码:
AppController.php:
public function initialize() {
parent::initialize();
$this->loadComponent('RequestHandler');
$this->loadComponent('Flash');
$this->loadComponent('Auth', [
'loginAction' => [
'controller' => 'Users',
'action' => 'googlelogin',
],
'authenticate' => [
'Form' => [
'fields' => [
'username' => 'email',
'password' => 'password'
]
]
],
'authError' => __("You don't have rights for this page"),
'authorize' => ['Controller'],
'unauthorizedRedirect' => [
'controller' => 'Users',
'action' => 'forbidden'
],
'loginRedirect' => [
'controller' => 'myHomePage',
],
'logoutRedirect' => [
'controller' => 'Users',
'action' => 'googlelogin'
]
]);
}
UsersController.php
public function googlelogin() {
$client = new Google_Client();
$client->setClientId(GOOGLE_OAUTH_CLIENT_ID);
$client->setClientSecret(GOOGLE_OAUTH_CLIENT_SECRET);
$client->setRedirectUri(GOOGLE_OAUTH_REDIRECT_URI);
$client->setScopes(array(
"https://www.googleapis.com/auth/userinfo.profile",
'https://www.googleapis.com/auth/userinfo.email'
));
$url = $client->createAuthUrl();
$this->redirect($url);
}
public function confirmLogin() {
$client = new Google_Client();
$client->setClientId(GOOGLE_OAUTH_CLIENT_ID);
$client->setClientSecret(GOOGLE_OAUTH_CLIENT_SECRET);
$client->setRedirectUri(GOOGLE_OAUTH_REDIRECT_URI);
$client->setScopes(array(
"https://www.googleapis.com/auth/userinfo.profile",
'https://www.googleapis.com/auth/userinfo.email'
));
$client->setApprovalPrompt('auto');
if (isset($this->request->query['code'])) {
$client->authenticate($this->request->query['code']);
$this->request->Session()->write('access_token', $client->getAccessToken());
}
if ($this->request->Session()->check('access_token') && ($this->request->Session()->read('access_token'))) {
$client->setAccessToken($this->request->Session()->read('access_token'));
}
if ($client->getAccessToken()) {
$this->request->Session()->write('access_token', $client->getAccessToken());
$oauth2 = new Google_Service_Oauth2($client);
$user = $oauth2->userinfo->get();
try {
if (!empty($user)) {
if (preg_match("/(@mydomain\.com)$/", $user['email'])) {
$result = $this->Users->find('all')
->where(['email' => $user['email']])
->first();
if ($result) {
$this->Auth->setUser($result->toArray());
$this->redirect($this->Auth->redirectUrl());
} else {
$data = array();
$data['email'] = $user['email'];
$data['first_name'] = $user['givenName'];
$data['last_name'] = $user['familyName'];
$data['socialId'] = $user['id'];
//$data matches my Users table
$entity = $this->Users->newEntity($data);
if ($this->Users->save($entity)) {
$data['id'] = $entity->id;
$this->Auth->setUser($data);
$this->redirect($this->Auth->redirectUrl());
} else {
$this->Flash->set('Logging error');
$this->redirect(['action' => 'login']);
}
}
} else {
$this->Flash->set('Forbidden');
$this->redirect(['action' => 'login']);
}
} else {
$this->Flash->set('Google infos not found');
$this->redirect(['action' => 'login']);
}
} catch (\Exception $e) {
$this->Flash->set('Google error');
return $this->redirect(['action' => 'login']);
}
}
}
我还在文件中添加了以下几行
paths.php
define('GOOGLE_OAUTH_CLIENT_ID', 'My_client_id');
define('GOOGLE_OAUTH_CLIENT_SECRET', 'My_client_secret');
define('GOOGLE_OAUTH_REDIRECT_URI', 'mylinkto/confirmLogin');
在 Chrome 的调试工具中,似乎使用有效代码作为查询参数调用了 confimLogin(顺便说一下两次),然后调用了 googlelogin。所以我每次都会出现在日志页面上......
我觉得这里一定有我遗漏的东西。有人有什么主意吗? (谢谢!)
【问题讨论】:
标签: php angularjs cakephp oauth-2.0 google-oauth