Skip to content

Commit 48de7d7

Browse files
authored
#97 Support SimpleSAMLphp 2.0.x in Drupal 7
2 parents 51e4ec5 + 6d02394 commit 48de7d7

File tree

4 files changed

+69
-53
lines changed

4 files changed

+69
-53
lines changed

www/resume.php renamed to public/resume.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@
99
* @package simpleSAMLphp
1010
* @version $Id$
1111
*/
12-
sspmod_drupalauth_Auth_Source_External::resume();
12+
13+
use SimpleSAML\Module\drupalauth\Auth\Source\External;
14+
15+
External::resume($_REQUEST['State']);

lib/Auth/Source/External.php renamed to src/Auth/Source/External.php

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
<?php
22

3+
namespace SimpleSAML\Module\drupalauth\Auth\Source;
4+
5+
use SimpleSAML\Auth\Source;
6+
use SimpleSAML\Module\drupalauth\ConfigHelper;
7+
38
/**
49
* Drupalath authentication source for using Drupal's login page.
510
*
@@ -92,7 +97,7 @@
9297
* @package drupalauth
9398
* @version $Id$
9499
*/
95-
class sspmod_drupalauth_Auth_Source_External extends SimpleSAML_Auth_Source {
100+
class External extends Source {
96101

97102
/**
98103
* Whether to turn on debugging
@@ -149,7 +154,7 @@ public function __construct($info, $config) {
149154

150155

151156
/* Get the configuration for this module */
152-
$drupalAuthConfig = new sspmod_drupalauth_ConfigHelper($config,
157+
$drupalAuthConfig = new ConfigHelper($config,
153158
'Authentication source ' . var_export($this->authId, TRUE));
154159

155160
$this->debug = $drupalAuthConfig->getDebug();
@@ -162,7 +167,7 @@ public function __construct($info, $config) {
162167
define('DRUPAL_ROOT', $drupalAuthConfig->getDrupalroot());
163168
}
164169

165-
$ssp_config = SimpleSAML_Configuration::getInstance();
170+
$ssp_config = \SimpleSAML\Configuration::getInstance();
166171
$this->cookie_path = '/' . $ssp_config->getValue('baseurlpath');
167172
$this->cookie_salt = $ssp_config->getValue('secretsalt');
168173

@@ -211,7 +216,7 @@ private function getUser() {
211216
if(sha1($this->cookie_salt . $arrCookie[1]) == $arrCookie[0]) {
212217
$drupaluid = $arrCookie[1];
213218
} else {
214-
throw new SimpleSAML_Error_Exception('Cookie hash invalid. This indicates either tampering or an out of date drupal4ssp module.');
219+
throw new \SimpleSAML\Error\Exception('Cookie hash invalid. This indicates either tampering or an out of date drupal4ssp module.');
215220
}
216221
}
217222

@@ -296,7 +301,7 @@ private function getUser() {
296301
}
297302
$attributes[$userAttrNames[$userKey]] = is_array($value) ? $value : array($value);
298303
}
299-
catch (Exception $e) {
304+
catch (\Exception $e) {
300305
watchdog_exception('simplesaml', $e);
301306
}
302307
}
@@ -317,7 +322,7 @@ private function getUser() {
317322
*
318323
* @param array &$state Information about the current authentication.
319324
*/
320-
public function authenticate(&$state) {
325+
public function authenticate(array &$state): void {
321326
assert(is_array($state));
322327

323328
$attributes = $this->getUser();
@@ -357,14 +362,14 @@ public function authenticate(&$state) {
357362
* and restores it in another location, and thus bypasses steps in
358363
* the authentication process.
359364
*/
360-
$stateId = SimpleSAML_Auth_State::saveState($state, 'drupalauth:External');
365+
$stateId = \SimpleSAML\Auth\State::saveState($state, 'drupalauth:External');
361366

362367
/*
363368
* Now we generate an URL the user should return to after authentication.
364369
* We assume that whatever authentication page we send the user to has an
365370
* option to return the user to a specific page afterwards.
366371
*/
367-
$returnTo = SimpleSAML_Module::getModuleURL('drupalauth/resume.php', array(
372+
$returnTo = \SimpleSAML\Module::getModuleURL('drupalauth/resume.php', array(
368373
'State' => $stateId,
369374
));
370375

@@ -383,7 +388,8 @@ public function authenticate(&$state) {
383388
* Note the 'ReturnTo' parameter. This must most likely be replaced with
384389
* the real name of the parameter for the login page.
385390
*/
386-
SimpleSAML_Utilities::redirect($authPage, array(
391+
$http = new \SimpleSAML\Utils\HTTP();
392+
$http->redirectTrustedURL($authPage, array(
387393
'ReturnTo' => $returnTo,
388394
));
389395

@@ -409,27 +415,27 @@ public static function resume() {
409415
* it in the 'State' request parameter.
410416
*/
411417
if (!isset($_REQUEST['State'])) {
412-
throw new SimpleSAML_Error_BadRequest('Missing "State" parameter.');
418+
throw new \SimpleSAML\Error\BadRequest('Missing "State" parameter.');
413419
}
414420
$stateId = (string)$_REQUEST['State'];
415421

416422
/*
417423
* Once again, note the second parameter to the loadState function. This must
418424
* match the string we used in the saveState-call above.
419425
*/
420-
$state = SimpleSAML_Auth_State::loadState($stateId, 'drupalauth:External');
426+
$state = \SimpleSAML\Auth\State::loadState($stateId, 'drupalauth:External');
421427

422428
/*
423429
* Now we have the $state-array, and can use it to locate the authentication
424430
* source.
425431
*/
426-
$source = SimpleSAML_Auth_Source::getById($state['drupalauth:AuthID']);
432+
$source = Source::getById($state['drupalauth:AuthID']);
427433
if ($source === NULL) {
428434
/*
429435
* The only way this should fail is if we remove or rename the authentication source
430436
* while the user is at the login page.
431437
*/
432-
throw new SimpleSAML_Error_Exception('Could not find authentication source with id ' . $state[self::AUTHID]);
438+
throw new \SimpleSAML\Error\Exception('Could not find authentication source with id ' . $state['drupalauth:AuthID']);
433439
}
434440

435441
/*
@@ -438,7 +444,7 @@ public static function resume() {
438444
* change config/authsources.php while an user is logging in.
439445
*/
440446
if (! ($source instanceof self)) {
441-
throw new SimpleSAML_Error_Exception('Authentication source type changed.');
447+
throw new \SimpleSAML\Error\Exception('Authentication source type changed.');
442448
}
443449

444450

@@ -455,7 +461,7 @@ public static function resume() {
455461
* Here we simply throw an exception, but we could also redirect the user back to the
456462
* login page.
457463
*/
458-
throw new SimpleSAML_Error_Exception('User not authenticated after login page.');
464+
throw new \SimpleSAML\Error\Exception('User not authenticated after login page.');
459465
}
460466

461467
/*
@@ -464,7 +470,7 @@ public static function resume() {
464470
*/
465471

466472
$state['Attributes'] = $attributes;
467-
SimpleSAML_Auth_Source::completeAuth($state);
473+
Source::completeAuth($state);
468474

469475
/*
470476
* The completeAuth-function never returns, so we never get this far.
@@ -479,7 +485,7 @@ public static function resume() {
479485
*
480486
* @param array &$state The logout state array.
481487
*/
482-
public function logout(&$state) {
488+
public function logout(array &$state): void {
483489
assert(is_array($state));
484490

485491
if (!session_id()) {

lib/Auth/Source/UserPass.php renamed to src/Auth/Source/UserPass.php

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
<?php
22

3+
namespace SimpleSAML\Module\drupalauth\Auth\Source;
4+
5+
use SimpleSAML\Module\core\Auth\UserPassBase;
6+
use SimpleSAML\Module\drupalauth\ConfigHelper;
7+
38
/**
49
* Drupal authentication source for simpleSAMLphp
510
*
6-
* Copyright SIL International, Steve Moitozo, <[email protected]>, http://www.sil.org
11+
* Copyright SIL International, Steve Moitozo, <[email protected]>, http://www.sil.org
712
*
813
* This class is a Drupal authentication source which authenticates users
914
* against a Drupal site located on the same server.
@@ -21,18 +26,18 @@
2126
* -------------------------------------------------------------------
2227
*
2328
* To use this put something like this into config/authsources.php:
24-
*
29+
*
2530
* 'drupal-userpass' => array(
2631
* 'drupalauth:UserPass',
27-
*
32+
*
2833
* // The filesystem path of the Drupal directory.
2934
* 'drupalroot' => '/var/www/drupal-7.0',
30-
*
35+
*
3136
* // Whether to turn on debug
3237
* 'debug' => true,
33-
*
34-
* // Which attributes should be retrieved from the Drupal site.
35-
*
38+
*
39+
* // Which attributes should be retrieved from the Drupal site.
40+
*
3641
* 'attributes' => array(
3742
* array('drupaluservar' => 'uid', 'callit' => 'uid'),
3843
* array('drupaluservar' => 'name', 'callit' => 'cn'),
@@ -43,12 +48,12 @@
4348
* array('drupaluservar' => 'roles','callit' => 'roles'),
4449
* ),
4550
* ),
46-
*
51+
*
4752
* Format of the 'attributes' array explained:
4853
*
4954
* 'attributes' can be an associate array of attribute names, or NULL, in which case
5055
* all attributes are fetched.
51-
*
56+
*
5257
* If you want everything (except) the password hash do this:
5358
* 'attributes' => NULL,
5459
*
@@ -59,10 +64,10 @@
5964
* array('drupaluservar' => 'mail', 'callit' => 'mail'),
6065
* array('drupaluservar' => 'roles','callit' => 'roles'),
6166
* ),
62-
*
63-
* The value for 'drupaluservar' is the variable name for the attribute in the
67+
*
68+
* The value for 'drupaluservar' is the variable name for the attribute in the
6469
* Drupal user object.
65-
*
70+
*
6671
* The value for 'callit' is the name you want the attribute to have when it's
6772
* returned after authentication. You can use the same value in both or you can
6873
* customize by putting something different in for 'callit'. For an example,
@@ -73,7 +78,7 @@
7378
* @package drupalauth
7479
* @version $Id$
7580
*/
76-
class sspmod_drupalauth_Auth_Source_UserPass extends sspmod_core_Auth_UserPassBase {
81+
class UserPass extends UserPassBase {
7782

7883
/**
7984
* Whether to turn on debugging
@@ -103,9 +108,9 @@ public function __construct($info, $config) {
103108

104109
/* Call the parent constructor first, as required by the interface. */
105110
parent::__construct($info, $config);
106-
107-
/* Get the configuration for this module */
108-
$drupalAuthConfig = new sspmod_drupalauth_ConfigHelper($config,
111+
112+
/* Get the configuration for this module */
113+
$drupalAuthConfig = new ConfigHelper($config,
109114
'Authentication source ' . var_export($this->authId, TRUE));
110115

111116
$this->debug = $drupalAuthConfig->getDebug();
@@ -125,7 +130,7 @@ public function __construct($info, $config) {
125130
* prevents the need for hackery here and makes this module work better in different environments.
126131
*/
127132
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
128-
133+
129134
// we need to be able to call Drupal user function so we load some required modules
130135
drupal_load('module', 'system');
131136
drupal_load('module', 'user');
@@ -154,51 +159,51 @@ protected function login($username, $password) {
154159
// authenticate the user
155160
$drupaluid = user_authenticate($username, $password);
156161
if(0 == $drupaluid){
157-
throw new SimpleSAML_Error_Error('WRONGUSERPASS');
162+
throw new \SimpleSAML\Error\Error('WRONGUSERPASS');
158163
}
159164

160165
// load the user object from Drupal
161166
$drupaluser = user_load($drupaluid);
162167

163168
// get all the attributes out of the user object
164169
$userAttrs = get_object_vars($drupaluser);
165-
170+
166171
// define some variables to use as arrays
167172
$userAttrNames = null;
168173
$attributes = null;
169-
174+
170175
// figure out which attributes to include
171176
if(NULL == $this->attributes){
172177
$userKeys = array_keys($userAttrs);
173-
178+
174179
// populate the attribute naming array
175180
foreach($userKeys as $userKey){
176181
$userAttrNames[$userKey] = $userKey;
177182
}
178-
183+
179184
}else{
180185
// populate the array of attribute keys
181186
// populate the attribute naming array
182187
foreach($this->attributes as $confAttr){
183-
188+
184189
$userKeys[] = $confAttr['drupaluservar'];
185190
$userAttrNames[$confAttr['drupaluservar']] = $confAttr['callit'];
186-
191+
187192
}
188-
193+
189194
}
190-
195+
191196
// an array of the keys that should never be included
192197
// (e.g., pass)
193198
$skipKeys = array('pass');
194199

195-
// package up the user attributes
200+
// package up the user attributes
196201
foreach($userKeys as $userKey){
197202

198203
// skip any keys that should never be included
199204
if(!in_array($userKey, $skipKeys)){
200205

201-
if( is_string($userAttrs[$userKey])
206+
if( is_string($userAttrs[$userKey])
202207
|| is_numeric($userAttrs[$userKey])
203208
|| is_bool($userAttrs[$userKey]) ){
204209

@@ -217,7 +222,7 @@ protected function login($username, $password) {
217222

218223
}
219224
}
220-
225+
221226
return $attributes;
222227
}
223228

lib/ConfigHelper.php renamed to src/ConfigHelper.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<?php
22

3+
namespace SimpleSAML\Module\drupalauth;
4+
35
/**
46
* Drupal authentication source configuration parser.
57
*
6-
* Copyright SIL International, Steve Moitozo, <[email protected]>, http://www.sil.org
8+
* Copyright SIL International, Steve Moitozo, <[email protected]>, http://www.sil.org
79
*
810
* This class is a Drupal authentication source which authenticates users
911
* against a Drupal site located on the same server.
@@ -18,7 +20,7 @@
1820
* @package drupalauth
1921
* @version $Id$
2022
*/
21-
class sspmod_drupalauth_ConfigHelper {
23+
class ConfigHelper {
2224

2325

2426
/**
@@ -79,7 +81,7 @@ public function __construct($config, $location) {
7981
$this->location = $location;
8082

8183
/* Parse configuration. */
82-
$config = SimpleSAML_Configuration::loadFromArray($config, $location);
84+
$config = \SimpleSAML\Configuration::loadFromArray($config, $location);
8385

8486
$this->drupalroot = $config->getString('drupalroot');
8587
$this->debug = $config->getBoolean('debug', FALSE);
@@ -89,15 +91,15 @@ public function __construct($config, $location) {
8991
$this->drupal_login_url = $config->getString('drupal_login_url', NULL);
9092

9193
}
92-
94+
9395

9496
/**
9597
* Return the debug
9698
*
9799
* @param boolean $debug whether or not debugging should be turned on
98100
*/
99101
public function getDebug() {
100-
return $this->debug;
102+
return $this->debug;
101103
}
102104

103105
/**
@@ -106,7 +108,7 @@ public function getDebug() {
106108
* @param string $drupalroot the directory of the Drupal site
107109
*/
108110
public function getDrupalroot() {
109-
return $this->drupalroot;
111+
return $this->drupalroot;
110112
}
111113

112114
/**

0 commit comments

Comments
 (0)