-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelper.php
303 lines (263 loc) · 10.1 KB
/
Helper.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
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\RebelOIDC;
use Piwik\Plugins\UsersManager\Model;
use Piwik\Common;
use Piwik\Piwik;
use Piwik\Db;
use Piwik\Nonce;
use Exception;
trait Helper
{
/**
* Generate cryptographically secure random string.
*
* @param int $length
* @return string
*/
private function generateKey(int $length = 64): string
{
// thanks ccbsschucko at gmail dot com
// http://docs.php.net/manual/pl/function.random-bytes.php#122766
$length = ($length < 4) ? 4 : $length;
return bin2hex(random_bytes(($length - ($length % 2)) / 2));
}
/**
* Check whether the given user has superuser access.
* The function in Piwik\Core cannot be used because it requires an admin user being signed in.
* It was used as a template for this function.
* See: {@link \Piwik\Core::hasTheUserSuperUserAccess($theUser)} method.
* See: {@link \Piwik\Plugins\UsersManager\Model::getUsersHavingSuperUserAccess()} method.
*
* @param string $theUser A username to be checked for superuser access
* @return bool
*/
private function hasTheUserSuperUserAccess(string $theUser)
{
$userModel = new Model();
$superUsers = $userModel->getUsersHavingSuperUserAccess();
foreach ($superUsers as $superUser) {
if ($theUser === $superUser['login']) {
return true;
}
}
return false;
}
/**
* Create a link between the remote user and the currently signed in user.
*
* @param string $providerUserId
* @param string $matomoUserLogin Override the local user if non-null
* @return void
*/
private function linkAccount(string $providerUserId, string $matomoUserLogin = null)
{
if ($matomoUserLogin === null) {
$matomoUserLogin = Piwik::getCurrentUserLogin();
}
$sql = "INSERT INTO " . Common::prefixTable("rebeloidc_provider") . " (user, provider_user, provider, date_connected) VALUES (?, ?, ?, ?)";
$bind = [$matomoUserLogin, $providerUserId, "oidc", date("Y-m-d H:i:s")];
Db::query($sql, $bind);
}
/**
* Remove link between the currently signed user and the remote user.
*
* @return void
*/
public function unlink()
{
if ($_SERVER["REQUEST_METHOD"] !== "POST") {
throw new Exception(Piwik::translate("RebelOIDC_MethodNotAllowed"));
}
// csrf protection
Nonce::checkNonce("RebelOIDC.nonce", $_POST["form_nonce"]);
$sql = "DELETE FROM " . Common::prefixTable("rebeloidc_provider") . " WHERE user=? AND provider=?";
$bind = array(Piwik::getCurrentUserLogin(), "oidc");
Db::query($sql, $bind);
$this->redirectToIndex("UsersManager", "userSecurity");
}
/**
* Fetch user from database given the provider and remote user id.
*
* @param string $provider
* @param string $remoteId
* @return array
*/
private function getUserByRemoteId($provider, $remoteId)
{
$sql = "SELECT user FROM " . Common::prefixTable("rebeloidc_provider") . " WHERE provider=? AND provider_user=?";
$result = Db::fetchRow($sql, array($provider, $remoteId));
if (empty($result)) {
return $result;
} else {
$userModel = new Model();
return $userModel->getUser($result["user"]);
}
}
/**
* Determine if all the required settings have been setup.
*
* @param SystemSettings $settings
* @return bool
*/
private function isPluginSetup($settings): bool
{
return !empty($settings->authorizeUrl->getValue())
&& !empty($settings->tokenUrl->getValue())
&& !empty($settings->userInfoUrl->getValue())
&& !empty($settings->clientId->getValue())
&& !empty($settings->clientSecret->getValue());
}
/**
* Fetch provider information for the currently signed in user.
*
* @param string $provider
* @return array
*/
private function getProviderUser($provider)
{
$sql = "SELECT user, provider_user, provider FROM " . Common::prefixTable("rebeloidc_provider") . " WHERE provider=? AND user=?";
return Db::fetchRow($sql, array($provider, Piwik::getCurrentUserLogin()));
}
/**
* Fetch an access token from Keycloak using the client credentials.
*
* @param string $baseUrl Base URL of the Keycloak server.
* @param string $realm Keycloak realm.
* @param string $clientId Client ID for the credentials.
* @param string $clientSecret Client secret for the credentials.
* @return string Access token.
* @throws Exception
*/
private function getAccessToken(string $baseUrl, string $realm, string $clientId, string $clientSecret): string
{
$tokenUrl = $baseUrl . "/realms/" . $realm . "/protocol/openid-connect/token";
$ch = curl_init($tokenUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
'grant_type' => 'client_credentials',
'client_id' => $clientId,
'client_secret' => $clientSecret,
]));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/x-www-form-urlencoded',
]);
$response = curl_exec($ch);
if (curl_errno($ch)) {
throw new Exception('Error getting access token: ' . curl_error($ch));
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode >= 400) {
throw new Exception("Failed to retrieve token. HTTP Code: $httpCode. Response: $response");
}
$tokenData = json_decode($response, true);
curl_close($ch);
if (!isset($tokenData['access_token'])) {
throw new Exception('Access token not found in Keycloak response');
}
return $tokenData['access_token'];
}
/**
* Fetch users from the Keycloak Admin API.
*
* @param string $baseUrl Base URL of the Keycloak server.
* @param string $realm Keycloak realm.
* @param string $token Access token for the API.
* @return array List of users.
* @throws Exception
*/
private function fetchUsers(string $baseUrl, string $realm, string $token): array
{
$usersUrl = $baseUrl . "/admin/realms/" . $realm . "/users";
$ch = curl_init($usersUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $token,
'Content-Type: application/json',
]);
$response = curl_exec($ch);
if (curl_errno($ch)) {
throw new Exception('Error retrieving users: ' . curl_error($ch));
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode >= 400) {
throw new Exception("Failed to fetch users. HTTP Code: $httpCode. Response: $response");
}
$users = json_decode($response, true);
return is_array($users) ? $users : [];
}
/**
* Fetch realm-level roles for a specific user from Keycloak.
*
* @param string $baseUrl Base URL of the Keycloak server.
* @param string $realm Keycloak realm.
* @param string $userId User ID in Keycloak.
* @param string $token Access token for the API.
* @return array Roles assigned to the user at the realm level.
* @throws Exception
*/
private function fetchRealmRoles(string $baseUrl, string $realm, string $userId, string $token): array
{
// Use the /role-mappings/realm endpoint
$rolesUrl = $baseUrl . "/admin/realms/" . $realm . "/users/" . $userId . "/role-mappings/realm";
$ch = curl_init($rolesUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $token,
'Content-Type: application/json',
]);
$response = curl_exec($ch);
if (curl_errno($ch)) {
throw new Exception('Error retrieving realm roles for user ' . $userId . ': ' . curl_error($ch));
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode === 404 || empty($response)) {
return []; // No roles assigned
} elseif ($httpCode >= 400) {
throw new Exception("Failed to fetch realm roles for user $userId. HTTP Code: $httpCode. Response: $response");
}
// Decode the JSON response
$rolesData = json_decode($response, true);
if (!is_array($rolesData)) {
return [];
}
// Extract the role names
$roles = [];
foreach ($rolesData as $role) {
if (isset($role['name'])) {
$roles[] = $role['name']; // Role name
}
}
return $roles; // Return the list of realm roles
}
/**
* Fetch all users from Keycloak along with their realm roles.
*
* @param string $baseUrl Base URL of the Keycloak server.
* @param string $realm Keycloak realm.
* @param string $clientId Client ID for the API (not used for roles).
* @param string $clientSecret Client secret for the API.
* @return array List of users with their realm roles.
* @throws Exception
*/
private function getUsers(string $baseUrl, string $realm, string $clientId, string $clientSecret): array
{
$token = $this->getAccessToken($baseUrl, $realm, $clientId, $clientSecret);
$users = $this->fetchUsers($baseUrl, $realm, $token);
foreach ($users as &$user) {
$userId = $user['id'];
$roles = $this->fetchRealmRoles($baseUrl, $realm, $userId, $token);
$user['roles'] = $roles; // Append roles directly to the user array
}
return $users;
}
}