|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Damax\Bundle\ApiAuthBundle\Tests\Jwt\Lcobucci; |
|
6
|
|
|
|
|
7
|
|
|
use Damax\Bundle\ApiAuthBundle\Jwt\Lcobucci\Parser; |
|
8
|
|
|
use DateTimeImmutable; |
|
9
|
|
|
use Lcobucci\Clock\FrozenClock; |
|
10
|
|
|
use Lcobucci\JWT\Configuration as JwtConfiguration; |
|
11
|
|
|
use Lcobucci\JWT\Parser as JwtParser; |
|
12
|
|
|
use Lcobucci\JWT\Signer; |
|
13
|
|
|
use Lcobucci\JWT\Token; |
|
14
|
|
|
use Lcobucci\JWT\Validation\Constraint; |
|
15
|
|
|
use Lcobucci\JWT\Validator; |
|
16
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
17
|
|
|
use PHPUnit\Framework\TestCase; |
|
18
|
|
|
|
|
19
|
|
|
class ParserTest extends TestCase |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var Signer |
|
23
|
|
|
*/ |
|
24
|
|
|
private $signer; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var JwtParser|MockObject |
|
28
|
|
|
*/ |
|
29
|
|
|
private $jwtParser; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var Validator|MockObject |
|
33
|
|
|
*/ |
|
34
|
|
|
private $validator; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var Signer\Key |
|
38
|
|
|
*/ |
|
39
|
|
|
private $key; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var FrozenClock |
|
43
|
|
|
*/ |
|
44
|
|
|
private $clock; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var Parser |
|
48
|
|
|
*/ |
|
49
|
|
|
private $parser; |
|
50
|
|
|
|
|
51
|
|
|
protected function setUp() |
|
52
|
|
|
{ |
|
53
|
|
|
$this->signer = new Signer\None(); |
|
54
|
|
|
$this->jwtParser = $this->createMock(JwtParser::class); |
|
55
|
|
|
$this->validator = $this->createMock(Validator::class); |
|
56
|
|
|
$this->key = new Signer\Key(''); |
|
57
|
|
|
|
|
58
|
|
|
$config = JwtConfiguration::forSymmetricSigner($this->signer, $this->key); |
|
59
|
|
|
$config->setParser($this->jwtParser); |
|
60
|
|
|
$config->setValidator($this->validator); |
|
61
|
|
|
|
|
62
|
|
|
$this->clock = new FrozenClock(new DateTimeImmutable('2018-02-09 06:10:00')); |
|
63
|
|
|
$this->parser = new Parser($config, $this->clock, ['github', 'bitbucket'], 'app'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @test |
|
68
|
|
|
*/ |
|
69
|
|
|
public function it_validates_jwt_string() |
|
70
|
|
|
{ |
|
71
|
|
|
/** @var Token $token */ |
|
72
|
|
|
$token = $this->createMock(Token::class); |
|
73
|
|
|
|
|
74
|
|
|
$this->jwtParser |
|
75
|
|
|
->expects($this->once()) |
|
76
|
|
|
->method('parse') |
|
77
|
|
|
->with('XYZ') |
|
78
|
|
|
->willReturn($token) |
|
79
|
|
|
; |
|
80
|
|
|
$this->validator |
|
81
|
|
|
->expects($this->once()) |
|
82
|
|
|
->method('validate') |
|
83
|
|
|
->willReturnCallback(function (Token $jwtToken, ...$constraints) use ($token) { |
|
84
|
|
|
$this->assertSame($token, $jwtToken); |
|
85
|
|
|
$this->assertCount(4, $constraints); |
|
86
|
|
|
$this->assertContainsOnlyInstancesOf(Constraint::class, $constraints); |
|
87
|
|
|
|
|
88
|
|
|
$constraint = $constraints[0]; |
|
89
|
|
|
$this->assertInstanceOf(Constraint\ValidAt::class, $constraint); |
|
90
|
|
|
$this->assertAttributeSame($this->clock, 'clock', $constraint); |
|
91
|
|
|
|
|
92
|
|
|
$constraint = $constraints[1]; |
|
93
|
|
|
$this->assertInstanceOf(Constraint\SignedWith::class, $constraint); |
|
94
|
|
|
$this->assertAttributeSame($this->signer, 'signer', $constraint); |
|
95
|
|
|
$this->assertAttributeSame($this->key, 'key', $constraint); |
|
96
|
|
|
|
|
97
|
|
|
$constraint = $constraints[2]; |
|
98
|
|
|
$this->assertInstanceOf(Constraint\IssuedBy::class, $constraint); |
|
99
|
|
|
$this->assertAttributeEquals(['github', 'bitbucket'], 'issuers', $constraint); |
|
100
|
|
|
|
|
101
|
|
|
$constraint = $constraints[3]; |
|
102
|
|
|
$this->assertInstanceOf(Constraint\PermittedFor::class, $constraint); |
|
103
|
|
|
$this->assertAttributeEquals('app', 'audience', $constraint); |
|
104
|
|
|
|
|
105
|
|
|
return true; |
|
106
|
|
|
}) |
|
107
|
|
|
; |
|
108
|
|
|
|
|
109
|
|
|
$this->assertTrue($this->parser->isValid('XYZ')); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @test |
|
114
|
|
|
*/ |
|
115
|
|
|
public function it_parses_jwt_string() |
|
116
|
|
|
{ |
|
117
|
|
|
$claims = new Token\DataSet(['foo' => 'bar', 'baz' => 'qux'], ''); |
|
118
|
|
|
|
|
119
|
|
|
$jwtToken = new Token\Plain(new Token\DataSet([], ''), $claims, Token\Signature::fromEmptyData()); |
|
120
|
|
|
|
|
121
|
|
|
$this->jwtParser |
|
122
|
|
|
->expects($this->once()) |
|
123
|
|
|
->method('parse') |
|
124
|
|
|
->with('XYZ') |
|
125
|
|
|
->willReturn($jwtToken) |
|
126
|
|
|
; |
|
127
|
|
|
|
|
128
|
|
|
$this->assertEquals(['foo' => 'bar', 'baz' => 'qux'], $this->parser->parse('XYZ')->all()); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|