Test Failed
Pull Request — master (#52)
by Matthieu
06:17 queued 02:01
created

JWTAuthenticator::getCredentials()   B

Complexity

Conditions 9
Paths 15

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 19
nc 15
nop 1
dl 0
loc 33
rs 8.0555
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A JWTAuthenticator::onAuthenticationFailure() 0 3 1
A JWTAuthenticator::onAuthenticationSuccess() 0 3 1
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
15
/**
16
 * Class JWTAuthenticator
17
 */
18
class JWTAuthenticator extends AbstractAuthenticator
19
{
20
    /**
21
     * @var JWTUserProviderInterface
22
     */
23
    private $userProvider;
24
25
    /**
26
     * @var JWTSecurityHelperInterface
27
     */
28
    private $securityHelper;
29
30
    /**
31
     * JWTAuthenticator constructor.
32
     *
33
     * @param JWTUserProviderInterface   $userProvider
34
     * @param JWTSecurityHelperInterface $securityHelper
35
     */
36
    public function __construct(JWTUserProviderInterface $userProvider, JWTSecurityHelperInterface $securityHelper)
37
    {
38
        $this->userProvider = $userProvider;
39
        $this->securityHelper = $securityHelper;
40
    }
41
42
    /**
43
     * @param Request $request
44
     *
45
     * @return bool|null
46
     */
47
    public function supports(Request $request): ?bool
48
    {
49
        return $this->securityHelper->supportsRequest($request);
50
    }
51
52
    /**
53
     * @param Request $request
54
     *
55
     * @return PassportInterface
56
     */
57
    public function authenticate(Request $request): PassportInterface
58
    {
59
        $jwt = $this->securityHelper->getJWTToken($request);
60
61
        if (!$jwt) {
62
            throw new CustomUserMessageAuthenticationException('JWT Token not provided');
63
        }
64
65
        $token = $this->userProvider->getDecodedToken($jwt);
66
        $clientKey = $token->iss;
67
68
        if (!$clientKey) {
69
            throw new CustomUserMessageAuthenticationException(
70
                \sprintf('API Key %s does not exist', $jwt)
71
            );
72
        }
73
74
        $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

74
        /** @scrutinizer ignore-call */ 
75
        $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...
75
76
        if (\property_exists($token, 'sub')) {
77
            // for some reasons, when webhooks are called - field sub is undefined
78
            $user->setUsername($token->sub);
79
        }
80
81
        return new SelfValidatingPassport(new UserBadge($user->getUserIdentifier()));
82
    }
83
84
    /**
85
     * @param Request        $request
86
     * @param TokenInterface $token
87
     * @param string         $firewallName
88
     *
89
     * @return Response|null
90
     */
91
    public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
92
    {
93
        return null;
94
    }
95
96
    /**
97
     * @param Request                 $request
98
     * @param AuthenticationException $exception
99
     *
100
     * @return Response|null
101
     */
102
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response
103
    {
104
        return new Response('Authentication Failed: '.$exception->getMessage(), 403);
105
    }
106
}
107