Completed
Pull Request — master (#1)
by Artem
01:30
created

JwtSubscriber   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 57
c 0
b 0
f 0
wmc 6
lcom 1
cbo 3
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getSubscribedEvents() 0 11 1
A onAuthenticationFailureResponse() 0 23 4
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 Symfony\Component\EventDispatcher\EventSubscriberInterface;
21
use Symfony\Component\HttpFoundation\JsonResponse;
22
use Symfony\Contracts\Translation\TranslatorInterface;
23
24
/**
25
 * JwtSubscriber.
26
 */
27
final class JwtSubscriber implements EventSubscriberInterface
28
{
29
    /** @var TranslatorInterface */
30
    protected $translator;
31
32
    /**
33
     * @param TranslatorInterface $translator
34
     */
35
    public function __construct(TranslatorInterface $translator)
36
    {
37
        $this->translator = $translator;
38
    }
39
40
    private const ON_AUTHENTICATION_FAILURE_RESPONSE_FUNCTION = 'onAuthenticationFailureResponse';
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public static function getSubscribedEvents(): iterable
46
    {
47
        yield AuthenticationFailureEvent::class => self::ON_AUTHENTICATION_FAILURE_RESPONSE_FUNCTION;
48
        yield JWTInvalidEvent::class => self::ON_AUTHENTICATION_FAILURE_RESPONSE_FUNCTION;
49
        yield JWTNotFoundEvent::class => self::ON_AUTHENTICATION_FAILURE_RESPONSE_FUNCTION;
50
        yield JWTExpiredEvent::class => self::ON_AUTHENTICATION_FAILURE_RESPONSE_FUNCTION;
51
        yield Events::AUTHENTICATION_FAILURE => self::ON_AUTHENTICATION_FAILURE_RESPONSE_FUNCTION;
52
        yield Events::JWT_INVALID => self::ON_AUTHENTICATION_FAILURE_RESPONSE_FUNCTION;
53
        yield Events::JWT_NOT_FOUND => self::ON_AUTHENTICATION_FAILURE_RESPONSE_FUNCTION;
54
        yield Events::JWT_EXPIRED => self::ON_AUTHENTICATION_FAILURE_RESPONSE_FUNCTION;
55
    }
56
57
    /**
58
     * @param AuthenticationFailureEvent $event
59
     */
60
    public function onAuthenticationFailureResponse(AuthenticationFailureEvent $event): void
61
    {
62
        switch (true) {
63
            case $event instanceof JWTInvalidEvent:
64
                $message = 'invalid_jwt_token_message';
65
                break;
66
            case $event instanceof JWTNotFoundEvent:
67
                $message = 'not_found_jwt_token_message';
68
                break;
69
            case $event instanceof JWTExpiredEvent:
70
                $message = 'expired_jwt_token_message';
71
                break;
72
            default:
73
                $message = 'unauthorised_user_message';
74
        }
75
76
        $data = [
77
            'error' => 'unauthorised_user',
78
            'errorDescription' => $this->translator->trans($message),
79
        ];
80
81
        $event->setResponse(new JsonResponse($data, JsonResponse::HTTP_UNAUTHORIZED));
82
    }
83
}
84