Passed
Push — master ( 77d6c9...732152 )
by Florian
03:45
created

PasswordAuthenticator   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 24
dl 0
loc 48
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B authenticateToken() 0 34 7
A createToken() 0 3 1
A supportsToken() 0 4 2
1
<?php
2
3
/*
4
 * This file is part of the feedback project.
5
 *
6
 * (c) Florian Moser <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace App\Security;
13
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
16
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
17
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
18
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
19
use Symfony\Component\Security\Core\User\UserInterface;
20
use Symfony\Component\Security\Core\User\UserProviderInterface;
21
use Symfony\Component\Security\Http\Authentication\SimpleFormAuthenticatorInterface;
22
23
class PasswordAuthenticator implements SimpleFormAuthenticatorInterface
24
{
25
    public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
26
    {
27
        $user = $userProvider->loadUserByUsername($token->getUsername());
28
        $currentUser = $token->getUser();
29
30
        if ($currentUser instanceof UserInterface) {
31
            if ($currentUser->getPassword() !== $user->getPassword()) {
32
                throw new BadCredentialsException('The credentials were changed from another session.');
33
            }
34
        } else {
35
            if ('' === ($givenPassword = $token->getCredentials())) {
36
                throw new BadCredentialsException('The given password cannot be empty.');
37
            }
38
            if (!$user->getPassword() !== $givenPassword) {
39
                throw new BadCredentialsException('The given password is invalid.');
40
            }
41
        }
42
43
        $currentHour = date('G');
44
        if ($currentHour < 14 || $currentHour > 16) {
45
            // CAUTION: this message will be returned to the client
46
            // (so don't put any un-trusted messages / error strings here)
47
            throw new CustomUserMessageAuthenticationException(
48
                'You can only log in between 2 and 4!',
49
                [], // Message Data
50
                412 // HTTP 412 Precondition Failed
51
            );
52
        }
53
54
        return new UsernamePasswordToken(
55
            $user,
56
            $user->getPassword(),
57
            $providerKey,
58
            $user->getRoles()
59
        );
60
    }
61
62
    public function supportsToken(TokenInterface $token, $providerKey)
63
    {
64
        return $token instanceof UsernamePasswordToken
65
            && $token->getProviderKey() === $providerKey;
66
    }
67
68
    public function createToken(Request $request, $username, $password, $providerKey)
69
    {
70
        return new UsernamePasswordToken($username, $password, $providerKey);
71
    }
72
}
73