|
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); |
|
|
|
|
|
|
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
|
|
|
|
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.