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: /**
38: * Performs an authentication against e.g. database.
39: * and returns IIdentity on success or throws AuthenticationException
40: * @param array
41: * @return IIdentity
42: * @throws AuthenticationException
43: */
44: public function authenticate(array $credentials)
45: {
46: list($username, $password) = $credentials;
47: foreach ($this->userlist as $name => $pass) {
48: if (strcasecmp($name, $username) === 0) {
49: if ((string) $pass === (string) $password) {
50: return new Identity($name);
51: } else {
52: throw new AuthenticationException("Invalid password.", self::INVALID_CREDENTIAL);
53: }
54: }
55: }
56: throw new AuthenticationException("User '$username' not found.", self::IDENTITY_NOT_FOUND);
57: }
58:
59: }
60: