|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Damax\Bundle\ApiAuthBundle\Tests\Jwt\Lcobucci; |
|
6
|
|
|
|
|
7
|
|
|
use Damax\Bundle\ApiAuthBundle\Jwt\Claims\FixedClaims; |
|
8
|
|
|
use Damax\Bundle\ApiAuthBundle\Jwt\Lcobucci\Builder; |
|
9
|
|
|
use DateTimeImmutable; |
|
10
|
|
|
use Lcobucci\JWT\Configuration as JwtConfiguration; |
|
11
|
|
|
use Lcobucci\JWT\Signer; |
|
12
|
|
|
use PHPUnit\Framework\TestCase; |
|
13
|
|
|
use Symfony\Component\Security\Core\User\User; |
|
14
|
|
|
|
|
15
|
|
|
class BuilderTest extends TestCase |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var JwtConfiguration |
|
19
|
|
|
*/ |
|
20
|
|
|
private $config; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var Builder |
|
24
|
|
|
*/ |
|
25
|
|
|
private $builder; |
|
26
|
|
|
|
|
27
|
|
|
protected function setUp() |
|
28
|
|
|
{ |
|
29
|
|
|
$this->config = JwtConfiguration::forSymmetricSigner(new Signer\None(), new Signer\Key('')); |
|
30
|
|
|
$this->builder = new Builder($this->config, new FixedClaims([ |
|
31
|
|
|
'sub' => '[email protected]', |
|
32
|
|
|
'iss' => 'github', |
|
33
|
|
|
'aud' => 'app', |
|
34
|
|
|
'exp' => new DateTimeImmutable('2018-02-09 07:10:00'), |
|
35
|
|
|
'iat' => new DateTimeImmutable('2018-02-09 06:10:00'), |
|
36
|
|
|
'nbf' => new DateTimeImmutable('2018-02-09 06:10:00'), |
|
37
|
|
|
'jti' => '123', |
|
38
|
|
|
'foo' => 'bar', |
|
39
|
|
|
'baz' => 'qux', |
|
40
|
|
|
])); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @test |
|
45
|
|
|
*/ |
|
46
|
|
|
public function it_builds_jwt_string() |
|
47
|
|
|
{ |
|
48
|
|
|
$user = new User('[email protected]', 'qwerty'); |
|
49
|
|
|
|
|
50
|
|
|
$jwtToken = $this->config->getParser()->parse($this->builder->fromUser($user)); |
|
51
|
|
|
|
|
52
|
|
|
$this->assertEquals([ |
|
53
|
|
|
'sub' => '[email protected]', |
|
54
|
|
|
'iss' => 'github', |
|
55
|
|
|
'aud' => ['app'], |
|
56
|
|
|
'exp' => new DateTimeImmutable('2018-02-09 07:10:00'), |
|
57
|
|
|
'iat' => new DateTimeImmutable('2018-02-09 06:10:00'), |
|
58
|
|
|
'nbf' => new DateTimeImmutable('2018-02-09 06:10:00'), |
|
59
|
|
|
'jti' => '123', |
|
60
|
|
|
'foo' => 'bar', |
|
61
|
|
|
'baz' => 'qux', |
|
62
|
|
|
], $jwtToken->claims()->all()); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|