Passed
Pull Request — master (#52)
by Matthieu
04:51
created

JWTAuthenticator::checkCredentials()   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 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types = 1);
2
3
namespace AtlassianConnectBundle\Security;
4
5
use Symfony\Component\HttpFoundation\Request;
6
use Symfony\Component\HttpFoundation\Response;
7
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
8
use Symfony\Component\Security\Core\Exception\AuthenticationException;
9
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
10
use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;
11
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
12
use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface;
13
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
14
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
15
16
/**
17
 * Class JWTAuthenticator
18
 */
19
class JWTAuthenticator extends AbstractAuthenticator implements AuthenticationEntryPointInterface
20
{
21
    /**
22
     * @var JWTUserProviderInterface
23
     */
24
    private $userProvider;
25
26
    /**
27
     * @var JWTSecurityHelperInterface
28
     */
29
    private $securityHelper;
30
31
    /**
32
     * JWTAuthenticator constructor.
33
     *
34
     * @param JWTUserProviderInterface   $userProvider
35
     * @param JWTSecurityHelperInterface $securityHelper
36
     */
37
    public function __construct(JWTUserProviderInterface $userProvider, JWTSecurityHelperInterface $securityHelper)
38
    {
39
        $this->userProvider = $userProvider;
40
        $this->securityHelper = $securityHelper;
41
    }
42
43
    /**
44
     * @param Request $request
45
     *
46
     * @return bool|null
47
     */
48
    public function supports(Request $request): ?bool
49
    {
50
        return $this->securityHelper->supportsRequest($request);
51
    }
52
53
    /**
54
     * @param Request $request
55
     *
56
     * @return PassportInterface
57
     */
58
    public function authenticate(Request $request): PassportInterface
59
    {
60
        $jwt = $this->securityHelper->getJWTToken($request);
61
62
        if (!$jwt) {
63
            throw new CustomUserMessageAuthenticationException('JWT Token not provided');
64
        }
65
66
        $token = $this->userProvider->getDecodedToken($jwt);
67
        $clientKey = $token->iss;
68
69
        if (!$clientKey) {
70
            throw new CustomUserMessageAuthenticationException(
71
                \sprintf('API Key %s does not exist', $jwt)
72
            );
73
        }
74
75
        $user = $this->userProvider->loadUserByIdentifier($clientKey);
0 ignored issues
show
Bug introduced by
The method loadUserByIdentifier() does not exist on AtlassianConnectBundle\S...WTUserProviderInterface. Did you maybe mean loadUserByUsername()? ( Ignorable by Annotation )

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

75
        /** @scrutinizer ignore-call */ 
76
        $user = $this->userProvider->loadUserByIdentifier($clientKey);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
76
77
        if (\property_exists($token, 'sub')) {
78
            // for some reasons, when webhooks are called - field sub is undefined
79
            $user->setUsername($token->sub);
80
        }
81
82
        return new SelfValidatingPassport(new UserBadge($clientKey));
83
    }
84
85
    /**
86
     * @param Request        $request
87
     * @param TokenInterface $token
88
     * @param string         $firewallName
89
     *
90
     * @return Response|null
91
     */
92
    public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
93
    {
94
        return null;
95
    }
96
97
    /**
98
     * @param Request                 $request
99
     * @param AuthenticationException $exception
100
     *
101
     * @return Response|null
102
     */
103
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response
104
    {
105
        return new Response('Authentication Failed: '.$exception->getMessage(), 403);
106
    }
107
108
    /**
109
     * @param Request                      $request
110
     * @param AuthenticationException|null $authException
111
     *
112
     * @return Response
113
     */
114
    public function start(Request $request, AuthenticationException $authException = null)
115
    {
116
        return new Response('Authentication header required', 401);
117
    }
118
}
119