-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAuthDjango.body.php
311 lines (286 loc) · 11.7 KB
/
AuthDjango.body.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
<?php
/* AuthDjango.php */
/**
* This plugin allows you to use the django auth system with mediawiki.
*
* Copyright 2009-2010 Thomas Lilley <[email protected]> (tomlilley.co.uk)
* Copyright 2011 Jack Grigg <[email protected]> (jackgrigg.com)
* Copyright 2015-2016 Matthew Petroff (https://mpetroff.net/)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* http://www.gnu.org/copyleft/gpl.html
*/
/**
* Not actually used for authentication, UserLoadFromSession does that.
*
*/
class AuthDjango extends AuthPlugin {
/**
* DatabaseMysql database object for Django
*
* @var object
*/
private $dbd;
/**
* Users table name
*
* @var string
*/
private $user_table;
/**
* Sesion table name
*
* @var string
*/
private $session_table;
/**
* Connects to the database and inititaes some variables.
*
*/
public function __construct() {
// Disable mediawiki account creation
$GLOBALS['wgGroupPermissions']['*']['createaccount'] = false;
// Set table names
$this->authdjango_table = $GLOBALS['wgAuthDjangoConfig']['AuthDjangoTable'];
$this->user_table = $GLOBALS['wgAuthDjangoConfig']['UserTable'];
$this->session_table = $GLOBALS['wgAuthDjangoConfig']['SessionTable'];
// start database connection
$this->dbd = DatabaseBase::factory('mysql', array(
'host' => $GLOBALS['wgAuthDjangoConfig']['DjangoHost'],
'user' => $GLOBALS['wgAuthDjangoConfig']['DjangoUser'],
'password' => $GLOBALS['wgAuthDjangoConfig']['DjangoPass'],
'dbname' => $GLOBALS['wgAuthDjangoConfig']['DjangoDBName'],
'flags' => 0,
'tablePrefix' => ''
));
// Set hooks functions
$GLOBALS['wgHooks']['UserLogout'][] = $this;
$GLOBALS['wgHooks']['UserLoadFromSession'][] = $this;
$GLOBALS['wgHooks']['PersonalUrls'][] = $this;
}
/**
* Check whether there exists a user account with the given name.
* The name will be normalized to MediaWiki's requirements, so
* you might need to munge it (for instance, for lowercase initial
* letters).
*
* @param $username String: username.
* @return bool
*/
public function userExists($username) {
// Since if a user does not exist they will be created,
// always return true.
return true;
}
/**
* Return true if the wiki should create a new local account automatically
* when asked to login a user who doesn't exist locally but does in the
* external auth database.
*
* If you don't automatically create accounts, you must still create
* accounts in some way. It's not possible to authenticate without
* a local account.
*
* This is just a question, and shouldn't perform any actions.
*
* @return bool
*/
public function autoCreate() {
return true;
}
/**
* Can users change their passwords?
*
* @return bool
*/
public function allowPasswordChange() {
return false;
}
/**
* Set the given password in the authentication database.
* As a special case, the password may be set to null to request
* locking the password to an unusable value, with the expectation
* that it will be set later through a mail reset or other method.
*
* Return true if successful.
*
* @param $user User object.
* @param $password String: password.
* @return bool
*/
public function setPassword( $user, $password ) {
return false;
}
/**
* Return true to prevent logins that don't authenticate here from being
* checked against the local database's password fields.
*
* This is just a question, and shouldn't perform any actions.
*
* @return bool
*/
public function strict() {
return true;
}
/**
* Check if a user should authenticate locally if the global authentication fails.
* If either this or strict() returns true, local authentication is not used.
*
* @param $username String: username.
* @return bool
*/
public function strictUserAuth( $username ) {
return true;
}
/**
* Login in to mediawiki from an existing django session.
* User must be logged in to django for this to work.
*
* @param object $user
* @param bool $result
* @return bool
*/
public function onUserLoadFromSession($user, &$result) {
global $wgLanguageCode, $wgRequest, $wgOut;
$lg = Language::factory($wgLanguageCode);
if (isset($_REQUEST['title']) && strstr($_REQUEST['title'], SpecialPageFactory::getLocalNameFor('Userlogin'))) {
// Redirect to our login page
$returnto = $wgRequest->getVal('returnto');
// Don't redirect straight back to the logout page
$returnto = (strstr($returnto, SpecialPageFactory::getLocalNameFor('Userlogout'))) ? '' : $returnto;
$wgOut->redirect($GLOBALS['wgAuthDjangoConfig']['LinkToSiteLogin'] . '?next=' . $GLOBALS['wgAuthDjangoConfig']['LinkToWiki'] . $returnto);
} elseif (array_key_exists('sessionid', $_COOKIE)) {
$django_session = $_COOKIE['sessionid'];
// find if there is a user connected to this session
$r1 = $this->dbd->selectRow(
$this->session_table,
'session_data',
'session_key = \'' . $django_session . '\''
);
if ($r1) {
$decoded = json_decode(explode(':', base64_decode($r1->session_data), 2)[1]);
if (property_exists($decoded, '_auth_user_id')) {
$user_id = $decoded->_auth_user_id;
$r1 = $this->dbd->selectRow(
$this->user_table,
array(
'username',
'email'
),
'id=' . $user_id
);
} else {
$r1 = false;
}
}
if ($r1) {
// there is a Django session present
$dbr = wfGetDB(DB_SLAVE);
$mw_uid = $dbr->selectField(
$this->authdjango_table,
'mw_user_id',
array(
'd_user_id' => $user_id
)
);
$local_id = ($mw_uid) ? $mw_uid : 0;
if (!$mw_uid) {
// Django user does not exist in MW djangouser table
// create a new user if one does not exist, and update
// djangouser table if one does
$username = $r1->username;
$email = $r1->email;
// replace space with underscore
// (site login doesn't allow spaces in usernames)
$username = str_replace(' ', '_', $username);
$u = User::newFromName($username);
if ($u->getID() == 0) {
// FIXME: Is the AuthDjango::userExists call necessary here?
if (AuthDjango::autoCreate() && AuthDjango::userExists($username)) {
$u->setEmail($email);
$u->confirmEmail();
$u->addToDatabase();
$u->setToken();
}
}
// Either a new MW user hs been created or there was an existing
// user with the same (ignoring spaces) username.
// In any case, update authdjango table.
$local_id = $u->getID();
$dbw = wfGetDB(DB_MASTER);
$dbw->insert(
$this->authdjango_table,
array(
'd_user_id' => $user_id,
'mw_user_id' => $local_id
)
);
}
$user->setID($local_id);
$user->loadFromId();
$result = true;
$user->setCookies();
wfSetupSession();
} else {
// if we're not logged in on the site make sure we're logged out of the database.
setcookie('wikidb_session', '', time()-3600);
unset($_COOKIE['wikidb_session']);
if (session_id() != "") {
session_destroy();
}
}
} else {
// if we're not logged in on the site make sure we're logged out of the database.
setcookie('wikidb_session', '', time()-3600);
unset($_COOKIE['wikidb_session']);
if (session_id() != "") {
session_destroy();
}
}
return true;
}
/**
* Logs user out of django
*
* @param object $user
* @return bool
*/
public function onUserLogout(&$user) {
if (array_key_exists('sessionid', $_COOKIE)) {
// Delete session from session table and session profile table
$this->dbd->delete(
$this->session_table,
array(
'session_key' => $_COOKIE['sessionid']
)
);
}
// Clear cookies and session data
setcookie('sessionid', '', time()-3600);
unset($_COOKIE['sessionid']);
if (session_id() != "") {
session_destroy();
}
return true;
}
/**
* Unset anonymous login.
*
* @param object $personal_urls
* @param object $wgTitle
* @return bool
*/
public function onPersonalUrls($personal_urls, $wgTitle) {
unset( $personal_urls['anonlogin'] );
return true;
}
}