-
-
Notifications
You must be signed in to change notification settings - Fork 592
/
HmacTokenTest.php
167 lines (141 loc) · 5.78 KB
/
HmacTokenTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
declare(strict_types=1);
namespace Lcobucci\JWT\FunctionalTests;
use DateTimeImmutable;
use Lcobucci\JWT\Configuration;
use Lcobucci\JWT\Signer\Hmac\Sha256;
use Lcobucci\JWT\Signer\Hmac\Sha512;
use Lcobucci\JWT\Signer\Key\InMemory;
use Lcobucci\JWT\Token;
use Lcobucci\JWT\Validation\Constraint\SignedWith;
use Lcobucci\JWT\Validation\RequiredConstraintsViolated;
use PHPUnit\Framework\TestCase;
use function assert;
/**
* @covers \Lcobucci\JWT\Configuration
* @covers \Lcobucci\JWT\Encoding\JoseEncoder
* @covers \Lcobucci\JWT\Encoding\ChainedFormatter
* @covers \Lcobucci\JWT\Encoding\MicrosecondBasedDateConversion
* @covers \Lcobucci\JWT\Encoding\UnifyAudience
* @covers \Lcobucci\JWT\Token\Builder
* @covers \Lcobucci\JWT\Token\Parser
* @covers \Lcobucci\JWT\Token\Plain
* @covers \Lcobucci\JWT\Token\DataSet
* @covers \Lcobucci\JWT\Token\Signature
* @covers \Lcobucci\JWT\Signer\Key\InMemory
* @covers \Lcobucci\JWT\Signer\Hmac
* @covers \Lcobucci\JWT\Signer\Hmac\Sha256
* @covers \Lcobucci\JWT\Signer\Hmac\Sha512
* @covers \Lcobucci\JWT\Validation\Validator
* @covers \Lcobucci\JWT\Validation\RequiredConstraintsViolated
* @covers \Lcobucci\JWT\Validation\Constraint\SignedWith
*/
class HmacTokenTest extends TestCase
{
private Configuration $config;
/** @before */
public function createConfiguration(): void
{
$this->config = Configuration::forSymmetricSigner(new Sha256(), InMemory::plainText('testing'));
}
/** @test */
public function builderCanGenerateAToken(): Token
{
$user = ['name' => 'testing', 'email' => 'testing@abc.com'];
$builder = $this->config->builder();
$token = $builder->identifiedBy('1')
->permittedFor('http://client.abc.com')
->issuedBy('http://api.abc.com')
->withClaim('user', $user)
->withHeader('jki', '1234')
->getToken($this->config->signer(), $this->config->signingKey());
self::assertEquals('1234', $token->headers()->get('jki'));
self::assertEquals(['http://client.abc.com'], $token->claims()->get(Token\RegisteredClaims::AUDIENCE));
self::assertEquals('http://api.abc.com', $token->claims()->get(Token\RegisteredClaims::ISSUER));
self::assertEquals($user, $token->claims()->get('user'));
return $token;
}
/**
* @test
* @depends builderCanGenerateAToken
*/
public function parserCanReadAToken(Token $generated): void
{
$read = $this->config->parser()->parse($generated->toString());
assert($read instanceof Token\Plain);
self::assertEquals($generated, $read);
self::assertEquals('testing', $read->claims()->get('user')['name']);
}
/**
* @test
* @depends builderCanGenerateAToken
*/
public function signatureAssertionShouldRaiseExceptionWhenKeyIsNotRight(Token $token): void
{
$this->expectException(RequiredConstraintsViolated::class);
$this->expectExceptionMessage('The token violates some mandatory constraints');
$this->config->validator()->assert(
$token,
new SignedWith($this->config->signer(), InMemory::plainText('testing1'))
);
}
/**
* @test
* @depends builderCanGenerateAToken
*/
public function signatureAssertionShouldRaiseExceptionWhenAlgorithmIsDifferent(Token $token): void
{
$this->expectException(RequiredConstraintsViolated::class);
$this->expectExceptionMessage('The token violates some mandatory constraints');
$this->config->validator()->assert(
$token,
new SignedWith(new Sha512(), $this->config->verificationKey())
);
}
/**
* @test
* @depends builderCanGenerateAToken
*/
public function signatureValidationShouldSucceedWhenKeyIsRight(Token $token): void
{
$constraint = new SignedWith($this->config->signer(), $this->config->verificationKey());
self::assertTrue($this->config->validator()->validate($token, $constraint));
}
/** @test */
public function everythingShouldWorkWhenUsingATokenGeneratedByOtherLibs(): void
{
$data = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXUyJ9.eyJoZWxsbyI6IndvcmxkIn0.Rh'
. '7AEgqCB7zae1PkgIlvOpeyw9Ab8NGTbeOH7heHO0o';
$token = $this->config->parser()->parse($data);
assert($token instanceof Token\Plain);
$constraint = new SignedWith($this->config->signer(), $this->config->verificationKey());
self::assertTrue($this->config->validator()->validate($token, $constraint));
self::assertEquals('world', $token->claims()->get('hello'));
}
/**
* @test
* @dataProvider datesWithPotentialRoundingIssues
*/
public function timeFractionsPrecisionsAreRespected(DateTimeImmutable $issuedAt): void
{
$token = $this->config->builder()
->issuedAt($issuedAt)
->getToken($this->config->signer(), $this->config->signingKey());
$parsedToken = $this->config->parser()->parse($token->toString());
self::assertInstanceOf(Token\Plain::class, $parsedToken);
self::assertSame($issuedAt->format('U.u'), $parsedToken->claims()->get('iat')->format('U.u'));
}
/** @return iterable<DateTimeImmutable[]> */
public function datesWithPotentialRoundingIssues(): iterable
{
$create = static function (string $timeFraction): DateTimeImmutable {
$date = DateTimeImmutable::createFromFormat('U.u', $timeFraction);
assert($date instanceof DateTimeImmutable);
return $date;
};
yield [$create('1613938511.017448')];
yield [$create('1613938511.023691')];
yield [$create('1613938511.018045')];
yield [$create('1616074725.008455')];
}
}