forked from CakeDC/users
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocialAuthenticate.php
More file actions
executable file
·179 lines (164 loc) · 5.87 KB
/
Copy pathSocialAuthenticate.php
File metadata and controls
executable file
·179 lines (164 loc) · 5.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
/**
* Copyright 2010 - 2015, Cake Development Corporation (http://cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010 - 2015, Cake Development Corporation (http://cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace CakeDC\Users\Auth;
use CakeDC\Users\Auth\Social\Util\SocialUtils;
use CakeDC\Users\Controller\Component\UsersAuthComponent;
use CakeDC\Users\Exception\AccountNotActiveException;
use CakeDC\Users\Exception\MissingEmailException;
use CakeDC\Users\Exception\MissingProviderException;
use CakeDC\Users\Exception\UserNotActiveException;
use CakeDC\Users\Model\Table\SocialAccountsTable;
use Cake\Controller\ComponentRegistry;
use Cake\Core\Configure;
use Cake\Network\Request;
use Cake\ORM\TableRegistry;
use Muffin\OAuth2\Auth\OAuthAuthenticate;
/**
* Class SocialAuthenticate
*/
class SocialAuthenticate extends OAuthAuthenticate
{
/**
* Constructor
*
* @param \Cake\Controller\ComponentRegistry $registry The Component registry used on this request.
* @param array $config Array of config to use.
* @throws \Exception
*/
public function __construct(ComponentRegistry $registry, array $config = [])
{
$oauthConfig = Configure::read('OAuth');
unset($oauthConfig['providers']['twitter']);
Configure::write('Muffin/OAuth2', $oauthConfig);
parent::__construct($registry, array_merge($config, $oauthConfig));
}
/**
* Find or create local user
*
* @param array $data data
* @return array|bool|mixed
* @throws MissingEmailException
*/
protected function _touch(array $data)
{
try {
if (empty($data['provider']) && !empty($this->_provider)) {
$data['provider'] = SocialUtils::getProvider($this->_provider);
}
$user = $this->_socialLogin($data);
} catch (UserNotActiveException $ex) {
$exception = $ex;
} catch (AccountNotActiveException $ex) {
$exception = $ex;
} catch (MissingEmailException $ex) {
$exception = $ex;
}
if (!empty($exception)) {
$event = UsersAuthComponent::EVENT_FAILED_SOCIAL_LOGIN;
$args = ['exception' => $exception, 'rawData' => $data];
$event = $this->dispatchEvent($event, $args);
if ($exception instanceof MissingEmailException && $data['provider'] == SocialAccountsTable::PROVIDER_TWITTER) {
throw $exception;
}
return $event->result;
}
if (!empty($user->username)) {
$user = $this->_findUser($user->username);
}
return $user;
}
/**
* Get a user based on information in the request.
*
* @param \Cake\Network\Request $request Request object.
* @return mixed Either false or an array of user information
* @throws \RuntimeException If the `Muffin/OAuth2.newUser` event is missing or returns empty.
*/
public function getUser(Request $request)
{
$data = $request->session()->read(Configure::read('Users.Key.Session.social'));
$requestDataEmail = $request->data('email');
if (!empty($data) && (!empty($data['email']) || !empty($requestDataEmail))) {
if (!empty($requestDataEmail)) {
$data['email'] = $requestDataEmail;
}
$user = $data;
$request->session()->delete(Configure::read('Users.Key.Session.social'));
} else {
if (empty($data) && !$rawData = $this->_authenticate($request)) {
return false;
}
if (empty($rawData)) {
$rawData = $data;
}
$provider = $this->_getProviderName($request);
$user = $this->_mapUser($provider, $rawData);
}
if (!$user || !$this->config('userModel')) {
return false;
}
if (!$result = $this->_touch($user)) {
return false;
}
return $result;
}
/**
* Get the provider name based on the request or on the provider set.
*
* @param \Cake\Network\Request $request Request object.
* @return mixed Either false or an array of user information
*/
protected function _getProviderName($request = null)
{
$provider = false;
if (!is_null($this->_provider)) {
$provider = SocialUtils::getProvider($this->_provider);
} elseif (!empty($request)) {
$provider = ucfirst($request->param('provider'));
}
return $provider;
}
/**
* Get the provider name based on the request or on the provider set.
*
* @param string $provider Provider name.
* @param array $data User data
* @throws MissingProviderException
* @return mixed Either false or an array of user information
*/
protected function _mapUser($provider, $data)
{
if (empty($provider)) {
throw new MissingProviderException(__d('Users', "Provider cannot be empty"));
}
$providerMapperClass = "\\CakeDC\\Users\\Auth\\Social\\Mapper\\$provider";
$providerMapper = new $providerMapperClass($data);
$user = $providerMapper();
$user['provider'] = $provider;
return $user;
}
/**
* @param mixed $data data
* @return mixed
*/
protected function _socialLogin($data)
{
$options = [
'use_email' => Configure::read('Users.Email.required'),
'validate_email' => Configure::read('Users.Email.validate'),
'token_expiration' => Configure::read('Users.Token.expiration')
];
$userModel = Configure::read('Users.table');
$User = TableRegistry::get($userModel);
$user = $User->socialLogin($data, $options);
return $user;
}
}