-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathRequestBuilder.php
More file actions
402 lines (348 loc) · 9.55 KB
/
RequestBuilder.php
File metadata and controls
402 lines (348 loc) · 9.55 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
<?php
declare(strict_types=1);
namespace Yoti\Http;
use GuzzleHttp\Psr7\Request as RequestMessage;
use GuzzleHttp\Psr7\Utils;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\StreamInterface;
use Yoti\Util\Config;
use Yoti\Util\PemFile;
class RequestBuilder
{
/** Digest HTTP header key. */
private const YOTI_DIGEST_HEADER_KEY = 'X-Yoti-Auth-Digest';
/** SDK Identifier HTTP header key. */
private const YOTI_SDK_IDENTIFIER_HEADER_KEY = 'X-Yoti-SDK';
/** SDK Version HTTP header key. */
private const YOTI_SDK_VERSION_HEADER_KEY = 'X-Yoti-SDK-Version';
/**
* @var string
*/
private $baseUrl;
/**
* @var \Yoti\Util\PemFile
*/
private $pemFile;
/**
* @var array<string, string>
*/
private $headers = [];
/**
* @var array<string, string>
*/
private $queryParams = [];
/**
* @var string
*/
private $method;
/**
* @var string
*/
private $endpoint;
/**
* @var \Yoti\Http\Payload
*/
private $payload;
/**
* @var \Psr\Http\Client\ClientInterface
*/
private $client;
/**
* @var \Yoti\Util\Config
*/
private $config;
/**
* @var MultipartEntity
*/
private $multipartEntity;
/**
* @param \Yoti\Util\Config $config
*/
public function __construct(?Config $config = null)
{
$this->config = $config ?? new Config();
}
/**
* @param string $baseUrl
* Base URL with no trailing slashes.
*
* @return \Yoti\Http\RequestBuilder
*/
public function withBaseUrl(string $baseUrl): self
{
$this->baseUrl = rtrim($baseUrl, '/');
return $this;
}
/**
* @param string $endpoint
* Endpoint with a single leading slash.
*
* @return \Yoti\Http\RequestBuilder
*/
public function withEndpoint(string $endpoint): self
{
$this->endpoint = '/' . ltrim($endpoint, '/');
return $this;
}
/**
* @param \Yoti\Util\PemFile $pemFile
*
* @return RequestBuilder
*/
public function withPemFile(PemFile $pemFile): self
{
$this->pemFile = $pemFile;
return $this;
}
/**
* @param string $filePath
*
* @return \Yoti\Http\RequestBuilder
*/
public function withPemFilePath(string $filePath): self
{
return $this->withPemFile(PemFile::fromFilePath($filePath));
}
/**
* @param string $content
*
* @return \Yoti\Http\RequestBuilder
*/
public function withPemString(string $content): self
{
return $this->withPemFile(PemFile::fromString($content));
}
/**
* @param string $method
*
* @return \Yoti\Http\RequestBuilder
*/
public function withMethod(string $method): self
{
$this->method = $method;
return $this;
}
/**
* @return \Yoti\Http\RequestBuilder
*/
public function withGet(): self
{
return $this->withMethod(Request::METHOD_GET);
}
/**
* @return \Yoti\Http\RequestBuilder
*/
public function withPost(): self
{
return $this->withMethod(Request::METHOD_POST);
}
/**
* @return \Yoti\Http\RequestBuilder
*/
public function withPut(): self
{
return $this->withMethod(Request::METHOD_PUT);
}
/**
* @param \Yoti\Http\Payload $payload
*
* @return \Yoti\Http\RequestBuilder
*/
public function withPayload(Payload $payload): self
{
$this->payload = $payload;
return $this;
}
/**
* @param \Psr\Http\Client\ClientInterface $client
*
* @return \Yoti\Http\RequestBuilder
*/
public function withClient(ClientInterface $client): self
{
$this->client = $client;
return $this;
}
/**
* @param string $name
* @param string $value
*
* @return \Yoti\Http\RequestBuilder
*/
public function withHeader(string $name, string $value): self
{
$this->headers[$name] = $value;
return $this;
}
/**
* @param string $name
* @param string $value
*
* @return \Yoti\Http\RequestBuilder
*/
public function withQueryParam(string $name, string $value): self
{
$this->queryParams[$name] = $value;
return $this;
}
/**
* @return void
*/
private function forMultipartRequest(): void
{
if ($this->multipartEntity == null) {
$this->multipartEntity = MultipartEntity::create();
}
}
/**
* Sets the boundary to be used on the multipart request
*
* @param string $multipartBoundary
* @return RequestBuilder
*/
public function withMultipartBoundary(string $multipartBoundary): RequestBuilder
{
$this->forMultipartRequest();
$this->multipartEntity->setBoundary($multipartBoundary);
return $this;
}
/**
* Adds a binary body to the multipart request.
*
* Note: the Signed Request must be specified with a boundary also
* in order to make use of the Multipart request
*
* @param string $name
* @param string $payload
* @param string $contentType
* @param string $fileName
* @return $this
*/
public function withMultipartBinaryBody(
string $name,
string $payload,
string $contentType,
string $fileName
): RequestBuilder {
$this->multipartEntity->addBinaryBody($name, $payload, $contentType, $fileName);
return $this;
}
/**
* Return the request headers including defaults.
*
* @return array<string, string>
*/
private function getHeaders(): array
{
$sdkIdentifier = $this->config->getSdkIdentifier();
$sdkVersion = $this->config->getSdkVersion();
// Prepare request Http Headers
$defaultHeaders = [
self::YOTI_SDK_IDENTIFIER_HEADER_KEY => $sdkIdentifier,
self::YOTI_SDK_VERSION_HEADER_KEY => "{$sdkIdentifier}-{$sdkVersion}",
'Accept' => 'application/json',
];
if (isset($this->payload)) {
$defaultHeaders['Content-Type'] = 'application/json';
}
return array_merge($defaultHeaders, $this->headers);
}
/**
* @throws \InvalidArgumentException
*/
private function validateMethod(): void
{
if (!isset($this->method)) {
throw new \InvalidArgumentException('HTTP Method must be specified');
}
if (
!in_array(
$this->method,
[
Request::METHOD_GET,
Request::METHOD_POST,
Request::METHOD_PUT,
Request::METHOD_PATCH,
Request::METHOD_DELETE,
],
true
)
) {
throw new \InvalidArgumentException("Unsupported HTTP Method {$this->method}");
}
}
/**
* @return string
*/
private static function generateNonce(): string
{
return sprintf(
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
// 32 bits for "time_low"
mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
// 16 bits for "time_mid"
mt_rand(0, 0xffff),
// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 4
mt_rand(0, 0x0fff) | 0x4000,
// 16 bits, 8 bits for "clk_seq_hi_res",
// 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
mt_rand(0, 0x3fff) | 0x8000,
// 48 bits for "node"
mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0xffff)
);
}
/**
* @return \Yoti\Http\Request
*
* @throws \InvalidArgumentException
*/
public function build(): Request
{
if (!isset($this->baseUrl)) {
throw new \InvalidArgumentException('Base URL must be provided to ' . __CLASS__);
}
if (!isset($this->pemFile)) {
throw new \InvalidArgumentException('Pem file must be provided to ' . __CLASS__);
}
$this->validateMethod();
// Add nonce and timestamp to the URL.
$this
->withQueryParam('nonce', self::generateNonce())
->withQueryParam('timestamp', (string)(round(microtime(true) * 1000)));
$endpointWithParams = $this->endpoint . '?' . http_build_query($this->queryParams);
$payload = isset($this->multipartEntity) ? Payload::fromStream($this->multipartEntity->createStream()) :
$this->payload;
$this->withHeader(self::YOTI_DIGEST_HEADER_KEY, RequestSigner::sign(
$this->pemFile,
$endpointWithParams,
$this->method,
$payload
));
$url = $this->baseUrl . $endpointWithParams;
$message = new RequestMessage(
$this->method,
Utils::uriFor($url),
$this->getHeaders(),
$this->getBodyByTypeOfRequest()
);
return new Request($message, $this->client ?? $this->config->getHttpClient());
}
/**
* @return StreamInterface|null
*/
private function getBodyByTypeOfRequest(): ?StreamInterface
{
if (isset($this->payload)) {
return $this->payload->toStream();
} elseif (isset($this->multipartEntity)) {
return $this->multipartEntity->createStream();
} else {
return null;
}
}
}