-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImbaAuth.php
430 lines (392 loc) · 16.4 KB
/
ImbaAuth.php
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
<?php
header('Access-Control-Allow-Origin: *');
/**
* start the php session
*/
session_set_cookie_params(3600);
session_start();
/**
*
$lifetime = 600;
session_start();
setcookie(session_name(), session_id(), time() + $lifetime);
*/
/**
* Load dependencies
*/
require_once "ImbaConstants.php";
require_once 'Controller/ImbaManagerLog.php';
require_once 'Controller/ImbaManagerOpenID.php';
require_once 'Controller/ImbaManagerOauth.php';
require_once 'Controller/ImbaManagerUser.php';
require_once 'Controller/ImbaManagerAuthRequest.php';
require_once 'Controller/ImbaUserContext.php';
require_once 'Controller/ImbaSharedFunctions.php';
require_once 'Model/ImbaUser.php';
require_once 'Model/ImbaUserRole.php';
/**
* Load Auth Managers
* - OpenID
* - OAuth
*/
//$tmpPath = getcwd();
$managerOpenId = new ImbaManagerOpenID();
//$managerOauth = new ImbaManagerOauth();
/* chdir("Libs/");
//require_once "Oauth.php";
require_once "Zend/Oauth/Consumer.php";
chdir($tmpPath); */
/**
* Manager for users
*/
$managerUser = ImbaManagerUser::getInstance();
/**
* Manager for AuthRequests
*/
$managerAuthRequest = ImbaManagerAuthRequest::getInstance();
/**
* private function to write to logs
*/
function writeAuthLog($message, $level = 3) {
/**
* Load the logger
*/
$managerLog = ImbaManagerLog::getInstance();
$log = $managerLog->getNew();
$log->setModule("ImbaAuth");
$log->setMessage($message);
$log->setLevel($level);
$managerLog->insert($log);
ImbaUserContext::setImbaErrorMessage($message);
return $message;
}
/**
* Kill our session and go to URL, function
*/
function killAndRedirect($targetUrl) {
setcookie(session_id(), "", time() - 3600);
session_destroy();
session_write_close();
header("Location: " . $targetUrl);
}
/**
* Redirect with domain magic
*/
function redirectTo($line, $url, $message = "") {
$myDomain = ImbaSharedFunctions::getDomain($url);
/**
* Discover if we need to do the html redirect and make it so
*/
//if (ImbaSharedFunctions::getDomain($_SERVER['HTTP_REFERER']) != ImbaSharedFunctions::getDomain($url)) {
if (headers_sent()) {
$smarty = ImbaSharedFunctions::newSmarty();
$smarty->assign("redirectUrl", $url);
$smarty->assign("redirectDomain", $myDomain);
$smarty->assign("internalCode", $line);
$smarty->assign("internalMessage", $message);
$smarty->assign("phpsession", session_id());
$smarty->display("ImbaAuthRedirect.tpl");
exit;
} else {
header("Location: " . $url);
exit;
}
}
/**
* OpenID auth logic
*/
if ($_GET["logout"] == true || $_POST["logout"] == true) {
/**
* we want to log out
*/
writeAuthLog("Logging out (Redirecting)", 2);
if (empty($_POST['imbaSsoOpenIdLogoutReferer'])) {
$targetUrl = ImbaSharedFunctions::getTrustRoot();
} else {
$targetUrl = $_POST['imbaSsoOpenIdLogoutReferer'];
}
killAndRedirect($targetUrl);
exit;
} elseif (!ImbaUserContext::getLoggedIn()) {
/**
* we are NOT logged in
*/
if (empty($_SESSION["IUC_WaitingForVerify"])) {
/**
* Save our referer to session if there is none safed till now
*/
if ($_POST['imbaSsoOpenIdLoginReferer'] != "") {
ImbaUserContext::setRedirectUrl($_POST['imbaSsoOpenIdLoginReferer']);
} else {
if ($_SESSION["IUC_redirectUrl"] == "") {
ImbaUserContext::setRedirectUrl($_SERVER['HTTP_REFERER']);
}
}
/**
* Determine Authentication method (we also don't have to be verified)
*/
if (!(empty($_POST['openid']) && (empty($_GET['openid'])))) {
/**
* OpenID Authentification
*/
$authMethod = "openid";
} else {
/**
* Send the User to the registration page
*/
if (empty($_SERVER['HTTP_REFERER'])) {
$tmpUrl = ImbaSharedFunctions::getTrustRoot();
} else {
$tmpUrl = $_SERVER['HTTP_REFERER'];
}
$tmpMsg = writeAuthLog("Authentificationmethod not found");
/* header("Location: " . $_SERVER['HTTP_REFERER']); */
redirectTo(__LINE__, $tmpUrl, $tmpMsg);
exit;
}
/**
* Do the Authentication
*/
switch ($authMethod) {
/**
* Determine the authentification method
*/
case "openid":
if (empty($_POST["openid"]) && (!empty($_GET["openid"]))) {
$_POST["openid"] = $_GET["openid"];
}
if (!empty($_POST["openid"])) {
$_POST["openid"] = trim($_POST["openid"]);
$redirectUrl = null;
/**
* Get all users
*/
$allUsers = $managerUser->selectAllUser();
/**
* Check if this is a openid (which looks like a URL) or possibly the nickname of the user
*/
if (ImbaSharedFunctions::isValidURL($_POST["openid"])) {
/**
* This is a possible openid
*/
$openid = $_POST["openid"];
} else {
/**
* Try to lookup the nickname
*/
$securityCounter = 0;
$tmpOpenid = null;
foreach ($allUsers as $user) {
if (strtolower($user->getNickname()) == strtolower($_POST["openid"])) {
$securityCounter++;
$tmpOpenid = $user->getOpenId();
}
}
if (($securityCounter == 1) && (!empty($tmpOpenid))) {
$openid = $tmpOpenid;
} else {
throw new Exception(ImbaConstants::$ERROR_OPENID_Auth_OpenID_INVALID_URI);
}
}
/**
* Discovering our user
*/
$myUser = $managerUser->selectByOpenId($openid);
/**
* Saving our authrequest into the database
*/
$authRequest = $managerAuthRequest->getNew();
$authRequest->setUserId($myUser->getId());
$authRequest->setHash(ImbaSharedFunctions::getRandomString());
$authRequest->setRealm(ImbaSharedFunctions::getTrustRoot());
$authRequest->setReturnTo(ImbaSharedFunctions::getReturnTo($authRequest->getHash()));
$authRequest->setType($authMethod);
$authRequest->setDomain($_POST['imbaSsoOpenIdLoginReferer']);
$authRequest->setPhpsession(session_id());
$authRequest->setIp(ImbaSharedFunctions::getIP());
$managerAuthRequest->insert($authRequest);
/**
* try to do the first step of the openid authentication steps
*/
writeAuthLog("Determing Auth style for " . $openid);
try {
$redirectUrl = $managerOpenId->openidAuth($openid, $authRequest->getHash(), $authRequest->getRealm(), $authRequest->getReturnTo());
if (!empty($redirectUrl)) {
/**
* we got a redirection url as answer. go there now!
*/
$tmpMsg = writeAuthLog("OpenIdAuth redirecting to: " . ImbaSharedFunctions::getDomain($redirectUrl));
/**
* If this is set, the user will be sent to verification next time
*/
//ImbaUserContext::setWaitingForVerify(ImbaSharedFunctions::getReturnTo());
ImbaUserContext::setWaitingForVerify(ImbaUserContext::getRedirectUrl());
redirectTo(__LINE__, $redirectUrl, $tmpMsg);
exit;
} else {
/**
* something went wrong. display error end exit
*/
$tmpMsg = writeAuthLog("Special Error: Ehhrmm keine URL, weil ehhrmm", 0);
//header("Location: " . ImbaUserContext::getRedirectUrl());
redirectTo(__LINE__, ImbaUserContext::getRedirectUrl(), $tmpMsg);
exit;
}
} catch (Exception $ex) {
$tmpMsg = writeAuthLog("Authentification ERROR: " . $ex->getMessage() . " (" . $openid . ")", 1);
//header("Location: " . ImbaUserContext::getRedirectUrl());
redirectTo(__LINE__, ImbaUserContext::getRedirectUrl(), $tmpMsg);
exit;
}
} else {
$tmpMsg = writeAuthLog("No OpenId submitted", 2);
//header("Location: " . ImbaUserContext::getRedirectUrl());
redirectTo(__LINE__, ImbaUserContext::getRedirectUrl(), $tmpMsg);
exit;
}
break;
/**
* Default auth type
*/
default:
$tmpMsg = writeAuthLog("No Authtype submitted");
true;
}
/* header("Location: " . $_SERVER['HTTP_REFERER']); */
redirectTo(__LINE__, $_SERVER['HTTP_REFERER'], $tmpMsg);
exit;
} else {
/**
* first step completed. do the verification and actual login
* we shall go to the saved realm in the database after
* we are finished here.
*/
/**
* Convert imbaHash from possible GET and POST to local var (proxy...)
*/
if (!empty($_GET['imbaHash'])) {
$imbaHash = $_GET['imbaHash'];
unset($_GET['imbaHash']);
} else if (!empty($_POST['imbaHash'])) {
$imbaHash = $_POST['imbaHash'];
unset($_POST['imbaHash']);
} else {
/**
* We have no imbaHash, this is not good! kill yourself and go back where you came from
*/
$tmpMsg = writeAuthLog("Morpheus, help! Forwarding to: " . ImbaSharedFunctions::getTrustRoot());
/* header("Location: " . $_SERVER['HTTP_REFERER']); */
ImbaUserContext::setWaitingForVerify("");
redirectTo(__LINE__, ImbaSharedFunctions::getTrustRoot(), $tmpMsg);
exit;
//throw new Exception("There was an error in descovering your auth request! Please reload the website.");
}
/**
* Get the stored data for the current authrequest from the database
*/
$authRequest = $managerAuthRequest->select($imbaHash);
writeAuthLog("Verification starting", 2);
if (strpos($authRequest->getReturnTo(), "&imbaHash")) {
$authRequest->setReturnTo(substr($authRequest->getReturnTo(), 0, strpos($authRequest->getReturnTo(), "&imbaHash")));
}
//if (substr(0, strpos($authRequest->getReturnTo(), "&imbaHash"))) {
// $authRequest->setReturnTo(str_replace("&imbaHash=" . $authRequest->getHash(), "", $authRequest->getReturnTo()));
//}
try {
$esc_identity = $managerOpenId->openidVerify($authRequest->getRealm(), $authRequest->getReturnTo());
if (empty($esc_identity)) {
throw new Exception("OpenIdVerify failed! No Openid recieved from the OpenId Manager.");
}
writeAuthLog("OpenID Verification sucessful", 2);
$currentUser = $managerUser->selectByOpenId($esc_identity);
/**
* Check the status of the user
*/
if (empty($currentUser)) {
/**
* This is a new user. let him register
*/
writeAuthLog("Registering new user", 2);
if (!empty($esc_identity)) {
ImbaUserContext::setNeedToRegister(true);
ImbaUserContext::setOpenIdUrl($esc_identity);
}
} elseif ($currentUser->getRole() == 0) {
/**
* this user is banned
*/
writeAuthLog($currentUser->getName() . " is banned but tried to login", 1);
throw new Exception("You are Banned!");
} elseif ($currentUser->getRole() != null) {
/**
* this user is allowed to log in
*/
$tmpMsg = writeAuthLog($currentUser->getNickname() . " logged in", 1);
ImbaUserContext::setLoggedIn(true);
ImbaUserContext::setOpenIdUrl($esc_identity);
ImbaUserContext::setUserRole($currentUser->getRole());
ImbaUserContext::setUserId($currentUser->getId());
setcookie("ImbaSsoLastLoginName", "", (time() - 3600));
setcookie("ImbaSsoLastLoginName", $currentUser->getNickname(), (time() + (60 * 60 * 24 * 30)));
$managerUser->setMeOnline();
ImbaUserContext::setImbaErrorMessage("Du bist angemeldet als " . $currentUser->getNickname());
}
$myDomain = $authRequest->getDomain();
if (!empty($myDomain)) {
/* header("Location: " . $myDomain); */
$managerAuthRequest->delete($imbaHash);
redirectTo(__LINE__, $myDomain, $tmpMsg);
exit;
} else {
$tmpUrl = ImbaUserContext::getWaitingForVerify();
ImbaUserContext::setWaitingForVerify("");
/* header("Location: " . $tmpUrl); */
redirectTo(__LINE__, $tmpUrl, $tmpMsg);
exit;
}
} catch (Exception $ex) {
$esc_identity = $managerOpenId->getOpenId();
$tmpUrl = ImbaUserContext::getWaitingForVerify();
ImbaUserContext::setWaitingForVerify("");
if ($ex->getMessage() == "id_res_not_set") {
$tmpMsg = writeAuthLog("Aktuelle OpenID Anfrage ausgelaufen. Bitte nocheinmal von neuen probieren. (Hash: " . $imbaHash . ")");
} else {
$tmpMsg = writeAuthLog("Unnamed OpenID Verification ERROR (Hash: " . $imbaHash . "): " . $ex->getMessage(), 1);
}
$myDomain = $authRequest->getDomain();
if (!empty($myDomain)) {
/* header("Location: " . $authRequest->getDomain()); */
$managerAuthRequest->delete($imbaHash);
redirectTo(__LINE__, $myDomain, $tmpMsg);
exit;
} else {
$tmpUrl = ImbaUserContext::getWaitingForVerify();
ImbaUserContext::setWaitingForVerify("");
$managerAuthRequest->delete($imbaHash);
/* header("Location: " . $tmpUrl); */
redirectTo(__LINE__, $tmpUrl, $tmpMsg);
exit;
}
}
}
echo "we shall not get here ...";
exit;
} else {
ImbaUserContext::setWaitingForVerify("");
/**
* we are logged in! everithing is ok, we have a running session
* and we have a party here
* - set cookie with logged in openid for autofill login box
* - redirect back to page
*/
writeAuthLog("Already logged in with: " . ImbaUserContext::getOpenIdUrl() . ")", 1);
$tmpUrl = ImbaUserContext::getWaitingForVerify();
$tmpMsg = ImbaUserContext::setWaitingForVerify("");
/* header("Location: " . $tmpUrl); */
redirectTo(__LINE__, $tmpUrl, $tmpMsg);
exit;
}
/* header("Location: " . ImbaUserContext::getRedirectUrl()); */
redirectTo(__LINE__, ImbaUserContext::getRedirectUrl(), "We should never have gone so far...");
exit;
?>