46
46
* // Whether to turn on debug
47
47
* 'debug' => true,
48
48
*
49
- * // Cookie name.
50
- * 'cookie_name' => 'drupalauth4ssp'
51
- *
52
49
* // URL of the Drupal logout page.
53
50
* 'drupal_logout_url' => 'https://www.example.com/drupal7/user/logout',
54
51
*
78
75
class External extends Source
79
76
{
80
77
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
+ /**
82
94
* Configuration object.
83
95
*
84
96
* @var \SimpleSAML\Module\drupalauth\ConfigHelper
@@ -114,46 +126,14 @@ public function __construct($info, $config)
114
126
*
115
127
* @return array|NULL The user's attributes, or NULL if the user isn't authenticated.
116
128
*/
117
- private function getUser ()
129
+ private function getUser ($ drupaluid )
118
130
{
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
-
154
131
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 );
157
137
if ($ drupaluser ->isBlocked ()) {
158
138
throw new Error ('NOACCESS ' );
159
139
}
@@ -173,7 +153,7 @@ public function authenticate(&$state)
173
153
{
174
154
assert (is_array ($ state ));
175
155
176
- $ attributes = $ this ->getUser ();
156
+ $ attributes = $ this ->getUser ($ state [ self :: DRUPALAUTH_EXTERNAL_USER_ID ] );
177
157
if ($ attributes !== null ) {
178
158
/*
179
159
* The user is already authenticated.
@@ -194,7 +174,7 @@ public function authenticate(&$state)
194
174
* First we add the identifier of this authentication source
195
175
* to the state array, so that we know where to resume.
196
176
*/
197
- $ state [' drupalauth:AuthID ' ] = $ this ->getAuthId ();
177
+ $ state [self :: DRUPALAUTH_AUTH_ID ] = $ this ->getAuthId ();
198
178
199
179
/*
200
180
* We need to save the $state-array, so that we can resume the
@@ -209,7 +189,7 @@ public function authenticate(&$state)
209
189
* and restores it in another location, and thus bypasses steps in
210
190
* the authentication process.
211
191
*/
212
- $ stateId = State::saveState ($ state , ' drupalauth:External ' );
192
+ $ stateId = State::saveState ($ state , self :: DRUPALAUTH_EXTERNAL );
213
193
214
194
/*
215
195
* Now we generate a URL the user should return to after authentication.
@@ -253,33 +233,33 @@ public function authenticate(&$state)
253
233
*
254
234
* @param array &$state The authentication state.
255
235
*/
256
- public static function resume ()
236
+ public static function resume ($ stateID )
257
237
{
258
238
/*
259
239
* First we need to restore the $state-array. We should have the identifier for
260
240
* it in the 'State' request parameter.
261
241
*/
262
- if (!isset ($ _REQUEST [ ' State ' ] )) {
242
+ if (!isset ($ stateID )) {
263
243
throw new BadRequest ('Missing "State" parameter. ' );
264
244
}
265
245
266
246
/*
267
247
* Once again, note the second parameter to the loadState function. This must
268
248
* match the string we used in the saveState-call above.
269
249
*/
270
- $ state = State::loadState ($ _REQUEST [ ' State ' ], ' drupalauth:External ' );
250
+ $ state = State::loadState ($ stateID , self :: DRUPALAUTH_EXTERNAL );
271
251
272
252
/*
273
253
* Now we have the $state-array, and can use it to locate the authentication
274
254
* source.
275
255
*/
276
- $ source = Source::getById ($ state [' drupalauth:AuthID ' ]);
256
+ $ source = Source::getById ($ state [self :: DRUPALAUTH_AUTH_ID ]);
277
257
if ($ source === null ) {
278
258
/*
279
259
* The only way this should fail is if we remove or rename the authentication source
280
260
* while the user is at the login page.
281
261
*/
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 ]);
283
263
}
284
264
285
265
/*
@@ -291,12 +271,12 @@ public static function resume()
291
271
throw new Exception ('Authentication source type changed. ' );
292
272
}
293
273
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 ] );
300
280
if ($ attributes === null ) {
301
281
/*
302
282
* The user isn't authenticated.
@@ -336,11 +316,6 @@ public function logout(&$state)
336
316
session_start ();
337
317
}
338
318
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
-
344
319
$ logout_url = $ this ->config ->getDrupalLogoutURL ();
345
320
$ parameters = [];
346
321
if (!empty ($ state ['ReturnTo ' ])) {
0 commit comments