-
Notifications
You must be signed in to change notification settings - Fork 157
/
oauth2.php
691 lines (596 loc) · 22.5 KB
/
oauth2.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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
<?php
/*********************************************************************
class.oauth2.php
Oauth2 authentication backends using League\OAuth2 toolkit
Peter Rotich <peter@osticket.com>
Copyright (c) 2006-2022 osTicket
https://osticket.com
Credit:
* https://github.com/thephpleague/oauth2-client
* Interwebz
Released under the GNU General Public License WITHOUT ANY WARRANTY.
See LICENSE.TXT for details.
vim: expandtab sw=4 ts=4 sts=4:
**********************************************************************/
include_once 'auth.php';
use League\OAuth2\Client\Provider\GenericProvider;
/**
* OAuth2AuthBackend
*
* Interface class for OAuth2 authentication backends
*
* Not entirely necessary but used to decorate classes to make it easy to
* test if it's an instance of desired backend.
*
*/
interface OAuth2AuthBackend {
/**
* Function: handleCallback
*
* Called when we receive OAuth2 auth code
*/
function callBack($resp, $ref='');
/**
* Function: redirect util
*
* Called to redirect user to either internal or external urls.
*/
function redirectTo($url);
function setState($state);
function getState();
function resetState();
function getAccessToken($code);
}
/**
* OAuth2AuthenticationTrait
*
* Trait class with the core OAuth2 authentication functions used by
* downstream backends.
*
*/
trait OAuth2AuthenticationTrait {
// OAuth2 Id Provider
private $provider;
// debug mode flag
private $debug = false;
// SESSION store for data like AuthNRequestID
private $session;
// Configuration store
protected $config;
// Supported attributes mapped to scopes - hard coded for now
private $attributes = ['username', 'givenname', 'surname', 'email'];
function __construct($config, $provider=null) {
$this->config = $config;
// Session data stash for backends
$this->session = &$_SESSION[':oauth'][$this->getId()];
if ($provider instanceof OAuth2AuthorizationBackend)
$this->provider = $provider;
else
$this->provider = new GenericOauth2Provider();
// Get Oauth Client based on provider
$this->client = $this->provider->getClient($config);
}
function callback($resp, $ref=null) {
try {
if ($this->getState() == $resp['state']
&& ($token=$this->getAccessToken($resp['code']))
&& ($owner=$this->client->getResourceOwner($token))
&& ($attrs=$this->mapAttributes($owner->toArray()))) {
$this->resetState();
// Attempt to signIn the user based on returned attributes
$result = $this->signIn($attrs);
if ($result instanceof AuthenticatedUser) {
// SignIn successful - login the user and redirect to
// desired panel
if ($this->login($result, $this))
$this->onSignIn();
}
}
} catch (Exception $ex) {
return false;
}
}
function getId() {
return static::$id;
}
function getName() {
return $this->config->getName();
}
function getServiceName() {
return $this->config->getServiceName();
}
public function setState($state) {
$this->session['AuthState'] = $state;
}
public function resetState() {
$this->setState('');
}
public function getState() {
return $this->session['AuthState'];
}
private function mapAttributes(array $result) {
// Mapout the supported attributes only
$attributes = array();
$result = array_change_key_case($result, CASE_LOWER);
foreach ($this->attributes as $attr) {
if (!($key=strtolower($this->config->getAttributeFor($attr))))
continue;
$attributes[$attr] = $result[$key] ?: null;
}
// Use email as username if none is provided or vice versa!
if (!isset($attributes['username']) && isset($attributes['email']))
$attributes['username'] = $attributes['email'];
elseif (!isset($attributes['email'])
&& isset($attributes['username'])
&& Validator::is_email($attributes['username']))
$attributes['email'] = $attributes['username'];
return $attributes;
}
public function getAccessToken($code) {
return $this->client->getAccessToken('authorization_code',
['code' => $code]);
}
public function refreshAccessToken($refreshToken) {
return $this->client->getAccessToken('refresh_token',
['refresh_token' => $refreshToken]);
}
public function triggerAuth() {
parent::triggerAuth();
// Regenerate OAuth2 auth request
$authUrl = $this->client->getAuthorizationUrl();
// Get the state generated for you and store it to the session.
$this->setState($this->client->getState());
$this->redirectTo($authUrl);
}
function redirectTo($url) {
// No cache redirect
header('Pragma: no-cache');
header('Cache-Control: no-cache, must-revalidate');
Http::redirect($url);
}
static function signOut($user) {
parent::signOut($user);
}
abstract function signIn($attrs);
abstract function onSignIn();
}
/**
* OAuth2StaffAuthBackend
*
* Provides OAuth2 authentication backend for agents
*/
class OAuth2StaffAuthBackend extends ExternalStaffAuthenticationBackend
implements OAuth2AuthBackend {
use OAuth2AuthenticationTrait;
static $id = "oauth2.agent";
static $name = "OAuth2";
static $service_name = "OAuth2";
private function signIn($attrs) {
if ($attrs && isset($attrs['username'])) {
if (($staff = StaffSession::lookup($attrs['username']))
&& $staff->getId()) {
// Older versions
if (!$staff instanceof StaffSession)
$staff = new StaffSession($staff->getId());
return $staff;
} else {
$_SESSION['_staff']['auth']['msg'] = sprintf('%s (%s)',
'Have your administrator create a local account',
Format::htmlchars($attrs['username']));
}
}
}
private function onSignIn() {
$this->redirectTo($_SESSION['_staff']['auth']['dest'] ?: osTicket::get_base_url().'scp/');
}
}
/**
* OAuth2UserAuthBackend
*
* Provides OAuth2 authentication backend for users
*/
class OAuth2UserAuthBackend extends ExternalUserAuthenticationBackend
implements OAuth2AuthBackend {
use OAuth2AuthenticationTrait;
static $id = "oauth2.user";
static $name = "OAuth2";
static $service_name = "OAuth2";
private function signIn($attrs) {
if ($attrs && isset($attrs['username'])) {
if (($acct = ClientAccount::lookupByUsername($attrs['username']))
&& $acct->getId())
return new ClientSession(new EndUser($acct->getUser()));
// Auto-register user if possible
$email = $attrs['email'] ?: $attrs['username'];
if (Validator::is_email($email)) {
if (!($name = trim(sprintf('%s %s', $attrs['givenname'], $attrs['surname']))))
$name = $attrs['username'];
$info = ['email' => $email, 'name' => $name];
$req = new ClientCreateRequest($this, $attrs['username'], $info);
return $req->attemptAutoRegister();
}
}
}
private function onSignIn() {
$this->redirectTo($_SESSION['_client']['auth']['dest'] ?: osTicket::get_base_url());
}
}
/*
* OAuth Email Auth Backend
*
*/
class OAuth2EmailAuthBackend implements OAuth2AuthBackend {
use OAuth2AuthenticationTrait;
static $id = "oauth2.emailautho";
private $options;
public $account;
const ERR_UNKNOWN = 0;
const ERR_EMAIL_ATTR = 1;
const ERR_EMAIL_MISMATCH = 2;
const ERR_REFRESH_TOKEN = 3;
private function isStrict() {
// TODO: Require osTicket v1.18 and delegate strict checking to
// the email account ($this->account->isStrict())
// For now the flag is being set via the provider by overloading
// backend id
return ($this->provider && $this->provider->isStrict());
}
function getEmailId() {
return $this->account->getEmailId();
}
function getEmail() {
return $this->account->getEmail();
}
function getEmailAddress() {
return $this->account->email->getEmail();
}
private function updateCredentials($info, &$errors) {
return $this->account->updateCredentials(
$this->provider->getId(), $info, $errors);
}
public function callback($resp, $ref=null) {
$errors = [];
$err = sprintf('%s_auth_bk', $this->account->getType());
try {
if ($this->getState() == $resp['state']
&& ($token=$this->getAccessToken($resp['code']))
&& ($owner=$this->client->getResourceOwner($token))
&& ($attrs=$this->mapAttributes($owner->toArray()))) {
$this->resetState();
$info = [
'access_token' => $token->getToken(),
'refresh_token' => $token->getRefreshToken(),
'expires' => $token->getExpires(),
'resource_owner_id' => $token->getResourceOwnerId(),
'resource_owner_email' => $attrs['email'],
];
if (!isset($attrs['email']))
$errors[$err] = $this->error_msg(self::ERR_EMAIL_ATTR, $attrs);
elseif (!$info['refresh_token'])
$errors[$err] = $this->error_msg(self::ERR_REFRESH_TOKEN);
elseif (!$this->signIn($attrs) && $this->isStrict()) {
// On strict mode email mismatch is an error
// TODO: Move Strict checking to osTiket core on
// credentials update.
$errors[$err] = $this->error_msg(self::ERR_EMAIL_MISMATCH, $attrs);
}
// Update the credentials if no validation errors
if (!$errors
&& !$this->updateCredentials($info, $errors)
&& !isset($errors[$err]))
$errors[$err] = $this->error_msg(self::ERR_UNKNOWN);
}
} catch (Exception $ex) {
$errors[$err] = $ex->getMessage();
}
// stash the results before redirecting
$email = $this->getEmail();
// TODO: check if email implements StashableTrait
if ($errors)
$email->stash('errors', $errors);
else
$email->stash('notice', sprintf('%s: %s',
$this->account->getType(),
__('OAuth2 Authorization Successful')
));
// redirect back to email page
$this->onSignIn();
}
public function triggerAuth() {
// Regenerate OAuth2 auth request
$urlOptions = $this->provider->getUrlOptions() ?: [];
$authUrl = $this->client->getAuthorizationUrl($urlOptions);
// Get the state generated for you and store it to the session.
$this->setState($this->client->getState());
$this->redirectTo($authUrl);
}
private function signIn($attrs) {
return !strcasecmp($attrs['email'], $this->getEmailAddress());
}
private function onSignIn() {
$this->redirectTo(osTicket::get_base_url()
.sprintf('scp/emails.php?id=%d#%s',
$this->getEmailId(),
$this->account->getType())
);
}
private function error_msg($errorno, $attrs=[]) {
switch ($errorno) {
case self::ERR_EMAIL_ATTR:
return __('Invalid Email Atrribute');
break;
case self::ERR_EMAIL_MISMATCH:
return sprintf(__('Email Mismatch: Expecting Authorization for %s not %s'),
$this->getEmailAddress(),
$attrs['email']);
break;
case self::ERR_REFRESH_TOKEN:
return __('Unable to obtain Refresh Token');
break;
case self::ERR_UNKNOWN:
default:
return __('Unknown Error');
}
}
}
abstract class OAuth2ProviderBackend extends OAuth2AuthorizationBackend {
protected $config;
private $plugin;
private $plugin_id;
static $defaults = [];
// Strict flag
private $strict = false;
function __construct($options=[]) {
if (isset($options['plugin_id']))
$this->plugin_id = (int) $options['plugin_id'];
}
function isStrict() {
return (bool) $this->strict;
}
function getId() {
return static::$id;
}
function getName() {
return static::$name;
}
function getPluginId() {
return $this->plugin_id;
}
function getPlugin() {
if (!isset($this->plugin) && $this->plugin_id)
$this->plugin = PluginManager::lookup($this->plugin_id);
return $this->plugin;
}
function getConfig($instance=null, $vars=[]) {
if ($instance && !is_object($instance))
$instance = $this->getPluginInstance($instance);
if (!isset($this->config) || $instance) {
$this->config = new OAuth2EmailConfig($instance ?
$instance->getNamespace() : null, $vars);
$this->config->setInstance($instance);
}
return $this->config;
}
function getConfigForm($vars, $id=null) {
$vars = $vars ?: $this->getDefaults();
return $this->getConfig($id, $vars)->getForm($vars);
}
function getDefaults() {
return static::$defaults ?: [];
}
function getPluginInstance($id) {
return $this->getPlugin()->getInstance($id);
}
function addPluginInstance($vars, &$errors) {
if (!($plugin=$this->getPlugin()))
return false;
// Add some default values not set on Basic Config
$vars = array_merge($vars, array_intersect_key($this->getDefaults(),
array_flip(['attr_username', 'attr_email', 'attr_givenname',
'attr_surname'])));
return $plugin->addInstance($vars, $errors);
}
function getEmailAuthBackend($id) {
list($auth, $a, $i, $strict) = self::parseId($id);
if (!strcasecmp($auth, $this->getId())
&& ($plugin=$this->getPlugin())
&& $plugin->isActive()
&& ($instance=$this->getPluginInstance((int) $i))
&& ($config=$instance->getConfig())
&& ($account=EmailAccount::lookup((int) $a))
&& $account->isEnabled()) {
// Set strict flag
$this->strict = (bool) $strict;
$bk = new OAuth2EmailAuthBackend($config, $this);
$bk->account = $account;
return $bk;
}
}
function refreshAccessToken($refreshToken, $id, &$errors) {
if (!$refreshToken || !($bk=$this->getEmailAuthBackend($id)))
return false;
try {
$token = $bk->refreshAccessToken($refreshToken);
return array_filter([
'access_token' => $token->getToken(),
'refresh_token' => $token->getRefreshToken(),
'expires' => $token->getExpires()
]);
} catch( Exception $ex) {
$errors['refresh_token'] = $ex->getMessage();
}
}
function triggerEmailAuth($id) {
if (!($bk=$this->getEmailAuthBackend($id)))
return false;
$_SESSION['ext:bk:id'] = $id;
$bk->triggerAuth();
}
// We delegate call back to Email Authorization backend
function callback($resp, $id='') {
if (!$id || !($bk=$this->getEmailAuthBackend($id)))
return false;
return $bk->callback($resp, $id);
}
// Register Authentication Providers (Templates)
static function registerAuthenticationProviders($options=[]) {
OAuth2AuthenticationBackend::register(new
GoogleOAuth2Provider($options));
OAuth2AuthenticationBackend::register(new
MicrosoftOAuth2Provider($options));
OAuth2AuthenticationBackend::register(new
OktaOAuth2Provider($options));
OAuth2AuthenticationBackend::register(new
OtherOAuth2Provider($options));
}
// Register Authorization Providers
static function registerEmailAuthoProviders($options=[]) {
OAuth2AuthorizationBackend::register(new
GoogleEmailOAuth2Provider($options));
OAuth2AuthorizationBackend::register(new
MicrosoftEmailOAuth2Provider($options));
OAuth2AuthorizationBackend::register(new
OtherEmailOAuth2Provider($options));
}
static function registerProviders($options=[]) {
self::registerEmailAuthoProviders($options);
self::registerAuthenticationProviders($options);
}
abstract function getClient(PluginConfig $config);
}
class OAuth2Client extends GenericProvider {
protected function getAuthorizationParameters(array $options) {
// Cleanup prompt conflicts
// approval_prompt, hardcoded upstream, nolonger works for Google
// when attempting to force new refresh token.
$options = parent::getAuthorizationParameters($options);
if (isset($options['prompt']) && isset($options['approval_prompt']))
unset($options['approval_prompt']);
return $options;
}
}
class GenericOauth2Provider extends OAuth2ProviderBackend {
static $id = 'oauth2:other';
static $name = 'OAuth2 - Other';
static $defaults = [];
static $urlOptions = [];
function getUrlOptions() {
return static::$urlOptions;
}
function getClient(PluginConfig $config) {
return new OAuth2Client($config->getClientSettings());
}
}
class OtherOauth2Provider extends GenericOauth2Provider {
static $id = 'oauth2:other';
static $name = 'OAuth2 - Other';
static $icon = 'icon-plus-sign';
static $defaults = [];
static $urlOptions = [];
}
// Authentication Providers
class GoogleOauth2Provider extends GenericOauth2Provider {
static $id = 'oauth2:google';
static $name = 'Google';
static $icon = 'icon-google-plus-sign';
static $defaults = [
'urlAuthorize' => 'https://accounts.google.com/o/oauth2/v2/auth',
'urlAccessToken' => 'https://oauth2.googleapis.com/token',
'urlResourceOwnerDetails' => 'https://www.googleapis.com/oauth2/v2/userinfo',
'scopes' => 'profile https://www.googleapis.com/auth/userinfo.email',
'auth_name' => 'Google',
'auth_service' => 'Google',
'attr_username' => 'email',
'attr_email' => 'email',
'attr_givenname' => 'given_name',
'attr_surname' => 'family_name',
];
}
class MicrosoftOauth2Provider extends GenericOauth2Provider {
static $id = 'oauth2:microsoft';
static $name = 'Microsoft';
static $icon = 'icon-windows';
static $defaults = [
'urlAuthorize' => 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize',
'urlAccessToken' => 'https://login.microsoftonline.com/common/oauth2/v2.0/token',
'urlResourceOwnerDetails' => 'https://graph.microsoft.com/v1.0/me',
'scopes' => 'https://graph.microsoft.com/.default',
'auth_name' => 'Microsoft',
'auth_service' => 'Azure',
'attr_username' => 'userPrincipalName',
'attr_email' => 'mail',
'attr_givenname' => 'givenname',
'attr_surname' => 'surname',
];
}
class OktaOauth2Provider extends GenericOauth2Provider {
static $id = 'oauth2:okta';
static $name = 'Okta';
static $icon = 'icon-circle-blank';
static $defaults = [
'urlAuthorize' => 'https://${yourOktaDomain}/oauth2/v1/authorize',
'urlAccessToken' => 'https://${yourOktaDomain}/oauth2/v1/token',
'urlResourceOwnerDetails' => 'https://${yourOktaDomain}/oauth2/v1/userinfo',
'scopes' => 'openid profile email',
'auth_name' => 'Okta',
'auth_service' => 'Okta',
'attr_username' => 'userName',
'attr_email' => 'email',
'attr_givenname' => 'given_name',
'attr_surname' => 'family_name',
];
}
// Authorization Email OAuth Providers
class GenericEmailOauth2Provider extends GenericOauth2Provider {
function getPluginInstance($id) {
$i = parent::getPluginInstance($id);
// Set config class for Email Authorization Providers
$i->setConfigClass('OAuth2EmailConfig');
return $i;
}
}
class OtherEmailOauth2Provider extends GenericEmailOauth2Provider {
static $id = 'oauth2:othermail';
static $name = 'OAuth2 - Other Provider';
static $defaults = [];
static $urlOptions = [];
}
class GoogleEmailOauth2Provider extends GenericEmailOauth2Provider {
static $id = 'oauth2:gmail';
static $name = 'OAuth2 - Google';
static $defaults = [
'urlAuthorize' => 'https://accounts.google.com/o/oauth2/v2/auth',
'urlAccessToken' => 'https://oauth2.googleapis.com/token',
'urlResourceOwnerDetails' => 'https://www.googleapis.com/gmail/v1/users/me/profile',
'scopes' => 'https://mail.google.com/',
'attr_username' => 'emailaddress',
'attr_email' => 'emailaddress',
'attr_givenname' => 'given_name',
'attr_surname' => 'family_name',
];
static $urlOptions = [
'responseType' => 'code',
'access_type' => 'offline',
'prompt' => 'consent',
];
}
class MicrosoftEmailOauth2Provider extends GenericEmailOauth2Provider {
static $id = 'oauth2:msmail';
static $name = 'OAuth2 - Microsoft';
static $defaults = [
'urlAuthorize' => 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize',
'urlAccessToken' => 'https://login.microsoftonline.com/common/oauth2/v2.0/token',
'urlResourceOwnerDetails' => 'https://outlook.office.com/api/v2.0/me',
'scopes' => 'offline_access https://outlook.office.com/Mail.ReadWrite',
'attr_username' => 'EmailAddress',
'attr_email' => 'EmailAddress',
'attr_givenname' => 'givenname',
'attr_surname' => 'surname',
];
static $urlOptions = [
'tenant' => 'common',
'accessType' => 'offline_access',
'prompt' => 'select_account',
];
}
?>