This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
/
class.remoteAuthDriver.php
287 lines (263 loc) · 10.4 KB
/
class.remoteAuthDriver.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
<?php
/*
* Copyright 2007-2011 Charles du Jeu <contact (at) cdujeu.me>
* This file is part of AjaXplorer.
*
* AjaXplorer is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* AjaXplorer 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with AjaXplorer. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <http://www.ajaxplorer.info/>.
*/
defined('AJXP_EXEC') or die( 'Access not allowed');
/**
* AJXP_Plugin to bridge authentication between Ajxp and external CMS
* This class works in 2 modes (master / slave)
It requires the following arguments:
- SLAVE_MODE
- LOGIN_URL
- LOGOUT_URL
- SECRET
- USERS_FILEPATH (the users.ser filepath)
In master mode, the login dialog is still displayed in AJXP.
When the user attempt a login, the given credential are sent back to the given remote URL.
The LOGIN_URL is called as GET LOGIN_URL?name=<entered_user_name>&pass=<entered_password>&key=MD5(name.password.SECRET)
The method must return a valid PHP serialized object for us to continue (see below)
In slave mode, the login dialog is not displayed in AJXP.
If the user directly go to the main page, (s)he's redirected to the LOGIN_URL.
The logout button isn't displayed either, a back button linking to LOGOUT_URL is used instead.
The user will log in on the remote site, and the remote script will call us, as GET ajxpPath/plugins/auth.remote/login.php?object=<serialized object>&key=MD5(object.SECRET)
The serialized object contains the same data as the serialAuthDriver.
*
* @package AjaXplorer_Plugins
* @subpackage Auth
*/
class remoteAuthDriver extends AbstractAuthDriver {
var $usersSerFile;
/** The current authentication mode */
var $slaveMode;
/** The current secret */
var $secret;
/** The current url array */
var $urls;
function init($options){
// Migrate new version of the options
if(isSet($options["CMS_TYPE"])){
// Transform MASTER_URL + LOGIN_URI to MASTER_HOST, MASTER_URI, LOGIN_URL, LOGOUT_URI
$options["SLAVE_MODE"] = "false";
$cmsOpts = $options["CMS_TYPE"];
if($cmsOpts["cms"] != "custom"){
$loginURI = $cmsOpts["LOGIN_URI"];
if(strpos($cmsOpts["MASTER_URL"], "http") === 0){
$parse = parse_url($cmsOpts["MASTER_URL"]);
$rootHost = $parse["host"];
$rootURI = $parse["path"];
}else{
$rootHost = "";
$rootURI = $cmsOpts["MASTER_URL"];
}
$cmsOpts["MASTER_HOST"] = $rootHost;
$cmsOpts["LOGIN_URL"] = $cmsOpts["MASTER_URI"] = AJXP_Utils::securePath("/".$rootURI."/".$loginURI);
$logoutAction = $cmsOpts["LOGOUT_ACTION"];
switch($cmsOpts["cms"]){
case "wp":
$cmsOpts["LOGOUT_URL"] = ($logoutAction == "back" ? $cmsOpts["MASTER_URL"] : $cmsOpts["MASTER_URL"]."/wp-login.php?action=logout");
break;
case "joomla":
$cmsOpts["LOGOUT_URL"] = $cmsOpts["LOGIN_URL"];
break;
case "drupal":
$cmsOpts["LOGOUT_URL"] = ($logoutAction == "back" ? $cmsOpts["LOGIN_URL"] : $cmsOpts["MASTER_URL"]."/user/logout");
break;
default:
break;
}
}
$options = array_merge($options, $cmsOpts);
}
$this->slaveMode = $options["SLAVE_MODE"] == "true";
if($this->slaveMode && ConfService::getCoreConf("ALLOW_GUEST_BROWSING", "auth")){
$contribs = $this->xPath->query("registry_contributions/external_file");
foreach ($contribs as $contribNode){
if($contribNode->getAttribute('filename') == 'plugins/core.auth/standard_auth_actions.xml'){
$contribNode->parentNode->removeChild($contribNode);
}
}
}
parent::init($options);
$options = $this->options;
$this->usersSerFile = $options["USERS_FILEPATH"];
$this->secret = $options["SECRET"];
$this->urls = array($options["LOGIN_URL"], $options["LOGOUT_URL"]);
}
function supportsUsersPagination(){
return true;
}
function listUsers(){
$users = AJXP_Utils::loadSerialFile($this->usersSerFile);
if(AuthService::ignoreUserCase()){
$users = array_combine(array_map("strtolower", array_keys($users)), array_values($users));
}
ksort($users);
return $users;
}
function listUsersPaginated($baseGroup = "/", $regexp, $offset = -1 , $limit = -1){
$users = $this->listUsers($baseGroup);
$result = array();
$index = 0;
foreach($users as $usr => $pass){
if(!empty($regexp) && !preg_match("/$regexp/i", $usr)){
continue;
}
if($offset != -1 && $index < $offset) {
$index ++;
continue;
}
$result[$usr] = $pass;
$index ++;
if($limit != -1 && count($result) >= $limit) break;
}
return $result;
}
function getUsersCount($baseGroup = "/", $regexp = ""){
return count($this->listUsersPaginated($baseGroup, $regexp));
}
function userExists($login){
$users = $this->listUsers();
if(AuthService::ignoreUserCase()) $login = strtolower($login);
if(!is_array($users) || !array_key_exists($login, $users)) return false;
return true;
}
function checkPassword($login, $pass, $seed){
if(AuthService::ignoreUserCase()) $login = strtolower($login);
global $AJXP_GLUE_GLOBALS;
if(isSet($AJXP_GLUE_GLOBALS) || (isSet($this->options["LOCAL_PREFIX"]) && strpos($login, $this->options["LOCAL_PREFIX"]) === 0) ){
$userStoredPass = $this->getUserPass($login);
if(!$userStoredPass) return false;
if($seed == "-1"){ // Seed = -1 means that password is not encoded.
return AJXP_Utils::pbkdf2_validate_password($pass, $userStoredPass);// ($userStoredPass == md5($pass));
}else{
return (md5($userStoredPass.$seed) == $pass);
}
}else{
$crtSessionId = session_id();
session_write_close();
$host = "";
if(isSet($this->options["MASTER_HOST"])){
$host = $this->options["MASTER_HOST"];
}else{
$host = parse_url($_SERVER["SERVER_ADDR"], PHP_URL_HOST);
}
$formId = "";
if(isSet($this->options["MASTER_AUTH_FORM_ID"])){
$formId = $this->options["MASTER_AUTH_FORM_ID"];
}
$uri = $this->options["MASTER_URI"];
$funcName = $this->options["MASTER_AUTH_FUNCTION"];
require_once 'cms_auth_functions.php';
if(function_exists($funcName)){
$sessCookies = call_user_func($funcName, $host, $uri, $login, $pass, $formId);
if($sessCookies != ""){
if(is_array($sessCookies)){
$sessid = $sessCookies["AjaXplorer"];
session_id($sessid);
session_start();
if(!$this->slaveMode){
foreach($sessCookies as $k => $v){
if($k == "AjaXplorer") continue;
setcookie($k, urldecode($v), 0, $uri);
}
}
}else if(is_string($sessCookies)){
session_id($sessCookies);
session_start();
}
return true;
}
$sessid = call_user_func($funcName, $host, $uri, $login, $pass, $formId);
if($sessid != ""){
session_id($sessid);
session_start();
return true;
}
}
// NOW CHECK IN LOCAL USERS LIST
$userStoredPass = $this->getUserPass($login);
if(!$userStoredPass) return false;
if($seed == "-1"){ // Seed = -1 means that password is not encoded.
$res = AJXP_Utils::pbkdf2_validate_password($pass, $userStoredPass); //($userStoredPass == md5($pass));
}else{
$res = (md5($userStoredPass.$seed) == $pass);
}
if($res){
session_id($crtSessionId);
session_start();
return true;
}
return false;
}
}
function createCookieString($login){
$userPass = $this->getUserPass($login);
return md5($login.":".$userPass.":ajxp");
}
function usersEditable(){
return true;
}
function passwordsEditable(){
return false;
}
function createUser($login, $passwd){
if(AuthService::ignoreUserCase()) $login = strtolower($login);
$users = $this->listUsers();
if(!is_array($users)) $users = array();
if(array_key_exists($login, $users)) return "exists";
if($this->getOption("TRANSMIT_CLEAR_PASS") === true){
$users[$login] = AJXP_Utils::pbkdf2_create_hash($passwd);
}else{
$users[$login] = $passwd;
}
AJXP_Utils::saveSerialFile($this->usersSerFile, $users);
}
function changePassword($login, $newPass){
if(AuthService::ignoreUserCase()) $login = strtolower($login);
$users = $this->listUsers();
if(!is_array($users) || !array_key_exists($login, $users)) return ;
if($this->getOption("TRANSMIT_CLEAR_PASS") === true){
$users[$login] = AJXP_Utils::pbkdf2_create_hash($newPass);
}else{
$users[$login] = $newPass;
}
AJXP_Utils::saveSerialFile($this->usersSerFile, $users);
}
function deleteUser($login){
if(AuthService::ignoreUserCase()) $login = strtolower($login);
$users = $this->listUsers();
if(is_array($users) && array_key_exists($login, $users))
{
unset($users[$login]);
AJXP_Utils::saveSerialFile($this->usersSerFile, $users);
}
}
function getUserPass($login){
if(!$this->userExists($login)) return false;
$users = $this->listUsers();
return $users[$login];
}
function getLoginRedirect(){
return parent::getLoginRedirect();
}
function getLogoutRedirect(){
return $this->urls[1];
}
}
?>