Completed
Pull Request — master (#2)
by Vojtěch
10:05
created

Authenticator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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
			$this->logger->error((string) $e);
0 ignored issues
show
Documentation introduced by
The property logger does not exist on object<SixtyEightPublish...nticator\Authenticator>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
87
88
			throw $e;
89
		}
90
91
		if (NULL === $user) {
92
			throw new Nette\Security\AuthenticationException(sprintf(
93
				'User "%s" not found.',
94
				$login
95
			), self::IDENTITY_NOT_FOUND);
96
		}
97
98
		return $user;
99
	}
100
101
	/************* interface \Nette\Security\IAuthenticator *************/
102
103
	/**
104
	 * {@inheritdoc}
105
	 */
106
	public function authenticate(array $credentials): Nette\Security\Identity
107
	{
108
		[ $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...
109
		$user = $this->findUser($login);
110
111
		if (FALSE === $this->passwordHashStrategy->verify($password, $user->getPassword())) {
112
			throw new Nette\Security\AuthenticationException(sprintf(
113
				'Invalid password for user "%s"',
114
				$login
115
			), self::INVALID_CREDENTIAL);
116
		}
117
118
		return $user;
119
	}
120
}
121