Passed
Push — master ( 90db0f...396226 )
by Jafar
03:16
created

JwsAuthenticator::supportsRememberMe()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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\Api\ApiResponse\ApiProblem;
14
use Jafar\Bundle\GuardedAuthenticationBundle\Api\ApiResponse\ApiResponseFactory;
15
use Jafar\Bundle\GuardedAuthenticationBundle\Api\JWSEncoder\JWSEncoderInterface;
16
use Jafar\Bundle\GuardedAuthenticationBundle\Api\JWSExtractor\TokenExtractor;
17
use Jafar\Bundle\GuardedAuthenticationBundle\Exception\ApiException;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\Routing\RouterInterface;
20
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
21
use Symfony\Component\Security\Core\Exception\AuthenticationException;
22
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
23
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
24
use Symfony\Component\Security\Core\User\UserInterface;
25
use Symfony\Component\Security\Core\User\UserProviderInterface;
26
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;
27
28
/**
29
 * {@inheritdoc}
30
 *
31
 * Class JwsAuthenticator.
32
 *
33
 * @author Jafar Jabr <[email protected]>
34
 */
35
class JwsAuthenticator extends AbstractGuardAuthenticator
36
{
37
    /**
38
     * @var JWSEncoderInterface
39
     */
40
    private $jwtEncoder;
41
42
    /**
43
     * @var RouterInterface
44
     */
45
    private $router;
46
47
    /**
48
     * @var ApiResponseFactory
49
     */
50
    private $responseFactory;
51
52
    /**
53
     * @var string
54
     */
55
    private $loginRoute;
56
57
    /**
58
     * JwsAuthenticator constructor.
59
     *
60
     * @param JWSEncoderInterface $jwtEncoder
61
     * @param RouterInterface     $router
62
     * @param ApiResponseFactory  $responseFactory
63
     * @param string              $loginRoute
64
     */
65
    public function __construct(
66
        JWSEncoderInterface $jwtEncoder,
67
        RouterInterface $router,
68
        ApiResponseFactory $responseFactory,
69
        string $loginRoute
70
    ) {
71
        $this->jwtEncoder      = $jwtEncoder;
72
        $this->router          = $router;
73
        $this->responseFactory = $responseFactory;
74
        $this->loginRoute      = $loginRoute;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function getCredentials(Request $request)
81
    {
82
        $loginRoute    = $this->loginRoute;
83
        $isLoginSubmit = $request->attributes->get('_route') == $loginRoute && $request->isMethod('POST');
84
        if ($isLoginSubmit) {
85
            return null;
86
        }
87
        $extractor = new TokenExtractor('Bearer', 'Authorization');
88
        $token     = $extractor->extract($request);
89
        if (!$token) {
90
            return null;
91
        }
92
93
        return $token;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function getUser($credentials, UserProviderInterface $userProvider)
100
    {
101
        try {
102
            $data = $this->jwtEncoder->decode($credentials);
103
        } catch (ApiException $e) {
104
            throw new CustomUserMessageAuthenticationException($e->getMessage());
105
        }
106
107
        $username = $data['username'];
108
109
        return $this->loadUser($userProvider, $username);
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115
    public function checkCredentials($credentials, UserInterface $user)
116
    {
117
        return true;
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123
    protected function getLoginUrl()
124
    {
125
        $loginRoute = $this->loginRoute;
126
127
        return $this->router->generate($loginRoute);
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function supportsRememberMe()
134
    {
135
        return false;
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
142
    {
143
        $apiProblem = new ApiProblem(401);
144
        $apiProblem->set('detail', $exception->getMessageKey());
145
146
        return $this->responseFactory->createResponse($apiProblem);
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152
    public function start(Request $request, AuthenticationException $authException = null)
153
    {
154
        $apiProblem = new ApiProblem(401);
155
        $message    = $authException ? $authException->getMessageKey() : 'Invalid credentials';
156
        $apiProblem->set('detail', $message);
157
158
        return $this->responseFactory->createResponse($apiProblem);
159
    }
160
161
    /**
162
     * {@inheritdoc}
163
     */
164
    public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
165
    {
166
        return null;
167
    }
168
169
    /**
170
     * {@inheritdoc}
171
     */
172
    protected function getDefaultSuccessRedirectUrl()
173
    {
174
        return;
175
    }
176
177
    /**
178
     * {@inheritdoc}
179
     */
180
    public function supports(Request $request)
181
    {
182
        return (bool) $this->getCredentials($request);
183
    }
184
185
    /**
186
     * @param UserProviderInterface $userProvider
187
     * @param string                $username
188
     *
189
     * @return UserInterface
190
     */
191
    private function loadUser(UserProviderInterface $userProvider, string $username)
192
    {
193
        try {
194
            $user = $userProvider->loadUserByUsername($username);
195
        } catch (UsernameNotFoundException $e) {
196
            throw new CustomUserMessageAuthenticationException($e->getMessage());
197
        }
198
199
        return $user;
200
    }
201
}
202