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