-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
146 lines (122 loc) · 4.67 KB
/
index.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
<?php
session_start();
require_once('vendor/autoload.php');
$oidcConfig = [
'mainUrl' => '<URL-TO-UCP>',
'issuer' => '<OPENID-ISSUER>',
'cid' => '<OPENID-CLIENT-ID>',
'secret' => '<OPENID-CLIENT-SECRET>',
'ucpUserAttr' => '<OPENID-UCP-USER-ATTR>',
// Optional settings
//'additionalScopes' => [],
//'customRedirectUrl' => '<OPENID-CUSTOM-REDIRECT-URL>>'
];
class UcpOpenIdWrapper
{
private bool|mysqli $conn;
private ?Jumbojett\OpenIDConnectClient $oidc;
public function __construct(private readonly array $config)
{
$configFile = __DIR__ . '/freepbx-cfg-clone.php';
if (!file_exists($configFile)) {
$orgConfigFile = file_get_contents('/etc/freepbx.conf');
$lines = explode("\n", $orgConfigFile);
$lines[count($lines) - 2] = 'return $amp_conf;';
$newString = implode("\n", $lines);
$newString = str_replace('$amp_conf', '$db_conf', $newString);
file_put_contents($configFile, $newString);
}
}
public function auth(): bool
{
if (isset($_SESSION['QUERY_STRING']) && $_SESSION['QUERY_STRING']) {
return true;
}
if (!array_key_exists('UCP_token', $_SESSION)) {
$this->initOidc();
$this->oidc->authenticate();
$_SESSION['oidc_id_token'] = $this->oidc->getIdToken();
$ucpUid = $this->oidc->requestUserInfo($this->config['ucpUserAttr']);
if (!$ucpUid) {
echo "No user found for uid";
die();
}
$this->connectDb();
$testToken = $this->generateToken();
$this->saveSessionToken($testToken, $ucpUid);
//$this->saveSessionToken($testToken, "8");
$this->setSessionToken($testToken);
$this->conn->close();
header("Location: {$this->getMainUrl()}");
return false;
}
// if UCP_token is set, but empty, show logout success page an remove token to make reauth possible
if (!isset($_SESSION['UCP_token']) && array_key_exists('UCP_token', $_SESSION)) {
unset($_SESSION['UCP_token']);
try {
$this->initOidc();
$this->oidc->signOut($_SESSION['oidc_id_token'], null);
} catch (Exception $e) {
echo "ok fehler";
}
return false;
}
return true;
}
public function getMainUrl(): string
{
return $this->config['mainUrl'];
}
private function initOidc(): void
{
if (!isset($this->config['issuer']) || !isset($this->config['cid']) || !isset($this->config['secret'])) {
throw new Exception('invalid config');
}
$this->oidc = new Jumbojett\OpenIDConnectClient($this->config['issuer'], $this->config['cid'], $this->config['secret']);
if (isset($this->config['additionalScopes']) && is_array($this->config['additionalScopes'])) {
$this->oidc->addScope($this->config['additionalScopes']);
}
if (isset($this->config['customRedirectUrl'])) {
$this->oidc->setRedirectURL($this->config['customRedirectUrl']);
}
}
private function connectDb(): void
{
$dbConfig = require('freepbx-cfg-clone.php');
$dbHost = $dbConfig['AMPDBHOST'];
$dbUsername = $dbConfig['AMPDBUSER'];
$dbPassword = $dbConfig['AMPDBPASS'];
$dbName = $dbConfig['AMPDBNAME'];
$this->conn = new mysqli($dbHost, $dbUsername, $dbPassword);
if ($this->conn->connect_error) {
die("Connection failed: " . $this->conn->connect_error);
}
if (!$this->conn->select_db($dbName)) {
die("DB select failed");
}
}
private function generateToken(): string
{
return bin2hex(openssl_random_pseudo_bytes(16));
}
private function saveSessionToken($token, $uid): void
{
$sql = "INSERT INTO ucp_sessions (session, uid, address, time) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE time = VALUES(time)";
$stmt = $this->conn->prepare($sql);
$stmt->execute(array($token, $uid, $_SERVER['REMOTE_ADDR'], time()));
}
private function setSessionToken($token): void
{
$_SESSION['UCP_token'] = $token;
}
}
$openIdWrapper = new UcpOpenIdWrapper($oidcConfig);
if ($openIdWrapper->auth()) {
include('../admin/modules/ucp/htdocs/index.php');
$currentUser = $GLOBALS['user'];
if (!$currentUser) {
unset($_SESSION['UCP_token']);
echo "Session expired, refreshing from OIDC session...<meta http-equiv='refresh' content='2; URL={$openIdWrapper->getMainUrl()}'>";
return;
}
}