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