-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathSignedRequestStrategy.php
More file actions
93 lines (81 loc) · 2.14 KB
/
Copy pathSignedRequestStrategy.php
File metadata and controls
93 lines (81 loc) · 2.14 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
<?php
declare(strict_types=1);
namespace Yoti\Http\AuthStrategy;
use Yoti\Http\Payload;
use Yoti\Http\RequestSigner;
use Yoti\Util\PemFile;
/**
* Authentication strategy that signs requests using the Yoti digest mechanism.
*
* This generates nonce + timestamp query params and an X-Yoti-Auth-Digest header,
* matching the existing signed request behavior in the PHP SDK.
* Mirrors the Java SDK's DocsSignedRequestStrategy / SignedRequestStrategy.
*/
class SignedRequestStrategy implements AuthStrategyInterface
{
/**
* @var PemFile
*/
private $pemFile;
/**
* @var string|null
*/
private $sdkId;
/**
* @param PemFile $pemFile The PEM file used for signing
* @param string|null $sdkId Optional SDK ID to include as query param
*/
public function __construct(PemFile $pemFile, ?string $sdkId = null)
{
$this->pemFile = $pemFile;
$this->sdkId = $sdkId;
}
/**
* {@inheritdoc}
*/
public function createAuthHeaders(string $httpMethod, string $endpoint, ?Payload $payload = null): array
{
$digest = RequestSigner::sign(
$this->pemFile,
$endpoint,
$httpMethod,
$payload
);
return [
'X-Yoti-Auth-Digest' => $digest,
];
}
/**
* {@inheritdoc}
*/
public function createQueryParams(): array
{
$params = [
'nonce' => self::generateNonce(),
'timestamp' => sprintf('%.0F', microtime(true) * 1000),
];
if ($this->sdkId !== null) {
$params['sdkId'] = $this->sdkId;
}
return $params;
}
/**
* Generate a UUID v4 nonce.
*
* @return string
*/
private static function generateNonce(): string
{
return sprintf(
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0x0fff) | 0x4000,
mt_rand(0, 0x3fff) | 0x8000,
mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0xffff)
);
}
}