|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
/** |
|
4
|
|
|
* /src/Security/Handler/TranslatedAuthenticationFailureHandler.php |
|
5
|
|
|
* |
|
6
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace App\Security\Handler; |
|
10
|
|
|
|
|
11
|
|
|
use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationFailureEvent; |
|
12
|
|
|
use Lexik\Bundle\JWTAuthenticationBundle\Response\JWTAuthenticationFailureResponse; |
|
13
|
|
|
use Lexik\Bundle\JWTAuthenticationBundle\Security\Http\Authentication\AuthenticationFailureHandler; |
|
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
16
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException; |
|
17
|
|
|
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; |
|
18
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class TranslatedAuthenticationFailureHandler |
|
22
|
|
|
* |
|
23
|
|
|
* @package App\Security\Handler |
|
24
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class TranslatedAuthenticationFailureHandler extends AuthenticationFailureHandler |
|
27
|
|
|
{ |
|
28
|
18 |
|
public function __construct( |
|
29
|
|
|
EventDispatcherInterface $dispatcher, |
|
30
|
|
|
private readonly TranslatorInterface $translator, |
|
|
|
|
|
|
31
|
|
|
) { |
|
32
|
18 |
|
parent::__construct($dispatcher); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* {@inheritdoc} |
|
37
|
|
|
* |
|
38
|
|
|
* @see https://github.com/lexik/LexikJWTAuthenticationBundle/issues/944 |
|
39
|
|
|
* |
|
40
|
|
|
* @noinspection PhpMissingParentCallCommonInspection |
|
41
|
|
|
*/ |
|
42
|
4 |
|
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): Response |
|
43
|
|
|
{ |
|
44
|
|
|
/** |
|
45
|
|
|
* @psalm-suppress MissingDependency, InvalidArgument |
|
46
|
|
|
*/ |
|
47
|
4 |
|
$event = new AuthenticationFailureEvent( |
|
48
|
|
|
$exception, |
|
49
|
4 |
|
new JWTAuthenticationFailureResponse($this->translator->trans('Invalid credentials.', [], 'security')) |
|
50
|
|
|
); |
|
51
|
|
|
|
|
52
|
4 |
|
$this->dispatcher->dispatch($event); |
|
53
|
|
|
|
|
54
|
4 |
|
return $event->getResponse(); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|