|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Authentication & Authorization component class. |
|
5
|
|
|
* |
|
6
|
|
|
* Authenticate & Authorize the current user. |
|
7
|
|
|
* |
|
8
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
|
9
|
|
|
* @author Omar El Gabry <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
class AuthComponent extends Component{ |
|
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Default configurations |
|
16
|
|
|
* |
|
17
|
|
|
* @var array |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $config = [ |
|
20
|
|
|
'authenticate' => [], |
|
21
|
|
|
'authorize' => [] |
|
22
|
|
|
]; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Auth startup |
|
26
|
|
|
* All authentication and authorization checking are done in this method |
|
27
|
|
|
* |
|
28
|
|
|
*/ |
|
29
|
|
|
public function startup(){ |
|
30
|
|
|
|
|
31
|
|
|
// authenticate |
|
32
|
|
|
if(!empty($this->config["authenticate"])){ |
|
33
|
|
|
if(!$this->authenticate()){ |
|
34
|
|
|
return $this->unauthenticated(); |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
// authorize |
|
39
|
|
|
if(!empty($this->config["authorize"])){ |
|
40
|
|
|
if(!$this->authorize()){ |
|
41
|
|
|
return $this->unauthorized(); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Handles unauthenticated access attempt. |
|
48
|
|
|
* |
|
49
|
|
|
*/ |
|
50
|
|
|
public function unauthenticated(){ |
|
51
|
|
|
|
|
52
|
|
|
$this->controller->login->logOut(Session::getUserId()); |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
if($this->request->isAjax()) { |
|
55
|
|
|
return $this->controller->error(401); |
|
56
|
|
|
}else{ |
|
57
|
|
|
$redirect = $this->controller->request->isGet()? $this->controller->request->uri(): ""; |
|
58
|
|
|
return $this->controller->redirector->login($redirect); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Handles unauthorized access attempt. |
|
64
|
|
|
* |
|
65
|
|
|
*/ |
|
66
|
|
|
public function unauthorized(){ |
|
67
|
|
|
return $this->controller->error(403); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* authenticate the user using the defined methods in $config |
|
72
|
|
|
* |
|
73
|
|
|
* @return boolean |
|
74
|
|
|
*/ |
|
75
|
|
|
public function authenticate(){ |
|
76
|
|
|
return $this->check($this->config["authenticate"], "authenticate"); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* authorize the user using the defined methods in $config |
|
81
|
|
|
* |
|
82
|
|
|
* @return boolean |
|
83
|
|
|
*/ |
|
84
|
|
|
public function authorize(){ |
|
85
|
|
|
return $this->check($this->config["authorize"], "authorize"); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* check for authentication or authorization |
|
90
|
|
|
* |
|
91
|
|
|
* @param array $config |
|
92
|
|
|
* @param string $type |
|
93
|
|
|
* @throws Exception if $config is empty or method doesn't exists |
|
94
|
|
|
* @return boolean |
|
95
|
|
|
*/ |
|
96
|
|
|
private function check($config, $type){ |
|
97
|
|
|
|
|
98
|
|
|
if (empty($config)) { |
|
99
|
|
|
throw new Exception($type . ' methods arent initialized yet in config'); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$auth = Utility::normalize($config); |
|
103
|
|
|
|
|
104
|
|
|
foreach($auth as $method => $config){ |
|
105
|
|
|
|
|
106
|
|
|
$method = "_" . ucfirst($method) . ucfirst($type); |
|
107
|
|
|
|
|
108
|
|
|
if (!method_exists(__CLASS__, $method)) { |
|
109
|
|
|
throw new Exception('Auth Method doesnt exists: ' . $method); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
if($this->{$method}($config) === false){ |
|
113
|
|
|
return false; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
return true; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Is user is already logged in via session or cookie? |
|
121
|
|
|
* |
|
122
|
|
|
* @return boolean |
|
123
|
|
|
*/ |
|
124
|
|
|
public function isLoggedIn(){ |
|
125
|
|
|
|
|
126
|
|
|
if(Session::getIsLoggedIn() === true){ |
|
127
|
|
|
return true; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
if(Cookie::isCookieValid()){ |
|
131
|
|
|
return true; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return false; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Is user authorized for the requested Controller & Action method? |
|
139
|
|
|
* |
|
140
|
|
|
* @param array $config configuration data |
|
141
|
|
|
* @throws Exception if isAuthorized method doesn't exists in the controller class |
|
142
|
|
|
* @return boolean |
|
143
|
|
|
*/ |
|
144
|
|
|
private function _ControllerAuthorize($config){ |
|
|
|
|
|
|
145
|
|
|
|
|
146
|
|
|
if (!method_exists($this->controller, 'isAuthorized')) { |
|
147
|
|
|
throw new Exception(sprintf('%s does not implement an isAuthorized() method.', get_class($this->controller))); |
|
148
|
|
|
} |
|
149
|
|
|
return (bool)$this->controller->isAuthorized(); |
|
|
|
|
|
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Is user authenticated? |
|
154
|
|
|
* It checks for: |
|
155
|
|
|
* - concurrent session |
|
156
|
|
|
* - user credentials in session & cookies |
|
157
|
|
|
* - cookies theft and manipulations |
|
158
|
|
|
* - session Hijacking and fixation. |
|
159
|
|
|
* |
|
160
|
|
|
* @param array $config configuration data |
|
161
|
|
|
* @return boolean |
|
162
|
|
|
*/ |
|
163
|
|
|
private function _UserAuthenticate($config){ |
|
|
|
|
|
|
164
|
|
|
|
|
165
|
|
|
if($this->concurentSession()){ |
|
166
|
|
|
return false; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
if(!$this->loggedIn()){ |
|
170
|
|
|
return false; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
return true; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Checks if user is logged in or not. |
|
178
|
|
|
* It uses Session and Cookies to validate the current user. |
|
179
|
|
|
* |
|
180
|
|
|
* @access public |
|
181
|
|
|
* @static static method |
|
182
|
|
|
* @return boolean |
|
183
|
|
|
* |
|
184
|
|
|
*/ |
|
185
|
|
|
private function loggedIn(){ |
|
186
|
|
|
|
|
187
|
|
|
if (Session::isSessionValid($this->request->clientIp(), $this->request->userAgent())) { |
|
188
|
|
|
return true; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
if (Cookie::isCookieValid()) { |
|
192
|
|
|
|
|
193
|
|
|
// get role from user class, because cookies don't store roles |
|
194
|
|
|
$role = $this->controller->user->getProfileInfo(Cookie::getUserId())["role"]; |
|
|
|
|
|
|
195
|
|
|
Session::reset(["user_id" => Cookie::getUserId(), "role" => $role, "ip" => $this->request->clientIp(), "user_agent" => $this->request->userAgent()]); |
|
196
|
|
|
|
|
197
|
|
|
// reset cookie, Cookie token is usable only once |
|
198
|
|
|
Cookie::reset(Session::getUserId()); |
|
199
|
|
|
|
|
200
|
|
|
return true; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
return false; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
private function concurentSession(){ |
|
207
|
|
|
return Session::isConcurrentSessionExists(); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
} |
|
211
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.