Test Failed
Pull Request — master (#52)
by Matthieu
17:46
created

LegacyJWTAuthenticator::getUser()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 31
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 16
nc 6
nop 2
dl 0
loc 31
rs 9.4222
c 1
b 0
f 0
1
<?php declare(strict_types = 1);
2
3
namespace AtlassianConnectBundle\Security;
4
5
use AtlassianConnectBundle\Entity\TenantInterface;
6
use InvalidArgumentException;
7
use Symfony\Component\HttpFoundation\Request;
8
use Symfony\Component\HttpFoundation\Response;
9
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
10
use Symfony\Component\Security\Core\Exception\AuthenticationException;
11
use Symfony\Component\Security\Core\User\UserInterface;
12
use Symfony\Component\Security\Core\User\UserProviderInterface;
13
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;
14
15
/**
16
 * Class LegacyJWTAuthenticator
17
 */
18
class LegacyJWTAuthenticator extends AbstractGuardAuthenticator
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Securi...tractGuardAuthenticator has been deprecated: since Symfony 5.3, use the new authenticator system instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

18
class LegacyJWTAuthenticator extends /** @scrutinizer ignore-deprecated */ AbstractGuardAuthenticator
Loading history...
19
{
20
    /**
21
     * @var JWTSecurityHelperInterface
22
     */
23
    private $securityHelper;
24
25
    /**
26
     * LegacyJWTAuthenticator constructor.
27
     *
28
     * @param JWTSecurityHelperInterface $securityHelper
29
     */
30
    public function __construct(JWTSecurityHelperInterface $securityHelper)
31
    {
32
        $this->securityHelper = $securityHelper;
33
    }
34
35
    /**
36
     * @param Request                      $request
37
     * @param AuthenticationException|null $authException
38
     *
39
     * @return Response
40
     */
41
    public function start(Request $request, ?AuthenticationException $authException = null): Response
42
    {
43
        return new Response('Authentication header required', 401);
44
    }
45
46
    /**
47
     * @param Request $request
48
     *
49
     * @return bool
50
     */
51
    public function supports(Request $request): bool
52
    {
53
        return $this->securityHelper->supportsRequest($request);
54
    }
55
56
    /**
57
     * @param Request $request
58
     *
59
     * @return mixed Any non-null value
60
     */
61
    public function getCredentials(Request $request)
62
    {
63
        if (!$jwt = $this->securityHelper->getJWTToken($request)) {
64
            return null;
65
        }
66
67
        return ['jwt' => $jwt];
68
    }
69
70
    /**
71
     * @param mixed                 $credentials
72
     * @param UserProviderInterface $userProvider
73
     *
74
     * @return UserInterface|null
75
     */
76
    public function getUser($credentials, UserProviderInterface $userProvider): ?UserInterface
77
    {
78
        if (!$userProvider instanceof JWTUserProviderInterface) {
79
            throw new InvalidArgumentException(\sprintf(
80
                'UserProvider must implement %s',
81
                JWTUserProviderInterface::class
82
            ));
83
        }
84
85
        $token = $userProvider->getDecodedToken($credentials['jwt']);
86
        $clientKey = $token->iss;
87
88
        if (!$clientKey) {
89
            throw new AuthenticationException(
90
                \sprintf('API Key "%s" does not exist.', $credentials['jwt'])
91
            );
92
        }
93
94
        /** @var TenantInterface|UserInterface $user */
95
        $loadUserMethod = \method_exists($userProvider, 'loadUserByIdentifier')
96
            ? 'loadUserByIdentifier'
97
            : 'loadUserByUsername'
98
        ;
99
        $user = $userProvider->$loadUserMethod($clientKey);
100
101
        if (\property_exists($token, 'sub')) {
102
            // for some reasons, when webhooks are called - field sub is undefined
103
            $user->setUsername($token->sub);
104
        }
105
106
        return $user;
107
    }
108
109
    /**
110
     * @param mixed         $credentials
111
     * @param UserInterface $user
112
     *
113
     * @return bool
114
     */
115
    public function checkCredentials($credentials, UserInterface $user): bool
116
    {
117
        return true;
118
    }
119
120
    /**
121
     * @param Request                 $request
122
     * @param AuthenticationException $exception
123
     *
124
     * @return Response|null
125
     */
126
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response
127
    {
128
        return new Response('Authentication Failed: '.$exception->getMessage(), 403);
129
    }
130
131
    /**
132
     * @param Request        $request
133
     * @param TokenInterface $token
134
     * @param mixed|string   $providerKey The provider (i.e. firewall) key
135
     *
136
     * @return Response|null
137
     */
138
    public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey): ?Response
139
    {
140
        return null;
141
    }
142
143
    /**
144
     * @return bool
145
     */
146
    public function supportsRememberMe(): bool
147
    {
148
        return false;
149
    }
150
}
151