Skip to content

Commit 056aec6

Browse files
committed
Don't use cookie
1 parent 71cb642 commit 056aec6

File tree

4 files changed

+48
-136
lines changed

4 files changed

+48
-136
lines changed

README.md

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,6 @@ Configure the authentication source by putting following code into `simplesamlph
7171
// Whether to turn on debug
7272
'debug' => true,
7373

74-
// Cookie name. Set this to use a cache-busting cookie pattern
75-
// (e.g. 'SESSdrupalauth4ssp') if hosted on Pantheon so that the cookie
76-
// is is not stripped away by Varnish. See https://pantheon.io/docs/cookies#cache-busting-cookies .
77-
'cookie_name' => 'drupalauth4ssp',
78-
7974
// Which attributes should be retrieved from the Drupal site.
8075
'attributes' => array(
8176
array('field_name' => 'uid', 'attribute_name' => 'uid'),
@@ -108,11 +103,6 @@ Configure the authentication source by putting following code into `simplesamlph
108103
// Whether to turn on debug
109104
'debug' => true,
110105

111-
// Cookie name. Set this to use a cache-busting cookie pattern
112-
// (e.g. 'SESSdrupalauth4ssp') if hosted on Pantheon so that the cookie
113-
// is is not stripped away by Varnish. See https://pantheon.io/docs/cookies#cache-busting-cookies .
114-
'cookie_name' => 'drupalauth4ssp',
115-
116106
// the URL of the Drupal logout page
117107
'drupal_logout_url' => 'https://www.example.com/drupal/user/logout',
118108

lib/Auth/Source/External.php

Lines changed: 36 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@
4646
* // Whether to turn on debug
4747
* 'debug' => true,
4848
*
49-
* // Cookie name.
50-
* 'cookie_name' => 'drupalauth4ssp'
51-
*
5249
* // URL of the Drupal logout page.
5350
* 'drupal_logout_url' => 'https://www.example.com/drupal7/user/logout',
5451
*
@@ -78,7 +75,22 @@
7875
class External extends Source
7976
{
8077

81-
/**
78+
/**
79+
* The string used to identify Drupal user ID.
80+
*/
81+
const DRUPALAUTH_EXTERNAL_USER_ID = 'drupalauth:External:UserID';
82+
83+
/**
84+
* The string used to identify authentication source.
85+
*/
86+
const DRUPALAUTH_AUTH_ID = 'drupalauth:AuthID';
87+
88+
/**
89+
* The string used to identify our states.
90+
*/
91+
const DRUPALAUTH_EXTERNAL = 'drupalauth:External';
92+
93+
/**
8294
* Configuration object.
8395
*
8496
* @var \SimpleSAML\Module\drupalauth\ConfigHelper
@@ -114,46 +126,14 @@ public function __construct($info, $config)
114126
*
115127
* @return array|NULL The user's attributes, or NULL if the user isn't authenticated.
116128
*/
117-
private function getUser()
129+
private function getUser($drupaluid)
118130
{
119-
120-
$drupaluid = null;
121-
122-
// Pull the Drupal UID out of the cookie.
123-
$cookie_name = $this->config->getCookieName();
124-
if (isset($_COOKIE[$cookie_name]) && $_COOKIE[$cookie_name]) {
125-
$strCookie = $_COOKIE[$cookie_name];
126-
list($cookie_hash, $uid) = explode(':', $strCookie);
127-
128-
// make sure the hash matches
129-
// make sure the UID is passed
130-
if ((isset($cookie_hash) && !empty($cookie_hash)) && (isset($uid) && !empty($uid))) {
131-
$drupalHelper = new DrupalHelper();
132-
$drupalHelper->bootDrupal($this->config->getDrupalroot());
133-
134-
// Make sure no one manipulated the hash or the uid in the cookie before we trust the uid
135-
$hash = Crypt::hmacBase64(
136-
$uid,
137-
$this->config->getCookieSalt() . \Drupal::service('private_key')->get()
138-
);
139-
if (!hash_equals($hash, $cookie_hash)) {
140-
throw new Exception(
141-
'Cookie hash invalid. This indicates either tampering or an out of date drupal4ssp module.'
142-
);
143-
}
144-
$drupaluid = $uid;
145-
}
146-
}
147-
148-
149-
// Delete the cookie, we don't need it anymore
150-
if (isset($_COOKIE[$cookie_name])) {
151-
setcookie($cookie_name, "", time() - 3600, $this->config->getCookiePath());
152-
}
153-
154131
if (!empty($drupaluid)) {
155-
// Load the user object from Drupal.
156-
$drupaluser = User::load($uid);
132+
$drupalHelper = new DrupalHelper();
133+
$drupalHelper->bootDrupal($this->config->getDrupalroot());
134+
135+
// Load the user object from Drupal.
136+
$drupaluser = User::load($drupaluid);
157137
if ($drupaluser->isBlocked()) {
158138
throw new Error('NOACCESS');
159139
}
@@ -173,7 +153,7 @@ public function authenticate(&$state)
173153
{
174154
assert(is_array($state));
175155

176-
$attributes = $this->getUser();
156+
$attributes = $this->getUser($state[self::DRUPALAUTH_EXTERNAL_USER_ID]);
177157
if ($attributes !== null) {
178158
/*
179159
* The user is already authenticated.
@@ -194,7 +174,7 @@ public function authenticate(&$state)
194174
* First we add the identifier of this authentication source
195175
* to the state array, so that we know where to resume.
196176
*/
197-
$state['drupalauth:AuthID'] = $this->getAuthId();
177+
$state[self::DRUPALAUTH_AUTH_ID] = $this->getAuthId();
198178

199179
/*
200180
* We need to save the $state-array, so that we can resume the
@@ -209,7 +189,7 @@ public function authenticate(&$state)
209189
* and restores it in another location, and thus bypasses steps in
210190
* the authentication process.
211191
*/
212-
$stateId = State::saveState($state, 'drupalauth:External');
192+
$stateId = State::saveState($state, self::DRUPALAUTH_EXTERNAL);
213193

214194
/*
215195
* Now we generate a URL the user should return to after authentication.
@@ -253,33 +233,33 @@ public function authenticate(&$state)
253233
*
254234
* @param array &$state The authentication state.
255235
*/
256-
public static function resume()
236+
public static function resume($stateID)
257237
{
258238
/*
259239
* First we need to restore the $state-array. We should have the identifier for
260240
* it in the 'State' request parameter.
261241
*/
262-
if (!isset($_REQUEST['State'])) {
242+
if (!isset($stateID)) {
263243
throw new BadRequest('Missing "State" parameter.');
264244
}
265245

266246
/*
267247
* Once again, note the second parameter to the loadState function. This must
268248
* match the string we used in the saveState-call above.
269249
*/
270-
$state = State::loadState($_REQUEST['State'], 'drupalauth:External');
250+
$state = State::loadState($stateID, self::DRUPALAUTH_EXTERNAL);
271251

272252
/*
273253
* Now we have the $state-array, and can use it to locate the authentication
274254
* source.
275255
*/
276-
$source = Source::getById($state['drupalauth:AuthID']);
256+
$source = Source::getById($state[self::DRUPALAUTH_AUTH_ID]);
277257
if ($source === null) {
278258
/*
279259
* The only way this should fail is if we remove or rename the authentication source
280260
* while the user is at the login page.
281261
*/
282-
throw new Exception('Could not find authentication source with ID: ' . $state['drupalauth:AuthID']);
262+
throw new Exception('Could not find authentication source with ID: ' . $state[self::DRUPALAUTH_AUTH_ID]);
283263
}
284264

285265
/*
@@ -291,12 +271,12 @@ public static function resume()
291271
throw new Exception('Authentication source type changed.');
292272
}
293273

294-
/*
295-
* OK, now we know that our current state is sane. Time to actually log the user in.
296-
*
297-
* First we check that the user is acutally logged in, and didn't simply skip the login page.
298-
*/
299-
$attributes = $source->getUser();
274+
/*
275+
* OK, now we know that our current state is sane. Time to actually log the user in.
276+
*
277+
* First we check that the user is acutally logged in, and didn't simply skip the login page.
278+
*/
279+
$attributes = $source->getUser($state[self::DRUPALAUTH_EXTERNAL_USER_ID]);
300280
if ($attributes === null) {
301281
/*
302282
* The user isn't authenticated.
@@ -336,11 +316,6 @@ public function logout(&$state)
336316
session_start();
337317
}
338318

339-
// Added armor plating, just in case.
340-
if (isset($_COOKIE[$this->config->getCookieName()])) {
341-
setcookie($this->config->getCookieName(), "", time() - 3600, $this->config->getCookiePath());
342-
}
343-
344319
$logout_url = $this->config->getDrupalLogoutURL();
345320
$parameters = [];
346321
if (!empty($state['ReturnTo'])) {

lib/ConfigHelper.php

Lines changed: 11 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,6 @@ class ConfigHelper
3939
private $attributes;
4040

4141

42-
/**
43-
* The name of the cookie
44-
*/
45-
private $cookie_name;
46-
47-
48-
/**
49-
* Cookie path.
50-
*/
51-
private $cookie_path;
52-
53-
54-
/**
55-
* Cookie salt.
56-
*/
57-
private $cookie_salt;
58-
59-
6042
/**
6143
* The Drupal logout URL
6244
*/
@@ -75,28 +57,22 @@ class ConfigHelper
7557
* @param array $config Configuration.
7658
* @param string $location The location of this configuration. Used for error reporting.
7759
*/
78-
public function __construct($config, $location)
79-
{
80-
assert(is_array($config));
81-
assert(is_string($location));
82-
83-
$this->location = $location;
60+
public function __construct($config, $location) {
61+
assert(is_array($config));
62+
assert(is_string($location));
8463

85-
/* Get authsource configuration. */
86-
$config = Configuration::loadFromArray($config, $location);
64+
$this->location = $location;
8765

88-
$this->drupalroot = $config->getString('drupalroot');
89-
$this->debug = $config->getBoolean('debug', false);
90-
$this->attributes = $config->getArray('attributes', []);
91-
$this->cookie_name = $config->getString('cookie_name', 'drupalauth4ssp');
92-
$this->drupal_logout_url = $config->getString('drupal_logout_url', null);
93-
$this->drupal_login_url = $config->getString('drupal_login_url', null);
66+
/* Get authsource configuration. */
67+
$config = Configuration::loadFromArray($config, $location);
9468

95-
$this->cookie_path = Configuration::getInstance()->getBasePath();
96-
$this->cookie_salt = Config::getSecretSalt();
69+
$this->drupalroot = $config->getString('drupalroot');
70+
$this->debug = $config->getBoolean('debug', FALSE);
71+
$this->attributes = $config->getArray('attributes', []);
72+
$this->drupal_logout_url = $config->getString('drupal_logout_url', NULL);
73+
$this->drupal_login_url = $config->getString('drupal_login_url', NULL);
9774
}
9875

99-
10076
/**
10177
* Returns debug mode.
10278
*
@@ -127,35 +103,6 @@ public function getAttributes()
127103
return $this->attributes;
128104
}
129105

130-
/**
131-
* Returns cookie name.
132-
*
133-
* @return string
134-
*/
135-
public function getCookieName()
136-
{
137-
return $this->cookie_name;
138-
}
139-
140-
/**
141-
* Returns cookie path.
142-
*
143-
* @return string
144-
*/
145-
public function getCookiePath()
146-
{
147-
return $this->cookie_path;
148-
}
149-
150-
/**
151-
* Returns cookie salt.
152-
*
153-
* @return string
154-
*/
155-
public function getCookieSalt()
156-
{
157-
return $this->cookie_salt;
158-
}
159106

160107
/**
161108
* Returns Drupal logout URL.

www/resume.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99

1010
use SimpleSAML\Module\drupalauth\Auth\Source\External;
1111

12-
External::resume();
12+
External::resume($_REQUEST['State']);

0 commit comments

Comments
 (0)