1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (http://nette.org)
5: *
6: * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
7: *
8: * For the full copyright and license information, please view
9: * the file license.txt that was distributed with this source code.
10: * @package Nette\Security
11: */
12:
13:
14:
15: /**
16: * Trivial implementation of IAuthenticator.
17: *
18: * @author David Grudl
19: * @package Nette\Security
20: */
21: class SimpleAuthenticator extends Object implements IAuthenticator
22: {
23: /** @var array */
24: private $userlist;
25:
26:
27: /**
28: * @param array list of pairs username => password
29: */
30: public function __construct(array $userlist)
31: {
32: $this->userlist = $userlist;
33: }
34:
35:
36: /**
37: * Performs an authentication against e.g. database.
38: * and returns IIdentity on success or throws AuthenticationException
39: * @return IIdentity
40: * @throws AuthenticationException
41: */
42: public function authenticate(array $credentials)
43: {
44: list($username, $password) = $credentials;
45: foreach ($this->userlist as $name => $pass) {
46: if (strcasecmp($name, $username) === 0) {
47: if ((string) $pass === (string) $password) {
48: return new Identity($name);
49: } else {
50: throw new AuthenticationException("Invalid password.", self::INVALID_CREDENTIAL);
51: }
52: }
53: }
54: throw new AuthenticationException("User '$username' not found.", self::IDENTITY_NOT_FOUND);
55: }
56:
57: }
58: