|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
|
|
3
|
|
|
namespace AtlassianConnectBundle\Tests\Security; |
|
4
|
|
|
|
|
5
|
|
|
use AtlassianConnectBundle\Entity\Tenant; |
|
6
|
|
|
use AtlassianConnectBundle\Entity\TenantInterface; |
|
7
|
|
|
use AtlassianConnectBundle\Security\JWTSecurityHelper; |
|
8
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
9
|
|
|
use Doctrine\Persistence\ObjectRepository; |
|
10
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
11
|
|
|
use PHPUnit\Framework\TestCase; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class JWTSecurityHelperTest |
|
16
|
|
|
*/ |
|
17
|
|
|
final class JWTSecurityHelperTest extends TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var EntityManagerInterface|MockObject |
|
21
|
|
|
*/ |
|
22
|
|
|
private $em; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var JWTSecurityHelper |
|
26
|
|
|
*/ |
|
27
|
|
|
private $helper; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Setup method |
|
31
|
|
|
*/ |
|
32
|
|
|
protected function setUp(): void |
|
33
|
|
|
{ |
|
34
|
|
|
$this->em = $this->createMock(EntityManagerInterface::class); |
|
35
|
|
|
$this->helper = new JWTSecurityHelper($this->em, 1, 'dev', TenantInterface::class); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @dataProvider supportsRequestProvider |
|
40
|
|
|
* |
|
41
|
|
|
* @param Request $request |
|
42
|
|
|
* @param bool $supportsRequest |
|
43
|
|
|
* @param int|null $devTenant |
|
44
|
|
|
* @param string $environment |
|
45
|
|
|
*/ |
|
46
|
|
|
public function testSupportsRequest( |
|
47
|
|
|
Request $request, |
|
48
|
|
|
bool $supportsRequest, |
|
49
|
|
|
?int $devTenant, |
|
50
|
|
|
string $environment |
|
51
|
|
|
): void { |
|
52
|
|
|
$helper = new JWTSecurityHelper($this->em, $devTenant, $environment, TenantInterface::class); |
|
53
|
|
|
|
|
54
|
|
|
$this->assertSame($supportsRequest, $helper->supportsRequest($request)); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return \Generator |
|
59
|
|
|
*/ |
|
60
|
|
|
public function supportsRequestProvider(): \Generator |
|
61
|
|
|
{ |
|
62
|
|
|
$request = new Request(['jwt' => 'token']); |
|
63
|
|
|
|
|
64
|
|
|
yield 'query_parameter' => [$request, true, null, 'prod']; |
|
65
|
|
|
|
|
66
|
|
|
$request = new Request(); |
|
67
|
|
|
|
|
68
|
|
|
yield 'empty_request' => [$request, false, null, 'prod']; |
|
69
|
|
|
|
|
70
|
|
|
$request = new Request(); |
|
71
|
|
|
$request->headers->set('authorization', 'Bearer token'); |
|
72
|
|
|
|
|
73
|
|
|
yield 'header' => [$request, true, null, 'prod']; |
|
74
|
|
|
|
|
75
|
|
|
yield 'dev_tenant' => [new Request(), true, 1, 'dev']; |
|
76
|
|
|
|
|
77
|
|
|
yield 'no_dev_tenant' => [new Request(), false, null, 'dev']; |
|
78
|
|
|
|
|
79
|
|
|
yield 'dev_tenant_prod' => [new Request(), false, 1, 'prod']; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Test if the jwt token can be fetched from the query parameters. |
|
84
|
|
|
*/ |
|
85
|
|
|
public function testGetJWTFromQueryParameter(): void |
|
86
|
|
|
{ |
|
87
|
|
|
$request = new Request(['jwt' => 'token']); |
|
88
|
|
|
|
|
89
|
|
|
$this->assertSame('token', $this->helper->getJWTToken($request)); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Test if the jwt token can be fetched from the request headers. |
|
94
|
|
|
*/ |
|
95
|
|
|
public function testGetJWTFromAuthorizationHeader(): void |
|
96
|
|
|
{ |
|
97
|
|
|
$request = new Request(); |
|
98
|
|
|
$request->headers->set('authorization', 'Bearer token'); |
|
99
|
|
|
|
|
100
|
|
|
$this->assertSame('token', $this->helper->getJWTToken($request)); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Test if the jwt token can be fetched from the dev tenant. |
|
105
|
|
|
*/ |
|
106
|
|
|
public function testGetJWTFromDevTenant(): void |
|
107
|
|
|
{ |
|
108
|
|
|
$tenant = new Tenant(); |
|
109
|
|
|
$tenant->setClientKey('client_key'); |
|
110
|
|
|
$tenant->setSharedSecret('shared_secret'); |
|
111
|
|
|
|
|
112
|
|
|
$repository = $this->createMock(ObjectRepository::class); |
|
113
|
|
|
$repository |
|
114
|
|
|
->expects($this->once()) |
|
115
|
|
|
->method('find') |
|
116
|
|
|
->with(1) |
|
117
|
|
|
->willReturn($tenant); |
|
118
|
|
|
|
|
119
|
|
|
$this->em |
|
120
|
|
|
->expects($this->once()) |
|
121
|
|
|
->method('getRepository') |
|
122
|
|
|
->willReturn($repository); |
|
123
|
|
|
|
|
124
|
|
|
$jwt = $this->helper->getJWTToken(Request::create('/test')); |
|
125
|
|
|
$this->assertNotNull($jwt); |
|
126
|
|
|
$this->assertStringContainsString( |
|
127
|
|
|
'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.', |
|
128
|
|
|
$jwt |
|
|
|
|
|
|
129
|
|
|
); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Test repository cannot find tenant |
|
134
|
|
|
*/ |
|
135
|
|
|
public function testCannotFindTenant(): void |
|
136
|
|
|
{ |
|
137
|
|
|
$this->expectException(\RuntimeException::class); |
|
138
|
|
|
|
|
139
|
|
|
$repository = $this->createMock(ObjectRepository::class); |
|
140
|
|
|
$repository |
|
141
|
|
|
->expects($this->once()) |
|
142
|
|
|
->method('find') |
|
143
|
|
|
->with(1) |
|
144
|
|
|
->willReturn(null); |
|
145
|
|
|
|
|
146
|
|
|
$this->em |
|
147
|
|
|
->expects($this->once()) |
|
148
|
|
|
->method('getRepository') |
|
149
|
|
|
->willReturn($repository); |
|
150
|
|
|
|
|
151
|
|
|
$this->helper->getJWTToken(new Request()); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Test there is no jwt token at all |
|
156
|
|
|
*/ |
|
157
|
|
|
public function testNoJWTToken(): void |
|
158
|
|
|
{ |
|
159
|
|
|
$helper = new JWTSecurityHelper($this->em, 1, 'prod', TenantInterface::class); |
|
160
|
|
|
|
|
161
|
|
|
$this->em->expects($this->never())->method('getRepository'); |
|
162
|
|
|
$this->assertNull($helper->getJWTToken(new Request())); |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|