|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace PSR7CsrfTest; |
|
6
|
|
|
|
|
7
|
|
|
use Lcobucci\JWT\Signer; |
|
8
|
|
|
use Lcobucci\JWT\Signer\Hmac\Sha256; |
|
9
|
|
|
use PHPUnit\Framework\TestCase; |
|
10
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
11
|
|
|
use PSR7Csrf\Exception\InvalidExpirationTimeException; |
|
12
|
|
|
use PSR7Csrf\Exception\SessionAttributeNotFoundException; |
|
13
|
|
|
use PSR7Csrf\Session\ExtractUniqueKeyFromSessionInterface; |
|
14
|
|
|
use PSR7Csrf\TokenGenerator; |
|
15
|
|
|
use PSR7Sessions\Storageless\Session\SessionInterface; |
|
16
|
|
|
use stdClass; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @covers \PSR7Csrf\TokenGenerator |
|
20
|
|
|
*/ |
|
21
|
|
|
final class TokenGeneratorTest extends TestCase |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @dataProvider invalidExpirationTimeProvider |
|
25
|
|
|
* |
|
26
|
|
|
* @param int $invalidExpirationTime |
|
27
|
|
|
* @param bool $valid |
|
28
|
|
|
*/ |
|
29
|
|
|
public function testWillRejectInvalidExpirationTime(int $invalidExpirationTime, bool $valid) |
|
30
|
|
|
{ |
|
31
|
|
|
/* @var $signer Signer */ |
|
32
|
|
|
$signer = $this->createMock(Signer::class); |
|
33
|
|
|
/* @var $extractUniqueKeyFromSession ExtractUniqueKeyFromSessionInterface */ |
|
34
|
|
|
$extractUniqueKeyFromSession = $this->createMock(ExtractUniqueKeyFromSessionInterface::class); |
|
35
|
|
|
|
|
36
|
|
|
if (! $valid) { |
|
37
|
|
|
$this->expectException(InvalidExpirationTimeException::class); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
self::assertInstanceOf( |
|
41
|
|
|
TokenGenerator::class, |
|
42
|
|
|
new TokenGenerator($signer, $extractUniqueKeyFromSession, $invalidExpirationTime, 'session') |
|
43
|
|
|
); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function invalidExpirationTimeProvider() : array |
|
47
|
|
|
{ |
|
48
|
|
|
return [ |
|
49
|
|
|
[100, true], |
|
50
|
|
|
[1, true], |
|
51
|
|
|
[0, false], |
|
52
|
|
|
[-1, false], |
|
53
|
|
|
[-200, false], |
|
54
|
|
|
]; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @dataProvider validExpirationTimeProvider |
|
59
|
|
|
* |
|
60
|
|
|
* @param int $validExpirationTime |
|
61
|
|
|
*/ |
|
62
|
|
|
public function testWillGenerateAValidJWTToken(int $validExpirationTime) |
|
63
|
|
|
{ |
|
64
|
|
|
$signer = new Sha256(); |
|
65
|
|
|
/* @var $extractUniqueKeyFromSession ExtractUniqueKeyFromSessionInterface|\PHPUnit_Framework_MockObject_MockObject */ |
|
66
|
|
|
$extractUniqueKeyFromSession = $this->createMock(ExtractUniqueKeyFromSessionInterface::class); |
|
67
|
|
|
/* @var $session SessionInterface */ |
|
68
|
|
|
$session = $this->createMock(SessionInterface::class); |
|
69
|
|
|
/* @var $request ServerRequestInterface|\PHPUnit_Framework_MockObject_MockObject */ |
|
70
|
|
|
$request = $this->createMock(ServerRequestInterface::class); |
|
71
|
|
|
$sessionAttribute = uniqid('session', true); |
|
72
|
|
|
|
|
73
|
|
|
$generator = new TokenGenerator($signer, $extractUniqueKeyFromSession, $validExpirationTime, $sessionAttribute); |
|
|
|
|
|
|
74
|
|
|
$secretKey = uniqid('secretKey', true); |
|
75
|
|
|
|
|
76
|
|
|
$request->expects(self::any())->method('getAttribute')->with($sessionAttribute)->willReturn($session); |
|
77
|
|
|
$extractUniqueKeyFromSession->expects(self::any())->method('__invoke')->with($session)->willReturn($secretKey); |
|
78
|
|
|
|
|
79
|
|
|
$token = $generator->__invoke($request); |
|
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
self::assertTrue($token->verify($signer, $secretKey)); |
|
82
|
|
|
self::assertLessThanOrEqual(time(), $token->getClaim('iat')); |
|
83
|
|
|
self::assertGreaterThan(time(), $token->getClaim('exp')); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function validExpirationTimeProvider() : array |
|
87
|
|
|
{ |
|
88
|
|
|
return [ |
|
89
|
|
|
[10], |
|
90
|
|
|
[100], |
|
91
|
|
|
]; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function testWillFailIfTheSessionAttributeIsNotASession() |
|
95
|
|
|
{ |
|
96
|
|
|
/* @var $extractUniqueKeyFromSession ExtractUniqueKeyFromSessionInterface|\PHPUnit_Framework_MockObject_MockObject */ |
|
97
|
|
|
$extractUniqueKeyFromSession = $this->createMock(ExtractUniqueKeyFromSessionInterface::class); |
|
98
|
|
|
/* @var $request ServerRequestInterface|\PHPUnit_Framework_MockObject_MockObject */ |
|
99
|
|
|
$request = $this->createMock(ServerRequestInterface::class); |
|
100
|
|
|
$sessionAttribute = uniqid('session', true); |
|
101
|
|
|
|
|
102
|
|
|
$generator = new TokenGenerator(new Sha256(), $extractUniqueKeyFromSession, 10, $sessionAttribute); |
|
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
$request->expects(self::any())->method('getAttribute')->with($sessionAttribute)->willReturn(new stdClass()); |
|
105
|
|
|
$request->expects(self::any())->method('getAttributes')->willReturn([]); |
|
106
|
|
|
|
|
107
|
|
|
$this->expectException(SessionAttributeNotFoundException::class); |
|
108
|
|
|
|
|
109
|
|
|
$generator->__invoke($request); |
|
|
|
|
|
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.