Completed
Pull Request — master (#2)
by Tomáš
09:22
created

Authenticator::validateCredentials()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SixtyEightPublishers\User\Authentication\Authenticator;
6
7
use Nette;
8
use Doctrine;
9
use SixtyEightPublishers;
10
11
final class Authenticator implements Nette\Security\IAuthenticator
12
{
13
	use Nette\SmartObject;
14
15
	/** @var \SixtyEightPublishers\User\Authentication\Query\IAuthenticatorQueryFactory  */
16
	private $authenticatorQueryFactory;
17
18
	/** @var \SixtyEightPublishers\User\Common\PasswordHashStrategy\IPasswordHashStrategy  */
19
	private $passwordHashStrategy;
20
21
	/**
22
	 * @param \SixtyEightPublishers\User\Authentication\Query\IAuthenticatorQueryFactory   $authenticatorQueryFactory
23
	 * @param \SixtyEightPublishers\User\Common\PasswordHashStrategy\IPasswordHashStrategy $passwordHashStrategy
24
	 */
25
	public function __construct(
26
		SixtyEightPublishers\User\Authentication\Query\IAuthenticatorQueryFactory $authenticatorQueryFactory,
27
		SixtyEightPublishers\User\Common\PasswordHashStrategy\IPasswordHashStrategy $passwordHashStrategy
28
	) {
29
		$this->authenticatorQueryFactory = $authenticatorQueryFactory;
30
		$this->passwordHashStrategy = $passwordHashStrategy;
31
	}
32
33
	/**
34
	 * @param array $credentials
35
	 *
36
	 * @return array
37
	 * @throws \Nette\Security\AuthenticationException
38
	 */
39
	private function validateCredentials(array $credentials): array
40
	{
41
		if (!isset($credentials[self::USERNAME])) {
42
			throw new Nette\Security\AuthenticationException(sprintf(
43
				'Missing login field in credentials (key %s)',
44
				self::USERNAME
45
			), self::FAILURE);
46
		}
47
48
		if (!isset($credentials[self::PASSWORD])) {
49
			throw new Nette\Security\AuthenticationException(sprintf(
50
				'Missing password field in credentials (key %s)',
51
				self::PASSWORD
52
			), self::FAILURE);
53
		}
54
55
		return [
56
			(string) $credentials[self::USERNAME],
57
			(string) $credentials[self::PASSWORD],
58
		];
59
	}
60
61
	/**
62
	 * @param string $login
63
	 *
64
	 * @return \SixtyEightPublishers\User\Authentication\DoctrineEntity\IUser
65
	 * @throws \Nette\Security\AuthenticationException
66
	 */
67
	private function findUser(string $login): SixtyEightPublishers\User\Authentication\DoctrineEntity\IUser
68
	{
69
		try {
70
			$user = $this->authenticatorQueryFactory
71
				->create($login)
72
				->getOneOrNullResult();
73
		} catch (Doctrine\ORM\NonUniqueResultException $e) {
74
			$e = new Nette\Security\AuthenticationException(sprintf(
75
				'User\'s login field is not unique! Value was "%s"',
76
				$login
77
			), self::FAILURE, $e);
78
		} catch (Doctrine\DBAL\DBALException $e) {
79
			$e = new Nette\Security\AuthenticationException(sprintf(
80
				'DBAL throws unexpected exception, login values was "%s"',
81
				$login
82
			), self::FAILURE, $e);
83
		}
84
85
		if (isset($e)) {
86
			throw $e;
87
		}
88
89
		if (!isset($user)) {
90
			throw new Nette\Security\AuthenticationException(sprintf(
91
				'User "%s" not found.',
92
				$login
93
			), self::IDENTITY_NOT_FOUND);
94
		}
95
96
		return $user;
97
	}
98
99
	/************* interface \Nette\Security\IAuthenticator *************/
100
101
	/**
102
	 * {@inheritdoc}
103
	 */
104
	public function authenticate(array $credentials): Nette\Security\Identity
105
	{
106
		[ $login, $password ] = $this->validateCredentials($credentials);
0 ignored issues
show
Bug introduced by
The variable $login does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $password does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
107
		$user = $this->findUser($login);
108
109
		if (FALSE === $this->passwordHashStrategy->verify($password, $user->getPassword())) {
110
			throw new Nette\Security\AuthenticationException(sprintf(
111
				'Invalid password for user "%s"',
112
				$login
113
			), self::INVALID_CREDENTIAL);
114
		}
115
116
		return $user;
117
	}
118
}
119