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: */
11:
12: namespace Nette\Security;
13:
14: use Nette;
15:
16:
17: /**
18: * Trivial implementation of IAuthenticator.
19: *
20: * @author David Grudl
21: */
22: class SimpleAuthenticator extends Nette\Object implements IAuthenticator
23: {
24: /** @var array */
25: private $userlist;
26:
27:
28: /**
29: * @param array list of pairs username => password
30: */
31: public function __construct(array $userlist)
32: {
33: $this->userlist = $userlist;
34: }
35:
36:
37: /**
38: * Performs an authentication against e.g. database.
39: * and returns IIdentity on success or throws AuthenticationException
40: * @return IIdentity
41: * @throws AuthenticationException
42: */
43: public function authenticate(array $credentials)
44: {
45: list($username, $password) = $credentials;
46: foreach ($this->userlist as $name => $pass) {
47: if (strcasecmp($name, $username) === 0) {
48: if ((string) $pass === (string) $password) {
49: return new Identity($name);
50: } else {
51: throw new AuthenticationException("Invalid password.", self::INVALID_CREDENTIAL);
52: }
53: }
54: }
55: throw new AuthenticationException("User '$username' not found.", self::IDENTITY_NOT_FOUND);
56: }
57:
58: }
59: