|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace ShlinkioTest\Shlink\Rest\Authentication\Plugin; |
|
5
|
|
|
|
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
|
7
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
|
8
|
|
|
use Shlinkio\Shlink\Rest\Authentication\JWTServiceInterface; |
|
9
|
|
|
use Shlinkio\Shlink\Rest\Authentication\Plugin\AuthorizationHeaderPlugin; |
|
10
|
|
|
use Shlinkio\Shlink\Rest\Exception\VerifyAuthenticationException; |
|
11
|
|
|
use Zend\Diactoros\Response; |
|
12
|
|
|
use Zend\Diactoros\ServerRequestFactory; |
|
13
|
|
|
use Zend\I18n\Translator\Translator; |
|
14
|
|
|
|
|
15
|
|
|
class AuthorizationHeaderPluginTest extends TestCase |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var AuthorizationHeaderPlugin |
|
19
|
|
|
*/ |
|
20
|
|
|
private $plugin; |
|
21
|
|
|
/** |
|
22
|
|
|
* @var ObjectProphecy |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $jwtService; |
|
25
|
|
|
|
|
26
|
|
|
public function setUp() |
|
27
|
|
|
{ |
|
28
|
|
|
$this->jwtService = $this->prophesize(JWTServiceInterface::class); |
|
29
|
|
|
$this->plugin = new AuthorizationHeaderPlugin($this->jwtService->reveal(), Translator::factory([])); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @test |
|
34
|
|
|
*/ |
|
35
|
|
View Code Duplication |
public function verifyAnAuthorizationWithoutBearerTypeThrowsException() |
|
|
|
|
|
|
36
|
|
|
{ |
|
37
|
|
|
$authToken = 'ABC-abc'; |
|
38
|
|
|
$request = ServerRequestFactory::fromGlobals()->withHeader( |
|
39
|
|
|
AuthorizationHeaderPlugin::HEADER_NAME, |
|
40
|
|
|
$authToken |
|
41
|
|
|
); |
|
42
|
|
|
|
|
43
|
|
|
$this->expectException(VerifyAuthenticationException::class); |
|
44
|
|
|
$this->expectExceptionMessage(sprintf( |
|
45
|
|
|
'You need to provide the Bearer type in the %s header.', |
|
46
|
|
|
AuthorizationHeaderPlugin::HEADER_NAME |
|
47
|
|
|
)); |
|
48
|
|
|
|
|
49
|
|
|
$this->plugin->verify($request); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @test |
|
54
|
|
|
*/ |
|
55
|
|
View Code Duplication |
public function verifyAnAuthorizationWithWrongTypeThrowsException() |
|
|
|
|
|
|
56
|
|
|
{ |
|
57
|
|
|
$authToken = 'Basic ABC-abc'; |
|
58
|
|
|
$request = ServerRequestFactory::fromGlobals()->withHeader( |
|
59
|
|
|
AuthorizationHeaderPlugin::HEADER_NAME, |
|
60
|
|
|
$authToken |
|
61
|
|
|
); |
|
62
|
|
|
|
|
63
|
|
|
$this->expectException(VerifyAuthenticationException::class); |
|
64
|
|
|
$this->expectExceptionMessage( |
|
65
|
|
|
'Provided authorization type Basic is not supported. Use Bearer instead.' |
|
66
|
|
|
); |
|
67
|
|
|
|
|
68
|
|
|
$this->plugin->verify($request); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @test |
|
73
|
|
|
*/ |
|
74
|
|
|
public function verifyAnExpiredTokenThrowsException() |
|
75
|
|
|
{ |
|
76
|
|
|
$authToken = 'Bearer ABC-abc'; |
|
77
|
|
|
$request = ServerRequestFactory::fromGlobals()->withHeader( |
|
78
|
|
|
AuthorizationHeaderPlugin::HEADER_NAME, |
|
79
|
|
|
$authToken |
|
80
|
|
|
); |
|
81
|
|
|
$jwtVerify = $this->jwtService->verify('ABC-abc')->willReturn(false); |
|
82
|
|
|
|
|
83
|
|
|
$this->expectException(VerifyAuthenticationException::class); |
|
84
|
|
|
$this->expectExceptionMessage(sprintf( |
|
85
|
|
|
'Missing or invalid auth token provided. Perform a new authentication request and send provided ' |
|
86
|
|
|
. 'token on every new request on the %s header', |
|
87
|
|
|
AuthorizationHeaderPlugin::HEADER_NAME |
|
88
|
|
|
)); |
|
89
|
|
|
|
|
90
|
|
|
$this->plugin->verify($request); |
|
91
|
|
|
|
|
92
|
|
|
$jwtVerify->shouldHaveBeenCalledTimes(1); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @test |
|
97
|
|
|
*/ |
|
98
|
|
|
public function verifyValidTokenDoesNotThrowException() |
|
99
|
|
|
{ |
|
100
|
|
|
$authToken = 'Bearer ABC-abc'; |
|
101
|
|
|
$request = ServerRequestFactory::fromGlobals()->withHeader( |
|
102
|
|
|
AuthorizationHeaderPlugin::HEADER_NAME, |
|
103
|
|
|
$authToken |
|
104
|
|
|
); |
|
105
|
|
|
$jwtVerify = $this->jwtService->verify('ABC-abc')->willReturn(true); |
|
106
|
|
|
|
|
107
|
|
|
$this->plugin->verify($request); |
|
108
|
|
|
|
|
109
|
|
|
$jwtVerify->shouldHaveBeenCalledTimes(1); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @test |
|
114
|
|
|
*/ |
|
115
|
|
|
public function updateReturnsAnUpdatedResponseWithNewJwt() |
|
116
|
|
|
{ |
|
117
|
|
|
$authToken = 'Bearer ABC-abc'; |
|
118
|
|
|
$request = ServerRequestFactory::fromGlobals()->withHeader( |
|
119
|
|
|
AuthorizationHeaderPlugin::HEADER_NAME, |
|
120
|
|
|
$authToken |
|
121
|
|
|
); |
|
122
|
|
|
$jwtRefresh = $this->jwtService->refresh('ABC-abc')->willReturn('DEF-def'); |
|
123
|
|
|
|
|
124
|
|
|
$response = $this->plugin->update($request, new Response()); |
|
125
|
|
|
|
|
126
|
|
|
$this->assertTrue($response->hasHeader(AuthorizationHeaderPlugin::HEADER_NAME)); |
|
127
|
|
|
$this->assertEquals('Bearer DEF-def', $response->getHeaderLine(AuthorizationHeaderPlugin::HEADER_NAME)); |
|
128
|
|
|
$jwtRefresh->shouldHaveBeenCalledTimes(1); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.