-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopenid_ms.php
224 lines (194 loc) · 9.1 KB
/
openid_ms.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
<?php
require __DIR__ . '/vendor/autoload.php';
use Jumbojett\OpenIDConnectClient;
class MicrosoftProviderAuth {
var $config;
var $access_token;
function __construct($config) {
$this->config = $config;
}
function triggerAuth() {
error_log('Performing authentication using MS365');
global $ost;
$self = $this;
$login_type = $_SESSION['ext:bk:login_type'];
$redirectUri = rawurlencode(rtrim($ost->getConfig()->getURL(), '/') . '/api/auth/ext');
$clientId = $this->config->get('CLIENT_ID');
$clientSecret = $this->config->get('CLIENT_SECRET');
$scopes = rawurlencode($this->config->get('SCOPES'));
$resourceUrl = $this->config->get('RESOURCE_ID') . $this->config->get('RESOURCE_ENDPOINT');
$nonce = $_COOKIE['OSTSESSID'];
if (!isset($_REQUEST['id_token'])) {
$authUrl = $this->config->get('AUTHORITY_URL') . $this->config->get('AUTHORIZE_ENDPOINT') . '?client_id='. $clientId . '&response_type=id_token%20code&redirect_uri=' . $redirectUri . '&response_mode=form_post&scope=' . $scopes . '&state=' . $login_type . '&nonce=' . $nonce;
header('Location: ' . $authUrl);
echo 'Redirecting to MS365 authentication page...';
exit;
} else {
error_log('Validating ID token');
$jwt = $_REQUEST['id_token'];
$oidc = new OpenIDConnectClient(
$this->config->get('AUTHORITY_URL'),
$clientId,
$clientSecret,
);
if (!$oidc->verifyJWTsignature($jwt)) {
http_response_code(400);
echo('Token de autentificare invalid.');
exit;
}
error_log('MS365 ID token is valid, constructing user session');
$jwt = explode('.', $jwt);
$authInfo = json_decode(base64_decode($jwt[1]), true);
$_SESSION[':openid-ms']['name'] = $authInfo['name'];
$_SESSION[':openid-ms']['oid'] = $authInfo['oid'];
if (isset($authInfo['email'])) {
$_SESSION[':openid-ms']['email'] = $authInfo['email'];
} elseif (isset($authInfo['preferred_username']) && (filter_var($authInfo['preferred_username'], FILTER_VALIDATE_EMAIL))) {
$_SESSION[':openid-ms']['email'] = $authInfo['preferred_username'];
}
$_SESSION[':openid-ms']['nonce'] = $authInfo['nonce'];
error_log('Performing redirect for login type ' . $login_type);
if ($login_type == 'CLIENT') {
Http::redirect(ROOT_PATH . 'home.php');
echo 'Redirecting to client home page';
} else if ($login_type == 'STAFF') {
Http::redirect(ROOT_PATH . 'scp/login.php');
echo 'Redirecting to staff home page';
} else {
http_response_code(400);
echo 'Invalid login type!';
}
exit;
}
}
}
class MicrosoftOpenIDClientAuthBackend extends ExternalUserAuthenticationBackend {
static $id = "openid_ms.client";
static $name = "Microsoft OpenID Auth - Client";
static $sign_in_image_url = "https://docs.microsoft.com/en-us/azure/active-directory/develop/media/active-directory-branding-guidelines/sign-in-with-microsoft-light.png";
static $service_name = "Microsoft OpenID Auth - Client";
function __construct($config) {
$this->config = $config;
if ($_SERVER['SCRIPT_NAME'] === '/login.php' || $_SERVER['SCRIPT_NAME'] === '/open.php') {
$_SESSION['ext:bk:login_type'] = 'CLIENT';
if ($this->config->get('HIDE_LOCAL_CLIENT_LOGIN')) {
if ($this->config->get('PLUGIN_ENABLED_AWESOME')) {
?>
<script>
window.onload = function () {
"use strict";
document.getElementById("one-view-page").remove();
document.getElementById("middle-view-page").remove();
/*something odd happens to this DIV when using these hacks.*/
document.getElementById("header-logo-subtitle").remove();
var eAuth = document.getElementsByClassName("external-auth");
while (eAuth[0].nextSibling) {
eAuth[0].nextSibling.remove();
}
};
</script>
<?php
} else {
?>
<script>window.onload = function() {
var loginBox = document.getElementsByClassName('login-box');
loginBox[0].remove();
var eAuth = document.getElementsByClassName('external-auth');
while (eAuth[0].nextSibling) {
eAuth[0].nextSibling.remove();
}
};
</script>
<?php
}
}
}
$this->MicrosoftAuth = new MicrosoftProviderAuth($config);
}
function supportsInteractiveAuthentication() {
return false;
}
function signOn() {
global $errors;
$self = $this;
if (isset($_SESSION[':openid-ms']['email'])) {
// Check email for access
$emailDomain = substr(strrchr($_SESSION[':openid-ms']['email'], "@"), 1);
$allowedDomains = explode(',', $this->config->get('ALLOWED_CLIENT_DOMAINS'));
if (in_array(strtolower($emailDomain), array_map('strtolower', $allowedDomains)) || ($this->config->get('ALLOWED_CLIENT_DOMAINS') == '')) {
if (($acct = ClientAccount::lookupByUsername($_SESSION[':openid-ms']['email'])) && $acct->getId() && ($client = new ClientSession(new EndUser($acct->getUser())))) {
return $client;
} else {
$info = array(
'email' => $_SESSION[':openid-ms']['email'],
'name' => $_SESSION[':openid-ms']['name'],
);
return new ClientCreateRequest($this, $info['email'], $info);
}
}
}
}
static function signOut($user) {
parent::signOut($user);
unset($_SESSION[':openid-ms']);
//https://login.microsoftonline.com/common/oauth2/logout?post_logout_redirect_uri=http%3A%2F%2Flocalhost%2Fmyapp%2F
}
function triggerAuth() {
parent::triggerAuth();
$MicrosoftAuth = $this->MicrosoftAuth->triggerAuth();
}
}
class MicrosoftOpenIDStaffAuthBackend extends ExternalStaffAuthenticationBackend {
static $id = "openid_ms.staff";
static $name = "Micrsoft OpenID Auth - Staff";
static $service_name = "Microsoft OpenID Auth - Staff";
static $sign_in_image_url = "https://docs.microsoft.com/en-us/azure/active-directory/develop/media/active-directory-branding-guidelines/sign-in-with-microsoft-light.png";
function __construct($config) {
$this->config = $config;
$sign_in_image_url = $this->config->get('LOGIN_LOGO');
if ($_SERVER['SCRIPT_NAME'] === '/scp/login.php') {
$_SESSION['ext:bk:login_type'] = 'STAFF';
if ($this->config->get('HIDE_LOCAL_STAFF_LOGIN')) {
?>
<script>window.onload = function() {
var login = document.getElementById('login');
login.remove();
};
</script>
<?php
}
}
$this->MicrosoftAuth = new MicrosoftProviderAuth($config);
}
function supportsInteractiveAuthentication() {
return false;
}
function signOn() {
if (isset($_SESSION[':openid-ms']['email'])) {
$emailDomain = substr(strrchr($_SESSION[':openid-ms']['email'], "@"), 1);
$allowedDomains = explode(',', $this->config->get('ALLOWED_STAFF_DOMAINS'));
if (in_array(strtolower($emailDomain), array_map('strtolower', $allowedDomains)) || ($this->config->get('ALLOWED_STAFF_DOMAINS') == '')) {
if (($staff = StaffSession::lookup(array('email' => $_SESSION[':openid-ms']['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[':openid-ms']);
//https://login.microsoftonline.com/common/oauth2/logout?post_logout_redirect_uri=http%3A%2F%2Flocalhost%2Fmyapp%2F
}
function triggerAuth() {
parent::triggerAuth();
$MicrosoftAuth = $this->MicrosoftAuth->triggerAuth();
}
}