1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AtlassianConnectBundle\Tests\Security; |
6
|
|
|
|
7
|
|
|
use AtlassianConnectBundle\Entity\Tenant; |
8
|
|
|
use AtlassianConnectBundle\Entity\TenantInterface; |
9
|
|
|
use AtlassianConnectBundle\Repository\TenantRepositoryInterface; |
10
|
|
|
use AtlassianConnectBundle\Security\JWTUserProvider; |
11
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException; |
14
|
|
|
use Symfony\Component\Security\Core\Exception\UnsupportedUserException; |
15
|
|
|
use Symfony\Component\Security\Core\Exception\UserNotFoundException; |
16
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
17
|
|
|
|
18
|
|
|
final class JWTUserProviderTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
private TenantRepositoryInterface|MockObject $repository; |
21
|
|
|
private JWTUserProvider $userProvider; |
22
|
|
|
|
23
|
|
|
protected function setUp(): void |
24
|
|
|
{ |
25
|
|
|
$this->repository = $this->createMock(TenantRepositoryInterface::class); |
26
|
|
|
$this->userProvider = new JWTUserProvider($this->repository); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @dataProvider jwtTokenProvider |
31
|
|
|
*/ |
32
|
|
|
public function testItDecodesAToken(string $jwt, string $secret, string $isstoken, string $sub, string $name, int $iat): void |
33
|
|
|
{ |
34
|
|
|
$tenant = $this->createMock(TenantInterface::class); |
35
|
|
|
$tenant |
36
|
|
|
->expects($this->once()) |
37
|
|
|
->method('getSharedSecret') |
38
|
|
|
->willReturn($secret); |
39
|
|
|
|
40
|
|
|
$this->repository->expects($this->once()) |
|
|
|
|
41
|
|
|
->method('findByClientKey') |
42
|
|
|
->with($isstoken) |
43
|
|
|
->willReturn($tenant); |
44
|
|
|
|
45
|
|
|
$token = $this->userProvider->getDecodedToken($jwt); |
46
|
|
|
|
47
|
|
|
$this->assertEquals($sub, $token->sub); |
48
|
|
|
$this->assertEquals($name, $token->name); |
49
|
|
|
$this->assertEquals($isstoken, $token->iss); |
50
|
|
|
$this->assertEquals($iat, $token->iat); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function jwtTokenProvider(): \Generator |
54
|
|
|
{ |
55
|
|
|
yield [ |
56
|
|
|
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJpc3MiOiJpc3N0b2tlbiJ9.vcwW8PMPwPF2E-CkWflDrhAulR5dPWbbl-lOJheOwIY', |
57
|
|
|
'secret', |
58
|
|
|
'isstoken', |
59
|
|
|
'1234567890', |
60
|
|
|
'John Doe', |
61
|
|
|
1516239022, |
62
|
|
|
]; |
63
|
|
|
|
64
|
|
|
yield [ |
65
|
|
|
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI5ODc2NTQzMjEwIiwibmFtZSI6IkphbmUgRG9lIiwiaWF0IjoxNTE2MjM5MDIzLCJpc3MiOiJhbm90aGVySXNzVG9rZW4ifQ.wzTiSSNtS6rXoAYXL4tdmVzEbUvRd7BSuMq3kbboSA4', |
66
|
|
|
'anotherSecret', |
67
|
|
|
'anotherIssToken', |
68
|
|
|
'9876543210', |
69
|
|
|
'Jane Doe', |
70
|
|
|
1516239023, |
71
|
|
|
]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testItFailsToDecodeToken(): void |
75
|
|
|
{ |
76
|
|
|
$this->expectException(AuthenticationException::class); |
77
|
|
|
$this->expectExceptionMessage('Failed to parse token'); |
78
|
|
|
$this->userProvider->getDecodedToken('invalid_token'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testLoadsUserByUserName(): void |
82
|
|
|
{ |
83
|
|
|
$tenant = $this->createMock(TenantInterface::class); |
84
|
|
|
|
85
|
|
|
$this->repository |
86
|
|
|
->expects($this->once()) |
87
|
|
|
->method('findByClientKey') |
88
|
|
|
->with('key') |
89
|
|
|
->willReturn($tenant); |
90
|
|
|
|
91
|
|
|
$result = $this->userProvider->loadUserByUsername('key'); |
92
|
|
|
$this->assertSame($result, $tenant); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function testItFailsToLoadAUserByUserName(): void |
96
|
|
|
{ |
97
|
|
|
$this->expectException(UserNotFoundException::class); |
98
|
|
|
|
99
|
|
|
$this->repository |
100
|
|
|
->expects($this->once()) |
101
|
|
|
->method('findByClientKey') |
102
|
|
|
->with('key') |
103
|
|
|
->willReturn(null); |
104
|
|
|
|
105
|
|
|
$this->userProvider->loadUserByUsername('key'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function testRefreshUserIsNotSupported(): void |
109
|
|
|
{ |
110
|
|
|
$this->expectException(UnsupportedUserException::class); |
111
|
|
|
|
112
|
|
|
$this->userProvider->refreshUser($this->createMock(UserInterface::class)); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @dataProvider classProvider |
117
|
|
|
*/ |
118
|
|
|
public function testItSupportsAclass($class, bool $isSupported): void |
119
|
|
|
{ |
120
|
|
|
$result = $this->userProvider->supportsClass($class); |
121
|
|
|
|
122
|
|
|
$this->assertEquals($isSupported, $result); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function classProvider(): \Generator |
126
|
|
|
{ |
127
|
|
|
yield [new Tenant(), true]; |
128
|
|
|
|
129
|
|
|
yield [new StubbedTenant(), true]; |
130
|
|
|
|
131
|
|
|
yield [new \stdClass(), false]; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|