1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (https://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 usernames and passwords
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: * @param array
41: * @return IIdentity
42: * @throws NAuthenticationException
43: */
44: public function authenticate(array $credentials)
45: {
46: $username = $credentials[self::USERNAME];
47: foreach ($this->userlist as $name => $pass) {
48: if (strcasecmp($name, $credentials[self::USERNAME]) === 0) {
49: if (strcasecmp($pass, $credentials[self::PASSWORD]) === 0) {
50: // matched!
51: return new NIdentity($name);
52: }
53:
54: throw new NAuthenticationException("Invalid password.", self::INVALID_CREDENTIAL);
55: }
56: }
57:
58: throw new NAuthenticationException("User '$username' not found.", self::IDENTITY_NOT_FOUND);
59: }
60:
61: }
62: