From 108fd6fd8becaf398dd81721c5d29c479543df8e Mon Sep 17 00:00:00 2001 From: briskt <3172830+briskt@users.noreply.github.com> Date: Wed, 19 Jun 2024 14:52:51 +0800 Subject: [PATCH] update UserPass.php with latest from SSP library --- development/UserPass.php | 69 ++++++++++++++++++++++++++-------------- 1 file changed, 46 insertions(+), 23 deletions(-) diff --git a/development/UserPass.php b/development/UserPass.php index aab070cf..d30204a8 100644 --- a/development/UserPass.php +++ b/development/UserPass.php @@ -1,24 +1,39 @@ :", * while the value of each element is a new array with the attributes for each user. + * + * @var array */ - private $users; + private array $users; + /** * Constructor for this authentication source. @@ -26,51 +41,62 @@ class UserPass extends \SimpleSAML\Module\core\Auth\UserPassBase * @param array $info Information about this authentication source. * @param array $config Configuration. */ - public function __construct($info, $config) + public function __construct(array $info, array $config) { - assert(is_array($info)); - assert(is_array($config)); - // Call the parent constructor first, as required by the interface parent::__construct($info, $config); $this->users = []; + // Old version of SimpleSAMLphp had the username:password just be a list in the top level + // configuration. We now have them under the "users" key, so that exampleauth can be used + // for testing things like core:loginpage_links, etc. that require top level configuration. + if (array_key_exists('users', $config)) { + $config_users = $config['users']; + } else { + Logger::warning("Module exampleauth:UserPass configured in legacy mode. Please put your " . + "username:password entries under the \"users\" key in your authsource."); + $config_users = $config; + } + // Validate and parse our configuration - foreach ($config as $userpass => $attributes) { + foreach ($config_users as $userpass => $attributes) { if (!is_string($userpass)) { - throw new \Exception( - 'Invalid : for authentication source '.$this->authId.': '.$userpass + throw new Exception( + 'Invalid : for authentication source ' . $this->authId . ': ' . $userpass ); } $userpass = explode(':', $userpass, 2); if (count($userpass) !== 2) { - throw new \Exception( - 'Invalid : for authentication source '.$this->authId.': '.$userpass[0] + throw new Exception( + 'Invalid : for authentication source ' . $this->authId . ': ' . $userpass[0] ); } $username = $userpass[0]; $password = $userpass[1]; -// $attrUtils = new \SimpleSAML\Utils\Attributes(); + // GTIS begin +// $attrUtils = new Utils\Attributes(); // // try { // $attributes = $attrUtils->normalizeAttributesArray($attributes); -// } catch (\Exception $e) { -// throw new \Exception('Invalid attributes for user '.$username. +// } catch (Exception $e) { +// throw new Exception('Invalid attributes for user '.$username. // ' in authentication source '.$this->authId.': '.$e->getMessage()); // } - $this->users[$username.':'.$password] = $attributes; + // GTIS end + $this->users[$username . ':' . $password] = $attributes; } } + /** * Attempt to log in using the given username and password. * * On a successful login, this function should return the users attributes. On failure, * it should throw an exception. If the error was caused by the user entering the wrong - * username or password, a \SimpleSAML\Error\Error('WRONGUSERPASS') should be thrown. + * username or password, a \SimpleSAML\Error\Error(\SimpleSAML\Error\ErrorCodes::WRONGUSERPASS) should be thrown. * * Note that both the username and the password are UTF-8 encoded. * @@ -78,14 +104,11 @@ public function __construct($info, $config) * @param string $password The password the user wrote. * @return array Associative array with the users attributes. */ - protected function login($username, $password) + protected function login(string $username, string $password): array { - assert(is_string($username)); - assert(is_string($password)); - - $userpass = $username.':'.$password; + $userpass = $username . ':' . $password; if (!array_key_exists($userpass, $this->users)) { - throw new \SimpleSAML\Error\Error('WRONGUSERPASS'); + throw new Error\Error(Error\ErrorCodes::WRONGUSERPASS); } return $this->users[$userpass];