-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathSignedRequestStrategyTest.php
More file actions
199 lines (165 loc) · 6.11 KB
/
Copy pathSignedRequestStrategyTest.php
File metadata and controls
199 lines (165 loc) · 6.11 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
declare(strict_types=1);
namespace Yoti\Test\Http\AuthStrategy;
use Yoti\Http\AuthStrategy\SignedRequestStrategy;
use Yoti\Http\Payload;
use Yoti\Test\TestCase;
use Yoti\Test\TestData;
use Yoti\Util\PemFile;
/**
* @coversDefaultClass \Yoti\Http\AuthStrategy\SignedRequestStrategy
*/
class SignedRequestStrategyTest extends TestCase
{
private const SOME_SDK_ID = 'some-sdk-id';
private const SOME_HTTP_METHOD = 'GET';
private const SOME_ENDPOINT = '/some/endpoint';
/**
* @test
* @covers ::__construct
* @covers ::createAuthHeaders
*/
public function shouldReturnDigestHeader()
{
$pemFile = PemFile::fromFilePath(TestData::PEM_FILE);
$strategy = new SignedRequestStrategy($pemFile);
$headers = $strategy->createAuthHeaders(self::SOME_HTTP_METHOD, self::SOME_ENDPOINT, null);
$this->assertArrayHasKey('X-Yoti-Auth-Digest', $headers);
$this->assertNotEmpty($headers['X-Yoti-Auth-Digest']);
}
/**
* @test
* @covers ::createAuthHeaders
*/
public function shouldReturnDigestHeaderWithPayload()
{
$pemFile = PemFile::fromFilePath(TestData::PEM_FILE);
$strategy = new SignedRequestStrategy($pemFile);
$payload = Payload::fromString('some payload content');
$headers = $strategy->createAuthHeaders('POST', self::SOME_ENDPOINT, $payload);
$this->assertArrayHasKey('X-Yoti-Auth-Digest', $headers);
$this->assertNotEmpty($headers['X-Yoti-Auth-Digest']);
}
/**
* @test
* @covers ::createQueryParams
*/
public function shouldReturnNonceAndTimestampQueryParams()
{
$pemFile = PemFile::fromFilePath(TestData::PEM_FILE);
$strategy = new SignedRequestStrategy($pemFile);
$params = $strategy->createQueryParams();
$this->assertArrayHasKey('nonce', $params);
$this->assertArrayHasKey('timestamp', $params);
$this->assertNotEmpty($params['nonce']);
$this->assertNotEmpty($params['timestamp']);
}
/**
* @test
* @covers ::createQueryParams
* @dataProvider lowPrecisionProvider
*
* On PHP 8.4 FPM environments (e.g. WP Engine) the `precision` ini setting
* can cause (string)(round(microtime(true) * 1000)) to emit scientific notation
* (e.g. "1.78231576830E+12"), which breaks Yoti signature verification with 401.
*/
public function shouldReturnTimestampAsPlainIntegerStringUnderLowPrecision(string $precision)
{
$strategy = new SignedRequestStrategy(PemFile::fromFilePath(TestData::PEM_FILE));
$originalPrecision = ini_get('precision');
if ($originalPrecision === false || ini_set('precision', $precision) === false) {
$this->markTestSkipped('Unable to set ini precision for this runtime');
}
try {
$params = $strategy->createQueryParams();
} finally {
ini_set('precision', (string) $originalPrecision);
}
$this->assertMatchesRegularExpression(
'/^\d+$/',
$params['timestamp'],
"Timestamp must be a plain integer string (no scientific notation) at precision={$precision}"
);
}
public function lowPrecisionProvider(): array
{
return [
'precision=12' => ['12'],
'precision=8' => ['8'],
];
}
/**
* @test
* @covers ::createQueryParams
*/
public function shouldReturnTimestampAsUnixMilliseconds()
{
$strategy = new SignedRequestStrategy(PemFile::fromFilePath(TestData::PEM_FILE));
$originalPrecision = ini_get('precision');
if ($originalPrecision === false || ini_set('precision', '8') === false) {
$this->markTestSkipped('Unable to set ini precision for this runtime');
}
try {
$beforeMs = (int) floor(microtime(true) * 1000);
$params = $strategy->createQueryParams();
$afterMs = (int) ceil(microtime(true) * 1000);
} finally {
ini_set('precision', (string) $originalPrecision);
}
// Assert plain integer format first — (int) cast alone would mask scientific notation
$this->assertMatchesRegularExpression('/^\d+$/', $params['timestamp']);
$this->assertGreaterThanOrEqual($beforeMs, (int) $params['timestamp']);
$this->assertLessThanOrEqual($afterMs, (int) $params['timestamp']);
}
/**
* @test
* @covers ::createQueryParams
*/
public function shouldIncludeNonceAsUuidFormat()
{
$pemFile = PemFile::fromFilePath(TestData::PEM_FILE);
$strategy = new SignedRequestStrategy($pemFile);
$params = $strategy->createQueryParams();
// UUID v4 pattern: 8-4-4-4-12 hex chars
$this->assertMatchesRegularExpression(
'/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/',
$params['nonce']
);
}
/**
* @test
* @covers ::__construct
* @covers ::createQueryParams
*/
public function shouldIncludeSdkIdWhenProvided()
{
$pemFile = PemFile::fromFilePath(TestData::PEM_FILE);
$strategy = new SignedRequestStrategy($pemFile, self::SOME_SDK_ID);
$params = $strategy->createQueryParams();
$this->assertArrayHasKey('sdkId', $params);
$this->assertEquals(self::SOME_SDK_ID, $params['sdkId']);
}
/**
* @test
* @covers ::createQueryParams
*/
public function shouldNotIncludeSdkIdWhenNotProvided()
{
$pemFile = PemFile::fromFilePath(TestData::PEM_FILE);
$strategy = new SignedRequestStrategy($pemFile);
$params = $strategy->createQueryParams();
$this->assertArrayNotHasKey('sdkId', $params);
}
/**
* @test
* @covers ::createQueryParams
*/
public function shouldReturnDifferentNonceEachTime()
{
$pemFile = PemFile::fromFilePath(TestData::PEM_FILE);
$strategy = new SignedRequestStrategy($pemFile);
$params1 = $strategy->createQueryParams();
$params2 = $strategy->createQueryParams();
$this->assertNotEquals($params1['nonce'], $params2['nonce']);
}
}