|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Damax\Bundle\ApiAuthBundle\Tests\Security\Jwt; |
|
6
|
|
|
|
|
7
|
|
|
use Damax\Bundle\ApiAuthBundle\Extractor\Extractor; |
|
8
|
|
|
use Damax\Bundle\ApiAuthBundle\Jwt\Token; |
|
9
|
|
|
use Damax\Bundle\ApiAuthBundle\Jwt\TokenParser; |
|
10
|
|
|
use Damax\Bundle\ApiAuthBundle\Security\Jwt\Authenticator; |
|
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\User\UserInterface; |
|
15
|
|
|
use Symfony\Component\Security\Core\User\UserProviderInterface; |
|
16
|
|
|
|
|
17
|
|
|
class AuthenticatorTest extends TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var Extractor|MockObject |
|
21
|
|
|
*/ |
|
22
|
|
|
private $extractor; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var TokenParser|MockObject |
|
26
|
|
|
*/ |
|
27
|
|
|
private $tokenParser; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var JwtAuthenticator |
|
31
|
|
|
*/ |
|
32
|
|
|
private $authenticator; |
|
33
|
|
|
|
|
34
|
|
|
protected function setUp() |
|
35
|
|
|
{ |
|
36
|
|
|
$this->extractor = $this->createMock(Extractor::class); |
|
37
|
|
|
$this->tokenParser = $this->createMock(TokenParser::class); |
|
38
|
|
|
$this->authenticator = new Authenticator($this->extractor, $this->tokenParser, 'username'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @test |
|
43
|
|
|
*/ |
|
44
|
|
|
public function it_checks_credentials() |
|
45
|
|
|
{ |
|
46
|
|
|
/** @var UserInterface $user */ |
|
47
|
|
|
$user = $this->createMock(UserInterface::class); |
|
48
|
|
|
|
|
49
|
|
|
$this->tokenParser |
|
50
|
|
|
->expects($this->once()) |
|
51
|
|
|
->method('isValid') |
|
52
|
|
|
->with('ABC') |
|
53
|
|
|
->willReturn(true) |
|
54
|
|
|
; |
|
55
|
|
|
|
|
56
|
|
|
$this->assertTrue($this->authenticator->checkCredentials('ABC', $user)); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @test |
|
61
|
|
|
*/ |
|
62
|
|
|
public function it_retrieves_user_by_identity_field() |
|
63
|
|
|
{ |
|
64
|
|
|
$this->tokenParser |
|
65
|
|
|
->expects($this->once()) |
|
66
|
|
|
->method('parse') |
|
67
|
|
|
->with('ABC') |
|
68
|
|
|
->willReturn(Token::fromClaims(['username' => '[email protected]'])) |
|
69
|
|
|
; |
|
70
|
|
|
|
|
71
|
|
|
/** @var UserInterface $user */ |
|
72
|
|
|
$user = $this->createMock(UserInterface::class); |
|
73
|
|
|
|
|
74
|
|
|
/** @var UserProviderInterface|MockObject $userProvider */ |
|
75
|
|
|
$userProvider = $this->createMock(UserProviderInterface::class); |
|
76
|
|
|
$userProvider |
|
77
|
|
|
->expects($this->once()) |
|
78
|
|
|
->method('loadUserByUsername') |
|
79
|
|
|
->with('[email protected]') |
|
80
|
|
|
->willReturn($user) |
|
81
|
|
|
; |
|
82
|
|
|
|
|
83
|
|
|
$this->assertSame($user, $this->authenticator->getUser('ABC', $userProvider)); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @test |
|
88
|
|
|
*/ |
|
89
|
|
|
public function it_retrieves_user_by_subject_field() |
|
90
|
|
|
{ |
|
91
|
|
|
$this->tokenParser |
|
92
|
|
|
->expects($this->once()) |
|
93
|
|
|
->method('parse') |
|
94
|
|
|
->with('ABC') |
|
95
|
|
|
->willReturn(Token::fromClaims(['sub' => '[email protected]'])) |
|
96
|
|
|
; |
|
97
|
|
|
|
|
98
|
|
|
/** @var UserInterface $user */ |
|
99
|
|
|
$user = $this->createMock(UserInterface::class); |
|
100
|
|
|
|
|
101
|
|
|
/** @var UserProviderInterface|MockObject $userProvider */ |
|
102
|
|
|
$userProvider = $this->createMock(UserProviderInterface::class); |
|
103
|
|
|
$userProvider |
|
104
|
|
|
->expects($this->once()) |
|
105
|
|
|
->method('loadUserByUsername') |
|
106
|
|
|
->with('[email protected]') |
|
107
|
|
|
->willReturn($user) |
|
108
|
|
|
; |
|
109
|
|
|
|
|
110
|
|
|
$this->assertSame($user, (new Authenticator($this->extractor, $this->tokenParser))->getUser('ABC', $userProvider)); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @test |
|
115
|
|
|
*/ |
|
116
|
|
|
public function it_throws_exception_when_retrieving_user_with_unregistered_claim() |
|
117
|
|
|
{ |
|
118
|
|
|
$this->tokenParser |
|
119
|
|
|
->expects($this->once()) |
|
120
|
|
|
->method('parse') |
|
121
|
|
|
->with('ABC') |
|
122
|
|
|
->willReturn(Token::fromClaims([])) |
|
123
|
|
|
; |
|
124
|
|
|
|
|
125
|
|
|
/** @var UserProviderInterface|MockObject $userProvider */ |
|
126
|
|
|
$userProvider = $this->createMock(UserProviderInterface::class); |
|
127
|
|
|
$userProvider |
|
128
|
|
|
->expects($this->never()) |
|
129
|
|
|
->method('loadUserByUsername') |
|
130
|
|
|
; |
|
131
|
|
|
|
|
132
|
|
|
$this->expectException(AuthenticationException::class); |
|
133
|
|
|
$this->expectExceptionMessage('Username could not be identified.'); |
|
134
|
|
|
|
|
135
|
|
|
$this->authenticator->getUser('ABC', $userProvider); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|