示例#1
1
 public function signRequest(RequestInterface $request, CredentialsInterface $credentials)
 {
     $params = Psr7\parse_query($request->getBody());
     $params['Timestamp'] = gmdate('c');
     $params['SignatureVersion'] = '2';
     $params['SignatureMethod'] = 'HmacSHA256';
     $params['AWSAccessKeyId'] = $credentials->getAccessKeyId();
     if ($token = $credentials->getSecurityToken()) {
         $params['SecurityToken'] = $token;
     }
     // build string to sign
     $sign = $request->getMethod() . "\n" . $request->getHeaderLine('Host') . "\n" . '/' . "\n" . $this->getCanonicalizedParameterString($params);
     $params['Signature'] = base64_encode(hash_hmac('sha256', $sign, $credentials->getSecretKey(), true));
     return $request->withBody(Psr7\stream_for(http_build_query($params)));
 }
示例#2
0
 public function signRequest(RequestInterface $request, CredentialsInterface $credentials)
 {
     $params = Psr7\parse_query($request->getBody()->__toString());
     $params['SignatureVersion'] = '2';
     $params['SignatureMethod'] = 'HmacSHA256';
     $params['AWSAccessKeyId'] = $credentials->getAccessKeyId();
     if ($credentials->getSecurityToken()) {
         $params['MWSAuthToken'] = $credentials->getSecurityToken();
     }
     $params['Timestamp'] = gmdate(self::ISO8601_BASIC);
     ksort($params);
     $canonicalizedQueryString = $this->getCanonicalizedQuery($params);
     $stringToSign = implode("\n", [$request->getMethod(), $request->getUri()->getHost(), $request->getUri()->getPath(), $canonicalizedQueryString]);
     // calculate HMAC with SHA256 and base64-encoding
     $signature = base64_encode(hash_hmac('sha256', $stringToSign, $credentials->getSecretKey(), TRUE));
     // encode the signature for the request
     $signature = str_replace('%7E', '~', rawurlencode($signature));
     $signature = str_replace('+', '%20', $signature);
     $signature = str_replace('*', '%2A', $signature);
     $queryString = $canonicalizedQueryString . "&Signature=" . $signature;
     if ($request->getMethod() === 'POST') {
         return new Request('POST', $request->getUri(), ['Content-Length' => strlen($queryString), 'Content-Type' => 'application/x-www-form-urlencoded'], $queryString);
     } else {
         return new Request('GET', $request->getUri()->withQuery($queryString));
     }
 }
示例#3
0
 public function signRequest(RequestInterface $request, CredentialsInterface $credentials)
 {
     /** @var PostBodyInterface $body */
     $body = $request->getBody();
     $body->setField('Timestamp', gmdate('c'));
     $body->setField('SignatureVersion', '2');
     $body->setField('SignatureMethod', 'HmacSHA256');
     $body->setField('AWSAccessKeyId', $credentials->getAccessKeyId());
     if ($token = $credentials->getSecurityToken()) {
         $body->setField('SecurityToken', $token);
     }
     // build string to sign
     $sign = $request->getMethod() . "\n" . $request->getHost() . "\n" . '/' . "\n" . $this->getCanonicalizedParameterString($body);
     $request->getConfig()->set('aws.signature', $sign);
     $body->setField('Signature', base64_encode(hash_hmac('sha256', $sign, $credentials->getSecretKey(), true)));
 }
 public function signRequest(RequestInterface $request, CredentialsInterface $credentials)
 {
     $ldt = gmdate(self::ISO8601_BASIC);
     $sdt = substr($ldt, 0, 8);
     $parsed = $this->parseRequest($request);
     $parsed['headers']['X-Amz-Date'] = [$ldt];
     if ($token = $credentials->getSecurityToken()) {
         $parsed['headers']['X-Amz-Security-Token'] = [$token];
     }
     $cs = $this->createScope($sdt, $this->region, $this->service);
     $payload = $this->getPayload($request);
     $context = $this->createContext($parsed, $payload);
     $toSign = $this->createStringToSign($ldt, $cs, $context['creq']);
     $signingKey = $this->getSigningKey($sdt, $this->region, $this->service, $credentials->getSecretKey());
     $signature = hash_hmac('sha256', $toSign, $signingKey);
     $parsed['headers']['Authorization'] = ["AWS4-HMAC-SHA256 " . "Credential={$credentials->getAccessKeyId()}/{$cs}, " . "SignedHeaders={$context['headers']}, Signature={$signature}"];
     return $this->buildRequest($parsed);
 }
示例#5
0
 public function signRequest(RequestInterface $request, CredentialsInterface $credentials)
 {
     $ldt = gmdate(self::ISO8601_BASIC);
     $sdt = substr($ldt, 0, 8);
     $request->removeHeader('Authorization');
     $request->removeHeader('x-amz-date');
     $request->setHeader('Date', $ldt);
     if ($token = $credentials->getSecurityToken()) {
         $request->setHeader('x-amz-security-token', $token);
     }
     $cs = $this->createScope($sdt, $this->region, $this->service);
     $payload = $this->getPayload($request);
     $context = $this->createContext($request, $payload);
     $context['string_to_sign'] = $this->createStringToSign($ldt, $cs, $context['creq']);
     $signingKey = $this->getSigningKey($sdt, $this->region, $this->service, $credentials->getSecretKey());
     $signature = hash_hmac('sha256', $context['string_to_sign'], $signingKey);
     $request->setHeader('Authorization', "AWS4-HMAC-SHA256 " . "Credential={$credentials->getAccessKeyId()}/{$cs}, " . "SignedHeaders={$context['headers']}, Signature={$signature}");
     $request->getConfig()['aws.signature'] = $context;
 }
 protected function getPolicyAndSignature(CredentialsInterface $creds)
 {
     $jsonPolicy64 = base64_encode($this->jsonPolicy);
     return ['AWSAccessKeyId' => $creds->getAccessKeyId(), 'policy' => $jsonPolicy64, 'signature' => base64_encode(hash_hmac('sha1', $jsonPolicy64, $creds->getSecretKey(), true))];
 }
 protected function getPolicyAndSignature(CredentialsInterface $credentials, array $policy)
 {
     $ldt = gmdate(SignatureV4::ISO8601_BASIC);
     $sdt = substr($ldt, 0, 8);
     $policy['conditions'][] = ['X-Amz-Date' => $ldt];
     $region = $this->client->getRegion();
     $scope = $this->createScope($sdt, $region, 's3');
     $creds = "{$credentials->getAccessKeyId()}/{$scope}";
     $policy['conditions'][] = ['X-Amz-Credential' => $creds];
     $policy['conditions'][] = ['X-Amz-Algorithm' => "AWS4-HMAC-SHA256"];
     $jsonPolicy64 = base64_encode(json_encode($policy));
     $key = $this->getSigningKey($sdt, $region, 's3', $credentials->getSecretKey());
     return ['X-Amz-Credential' => $creds, 'X-Amz-Algorithm' => "AWS4-HMAC-SHA256", 'X-Amz-Date' => $ldt, 'Policy' => $jsonPolicy64, 'X-Amz-Signature' => bin2hex(hash_hmac('sha256', $jsonPolicy64, $key, true))];
 }
示例#8
0
 /**
  * Create an SMTP password for a given IAM user's credentials.
  *
  * The SMTP username is the Access Key ID for the provided credentials.
  *
  * @link http://docs.aws.amazon.com/ses/latest/DeveloperGuide/smtp-credentials.html#smtp-credentials-convert
  *
  * @param CredentialsInterface $creds
  *
  * @return string
  */
 public static function generateSmtpPassword(CredentialsInterface $creds)
 {
     static $version = "";
     static $algo = 'sha256';
     static $message = 'SendRawEmail';
     $signature = hash_hmac($algo, $message, $creds->getSecretKey(), true);
     return base64_encode($version . $signature);
 }
示例#9
0
 private function signString($string, CredentialsInterface $credentials)
 {
     return base64_encode(hash_hmac('sha1', $string, $credentials->getSecretKey(), true));
 }