Skip to content
This repository has been archived by the owner on Jul 8, 2022. It is now read-only.

Commit

Permalink
Merge pull request #24 from clousale/throw-exception-on-lwa-refresh-t…
Browse files Browse the repository at this point in the history
…oken-exchange

Throw exception on LWA Code -> Refresh Token exchange
  • Loading branch information
clousale authored Feb 2, 2021
2 parents 63c39b4 + b1cb0fa commit e1ad494
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 39 deletions.
9 changes: 6 additions & 3 deletions lib/Helpers/SellingPartnerApiRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ private function generateRequest(
string $method,
$httpBody
): Request {
if (null != $formParams) {
ksort($formParams);
}
if (null != $queryParams) {
ksort($queryParams);
}
// body params
$_tempBody = $httpBody;
if ($multipart) {
Expand All @@ -47,8 +53,6 @@ private function generateRequest(
// \stdClass has no __toString(), so we should encode it manually
if ($httpBody instanceof \stdClass && 'application/json' === $headers['Content-Type']) {
$httpBody = Utils::jsonEncode($httpBody);
// } else if (method_exists($httpBody, '__toString')) {
// $httpBody = $httpBody->__toString();
}
} elseif (count($formParams) > 0) {
if ($multipart) {
Expand All @@ -68,7 +72,6 @@ private function generateRequest(
$httpBody = Query::build($formParams);
}
}
ksort($queryParams);
$query = Query::build($queryParams);
$amazonHeader = Signature::calculateSignature(
$this->config,
Expand Down
9 changes: 4 additions & 5 deletions lib/SellingPartnerOAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,8 @@ public static function getAccessTokenFromRefreshToken($refreshToken, $clientId,
}

/**
* @param string $lwaAuthorizationCode
* @param string $clientId
* @param string $clientSecret
* @param string $redirectUri
* @return string|null
* @throws GuzzleException
* @throws SellingPartnerOAuthException
*/
public static function getRefreshTokenFromLwaAuthorizationCode(
string $lwaAuthorizationCode,
Expand Down Expand Up @@ -74,6 +70,9 @@ public static function getRefreshTokenFromLwaAuthorizationCode(

$body = $response->getBody()->getContents();
$bodyAsJson = json_decode($body, true);
if (isset($bodyAsJson['error_description'])) {
throw new SellingPartnerOAuthException($bodyAsJson['error_description'], $bodyAsJson['error']);
}

return $bodyAsJson['refresh_token'];
}
Expand Down
9 changes: 9 additions & 0 deletions lib/SellingPartnerOAuthException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace ClouSale\AmazonSellingPartnerAPI;

use Exception;

class SellingPartnerOAuthException extends Exception
{
}
40 changes: 9 additions & 31 deletions lib/Signature.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,15 @@ class Signature
* @param string $uri
* @param string $queryString
* @param array $data
* @param string $service
*
* @throws \Exception
*
* @author crazyfactory https://github.com/crazyfactory
*/
public static function calculateSignature(
Configuration $config,
$host,
$method,
string $host,
string $method,
$uri = '',
$queryString = '',
$data = []
Expand All @@ -56,38 +55,19 @@ public static function calculateSignature(
}

public static function calculateSignatureForService(
$host,
$method,
string $host,
string $method,
$uri,
$queryString,
$data,
$service,
$accessKey,
$secretKey,
$region,
string $service,
string $accessKey,
string $secretKey,
string $region,
$accessToken,
$securityToken,
$userAgent
): array {
if (is_null($service)) {
throw new \Exception('Service is required');
}
if (is_null($accessKey)) {
throw new \Exception('Access key is required');
}
if (is_null($secretKey)) {
throw new \Exception('Secret key is required');
}
if (is_null($region)) {
throw new \Exception('Region key is required');
}
if (is_null($host)) {
throw new \Exception('Host key is required');
}
if (is_null($method)) {
throw new \Exception('Method key is required');
}

$terminationString = 'aws4_request';
$algorithm = 'AWS4-HMAC-SHA256';
$amzdate = gmdate('Ymd\THis\Z');
Expand Down Expand Up @@ -153,10 +133,8 @@ public static function calculateSignatureForService(
//Finalize the authorization structure
$authorizationHeader = $algorithm." Credential={$accessKey}/{$credentialScope}, SignedHeaders={$signedHeadersStr}, Signature={$signature}";

$amazonHeader = array_merge($canonicalHeaders, [
return array_merge($canonicalHeaders, [
'Authorization' => $authorizationHeader,
]);

return $amazonHeader;
}
}

0 comments on commit e1ad494

Please sign in to comment.