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

JWTAuthenticator::getUser()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 31
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 16
nc 6
nop 2
dl 0
loc 31
rs 9.4222
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AtlassianConnectBundle\Security;
6
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\Exception\CustomUserMessageAuthenticationException;
12
use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;
13
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
14
use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface;
15
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
16
17
/**
18
 * Class JWTAuthenticator
19
 */
20
class JWTAuthenticator extends AbstractAuthenticator
21
{
22
    /**
23
     * @var JWTUserProviderInterface
24
     */
25
    private $userProvider;
26
27
    /**
28
     * @var JWTSecurityHelperInterface
29
     */
30
    private $securityHelper;
31
32
    /**
33
     * JWTAuthenticator constructor.
34
     *
35
     * @param JWTUserProviderInterface $userProvider
36
     * @param JWTSecurityHelperInterface $securityHelper
37
     */
38
    public function __construct(JWTUserProviderInterface $userProvider, JWTSecurityHelperInterface $securityHelper)
39
    {
40
        $this->userProvider = $userProvider;
41
        $this->securityHelper = $securityHelper;
42
    }
43
44
    public function supports(Request $request): ?bool
45
    {
46
        return $this->securityHelper->supportsRequest($request);
47
    }
48
49
    public function authenticate(Request $request): PassportInterface
50
    {
51
        if (!$jwt = $this->securityHelper->getJWTToken($request)) {
52
            throw new CustomUserMessageAuthenticationException('JWT Token not provided');
53
        }
54
55
        $token = $this->userProvider->getDecodedToken($jwt);
56
        $clientKey = $token->iss;
57
58
        if (!$clientKey) {
59
            throw new CustomUserMessageAuthenticationException(
60
                \sprintf('API Key %s does not exist', $jwt)
61
            );
62
        }
63
64
        $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

64
        /** @scrutinizer ignore-call */ 
65
        $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...
65
66
        if (\property_exists($token, 'sub')) {
67
            // for some reasons, when webhooks are called - field sub is undefined
68
            $user->setUsername($token->sub);
69
        }
70
71
        return new SelfValidatingPassport(new UserBadge($user->getUserIdentifier()));
72
    }
73
74
    public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
75
    {
76
        return null;
77
    }
78
79
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response
80
    {
81
        return new Response('Authentication Failed: '.$exception->getMessage(), 403);
82
    }
83
}
84