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 NSimpleAuthenticator extends NObject 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 NAuthenticationException
40: * @return IIdentity
41: * @throws NAuthenticationException
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 NIdentity($name);
50: } else {
51: throw new NAuthenticationException("Invalid password.", self::INVALID_CREDENTIAL);
52: }
53: }
54: }
55: throw new NAuthenticationException("User '$username' not found.", self::IDENTITY_NOT_FOUND);
56: }
57:
58: }
59: