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 *
7875class 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 ' ])) {
0 commit comments