LoginFormAuthenticator   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 16
eloc 42
c 5
b 1
f 0
dl 0
loc 153
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getCredentials() 0 18 6
A loadUser() 0 9 2
A supportsRememberMe() 0 3 1
A checkCredentials() 0 8 2
A onAuthenticationSuccess() 0 6 1
A supports() 0 3 1
A getUser() 0 5 1
A getLoginUrl() 0 5 1
A __construct() 0 12 1
1
<?php
2
/*
3
 * This file is part of the Guarded Authentication package.
4
 *
5
 * (c) Jafar Jabr <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Jafar\Bundle\GuardedAuthenticationBundle\Guard;
12
13
use Jafar\Bundle\GuardedAuthenticationBundle\Form\GuardedLoginForm;
14
use Symfony\Component\Form\FormFactoryInterface;
15
use Symfony\Component\HttpFoundation\RedirectResponse;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\Routing\RouterInterface;
18
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
19
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
20
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
21
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
22
use Symfony\Component\Security\Core\Security;
23
use Symfony\Component\Security\Core\User\UserInterface;
24
use Symfony\Component\Security\Core\User\UserProviderInterface;
25
use Symfony\Component\Security\Guard\Authenticator\AbstractFormLoginAuthenticator;
26
27
/**
28
 * Class LoginFormAuthenticator.
29
 *
30
 * @author Jafar Jabr <[email protected]>
31
 */
32
class LoginFormAuthenticator extends AbstractFormLoginAuthenticator
33
{
34
    /**
35
     * @var FormFactoryInterface
36
     */
37
    private $formFactory;
38
39
    /**
40
     * @var RouterInterface
41
     */
42
    private $router;
43
44
    /**
45
     * @var UserPasswordEncoderInterface
46
     */
47
    private $passwordEncoder;
48
49
    /**
50
     * @var string
51
     */
52
    private $loginRoute;
53
54
    /**
55
     * @var string
56
     */
57
    private $homeRoute;
58
59
    /**
60
     * @var string
61
     */
62
    private $wrongPassword = 'Incorrect Password Provided!';
63
64
    /**
65
     * LoginFormAuthenticator constructor.
66
     *
67
     * @param FormFactoryInterface         $formFactory
68
     * @param RouterInterface              $router
69
     * @param UserPasswordEncoderInterface $passwordEncoder
70
     * @param string                       $loginRoute
71
     * @param string                       $homeRoute
72
     */
73
    public function __construct(
74
        FormFactoryInterface $formFactory,
75
        RouterInterface $router,
76
        UserPasswordEncoderInterface $passwordEncoder,
77
        string $loginRoute,
78
        string $homeRoute
79
    ) {
80
        $this->formFactory     = $formFactory;
81
        $this->router          = $router;
82
        $this->passwordEncoder = $passwordEncoder;
83
        $this->loginRoute      = $loginRoute;
84
        $this->homeRoute       = $homeRoute;
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function getCredentials(Request $request)
91
    {
92
        $loginRoute    = $this->loginRoute;
93
        $isLoginSubmit = $request->attributes->get('_route') == $loginRoute && $request->isMethod('POST');
94
        if (!$isLoginSubmit) {
95
            return null;
96
        }
97
        $form = $this->formFactory->create(GuardedLoginForm::class);
98
        $form->handleRequest($request);
99
        $data = $form->getData();
100
        if ($request->getSession() && $data && isset($data['_username'])) {
101
            $request->getSession()->set(
102
                Security::LAST_USERNAME,
103
                $data['_username']
104
            );
105
        }
106
107
        return $data;
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    public function getUser($credentials, UserProviderInterface $userProvider)
114
    {
115
        $username = $credentials['_username'] ?? '';
116
117
        return $this->loadUser($userProvider, $username);
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123
    public function checkCredentials($credentials, UserInterface $user)
124
    {
125
        $password = $credentials['_password'] ?? '';
126
        if ($this->passwordEncoder->isPasswordValid($user, $password)) {
127
            return true;
128
        }
129
130
        throw new CustomUserMessageAuthenticationException($this->wrongPassword);
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136
    public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
137
    {
138
        $homeRoute = $this->homeRoute;
139
        $url       = $this->router->generate($homeRoute);
140
141
        return new RedirectResponse($url);
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147
    public function supportsRememberMe()
148
    {
149
        return true;
150
    }
151
152
    /**
153
     * {@inheritdoc}
154
     */
155
    protected function getLoginUrl()
156
    {
157
        $loginRoute = $this->loginRoute;
158
159
        return $this->router->generate($loginRoute);
160
    }
161
162
    /**
163
     * {@inheritdoc}
164
     */
165
    public function supports(Request $request)
166
    {
167
        return (bool) $this->getCredentials($request);
168
    }
169
170
    /**
171
     * @param UserProviderInterface $userProvider
172
     * @param string                $username
173
     *
174
     * @return UserInterface
175
     */
176
    private function loadUser(UserProviderInterface $userProvider, string $username)
177
    {
178
        try {
179
            $user = $userProvider->loadUserByUsername($username);
180
        } catch (UsernameNotFoundException $e) {
181
            throw new CustomUserMessageAuthenticationException($e->getMessage());
182
        }
183
184
        return $user;
185
    }
186
}
187