-
Notifications
You must be signed in to change notification settings - Fork 30
/
JwtVerifierTest.php
168 lines (140 loc) · 6.44 KB
/
JwtVerifierTest.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
168
<?php declare(strict_types=1);
namespace Azimo\Apple\Tests\Unit\Auth\Jwt;
use Azimo\Apple\APi;
use Azimo\Apple\Auth\Exception;
use Azimo\Apple\Auth\Jwt\JwtVerifier;
use Lcobucci\JWT;
use Mockery;
use Mockery\Adapter\Phpunit\MockeryTestCase;
use OutOfBoundsException;
class JwtVerifierTest extends MockeryTestCase
{
private APi\AppleApiClientInterface $appleApiClient;
private JWT\Token $jwtTokenMock;
private JWT\Validator $validatorMock;
private JwtVerifier $jwtVerifier;
protected function setUp(): void
{
parent::setUp();
$this->validatorMock = Mockery::mock(JWT\Validator::class);
$this->appleApiClient = Mockery::mock(Api\AppleApiClientInterface::class);
$this->jwtTokenMock = Mockery::mock(JWT\Token::class);
$this->jwtVerifier = new JwtVerifier(
$this->appleApiClient,
$this->validatorMock,
new JWT\Signer\Rsa\Sha256()
);
}
public function testIfVerifyThrowsKeysFetchingFailedExceptionWhenFailedToFetchJsonWebKeySet(): void
{
$this->appleApiClient->shouldReceive('getAuthKeys')
->once()
->withNoArgs()
->andThrow(new Api\Exception\PublicKeyFetchingFailedException('Connection error'));
$this->expectException(Exception\KeysFetchingFailedException::class);
$this->expectExceptionMessage('Connection error');
$this->jwtVerifier->verify($this->jwtTokenMock);
}
public function testIfVerifyThrowsInvalidCryptographicAlgorithmExceptionWhenKidHeaderNotExist(): void
{
$this->appleApiClient->shouldReceive('getAuthKeys')
->once()
->withNoArgs()
->andReturn(new Api\Response\JsonWebKeySetCollection([]));
$this->jwtTokenMock->shouldReceive('headers')
->once()
->withNoArgs()
->andThrow(new OutOfBoundsException('`kid` header is missing'));
$this->expectException(Exception\InvalidCryptographicAlgorithmException::class);
$this->expectExceptionMessage('`kid` header is missing');
$this->jwtVerifier->verify($this->jwtTokenMock);
}
public function testIfVerifyThrowsInvalidCryptographicAlgorithmExceptionWhenAlgorithmIsNotSupported(): void
{
$this->appleApiClient->shouldReceive('getAuthKeys')
->once()
->withNoArgs()
->andReturn(new Api\Response\JsonWebKeySetCollection([]));
$this->jwtTokenMock->shouldReceive('headers')
->once()
->withNoArgs()
->andReturn(new JWT\Token\DataSet(['kid' => 'foo'], ''));
$this->expectException(Exception\InvalidCryptographicAlgorithmException::class);
$this->expectExceptionMessage(
'Cryptographic algorithm `foo` is not supported. Supported algorithms: `86D88Kf,eXaunmL,YuyXoY,W6WcOKB`'
);
$this->jwtVerifier->verify($this->jwtTokenMock);
}
public function testIfVerifyThrowsInvalidCryptographicAlgorithmExceptionWhenAuthKeyNotExistForGivenAlgorithm(): void
{
$this->appleApiClient->shouldReceive('getAuthKeys')
->once()
->withNoArgs()
->andReturn(new Api\Response\JsonWebKeySetCollection([]));
$this->jwtTokenMock->shouldReceive('headers')
->once()
->withNoArgs()
->andReturn(new JWT\Token\DataSet(['kid' => '86D88Kf'], ''));
$this->expectException(Exception\InvalidCryptographicAlgorithmException::class);
$this->expectExceptionMessage('Unsupported cryptographic algorithm passed `86D88Kf');
$this->jwtVerifier->verify($this->jwtTokenMock);
}
public function testIfVerifyReturnsTrueWhenTokenIsCorrect(): void
{
$this->appleApiClient->shouldReceive('getAuthKeys')
->once()
->withNoArgs()
->andReturn(
new Api\Response\JsonWebKeySetCollection(
[
'86D88Kf' => new Api\Response\JsonWebKeySet(
'RSA',
'86D88Kf',
'sig',
'RS256',
'iGaLqP6y-SJCCBq5Hv6pGDbG_SQ11MNjH7rWHcCFYz4hGwHC4lcSurTlV8u3avoVNM8jXevG1Iu1SY11qInqUvjJur--hghr1b56OPJu6H1iKulSxGjEIyDP6c5BdE1uwprYyr4IO9th8fOwCPygjLFrh44XEGbDIFeImwvBAGOhmMB2AD1n1KviyNsH0bEB7phQtiLk-ILjv1bORSRl8AK677-1T8isGfHKXGZ_ZGtStDe7Lu0Ihp8zoUt59kx2o9uWpROkzF56ypresiIl4WprClRCjz8x6cPZXU2qNWhu71TQvUFwvIvbkE1oYaJMb0jcOTmBRZA2QuYw-zHLwQ',
'AQAB'
),
]
)
);
$this->jwtTokenMock->shouldReceive('headers')
->once()
->withNoArgs()
->andReturn(new JWT\Token\DataSet(['kid' => '86D88Kf'], ''));
$this->validatorMock->shouldReceive('validate')
->once()
->with($this->jwtTokenMock, JWT\Validation\Constraint\SignedWith::class)
->andReturn(true);
self::assertTrue($this->jwtVerifier->verify($this->jwtTokenMock));
}
public function testIfVerifyReturnsTrueWhenTokenIsCorrectMalformed(): void
{
$this->appleApiClient->shouldReceive('getAuthKeys')
->once()
->withNoArgs()
->andReturn(
new Api\Response\JsonWebKeySetCollection(
[
'86D88Kf' => new Api\Response\JsonWebKeySet(
'RSA',
'86D88Kf',
'sig',
'RS256',
'iGaLqP6y-SJCCBq5Hv6pGDbG_SQ11MNjH7rWHcCFYz4hGwHC4lcSurTlV8u3avoVNM8jXevG1Iu1SY11qInqUvjJur--hghr1b56OPJu6H1iKulSxGjEIyDP6c5BdE1uwprYyr4IO9th8fOwCPygjLFrh44XEGbDIFeImwvBAGOhmMB2AD1n1KviyNsH0bEB7phQtiLk-ILjv1bORSRl8AK677-1T8isGfHKXGZ_ZGtStDe7Lu0Ihp8zoUt59kx2o9uWpROkzF56ypresiIl4WprClRCjz8x6cPZXU2qNWhu71TQvUFwvIvbkE1oYaJMb0jcOTmBRZA2QuYw-zHLwQ',
'AQAB'
),
]
)
);
$this->jwtTokenMock->shouldReceive('headers')
->once()
->withNoArgs()
->andReturn(new JWT\Token\DataSet(['kid' => '86D88Kf'], ''));
$this->validatorMock->shouldReceive('validate')
->once()
->with($this->jwtTokenMock, JWT\Validation\Constraint\SignedWith::class)
->andReturn(false);
self::assertFalse($this->jwtVerifier->verify($this->jwtTokenMock));
}
}