1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* This file is part of the daikon-cqrs/security-interop project. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Daikon\Tests\Security\Middleware; |
10
|
|
|
|
11
|
|
|
use Daikon\Config\ConfigProviderInterface; |
12
|
|
|
use Daikon\Security\Middleware\JwtDecoder; |
13
|
|
|
use Firebase\JWT\JWT; |
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
16
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
17
|
|
|
|
18
|
|
|
final class JwtDecoderTest extends TestCase |
19
|
|
|
{ |
20
|
1 |
|
public function testEmptyAuth(): void |
21
|
|
|
{ |
22
|
1 |
|
$config = $this->createMock(ConfigProviderInterface::class); |
23
|
1 |
|
$config->expects($this->once())->method('get')->with('project.authentication')->willReturn([]); |
24
|
1 |
|
$request = $this->createMock(ServerRequestInterface::class); |
25
|
1 |
|
$request->expects($this->once())->method('getCookieParams')->willReturn(null); |
26
|
1 |
|
$request->expects($this->exactly(2))->method('getHeaderLine')->willReturn(''); |
27
|
1 |
|
$request->expects($this->exactly(2))->method('withAttribute')->willReturnSelf(); |
28
|
1 |
|
$handler = $this->createMock(RequestHandlerInterface::class); |
29
|
|
|
/** |
30
|
|
|
* @var ConfigProviderInterface $config |
31
|
|
|
* @var ServerRequestInterface $request |
32
|
|
|
* @var RequestHandlerInterface $handler |
33
|
|
|
*/ |
34
|
1 |
|
$decoder = new JwtDecoder($config); |
35
|
1 |
|
$decoder->process($request, $handler); |
36
|
1 |
|
} |
37
|
|
|
|
38
|
1 |
|
public function testHeaderAuthWithInvalidJwt(): void |
39
|
|
|
{ |
40
|
1 |
|
$config = $this->createMock(ConfigProviderInterface::class); |
41
|
1 |
|
$config->expects($this->at(0))->method('get')->with('project.authentication')->willReturn([]); |
42
|
1 |
|
$config->expects($this->at(1))->method('get')->with('project.authentication.jwt.secret')->willReturn('key'); |
43
|
1 |
|
$request = $this->createMock(ServerRequestInterface::class); |
44
|
1 |
|
$request->expects($this->at(0))->method('getCookieParams')->willReturn(null); |
45
|
1 |
|
$request->expects($this->at(1))->method('getHeaderLine')->with('Authorization')->willReturn('Bearer xyz'); |
46
|
1 |
|
$request->expects($this->at(2))->method('getHeaderLine')->with('X-XSRF-TOKEN')->willReturn('xsrf'); |
47
|
1 |
|
$request->expects($this->at(3))->method('withAttribute')->with('__Host-_jwt', null)->willReturnSelf(); |
48
|
1 |
|
$request->expects($this->at(4))->method('withAttribute')->with('__Host-_xsrf', 'xsrf')->willReturnSelf(); |
49
|
1 |
|
$handler = $this->createMock(RequestHandlerInterface::class); |
50
|
|
|
/** |
51
|
|
|
* @var ConfigProviderInterface $config |
52
|
|
|
* @var ServerRequestInterface $request |
53
|
|
|
* @var RequestHandlerInterface $handler |
54
|
|
|
*/ |
55
|
1 |
|
$decoder = new JwtDecoder($config); |
56
|
1 |
|
$decoder->process($request, $handler); |
57
|
1 |
|
} |
58
|
|
|
|
59
|
1 |
|
public function testHeaderAuthWithValidJwt(): void |
60
|
|
|
{ |
61
|
1 |
|
$jwt = JWT::encode(['iss' => 'test', 'xsrf' => 'xsrf'], 'key'); |
62
|
1 |
|
$decodedJwt = JWT::decode($jwt, 'key', ['HS256']); |
63
|
1 |
|
$config = $this->createMock(ConfigProviderInterface::class); |
64
|
1 |
|
$config->expects($this->at(0))->method('get')->with('project.authentication')->willReturn([]); |
65
|
1 |
|
$config->expects($this->at(1))->method('get')->with('project.authentication.jwt.secret')->willReturn('key'); |
66
|
1 |
|
$request = $this->createMock(ServerRequestInterface::class); |
67
|
1 |
|
$request->expects($this->at(0))->method('getCookieParams')->willReturn(null); |
68
|
1 |
|
$request->expects($this->at(1))->method('getHeaderLine')->with('Authorization')->willReturn("Bearer $jwt"); |
69
|
1 |
|
$request->expects($this->at(2))->method('getHeaderLine')->with('X-XSRF-TOKEN')->willReturn('xsrf'); |
70
|
1 |
|
$request->expects($this->at(3))->method('withAttribute')->with('__Host-_jwt', $decodedJwt)->willReturnSelf(); |
71
|
1 |
|
$request->expects($this->at(4))->method('withAttribute')->with('__Host-_xsrf', 'xsrf')->willReturnSelf(); |
72
|
1 |
|
$handler = $this->createMock(RequestHandlerInterface::class); |
73
|
|
|
/** |
74
|
|
|
* @var ConfigProviderInterface $config |
75
|
|
|
* @var ServerRequestInterface $request |
76
|
|
|
* @var RequestHandlerInterface $handler |
77
|
|
|
*/ |
78
|
1 |
|
$decoder = new JwtDecoder($config); |
79
|
1 |
|
$decoder->process($request, $handler); |
80
|
1 |
|
} |
81
|
|
|
} |
82
|
|
|
|