|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace SimpleSAML\Module\authcrypt\Auth\Source; |
| 4 | + |
| 5 | +/** |
| 6 | + * Authentication source for username & hashed password. |
| 7 | + * |
| 8 | + * This class is an authentication source which stores all username/hashes in an array, |
| 9 | + * and authenticates users against this array. |
| 10 | + * |
| 11 | + * @author Dyonisius Visser, TERENA. |
| 12 | + * @package SimpleSAMLphp |
| 13 | + */ |
| 14 | + |
| 15 | +class Hash extends \SimpleSAML\Module\core\Auth\UserPassBase |
| 16 | +{ |
| 17 | + /** |
| 18 | + * Our users, stored in an associative array. The key of the array is "<username>:<passwordhash>", |
| 19 | + * while the value of each element is a new array with the attributes for each user. |
| 20 | + */ |
| 21 | + private $users; |
| 22 | + |
| 23 | + |
| 24 | + /** |
| 25 | + * Constructor for this authentication source. |
| 26 | + * |
| 27 | + * @param array $info Information about this authentication source. |
| 28 | + * @param array $config Configuration. |
| 29 | + * |
| 30 | + * @throws Exception in case of a configuration error. |
| 31 | + */ |
| 32 | + public function __construct($info, $config) |
| 33 | + { |
| 34 | + assert(is_array($info)); |
| 35 | + assert(is_array($config)); |
| 36 | + |
| 37 | + // Call the parent constructor first, as required by the interface |
| 38 | + parent::__construct($info, $config); |
| 39 | + |
| 40 | + $this->users = []; |
| 41 | + |
| 42 | + // Validate and parse our configuration |
| 43 | + foreach ($config as $userpass => $attributes) { |
| 44 | + if (!is_string($userpass)) { |
| 45 | + throw new \Exception('Invalid <username>:<passwordhash> for authentication source '. |
| 46 | + $this->authId.': '.$userpass); |
| 47 | + } |
| 48 | + |
| 49 | + $userpass = explode(':', $userpass, 2); |
| 50 | + if (count($userpass) !== 2) { |
| 51 | + throw new \Exception('Invalid <username>:<passwordhash> for authentication source '. |
| 52 | + $this->authId.': '.$userpass[0]); |
| 53 | + } |
| 54 | + $username = $userpass[0]; |
| 55 | + $passwordhash = $userpass[1]; |
| 56 | + |
| 57 | + try { |
| 58 | + $attributes = \SimpleSAML\Utils\Attributes::normalizeAttributesArray($attributes); |
| 59 | + } catch (\Exception $e) { |
| 60 | + throw new \Exception('Invalid attributes for user '.$username. |
| 61 | + ' in authentication source '.$this->authId.': '. |
| 62 | + $e->getMessage()); |
| 63 | + } |
| 64 | + |
| 65 | + $this->users[$username.':'.$passwordhash] = $attributes; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | + /** |
| 71 | + * Attempt to log in using the given username and password. |
| 72 | + * |
| 73 | + * On a successful login, this function should return the users attributes. On failure, |
| 74 | + * it should throw an exception. If the error was caused by the user entering the wrong |
| 75 | + * username OR password, a \SimpleSAML\Error\Error('WRONGUSERPASS') should be thrown. |
| 76 | + * |
| 77 | + * The username is UTF-8 encoded, and the hash is base64 encoded. |
| 78 | + * |
| 79 | + * @param string $username The username the user wrote. |
| 80 | + * @param string $password The password the user wrote. |
| 81 | + * |
| 82 | + * @return array Associative array with the users attributes. |
| 83 | + * |
| 84 | + * @throws \SimpleSAML\Error\Error if authentication fails. |
| 85 | + */ |
| 86 | + protected function login($username, $password) |
| 87 | + { |
| 88 | + assert(is_string($username)); |
| 89 | + assert(is_string($password)); |
| 90 | + |
| 91 | + foreach ($this->users as $userpass => $attrs) { |
| 92 | + $matches = explode(':', $userpass, 2); |
| 93 | + if ($matches[0] === $username) { |
| 94 | + if (\SimpleSAML\Utils\Crypto::pwValid($matches[1], $password)) { |
| 95 | + return $attrs; |
| 96 | + } else { |
| 97 | + \SimpleSAML\Logger::debug('Incorrect password "'.$password.'" for user '.$username); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + throw new \SimpleSAML\Error\Error('WRONGUSERPASS'); |
| 102 | + } |
| 103 | +} |
0 commit comments