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