|
|
|
|
@ -12,6 +12,8 @@ final class CloudS3Signer
|
|
|
|
|
/**
|
|
|
|
|
* Return the request headers required for an AWS Signature Version 4 call.
|
|
|
|
|
*
|
|
|
|
|
* @param array<string,string> $metadata S3 user metadata without the
|
|
|
|
|
* x-amz-meta- prefix. Every entry is included in SignedHeaders.
|
|
|
|
|
* @return array<string, string>
|
|
|
|
|
*/
|
|
|
|
|
public static function sign(
|
|
|
|
|
@ -21,7 +23,8 @@ final class CloudS3Signer
|
|
|
|
|
string $accessKeyId,
|
|
|
|
|
string $secretAccessKey,
|
|
|
|
|
int $timestamp,
|
|
|
|
|
?string $payloadHash = null
|
|
|
|
|
?string $payloadHash = null,
|
|
|
|
|
array $metadata = []
|
|
|
|
|
): array {
|
|
|
|
|
$parts = parse_url($url);
|
|
|
|
|
if (!is_array($parts) || !isset($parts['scheme'], $parts['host'])) {
|
|
|
|
|
@ -37,10 +40,33 @@ final class CloudS3Signer
|
|
|
|
|
$amzDate = gmdate('Ymd\THis\Z', $timestamp);
|
|
|
|
|
$date = gmdate('Ymd', $timestamp);
|
|
|
|
|
$host = self::hostHeader($parts);
|
|
|
|
|
$canonicalHeaders = 'host:' . $host . "\n"
|
|
|
|
|
. 'x-amz-content-sha256:' . $payloadHash . "\n"
|
|
|
|
|
. 'x-amz-date:' . $amzDate . "\n";
|
|
|
|
|
$signedHeaders = 'host;x-amz-content-sha256;x-amz-date';
|
|
|
|
|
$headers = [
|
|
|
|
|
'host' => $host,
|
|
|
|
|
'x-amz-content-sha256' => $payloadHash,
|
|
|
|
|
'x-amz-date' => $amzDate,
|
|
|
|
|
];
|
|
|
|
|
foreach ($metadata as $name => $value) {
|
|
|
|
|
if (!is_string($name) || !is_string($value)) {
|
|
|
|
|
throw new \InvalidArgumentException('Invalid S3 metadata');
|
|
|
|
|
}
|
|
|
|
|
$name = strtolower(trim($name));
|
|
|
|
|
if (preg_match('/^[a-z0-9][a-z0-9-]{0,63}$/D', $name) !== 1
|
|
|
|
|
|| $value === '' || strlen($value) > 2048
|
|
|
|
|
|| preg_match('/[\x00-\x1f\x7f]/', $value) === 1) {
|
|
|
|
|
throw new \InvalidArgumentException('Invalid S3 metadata');
|
|
|
|
|
}
|
|
|
|
|
$header = 'x-amz-meta-' . $name;
|
|
|
|
|
if (isset($headers[$header])) {
|
|
|
|
|
throw new \InvalidArgumentException('Duplicate S3 metadata');
|
|
|
|
|
}
|
|
|
|
|
$headers[$header] = trim($value);
|
|
|
|
|
}
|
|
|
|
|
ksort($headers, SORT_STRING);
|
|
|
|
|
$canonicalHeaders = '';
|
|
|
|
|
foreach ($headers as $name => $value) {
|
|
|
|
|
$canonicalHeaders .= $name . ':' . $value . "\n";
|
|
|
|
|
}
|
|
|
|
|
$signedHeaders = implode(';', array_keys($headers));
|
|
|
|
|
$canonicalRequest = $method . "\n"
|
|
|
|
|
. self::canonicalUri((string) ($parts['path'] ?? '/')) . "\n"
|
|
|
|
|
. self::canonicalQuery((string) ($parts['query'] ?? '')) . "\n"
|
|
|
|
|
@ -56,7 +82,7 @@ final class CloudS3Signer
|
|
|
|
|
$signingKey = hash_hmac('sha256', 'aws4_request', $serviceKey, true);
|
|
|
|
|
$signature = hash_hmac('sha256', $stringToSign, $signingKey);
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
$result = [
|
|
|
|
|
'Host' => $host,
|
|
|
|
|
'X-Amz-Content-Sha256' => $payloadHash,
|
|
|
|
|
'X-Amz-Date' => $amzDate,
|
|
|
|
|
@ -64,6 +90,11 @@ final class CloudS3Signer
|
|
|
|
|
. '/' . $scope . ',SignedHeaders=' . $signedHeaders
|
|
|
|
|
. ',Signature=' . $signature,
|
|
|
|
|
];
|
|
|
|
|
foreach ($metadata as $name => $value) {
|
|
|
|
|
$result['x-amz-meta-' . strtolower(trim((string) $name))] =
|
|
|
|
|
trim((string) $value);
|
|
|
|
|
}
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|