Skip to content

Commit

Permalink
Pass headers into proxy rather than an auth token
Browse files Browse the repository at this point in the history
  • Loading branch information
keithbrink committed Nov 8, 2024
1 parent 4d042f2 commit 131cfa8
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 10 deletions.
4 changes: 1 addition & 3 deletions src/AmznSPAHttp.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,7 @@ private function refreshRdtToken(string $url, string $method)
private function setupHttp(PendingRequest $http, bool $grantless = false, string $url = '', string $method = ''): void
{
if ($proxy = $this->config->getProxy()) {
$this->http = $http->withHeaders([
'Authorization' => "Bearer {$proxy->auth_token}",
]);
$this->http = $http->withHeaders($proxy->headers);
} else {
$this->http = $http->withHeaders([
'x-amz-access-token' => $this->getToken($grantless, $url, $method),
Expand Down
2 changes: 1 addition & 1 deletion src/Data/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Proxy
{
public function __construct(
public readonly string $url,
public readonly string $auth_token,
public readonly array $headers,
) {
}
}
4 changes: 3 additions & 1 deletion src/Resources/ResourceGetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,11 @@ public function getListingsItems(): ListingsItemsResource

private function constructResource(string $class, ?string $grantless_resource = null): ResourceContract
{
$url = $this->config->getProxy() ? $this->config->getProxy()->url : $this->config->getMarketplace()->getBaseUrl();

return new $class(
$this->validateAndSetupHttpForStandardResource($grantless_resource),
$this->config->getMarketplace()->getBaseUrl(),
$url,
);
}

Expand Down
27 changes: 27 additions & 0 deletions tests/Setup/SetupAmznSPAConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Support\Str;
use Jasara\AmznSPA\AmznSPAConfig;
use Jasara\AmznSPA\Constants\MarketplacesList;
use Jasara\AmznSPA\Data\Proxy;

trait SetupAmznSPAConfig
{
Expand All @@ -31,6 +32,32 @@ public function setupMinimalConfig(string $marketplace_id = null, Factory $http
return $config;
}

public function setupMinimalProxyConfig(
Proxy $proxy,
string $marketplace_id = null,
Factory $http = null,
): AmznSPAConfig {
$config = new AmznSPAConfig(
marketplace_id: $marketplace_id ?: MarketplacesList::allIdentifiers()[rand(0, 15)],
application_id: Str::random(),
redirect_url: Str::random() . '.com',
lwa_refresh_token: Str::random(),
lwa_access_token: Str::random(),
grantless_access_token: Str::random(),
aws_access_key: Str::random(),
aws_secret_key: Str::random(),
lwa_client_id: Str::random(),
lwa_client_secret: Str::random(),
proxy: $proxy,
);

if ($http) {
$config->setHttp($http);
}

return $config;
}

public function setupLiveConfig(): AmznSPAConfig
{
$config = new AmznSPAConfig(
Expand Down
8 changes: 6 additions & 2 deletions tests/Unit/AmznSPAConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,13 @@ public function testGetNewConfig()
$grantless_access_token_expires_at = CarbonImmutable::now()->addSeconds(rand(100, 500));
$restricted_data_token = Str::random();
$restricted_data_token_expires_at = CarbonImmutable::now()->addSeconds(rand(100, 500));

$proxy_auth_token = Str::random();
$proxy = new Proxy(
url: Str::random(),
auth_token: Str::random(),
headers: [
'Authorization' => "Bearer {$proxy_auth_token}",
],
);

$config = new AmznSPAConfig(
Expand Down Expand Up @@ -99,7 +103,7 @@ public function testGetNewConfig()
$this->assertTrue($config->shouldGetRdtTokens());

$this->assertEquals($proxy->url, $config->getProxy()->url);
$this->assertEquals($proxy->auth_token, $config->getProxy()->auth_token);
$this->assertEquals($proxy->headers, $config->getProxy()->headers);
}

public function testSetters()
Expand Down
30 changes: 29 additions & 1 deletion tests/Unit/AmznSPAHttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,34 @@ function (Request $request) {
$http->assertSentInOrder($request_validation);
}

public function testSetProxyHeaders()
{
[$config, $http] = $this->setupConfigWithFakeHttp(['tokens/create-restricted-data-token', 'orders/get-orders']);

$amzn = new AmznSPA($config);
$amzn = $amzn->usingMarketplace('ATVPDKIKX0DER');
$amzn->orders->getOrders(
marketplace_ids: ['ATVPDKIKX0DER'],
);

$request_validation = [
function (Request $request) {
$this->assertEquals('POST', $request->method());
$this->assertEquals('https://sellingpartnerapi-na.amazon.com/tokens/2021-03-01/restrictedDataToken', $request->url());

return true;
},
function (Request $request) {
$this->assertEquals('GET', $request->method());
$this->assertEquals('https://sellingpartnerapi-na.amazon.com/orders/v0/orders?MarketplaceIds=ATVPDKIKX0DER', urldecode($request->url()));

return true;
},
];

$http->assertSentInOrder($request_validation);
}

public function testInvalidPartyId()
{
$this->expectException(AuthenticationException::class);
Expand Down Expand Up @@ -484,7 +512,7 @@ public function testSetupHttpProxy()
application_id: Str::random(),
proxy: new Proxy(
url: 'https://www.amazon.com',
auth_token: Str::random(),
headers: [],
)
);

Expand Down
7 changes: 5 additions & 2 deletions tests/Unit/Data/ProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ class ProxyTest extends UnitTestCase
{
public function testSetupTokens()
{
$proxy_auth_token = Str::random();
$proxy = new Proxy(
url: $proxy_url = Str::random(),
auth_token: $proxy_auth_token = Str::random(),
headers: [
'Authorization' => "Bearer {$proxy_auth_token}",
],
);

$this->assertEquals($proxy_url, $proxy->url);
$this->assertEquals($proxy_auth_token, $proxy->auth_token);
$this->assertEquals("Bearer {$proxy_auth_token}", $proxy->headers['Authorization']);
}
}

0 comments on commit 131cfa8

Please sign in to comment.