Repository navigation
Expand file tree
/
Copy pathSession.php
More file actions
233 lines (212 loc) · 6.74 KB
/
Copy pathSession.php
File metadata and controls
233 lines (212 loc) · 6.74 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
<?php
namespace dokuwiki\plugin\oauth;
/**
* Singleton to manage all oAuth related session and cookie data
*/
class Session
{
/** @var Session */
protected static $instance;
/**
* hidden constructor
*/
protected function __construct()
{
}
/**
* Get Singleton Instance
*
* @return Session
*/
public static function getInstance()
{
if (self::$instance === null) {
self::$instance = new Session();
}
return self::$instance;
}
/**
* Set the environment needed to verify a login in progress
*
* @param string $servicename the name of the service used
* @param string $id pageID to return to after login
* @return void
*/
public function setLoginData($servicename, $id)
{
$_SESSION[DOKU_COOKIE]['auth']['oauth'] = [
'servicename' => $servicename,
'id' => $id,
];
}
/**
* Get the current login environment
*
* @return false|array Either [servicename=>*, id=>*] or false
*/
public function getLoginData()
{
return $_SESSION[DOKU_COOKIE]['auth']['oauth'] ?? false;
}
/**
* Clear login environment after login
*
* @return void
*/
public function clearLoginData()
{
if (isset($_SESSION[DOKU_COOKIE]['auth']['oauth'])) {
unset($_SESSION[DOKU_COOKIE]['auth']['oauth']);
}
}
/**
* This basically duplicates what DokuWiki does when a user is logged in
*
* @param array $userdata
* @param bool $resettime Set a new session time? False only when restoring from session
* @return void
* @throws Exception
*/
public function setUser($userdata, $resettime = true)
{
global $USERINFO;
if (
!isset($userdata['user']) ||
!isset($userdata['name']) ||
!isset($userdata['mail']) ||
!isset($userdata['grps']) ||
!is_array($userdata['grps'])
) {
throw new Exception('Missing user data, cannot save to session');
}
$USERINFO = $userdata;
$_SERVER['REMOTE_USER'] = $userdata['user'];
$_SESSION[DOKU_COOKIE]['auth']['user'] = $userdata['user'];
$_SESSION[DOKU_COOKIE]['auth']['pass'] = 'not-set'; // pass is neither needed nor wanted
$_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO;
$_SESSION[DOKU_COOKIE]['auth']['buid'] = auth_browseruid();
if ($resettime) {
$_SESSION[DOKU_COOKIE]['auth']['time'] = time();
}
}
/**
* The user data currently saved in the session if any
*
* @return false|array
*/
public function getUser()
{
return $_SESSION[DOKU_COOKIE]['auth']['info'] ?? false;
}
/**
* Set oAuth info to cookie
*
* We use the same cookie as standard DokuWiki, but write different info.
*
* @param string $servicename the name of the service used
* @param string $storageId the ID the user's tokens are stored as
* @return void
*/
public function setCookie($servicename, $storageId)
{
$expires = time() + 60 * 60 * 24 * 365;
$this->sendCookie($this->buildCookie($servicename, $storageId, $expires), $expires);
}
/**
* Build the cookie value
*
* The storageId is derived from the user's mail address, so the data is signed with
* the cookie salt to make sure the cookie was issued by us. The salt is prefixed to get
* a key that is separate from the one core uses for its own cookie.
*
* @param string $servicename the name of the service used
* @param string $storageId the ID the user's tokens are stored as
* @param int $expires unix timestamp after which the cookie is no longer accepted
* @return string the signed cookie value
*/
public function buildCookie($servicename, $storageId, $expires)
{
$data = "$servicename|oauth|$storageId|$expires";
return $data . '|' . hash_hmac('sha256', $data, 'oauth' . auth_cookiesalt(false, true));
}
/**
* Get oAuth info from cookie
*
* Cookies with a missing or invalid signature (including those set by older versions) and
* expired cookies are removed, otherwise core's auth_login() would try to decrypt them as
* its own cookie and fail.
*
* @return array|false Either [servicename=>?, storageID=>?] or false if no valid oauth data in cookie
*/
public function getCookie()
{
if (!isset($_COOKIE[DOKU_COOKIE])) return false;
[$servicename, $oauth, $storageId, $expires] = sexplode('|', $_COOKIE[DOKU_COOKIE], 5, '');
if ($oauth !== 'oauth') return false;
if (
!ctype_digit($expires) || (int)$expires < time() ||
!hash_equals($this->buildCookie($servicename, $storageId, $expires), $_COOKIE[DOKU_COOKIE])
) {
$this->clearCookie();
return false;
}
return ['servicename' => $servicename, 'storageId' => $storageId];
}
/**
* Remove the cookie for the current request and from the browser
*
* @return void
*/
protected function clearCookie()
{
unset($_COOKIE[DOKU_COOKIE]);
$this->sendCookie('', time() - 600000);
}
/**
* Send the cookie to the browser
*
* @param string $value the cookie value, empty to remove the cookie
* @param int $expires unix timestamp after which the browser drops the cookie
* @return void
*/
protected function sendCookie($value, $expires)
{
global $conf;
$cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir'];
setcookie(
DOKU_COOKIE,
$value,
[
'expires' => $expires,
'path' => $cookieDir,
'domain' => '',
'secure' => $conf['securecookie'] && is_ssl(),
'httponly' => true
]
);
}
/**
* Is any auth data in the session currently trustworthy?
* @return bool
*/
public function isValid()
{
global $conf;
if (!isset($_SESSION[DOKU_COOKIE]['auth']['buid'])) return false;
if (!isset($_SESSION[DOKU_COOKIE]['auth']['time'])) return false;
if ($_SESSION[DOKU_COOKIE]['auth']['buid'] != auth_browseruid()) return false;
if ($_SESSION[DOKU_COOKIE]['auth']['time'] < time() - $conf['auth_security_timeout']) return false;
return true;
}
/**
* Clear the session from auth related data
*
* The cookie is kept, it is needed to log the user in again once the session expired.
*
* @return void
*/
public function clear()
{
$this->clearLoginData();
}
}