public function signRequest(RequestInterface $request, CredentialsInterface $credentials)
 {
     // Add a date header if one is not set
     if (!$request->hasHeader('date') && !$request->hasHeader('x-amz-date')) {
         $request->setHeader('Date', gmdate(DateFormat::RFC1123, $this->getTimestamp()));
     }
     // Add the security token if one is present
     if ($credentials->getSecurityToken()) {
         $request->setHeader('x-amz-security-token', $credentials->getSecurityToken());
     }
     // Determine the string to sign
     $stringToSign = (string) ($request->getHeader('Date') ?: $request->getHeader('x-amz-date'));
     $request->getParams()->set('aws.string_to_sign', $stringToSign);
     // Calculate the signature
     $signature = base64_encode(hash_hmac('sha256', $stringToSign, $credentials->getSecretKey(), true));
     // Add the authorization header to the request
     $headerFormat = 'AWS3-HTTPS AWSAccessKeyId=%s,Algorithm=HmacSHA256,Signature=%s';
     $request->setHeader('X-Amzn-Authorization', sprintf($headerFormat, $credentials->getAccessKeyId(), $signature));
 }
Example #2
0
 public function signString($string, CredentialsInterface $credentials)
 {
     return base64_encode(hash_hmac('sha1', $string, $credentials->getSecretKey(), true));
 }
Example #3
0
 private function addQueryValues($scope, RequestInterface $request, CredentialsInterface $credentials, $expires)
 {
     $credential = $credentials->getAccessKeyId() . '/' . $scope;
     // Set query params required for pre-signed URLs
     $request->getQuery()->set('X-Amz-Algorithm', 'AWS4-HMAC-SHA256')->set('X-Amz-Credential', $credential)->set('X-Amz-Date', gmdate('Ymd\\THis\\Z', $this->getTimestamp()))->set('X-Amz-SignedHeaders', 'Host')->set('X-Amz-Expires', $this->convertExpires($expires));
 }