1: <?php
2: namespace Worldline\Connect\Sdk\Authentication;
3:
4: use Worldline\Connect\Sdk\CommunicatorConfiguration;
5:
6: /**
7: * Class V1HMACAuthenticator
8: *
9: * @package Worldline\Connect\Sdk\Authentication
10: */
11: class V1HMACAuthenticator implements Authenticator
12: {
13: /**
14: * @var string
15: */
16: private string $apiKeyId;
17:
18: /**
19: * @var string
20: */
21: private string $apiSecret;
22:
23: /**
24: * @param CommunicatorConfiguration $communicatorConfiguration
25: */
26: public function __construct(CommunicatorConfiguration $communicatorConfiguration)
27: {
28: $this->apiKeyId = $communicatorConfiguration->getApiKeyId();
29: $this->apiSecret = $communicatorConfiguration->getApiSecret();
30: }
31:
32: /**
33: * @param string $httpMethod
34: * @param string $uriPath
35: * @param array<string, string> $requestHeaders
36: *
37: * @return string
38: */
39: public function getAuthorization(string $httpMethod, string $uriPath, array $requestHeaders): string
40: {
41: $signature = base64_encode(
42: hash_hmac(
43: 'sha256',
44: $this->getSignData($httpMethod, $uriPath, $requestHeaders),
45: $this->apiSecret,
46: true
47: )
48: );
49: return sprintf('GCS v1HMAC:%s:%s', $this->apiKeyId, $signature);
50: }
51:
52: /**
53: * @param string $httpMethod
54: * @param string $uriPath
55: * @param array<string, string> $requestHeaders
56: *
57: * @return string
58: */
59: private function getSignData(string $httpMethod, string $uriPath, array $requestHeaders): string
60: {
61: $signData = $httpMethod . "\n";
62: if (isset($requestHeaders['Content-Type'])) {
63: $signData .= $requestHeaders['Content-Type'] . "\n";
64: } else {
65: $signData .= "\n";
66: }
67: if (isset($requestHeaders['Date'])) {
68: $signData .= $requestHeaders['Date'] . "\n";
69: } else {
70: $signData .= "\n";
71: }
72: $gcsHeaders = array_filter(
73: $requestHeaders,
74: function ($headerKey) {
75: return preg_match('/X-GCS/i', $headerKey);
76: },
77: ARRAY_FILTER_USE_KEY
78: );
79: ksort($gcsHeaders);
80: foreach ($gcsHeaders as $gcsHeaderKey => $gcsHeaderValue) {
81: $gcsEncodedHeaderValue = trim(preg_replace('/\r?\n\h*/', ' ', $gcsHeaderValue));
82:
83: $signData .= strtolower($gcsHeaderKey) . ':' . $gcsEncodedHeaderValue . "\n";
84: }
85: $signData .= $uriPath . "\n";
86: return $signData;
87: }
88: }
89: