-
Notifications
You must be signed in to change notification settings - Fork 1
/
sso.php
193 lines (152 loc) · 5.94 KB
/
sso.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
<?php
use ohmy\Auth2;
function isEmailAllowed($email, $domains_string) {
$email_domain = end(explode('@', $email, 2));
$domains = explode(',', $domains_string);
foreach ($domains as $domain) {
if (strcasecmp($email_domain, trim($domain)) === 0) {
return TRUE;
}
}
return strlen(trim($domains_string)) === 0;
}
class SsoAuth {
var $config;
var $access_token;
function __construct($config) {
$this->config = $config;
}
function triggerAuth() {
global $ost;
$self = $this;
$scpf = "off";
$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if (str_contains($actual_link, '/scp') || str_contains($actual_link, 'scpf=on')){
$scpf = "on";
}
return Auth2::legs(3)
->set('id', $this->config->get('g-client-id'))
->set('secret', $this->config->get('g-client-secret'))
->set('redirect', rtrim($ost->getConfig()->getURL(), '/') . '/login.php?do=ext&bk=sso.client' . '&scpf=' . $scpf)
->set('scope', 'profile email')
->authorize($this->config->get('auth-url'))
->access($this->config->get('token-url'))
->finally(function($data) use ($self) {
$self->access_token = $data['access_token'];
});
}
}
class SsoStaffAuthBackend extends ExternalStaffAuthenticationBackend {
static $id = "sso";
static $name = "SSO";
static $sign_in_image_url = false;
static $service_name = "SSO";
var $config;
function __construct($config) {
$this->config = $config;
$this->sso = new SsoAuth($config);
}
function signOn() {
// TODO: Check session for auth token
if (isset($_SESSION[':oauth']['email'])) {
if (!isEmailAllowed($_SESSION[':oauth']['email'], $this->config->get(
'g-allowed-domains-agents')))
$_SESSION['_staff']['auth']['msg'] = 'Login with this email address is not permitted';
else if (($staff = StaffSession::lookup(array('email' => $_SESSION[':oauth']['email'])))
&& $staff->getId()
) {
if (!$staff instanceof StaffSession) {
// osTicket <= v1.9.7 or so
$staff = new StaffSession($user->getId());
}
return $staff;
}
else
$_SESSION['_staff']['auth']['msg'] = 'Have your administrator create a local account';
}
}
static function signOut($user) {
parent::signOut($user);
unset($_SESSION[':oauth']);
}
function triggerAuth() {
parent::triggerAuth();
$sso = $this->sso->triggerAuth();
$sso->GET(
$this->config->get('info-url') . "?access_token="
. $this->sso->access_token)
->then(function($response) {
require_once INCLUDE_DIR . 'class.json.php';
if ($json = JsonDataParser::decode($response->text))
$_SESSION[':oauth']['email'] = $json['email'];
Http::redirect(ROOT_PATH . 'scp');
}
);
}
}
class SsoClientAuthBackend extends ExternalUserAuthenticationBackend {
static $id = "sso.client";
static $name = "SSO";
static $sign_in_image_url = false;
static $service_name = "SSO";
function __construct($config) {
$this->config = $config;
$this->sso = new SsoAuth($config);
}
function supportsInteractiveAuthentication() {
return false;
}
function signOn() {
global $errors;
// TODO: Check session for auth token
if (isset($_SESSION[':oauth']['email'])) {
if (!isEmailAllowed($_SESSION[':oauth']['email'], $this->config->get(
'g-allowed-domains-clients')))
$errors['err'] = 'Login with this email address is not permitted';
else if (($acct = ClientAccount::lookupByUsername($_SESSION[':oauth']['email']))
&& $acct->getId()
&& ($client = new ClientSession(new EndUser($acct->getUser()))))
return $client;
else if (isset($_SESSION[':oauth']['profile'])) {
// TODO: Prepare ClientCreateRequest
$profile = $_SESSION[':oauth']['profile'];
$info = array(
'email' => $_SESSION[':oauth']['email'],
'name' => $profile['displayName'],
);
return new ClientCreateRequest($this, $info['email'], $info);
}
}
}
static function signOut($user) {
parent::signOut($user);
unset($_SESSION[':oauth']);
}
function triggerAuth() {
require_once INCLUDE_DIR . 'class.json.php';
$sso = $this->sso->triggerAuth();
$token = $this->sso->access_token;
$url = $this->config->get('info-url');
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array(
"Accept: application/json",
"Authorization: Bearer " . $token,
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
//curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
//curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
curl_close($curl);
$json = json_decode($resp, true);
$_SESSION[':oauth']['profile'] = $json;
$_SESSION[':oauth']['email'] = $json['email'];
$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if (str_contains($actual_link, 'scpf=on' )){
Http::redirect(ROOT_PATH . 'scp');
}else{
Http::redirect(ROOT_PATH . 'login.php');
}
}
}