forked from Stanford/WMD
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebAuthSession.inc
More file actions
343 lines (296 loc) · 8.55 KB
/
WebAuthSession.inc
File metadata and controls
343 lines (296 loc) · 8.55 KB
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
<?php
/**
* @file
* WMD's session handler.
*
*/
class WebAuthSession {
/**
* Session ID.
*/
protected $sid;
/**
* Session name.
*/
protected $name;
/**
* Session data.
*/
protected $data;
/**
* Cookie domain.
*/
protected $domain;
/**
* Cookie sent.
*/
protected $cookieSent = FALSE;
/**
* Error data.
*/
protected $error;
/**
* Auth URL
*/
protected $authUrl;
/**
* Headers from WebAuth.
*/
protected $headers = array();
/**
* Raw return data from check.php.
*/
protected $returnData = array();
/**
* WebAuth AT Cookie.
*/
protected $atCookie;
/**
* Return URL.
*/
protected $returnUrl;
/**
* Logged in status.
*/
protected $loggedIn;
/**
* Initialize the object.
*/
public function __construct() {
// Initialize the object and populate the headers.
$this->__init();
// Check for WebAuth AT Cookie.
$this->getAtCookie();
// If we don't have an at_cookie, we have not authenticated yet.
if (empty($this->atCookie)) {
$this->loggedIn = FALSE;
}
else {
$this->loggedIn = TRUE;
}
}
private function __init() {
global $cookie_domain;
$this->domain = $cookie_domain;
$this->name = session_name();
$this->sid = session_id();
// Set our authentication redirect URL.
$this->makeAuthUrl();
// Get AT cookie.
$this->getAtCookie();
// Get our session data.
$this->loadSessionData();
}
/**
* Public function to write cookie to user.
* @param string $name
* @param string $value
* @param string $expire
* @param string $path
* @param string $domain
* @param bool $secure
* @param bool $httpOnly
*/
public function writeCookie($name, $value) {
// see if we've already made this cookie request
static $cookieRequests = array();
if (isset($cookieRequests[$name]) && $cookieRequests[$name] === $value) {
return; // request already made
}
else {
$cookieRequests[$name] = $value; // store it for testing
}
// write a cookie to the browser - check for PHP version to see whether to include $httpOnly value
if (version_compare(PHP_VERSION, '5.2.0') === -1) {
setcookie($name, $value, 0, '/', $this->domain, TRUE);
}
else {
setcookie($name, $value, 0, '/', $this->domain, TRUE, TRUE);
}
}
public function getSessionName() {
return $this->name;
}
public function getSessionId() {
return $this->sid;
}
public function getSessionData($key = NULL) {
if (isset($this->data[$key]) && !empty($key)) {
return $this->data[$key];
}
elseif (!empty($key)) {
// If set but not available.
return NULL;
}
else {
return $this->data;
}
}
public function getReturnUrl($clear = FALSE) {
if ($clear && isset($_SESSION['wa_return_url'])) {
$this->returnUrl = $_SESSION['wa_return_url'];
unset($_SESSION['wa_return_url']);
}
return $this->returnUrl;
}
public function writeSessionData() {
if (isset($_SERVER['HTTPS'])) {
$_SESSION['wa_session_data'] = $this->data;
}
}
public function loadSessionData() {
if (isset($_SESSION['wa_session_data'])) {
$this->data = $_SESSION['wa_session_data'];
}
elseif ($this->isLoggedIn()) {
$this->getHeaderData();
foreach ($this->headers as $k => $v) {
// Check for WA_LDAPPRIVGROUP.
if (drupal_substr($k, 0, 16) == 'wa_ldapprivgroup') {
$this->data['ldap_groups'][$v] = $v;
}
else {
$this->data[$k] = $v;
}
}
if ($this->isLoggedIn()) {
$this->writeSessionData();
}
}
}
public function isValidSession() {
if (isset($this->atCookie)) {
if (empty($this->data)) {
$this->loadSessionData();
}
if (!empty($this->data)) {
if (REQUEST_TIME <= $this->data['wa_token_expiration'] && $this->isLoggedIn()) {
return TRUE;
}
}
}
return FALSE;
}
public function isLoggedIn() {
return (bool) $this->loggedIn;
}
public function getLdapGroups() {
return isset($this->data['ldap_groups']) ? $this->data['ldap_groups'] : array();
}
public function getAtCookie() {
if (isset($_COOKIE['webauth_at']) && $_COOKIE['webauth_at']) {
$this->atCookie = $_COOKIE['webauth_at'];
}
}
public function getWeblogin($redirect = NULL) {
if (!empty($redirect)) {
$this->makeAuthUrl($redirect);
}
header("Location: " . $this->authUrl);
exit();
}
public function makeReturnUrl($url = NULL) {
// Figure out our return destination and save to the session for later consumption
$returnDest = $url;
if (empty($returnDest)) {
$wa_dest = trim(variable_get('webauth_destination', 'user'));
if (isset($_GET['destination']) && strcmp($_GET['destination'], 'admin') !== TRUE) {
$returnDest = $_GET['destination'];
}
elseif (isset($wa_dest)) {
$returnDest = $wa_dest;
}
elseif (isset($_GET['q']) && strcmp($_GET['q'], 'user/login') != 0) {
$returnDest = $_GET['q'];
}
}
$this->returnUrl = $returnDest;
// Save query for later use.
$query = '';
$temp_query = $_GET;
// don't save q or destination
unset($temp_query['q']);
unset($temp_query['destination']);
// The string replacement is for aesthetic purposes only
$query = str_replace('%2F', '/', http_build_query($temp_query, '', '&'));
if ($query != '') {
if (strpos($this->returnUrl, '?') === FALSE) {
$this->returnUrl .= '?';
}
$this->returnUrl .= $query;
}
if ($url != NULL) {
$_SESSION['wa_return_url'] = $this->returnUrl;
}
}
public function makeAuthUrl($redirect = NULL) {
// Create auth URL.
$this->authUrl = $this->getLoginUrl($redirect);
}
public function getLoginUrl($url = NULL) {
// Make sure we create a return url before we make a login url.
$this->makeReturnUrl($url);
// This is a fix for non-clean urls. Need to provide a link to the full
// path so that we hit the webauth dir. That dir will redirect to proper
// non-clean path after authentication via webauth.
if (!variable_get('clean_url', FALSE)) {
$return = 'https://' . $_SERVER['SERVER_NAME'] . base_path() . variable_get('webauth_path', conf_path() . '/webauth')
. '/login';
if (isset($this->returnUrl)) {
$return .= '?destination=' . $this->returnUrl;
}
}
else {
$return = url(variable_get('webauth_path', conf_path() . '/webauth') . '/login',
array('absolute' => TRUE, 'query' => array('destination' => $this->returnUrl)));
}
return $return;
}
public function getHeaderData() {
// Read and parse the header data provided by requesting check.php.
// This is the source of all webauth data for the system.
$url = parse_url(url(variable_get('webauth_path', conf_path() . '/webauth') . '/check.php', array('absolute' => TRUE)));
if (variable_get('clean_url', FALSE) == FALSE) {
$url['path'] = base_path() . variable_get('webauth_path', conf_path() . '/webauth') . '/check.php';
}
$sock = fsockopen('ssl://' . $url['host'], 443, $errno, $errstr, 10);
if (!$sock) {
drupal_set_message(t('Unable to authenticate because of socket error: @error', array('@error' => $errstr)), 'error');
return FALSE;
}
fwrite($sock, "GET " . $url['path'] . " HTTP/1.1\r\n");
fwrite($sock, "Host: " . $url['host'] . "\r\n");
fwrite($sock, "Connection: close\r\n");
fwrite($sock, "Cookie: webauth_at=" . urlencode($this->atCookie) . "\r\n");
fwrite($sock, "Accept: */*\r\n");
fwrite($sock, "\r\n");
fwrite($sock, "\r\n");
$return = '';
while (!feof($sock)) {
$return .= fread($sock, 8192);
}
fclose($sock);
list($this->returnData['headers'],
$this->returnData['body']) = explode("\r\n\r\n", $return);
$headers = explode("\r\n", $this->returnData['headers']);
$status = (bool) (strpos($headers[0], 'HTTP/1.1 200 OK') !== FALSE);
if ($status) {
$this->loggedIn = TRUE;
foreach ($headers as $header) {
$header_array = explode(': ', $header, 2);
// Must have two or list() will warn.
if (count($header_array) == 2) {
list($hName, $hVal) = $header_array;
$hName = trim($hName);
$hVal = trim($hVal);
if (substr($hName, 0, 3) == 'wa_') {
$this->headers[$hName] = $hVal;
}
}
}
}
else {
$this->loggedIn = FALSE;
}
}
}