|
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); |
|
|
|
|
|
|
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
|
|
|
if (!\class_exists(UserBadge::class)) { |
|
83
|
|
|
return new SelfValidatingPassport($user); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return new SelfValidatingPassport(new UserBadge($clientKey)); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param Request $request |
|
91
|
|
|
* @param TokenInterface $token |
|
92
|
|
|
* @param string $firewallName |
|
93
|
|
|
* |
|
94
|
|
|
* @return Response|null |
|
95
|
|
|
*/ |
|
96
|
|
|
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response |
|
97
|
|
|
{ |
|
98
|
|
|
return null; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param Request $request |
|
103
|
|
|
* @param AuthenticationException $exception |
|
104
|
|
|
* |
|
105
|
|
|
* @return Response|null |
|
106
|
|
|
*/ |
|
107
|
|
|
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response |
|
108
|
|
|
{ |
|
109
|
|
|
return new Response('Authentication Failed: '.$exception->getMessage(), 403); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param Request $request |
|
114
|
|
|
* @param AuthenticationException|null $authException |
|
115
|
|
|
* |
|
116
|
|
|
* @return Response |
|
117
|
|
|
*/ |
|
118
|
|
|
public function start(Request $request, AuthenticationException $authException = null) |
|
119
|
|
|
{ |
|
120
|
|
|
return new Response('Authentication header required', 401); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
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.