Completed
Push — master ( 45188f...390fef )
by Artem
01:38
created

JwtSubscriber::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
c 0
b 0
f 0
rs 9.9
cc 1
nc 1
nop 0
1
<?php
2
/*
3
 * This file is part of the StfalconApiBundle.
4
 *
5
 * (c) Stfalcon LLC <stfalcon.com>
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
declare(strict_types=1);
12
13
namespace StfalconStudio\ApiBundle\EventListener\Jwt;
14
15
use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationFailureEvent;
16
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTExpiredEvent;
17
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTInvalidEvent;
18
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTNotFoundEvent;
19
use Lexik\Bundle\JWTAuthenticationBundle\Events;
20
use StfalconStudio\ApiBundle\Error\ErrorNames;
21
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
22
use Symfony\Component\HttpFoundation\JsonResponse;
23
use Symfony\Contracts\Translation\TranslatorInterface;
24
25
/**
26
 * JwtSubscriber.
27
 */
28
class JwtSubscriber implements EventSubscriberInterface
29
{
30
    private const ON_AUTHENTICATION_FAILURE_RESPONSE_FUNCTION = 'onAuthenticationFailureResponse';
31
32
    /** @var TranslatorInterface */
33
    protected $translator;
34
35
    /**
36
     * @param TranslatorInterface $translator
37
     */
38
    public function __construct(TranslatorInterface $translator)
39
    {
40
        $this->translator = $translator;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public static function getSubscribedEvents(): iterable
47
    {
48
        yield AuthenticationFailureEvent::class => self::ON_AUTHENTICATION_FAILURE_RESPONSE_FUNCTION;
49
        yield JWTInvalidEvent::class => self::ON_AUTHENTICATION_FAILURE_RESPONSE_FUNCTION;
50
        yield JWTNotFoundEvent::class => self::ON_AUTHENTICATION_FAILURE_RESPONSE_FUNCTION;
51
        yield JWTExpiredEvent::class => self::ON_AUTHENTICATION_FAILURE_RESPONSE_FUNCTION;
52
        yield Events::AUTHENTICATION_FAILURE => self::ON_AUTHENTICATION_FAILURE_RESPONSE_FUNCTION;
53
        yield Events::JWT_INVALID => self::ON_AUTHENTICATION_FAILURE_RESPONSE_FUNCTION;
54
        yield Events::JWT_NOT_FOUND => self::ON_AUTHENTICATION_FAILURE_RESPONSE_FUNCTION;
55
        yield Events::JWT_EXPIRED => self::ON_AUTHENTICATION_FAILURE_RESPONSE_FUNCTION;
56
    }
57
58
    /**
59
     * @param AuthenticationFailureEvent $event
60
     */
61
    public function onAuthenticationFailureResponse(AuthenticationFailureEvent $event): void
62
    {
63
        switch (true) {
64
            case $event instanceof JWTInvalidEvent:
65
                $message = 'invalid_jwt_token_message';
66
67
                break;
68
            case $event instanceof JWTNotFoundEvent:
69
                $message = 'not_found_jwt_token_message';
70
71
                break;
72
            case $event instanceof JWTExpiredEvent:
73
                $message = 'expired_jwt_token_message';
74
75
                break;
76
            default:
77
                $message = 'unauthorised_user_message';
78
        }
79
80
        $data = [
81
            'error' => ErrorNames::UNAUTHORISED_USER,
82
            'errorDescription' => $this->translator->trans($message),
83
        ];
84
85
        $event->setResponse(new JsonResponse($data, JsonResponse::HTTP_UNAUTHORIZED));
86
    }
87
}
88