-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOAuthCatcher_alt.php
More file actions
97 lines (90 loc) · 3.21 KB
/
OAuthCatcher_alt.php
File metadata and controls
97 lines (90 loc) · 3.21 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
<?php
/**
* INTER-Mediator
* Copyright (c) INTER-Mediator Directive Committee (http://inter-mediator.org)
* This project started at the end of 2009 by Masayuki Nii msyk@msyk.net.
*
* INTER-Mediator is supplied under MIT License.
* Please see the full license for details:
* https://github.com/INTER-Mediator/INTER-Mediator/blob/master/dist-docs/License.txt
*
* @copyright Copyright (c) INTER-Mediator Directive Committee (http://inter-mediator.org)
* @link https://inter-mediator.com/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
setlocale(LC_ALL, 'ja_JP', 'ja');
date_default_timezone_set('Asia/Tokyo');
// The variable pathToIM has to point the INTER-Mediator directory.
$pathToIM = "./vendor/inter-mediator/inter-mediator"; // Modify this to match your directories.
require_once("{$pathToIM}/INTER-Mediator.php"); // Loading INTER-Mediator and relevant libraries.
use INTERMediator\Auth\OAuthAuth;
use INTERMediator\DB\Logger;
use INTERMediator\DB\Proxy_ExtSupport;
use INTERMediator\IMUtil;
class AccountInfo
{
use Proxy_ExtSupport;
public function getMaxIDNumber(): int
{
$dataSource = [[
'name' => 'maxuserid',
'aggregation-select' => 'max(id) as maxuserid',
'aggregation-from' => 'authuser',]];
$reesult = $this->dbRead('maxuserid', null, null, $dataSource);
if (!isset($reesult[0]['maxuserid'])) {
return 0;
}
return $reesult[0]['maxuserid'];
}
public function getUsername(string $sub): ?string
{
$dataSource = [['name' => 'authuser', 'key' => 'id', 'records' => 1,]];
$reesult = $this->dbRead('authuser', ['sub' => $sub], null, $dataSource);
if (!isset($reesult[0]['username'])) {
return null;
}
return $reesult[0]['username'];
}
}
$authObj = new OAuthAuth($_GET["state"]);
//$authObj->debugMode = true; // or comment here
$authObj->setDoRedirect(false);
if (!$authObj->isActive) {
echo "Missing parameters for OAuth authentication. "
. ($_GET['error_description'] ?? "")
. $authObj->errorMessages();
exit;
}
$err = "No Error";
$jsCode = "";
if ($authObj->afterAuth()) { // Checking whether the authentication is successful.
$infoObj = new AccountInfo();
$username = $infoObj->getUsername($authObj->getUserInfo()['sub']);
if (is_null($username)) {
$username = "a" . (($infoObj->getMaxIDNumber() + 1) + 20000);
}
$newPassword = IMUtil::generatePassword(10);
$authObj->userInfoToLogin($username, $newPassword, true, true); // Set up user and automatic login.
if ($authObj->debugMode) {
$err = $authObj->errorMessages();
}
$param = $authObj->isCreate() ? "new" : "update";
$jsCode = "location.href = 'enroll.html?mode={$param}';";
} else {
$err = $authObj->errorMessages();
}
header("Content-Type: text/html; charset=UTF-8");
?>
<html>
<head>
<script type="text/javascript"><?php echo $jsCode; ?></script>
</head>
<body>
Provider: <?php echo $authObj->oAuthProvider(); ?><br>
Status: <?php echo $err; ?>
<hr/>
<p>This page must be redirected to another page.</p>
<hr>
<?php echo var_export(Logger::getInstance()->getDebugMessages(),true); ?>
</body>
</html>