-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoauthlink.php
379 lines (303 loc) · 12.4 KB
/
oauthlink.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
<?php
/**
* Copyright (C) 2023 rglss
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
// Restrict Direct Access
defined('_JEXEC') or die('Restricted access');
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\Log\Log;
use Joomla\CMS\Factory;
use Joomla\CMS\User\User;
use Joomla\CMS\User\UserHelper;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Session\Session;
use Joomla\CMS\Authentication\Authentication;
use Joomla\CMS\Authentication\AuthenticationResponse;
require_once JPATH_PLUGINS . DIRECTORY_SEPARATOR . 'system' . DIRECTORY_SEPARATOR . 'oauthlink' . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
JModelLegacy::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_fields/models');
/**
*
*/
class plgSystemOAuthLink extends JPlugin
{
/**
*
*/
public function onAfterInitialise()
{
$app = Factory::getApplication();
$session = $app->getSession();
$clientType = $this->params->get('oauthlink_client_provider_type');
$clientID = $this->params->get('client_id');
$clientSecret = $this->params->get('client_secret');
// TODO: Check plugin config
if (empty($clientType) || empty($clientID) || empty($clientSecret)) {
// throw new Exception("OAuthLink Not Configured");
}
$provider = new TheNetworg\OAuth2\Client\Provider\Azure([
'clientId' => $clientID,
'clientSecret' => $clientSecret,
'redirectUri' => Uri::root(),
'scopes' => ['openid'],
'defaultEndPointVersion' => '2.0'
]);
// Set the Azure tenant if defined
$azureTenantID = $this->params->get('tenant_id');
if (!empty($azureTenantID)) {
$provider->tenant = $azureTenantID;
}
$baseGraphUri = $provider->getRootMicrosoftGraphUri(null);
$provider->scope = ['openid', 'profile', 'email', 'offline_access', $baseGraphUri . '/User.Read'];
/**
* Auth Flow - Query Param ?oauthlink=login
*/
if (isset($_GET['oauthlink']) and $_GET['oauthlink'] == 'login') {
// JLog::add($clientType, JLog::ERROR, 'jerror');
$authorizationUrl = $provider->getAuthorizationUrl();
// Get the state generated for you and store it to the session.
$_SESSION['oauthlink.state'] = $provider->getState();
// Redirect the user to the authorization URL.
header('Location: ' . $authorizationUrl);
exit;
}
/**
* Callback Flow
*/
if (isset($_GET['code']) && isset($_SESSION['oauthlink.state']) && isset($_GET['state'])) {
// Extract vars
$code = $_GET['code'];
$state = $_GET['state'];
// CSRF Check
if ($state !== $_SESSION['oauthlink.state']) {
exit("Error: Invalid OAuth Session");
}
// Remove the session var
unset($_SESSION['oauthlink.state']);
// Try to get an access token (using the authorization code grant)
$token = $provider->getAccessToken('authorization_code', [
'scope' => $provider->scope,
'code' => $code,
]);
if (!isset($token)) {
$message = "Authentication Failed. Please contact an administrator.";
$app->enqueueMessage($message, 'error');
$app->redirect("/");
}
// Fetch the resource ownwer from the token
$resourceOwner = $provider->getResourceOwner($token);
// Lookup the user
$user = $this->findUser($resourceOwner);
if ($user == null) { //No user found
// Should we create a user account?
if ($this->params->get('oauthlink_autocreate_user')) {
$newUser = $this->createUserAccount($resourceOwner);
if (!$newUser) {
$message = "Error creating user account. Please contact an administrator.";
$app->enqueueMessage($message, 'error');
$app->redirect("/");
}
// We now have a user!
$user = $newUser;
} else {
$message = "You have logged in successfully, but don't have a user account for this site. Please contact an administrator.";
$app->enqueueMessage($message, 'error');
$app->redirect("/");
}
}
// Login
$this->authenticateUser($user, $session, $app);
// Syncs have to be run AFTER the user is logged in.
// Sync Groups (if enabled)
if ($this->params->get('oauthlink_updategroups')) {
$this->syncUserGroups($user, $provider, $token);
}
// Sync the user attributes (if enabled)
if ($this->params->get('oauthlink_updateuser')) {
$this->syncUserAttributes($user, $provider, $token);
}
$message = "Welcome $user->name";
$app->enqueueMessage($message, 'success');
// $app->redirect("/");
}
}
/**
*
*/
private function findUser($resourceOwner)
{
$matchType = $this->params->get('oauthlink_match_account');
$identifier = null;
switch ($matchType) {
case 'email':
$identifier = $resourceOwner->claim('email');
break;
case 'username':
$identifier = $resourceOwner->claim('preferred_username');
break;
}
if ($identifier == null) {
throw new Exception('No user parameter to try and match on!');
}
$db = Factory::getDbo();
// Prepare the SQL query
$query = $db->getQuery(true)
->select('*')
->from($db->quoteName('#__users'))
->where($db->quoteName($matchType) . ' = ' . $db->quote($identifier));
// Execute the query
$db->setQuery($query);
$result = $db->loadObject();
if ($result) {
// User found, return a JUser object
$user = new User($result->id);
return $user;
} else {
// User not found
return null;
}
}
/**
* authenticateUser
*
* $user - User object to login
* $session - Joomla Session
* $app - Joomla App
*/
private function authenticateUser($user, $session, $app)
{
if (!$user) {
die("Authentication Error");
}
$authenticator = Authentication::getInstance();
$authResponse = new AuthenticationResponse();
$authResponse->status == Authentication::STATUS_SUCCESS;
$session->set('user', $user);
// Set a flag so we know this user was logged in via OAuthLink
$session->set('oauthlink.login', 1);
}
/**
*
*/
private function createUserAccount($resourceOwner)
{
$user = new User();
// Set user information
$userdata = array();
$userdata['username'] = $resourceOwner->claim('email');
$userdata['name'] = $resourceOwner->claim('name');
$userdata['email'] = $resourceOwner->claim('email');
$userdata['block'] = 0;
$defaultGroupID = $this->params->get('default_user_group');
$userdata['groups'] = array($defaultGroupID);
// Try to save the user
if (!$user->bind($userdata)) {
// User creation failed, handle the error
return null;
}
if (!$user->save()) {
// User creation failed, handle the error
return null;
}
// User created successfully
return $user;
}
/**
*
*/
private function syncUserGroups($user, $provider, $token)
{
// Fetch the defined rules
$mappings = $this->params->get('group-mapping');
// Fetch the user's groups via MS Graph
$azureGroups = $provider->get($provider->getRootMicrosoftGraphUri($token) . '/v1.0/me/memberOf?$select=id', $token);
$azureGroupIds = $idsArray = array_column($azureGroups, 'id');
// For each of the mapping configurations
foreach ($mappings as $key => $mapping) {
$group_id = intval($mapping->joomla_user_group);
// Check if the user is a member of this group on Azure
if (in_array($mapping->azure_group_id, $azureGroupIds)) {
// Try and add the member to the group
try {
UserHelper::addUserToGroup($user->id, $group_id);
} catch (Exception $e) {
JLog::add("Error syncing user $user->id to group $group_id", JLog::ERROR, 'oauthlink');
}
}
// Not a member, continue
}
// If we're also removing users from groups - effectively the defined groups become Azure managed
if ($this->params->get('oauthlink_removegroups')) {
$userGroups = UserHelper::getUserGroups($user->id);
foreach ($mappings as $key => $mapping) {
$group_id = intval($mapping->joomla_user_group);
// Is the user in this group?
if (in_array($group_id, $userGroups)) {
JLog::add("User $user->id is in Joomla group $group_id", JLog::INFO, 'oauthlink');
// Is the user still in the corresponding Azure group?
if (!in_array($mapping->azure_group_id, $azureGroupIds)) {
// No longer a member, remove them
JLog::add("User $user->id is NOT in $mapping->azure_group_id, removing them!", JLog::INFO, 'oauthlink');
try {
UserHelper::removeUserFromGroup($user->id, $group_id);
} catch (Exception $e) {
JLog::add("Error removing user $user->id from group $group_id", JLog::ERROR, 'oauthlink');
}
} else {
JLog::add("User $user->id is still in $mapping->azure_group_id, yay!", JLog::INFO, 'oauthlink');
}
// Still a member, continue
}
}
}
}
/**
* CUSTOM ATTRIBUTE UPDATES ONLY WORK ONCE THE USER IS AUTHENTICATED
*/
private function syncUserAttributes($user, $provider, $token)
{
// Fetch the defined rules
$mappings = $this->params->get('attribute-mapping');
// Build the list of attrs to request vis MS Graph
$propertiesToFetch = array();
foreach ($mappings as $key => $mapping) {
$propertiesToFetch[] = $mapping->azure_user_attribute;
}
// Add the displayName property as we'll sync this by default
$propertiesToFetch[] = 'displayName';
// Build and fetch the query
$propertiesParam = implode(',', $propertiesToFetch);
// Fetch the user profile
$userProfile = $provider->get($provider->getRootMicrosoftGraphUri($token) . '/v1.0/me?$select=' . $propertiesParam, $token);
// Update
if ($userProfile) {
// Set the user name
$user->set('name', $userProfile['displayName']);
$user->save();
// Update custom fields
foreach ($mappings as $key => $mapping) {
$azureProperty = $mapping->azure_user_attribute;
$field_id = $mapping->joomla_user_field;
$user_id = $user->id;
$value = $userProfile[$azureProperty] ?? "";
$model_field = JModelLegacy::getInstance('Field', 'FieldsModel', ['ignore_request' => true]);
$status = $model_field->setFieldValue($field_id, $user_id, $value);
}
}
}
/**
* Blank, but needs to be here. Trust me on that.
*/
public function onAfterRender()
{
}
}