3 namespace SimpleSAML\Module\fsfdrupalauth\Auth\Source
;
12 * Extension of simple SQL authentication source
14 * @package SimpleSAMLphp
17 class FSFDrupalAuth
extends \SimpleSAML\Module\core\Auth\UserPassBase
20 * The DSN we should connect to.
25 * The username we should connect to the database with.
30 * The password we should connect to the database with.
35 * The options that we should connect to the database with.
40 * The query we should use to retrieve the attributes for the user.
42 * The username and password will be available as :username and :password.
45 private $query_membership;
48 private $query_nomination_process_donations;
49 private $query_nomination_process_gift_receipt;
50 private $query_nomination_process_adhoc;
52 private $query_discussion_process_old_membership;
53 private $query_discussion_process_donations;
54 private $query_discussion_process_adhoc;
57 * SQL query parameters, or variables that help determine which attributes
61 private $gift_redeem_page_id;
63 private $nomination_process_active;
64 private $nomination_process_contrib_start_date;
65 private $nomination_process_contrib_end_date;
66 private $nomination_process_adhoc_access_group_id;
67 private $membership_monthly_rate;
68 private $student_membership_monthly_rate;
70 private $discussion_process_active;
71 private $discussion_process_contrib_start_date;
72 private $discussion_process_contrib_end_date;
73 private $discussion_process_adhoc_access_group_id;
74 private $discussion_process_adhoc_no_access_group_id;
75 private $discussion_process_donation_amount;
77 private $discussion_moderator_access_group_id;
80 * Constructor for this authentication source.
82 * @param array $info Information about this authentication source.
83 * @param array $config Configuration.
85 public function __construct($info, $config)
87 assert(is_array($info));
88 assert(is_array($config));
90 // Call the parent constructor first, as required by the interface
91 parent
::__construct($info, $config);
93 // Make sure that all required parameters are present.
102 'query_nomination_process_donations',
103 'query_nomination_process_gift_receipt',
104 'query_nomination_process_adhoc',
107 'gift_redeem_page_id',
109 'nomination_process_active',
110 'nomination_process_contrib_start_date',
111 'nomination_process_contrib_end_date',
112 'nomination_process_adhoc_access_group_id',
113 'membership_monthly_rate',
114 'student_membership_monthly_rate',
116 'query_discussion_process_old_membership',
117 'query_discussion_process_donations',
118 'query_discussion_process_adhoc',
120 'discussion_process_active',
121 'discussion_process_contrib_start_date',
122 'discussion_process_contrib_end_date',
123 'discussion_process_adhoc_access_group_id',
124 'discussion_process_adhoc_no_access_group_id',
125 'discussion_process_donation_amount',
127 'discussion_moderator_access_group_id',]
130 if (!array_key_exists($param, $config)) {
131 throw new Exception('Missing required attribute \''.$param.
132 '\' for authentication source '.$this->authId
);
135 if (!is_string($config[$param])) {
136 throw new Exception('Expected parameter \''.$param.
137 '\' for authentication source '.$this->authId
.
138 ' to be a string. Instead it was: '.
139 var_export($config[$param], true));
142 $this->$param = $config[$param];
145 if (isset($config['options'])) {
146 $this->options
= $config['options'];
152 * Create a database connection.
154 * @return PDO The database connection.
156 private function connect()
159 $db = new PDO($this->dsn
, $this->username
, $this->password
, $this->options
);
160 } catch (PDOException
$e) {
161 // Obfuscate the password if it's part of the dsn
162 $obfuscated_dsn = preg_replace('/(user|password)=(.*?([;]|$))/', '${1}=***', $this->dsn
);
164 throw new Exception('fsfdrupalauth:' . $this->authId
. ': - Failed to connect to \'' .
165 $obfuscated_dsn . '\': ' . $e->getMessage());
168 $db->setAttribute(PDO
::ATTR_ERRMODE
, PDO
::ERRMODE_EXCEPTION
);
170 $driver = explode(':', $this->dsn
, 2);
171 $driver = strtolower($driver[0]);
173 // Driver specific initialization
177 $db->exec("SET NAMES 'utf8mb4'");
181 $db->exec("SET NAMES 'UTF8'");
189 * Check the password against a Drupal hash
192 private function check_password($password, $hash) {
195 // The reason for running a separate process is so that the PHP global
196 // env doesn't get clobbered by include / require.
199 // pipes code based off of https://www.php.net/manual/en/function.proc-open.php
200 // CC-BY 3.0 or later
201 $descriptorspec = array(
202 0 => array("pipe", "r"), // stdin is a pipe that the child may read from
203 1 => array("pipe", "w"), // stdout is a pipe that the child may write to
204 2 => array("pipe", "w") // stderr is a pipe that the child may write to
207 $cwd = "../modules/fsfdrupalauth/extlib";
208 //$env = array('some_option' => 'aeiou');
211 $process = proc_open('php drupal-pw-check.php', $descriptorspec, $pipes, $cwd, $env);
213 if (is_resource($process)) {
214 // $pipes now looks like this:
215 // 0 => writeable handle connected to child stdin
216 // 1 => readable handle connected to child stdout
218 fwrite($pipes[0], json_encode([$password, $hash]));
221 $result = stream_get_contents($pipes[1]);
224 $errors = stream_get_contents($pipes[2]);
227 // It is important that you close any pipes before calling
228 // proc_close in order to avoid a deadlock
229 $return_value = proc_close($process);
231 //Logger::debug('fsfdrupalauth:'.$this->authId.': authenticator stdout: '.$result);
233 $errors_found_yet = false;
235 Logger
::error('fsfdrupalauth:'.$this->authId
.': authenticator stderr: '.$errors);
236 $errors_found_yet = true;
239 if ($return_value != 0) {
240 Logger
::error('fsfdrupalauth:'.$this->authId
.': authenticator non-zero return code: '.$return_value);
241 $errors_found_yet = true;
244 return (!$errors_found_yet && is_string($result) && rtrim($result) == "true");
248 Logger
::error('fsfdrupalauth:'.$this->authId
.': unable to launch authenticator');
256 * query the database with arbitrary queries that only require a user name.
259 private function query_db($queryname, $query_params)
261 assert(is_string($queryname));
262 assert(is_string($username));
264 $db = $this->connect();
267 $sth = $db->prepare($this->$queryname);
268 } catch (PDOException
$e) {
269 throw new Exception('fsfdrupalauth:'.$this->authId
.
270 ': - Failed to prepare queryname: '.$queryname.': '.$e->getMessage());
274 $sth->execute($query_params);
275 } catch (PDOException
$e) {
276 throw new Exception('fsfdrupalauth:'.$this->authId
.
277 ': - Failed to execute queryname: '.$queryname.': '.$e->getMessage());
281 $data = $sth->fetchAll(PDO
::FETCH_ASSOC
);
282 } catch (PDOException
$e) {
283 throw new Exception('fsfdrupalauth:'.$this->authId
.
284 ': - Failed to fetch result set: '.$e->getMessage());
287 Logger
::info('fsfdrupalauth:'.$this->authId
.': Got '.count($data).
288 ' rows from database');
294 * add more CAS attributes to user, such as is_staff and is_member
296 private function add_more_attributes(&$attributes, $username) {
302 $staff_data = $this->query_db('query_staff', ['username' => $username, 'fsf_org_id' => $this->fsf_org_id
]);
304 if (count($staff_data) === 0) {
305 // No rows returned - invalid username
306 Logger
::debug('fsfdrupalauth:'.$this->authId
.
307 ': No rows in result set. Probably not FSF staff.');
310 $attributes['is_fsf_staff'] = ['false'];
312 foreach ($staff_data as $row) {
313 foreach ($row as $key => $value) {
315 if ($value === null) {
318 $value = (string) $value;
320 if (strtolower($value) === strtolower($username)) {
322 $attributes['is_fsf_staff'] = ['true'];
329 // query on membership
332 $membership_data = $this->query_db('query_membership', ['username' => $username]);
334 if (count($membership_data) === 0) {
335 // No rows returned - invalid username
336 Logger
::debug('fsfdrupalauth:'.$this->authId
.
337 ': No rows in result set. Probably no membership.');
340 $attributes['is_member'] = ['false'];
341 $attributes['was_member'] = ['false'];
343 foreach ($membership_data as $row) {
344 foreach ($row as $key => $value) {
345 if ($value === null) {
348 $value = (string) $value;
350 if ($value === '1' ||
$value === '2' ||
$value === '3') {
351 $attributes['is_member'] = ['true'];
352 $attributes['was_member'] = ['true'];
353 } elseif ($value === '4') {
354 $attributes['was_member'] = ['true'];
360 // helper functions for access to board nomination / discussion process
364 * @param string $query_name Name of query in authsources
365 * @param array $extra_params Associative array of parameters to include in query
367 $donation_query = function ($query_name, $extra_params)
370 $parameters = ['username' => $username];
372 foreach ($extra_params as $key => $value) {
373 $parameters[$key] = $value;
376 return $this->query_db($query_name, $parameters);
379 $old_membership_query = $donation_query;
381 $compare_res = function ($result, $amount) {
382 foreach ($result[0] as $key => $value) {
383 if (intval($value) >= $amount) {
390 // set dates here, used by helper functions below
391 $nomination_process_start_date = $this->nomination_process_contrib_start_date
;
392 $nomination_process_end_date = $this->nomination_process_contrib_end_date
;
393 $discussion_process_start_date = $this->discussion_process_contrib_start_date
;
394 $discussion_process_end_date = $this->discussion_process_contrib_end_date
;
397 // looks for memberships / comparable donations in time window. also
398 // looks for a membership or donation (included as a param) that
399 // occurred up to a year before, and that would have carried over into
400 // the time window with a single donation. this approximates whether
401 // the person was, or would have been, a member during the configured
403 $nomination_process_analyze_history = function ($selective_donations_history)
404 use ($nomination_process_start_date, $nomination_process_end_date) {
408 Logger
::debug('fsfdrupalauth:'.$this->authId
.
409 ': start date: '.$nomination_process_start_date. " end date: ".$nomination_process_end_date);
411 $start_date_obj = new \
DateTime($nomination_process_start_date);
412 $end_date_obj = new \
DateTime($nomination_process_end_date);
414 foreach ($selective_donations_history as $row) {
416 $amount = intval($row['amount']);
417 $member_type_id = $row['member_type_id'];
418 $receive_date_obj = new \
DateTime($row['receive_date']);
423 } elseif ($receive_date_obj >= $start_date_obj and $receive_date_obj <= $end_date_obj) {
426 } elseif ($receive_date_obj < $start_date_obj) {
427 switch ($member_type_id) {
430 $rate = intval($this->student_membership_monthly_rate
);
436 $rate = intval($this->membership_monthly_rate
);
439 $membership_end_date_obj = new \
DateTime($row['receive_date']);
440 $membership_end_date_obj->add(new \
DateInterval("P" . ceil($amount / $rate) . "M"));
442 if ($membership_end_date_obj >= $start_date_obj) {
450 $discussion_process_analyze_history = function ($selective_donations_history)
451 use ($discussion_process_start_date, $discussion_process_end_date) {
456 Logger
::debug('fsfdrupalauth:'.$this->authId
.
457 ': start date: '.$discussion_process_start_date. " end date: ".$discussion_process_end_date);
459 $start_date_obj = new \
DateTime($discussion_process_start_date);
460 $end_date_obj = new \
DateTime($discussion_process_end_date);
462 foreach ($selective_donations_history as $row) {
464 $amount = intval($row['amount']);
465 $member_type_id = $row['member_type_id'];
466 $receive_date_obj = new \
DateTime($row['receive_date']);
468 if (($receive_date_obj > $start_date_obj) && ($receive_date_obj < $end_date_obj)) {
473 Logger
::debug('fsfdrupalauth:'.$this->authId
.
474 ': total amount: $'.$total);
476 if ($total >= $this->discussion_process_donation_amount
) {
484 // nomination form participation specific checks
487 $donation_params = ['start_date' => $nomination_process_start_date, 'end_date' => $nomination_process_end_date];
488 $gift_member_params = ['start_date' => $nomination_process_start_date, 'end_date' => $nomination_process_end_date, 'gift_redeem_page_id' => intval($this->gift_redeem_page_id
)];
489 $adhoc_params = ['adhoc_access_group_id' => intval($this->nomination_process_adhoc_access_group_id
)];
491 if ($this->nomination_process_active
!= 'true' ) {
492 Logger
::debug('fsfdrupalauth:'.$this->authId
.': Nomination board process checks not active');
493 $attributes['nomination_process'] = ['false'];
495 } elseif ($compare_res($donation_query('query_nomination_process_adhoc', $adhoc_params), 1)) {
496 Logger
::debug('fsfdrupalauth:'.$this->authId
.': In adhoc list of contacts for nomination board process');
497 $attributes['nomination_process'] = ['true'];
499 } elseif ($attributes['is_member'] != ['true']) {
500 Logger
::debug('fsfdrupalauth:'.$this->authId
.': Not a current member for nomination board process');
501 $attributes['nomination_process'] = ['false'];
503 } elseif ($nomination_process_analyze_history($donation_query('query_nomination_process_donations', $donation_params))
504 ||
$compare_res($donation_query('query_nomination_process_gift_receipt', $gift_member_params), 1)) {
506 Logger
::debug('fsfdrupalauth:'.$this->authId
.': Past membership / donations meet threshold for nomination board process');
507 $attributes['nomination_process'] = ['true'];
510 Logger
::debug('fsfdrupalauth:'.$this->authId
.': Past membership / donations do not meet threshold for nomination board process');
511 $attributes['nomination_process'] = ['false'];
515 // discussion forum participation specific checks
518 $donation_params = ['start_date' => $discussion_process_start_date, 'end_date' => $discussion_process_end_date];
519 $old_member_params = $donation_params;
520 $adhoc_params = ['adhoc_access_group_id' => intval($this->discussion_process_adhoc_access_group_id
)];
521 $adhoc_params_no = ['adhoc_access_group_id' => intval($this->discussion_process_adhoc_no_access_group_id
)];
523 if ($this->discussion_process_active
!= 'true' ) {
524 Logger
::debug('fsfdrupalauth:'.$this->authId
.': Discussion board process checks not active');
525 $attributes['discussion_process'] = ['false'];
527 } elseif ($compare_res($donation_query('query_discussion_process_adhoc', $adhoc_params_no), 1)) {
528 Logger
::debug('fsfdrupalauth:'.$this->authId
.': Nominee not allowed to participate in board discussion process.');
529 $attributes['discussion_process'] = ['false'];
531 } elseif ($compare_res($donation_query('query_discussion_process_adhoc', $adhoc_params), 1)) {
532 Logger
::debug('fsfdrupalauth:'.$this->authId
.': In adhoc list of contacts for discussion board process');
533 $attributes['discussion_process'] = ['true'];
535 } elseif ($attributes['is_member'] != ['true']) {
536 Logger
::debug('fsfdrupalauth :'.$this->authId
.': Not a member, so not eligible for board nominee discussion process.');
537 $attributes['discussion_process'] = ['false'];
539 } elseif ($compare_res($old_membership_query('query_discussion_process_old_membership', $old_member_params), 1)
540 ||
$discussion_process_analyze_history($donation_query('query_discussion_process_donations', $donation_params))) {
542 Logger
::debug('fsfdrupalauth:'.$this->authId
.': Past membership / donations meet threshold for discussion board process');
543 $attributes['discussion_process'] = ['true'];
546 Logger
::debug('fsfdrupalauth:'.$this->authId
.': Past membership / donations do not meet threshold for discussion board process');
547 $attributes['discussion_process'] = ['false'];
551 // discussion forum moderator early access
554 $adhoc_params = ['adhoc_access_group_id' => intval($this->discussion_moderator_access_group_id
)];
556 if ($compare_res($donation_query('query_discussion_process_adhoc', $adhoc_params), 1)) {
557 Logger
::debug('fsfdrupalauth:'.$this->authId
.': In adhoc list of moderators for board discussion forum');
558 $attributes['discussion_moderator'] = ['true'];
561 Logger
::debug('fsfdrupalauth:'.$this->authId
.': Not in adhoc list of moderators for board discussion forum');
562 $attributes['discussion_moderator'] = ['false'];
566 // aggregate attribute
571 foreach ($attributes as $key => $value) {
572 if ($value == ['true']) {
574 $groups_list .= ', ';
576 $groups_list .= $key;
581 $attributes['groups_list'] = [$groups_list];
585 * Attempt to log in using the given username and password.
587 * On a successful login, this function should return the users attributes. On failure,
588 * it should throw an exception. If the error was caused by the user entering the wrong
589 * username or password, a Error\Error('WRONGUSERPASS') should be thrown.
591 * Note that both the username and the password are UTF-8 encoded.
593 * @param string $username The username the user wrote.
594 * @param string $password The password the user wrote.
595 * @return array Associative array with the users attributes.
597 protected function login($username, $password)
599 assert(is_string($username));
600 assert(is_string($password));
602 //// keep this commented when it's not in use. it prints user passwords to the log file
603 //Logger::debug('fsfdrupalauth:'.$this->authId.': entered password: '.$password);
606 $user_data = $this->query_db('query_main', ['username' => $username]);
609 if (count($user_data) === 0) {
610 // No rows returned - invalid username
611 Logger
::error('fsfdrupalauth:'.$this->authId
.
612 ': No rows in result set. Probably wrong username.');
613 throw new Error\
Error('WRONGUSERPASS');
616 /* Extract attributes. We allow the resultset to consist of multiple rows. Attributes
617 * which are present in more than one row will become multivalued. null values and
618 * duplicate values will be skipped. All values will be converted to strings.
622 // use the entered user name so we don't forcibly change it to all
623 // lower case. this is to preserve the behavior of the old cas server,
624 // and to remain compatible with our MW and Discourse sites that are
626 $attributes['name'][] = $username;
628 foreach ($user_data as $row) {
629 foreach ($row as $key => $value) {
630 if ($value === null) {
634 $value = (string) $value;
636 if (!array_key_exists($key, $attributes)) {
637 $attributes[$key] = [];
640 if (in_array($value, $attributes[$key], true)) {
641 // Value already exists in attribute
645 $attributes[$key][] = $value;
649 if (!$this->check_password($password, $attributes['pass'][0])) {
650 throw new Error\
Error('WRONGUSERPASS');
653 unset($attributes['pass']);
656 $this->add_more_attributes($attributes, $username);
659 Logger
::info('fsfdrupalauth:'.$this->authId
.': Attributes: '.
660 implode(',', array_keys($attributes)));