Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding PHP auth and auth lib #4574

Merged
merged 3 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions clients/sellingpartner-api-aa-php/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Selling Partner API Authentication/Authorization Library
This library provides helper classes for use when signing HTTP requests for Amazon Selling Partner APIs. It is intended for use
with the Selling Partner API Client Libraries generated by [openapi generator](https://openapi-generator.tech/)
using the Guzzlehttp library. It can also be integrated into custom projects.

## LWAAuthorizationSigner
Obtains and signs a request with an access token from LWA (Login with Amazon) for the specified endpoint using the provided LWA credentials.

*Example*
```
$request = new \GuzzleHttp\Psr7\Request(
"method", "uri", ...
);

// Seller APIs

$lwaAuthorizationCredentials = new LWAAuthorizationCredentials([
"clientId" => "...",
"clientSecret" => "...",
"refreshToken" => "...",
"endpoint" => "..."
]);

/* Sellerless APIs
The Selling Partner API scopes can be retrieved from the ScopeConstants class and passed as
argument(s) to either the "scopes" => "..." or setScopes(...) method during
lwaAuthorizationCredentials object instantiation.
*/

use SpApi\AuthAndAuth\ScopeConstants;

$lwaAuthorizationCredentials = new LWAAuthorizationCredentials([
"clientId" => "...",
"clientSecret" => "...",
"scopes" => "...",
"endpoint" => "..."
]);

$signedRequest = (new LWAAuthorizationSigner($lwaAuthorizationCredentials))
->sign($request);
```

## LWAAccessTokenCache
Implements cache for access token that is returned in LWAClient and reuses the access token until time to live.

## RateLimitConfiguration
Interface to set and get rateLimit configurations that are used with RateLimiter. RateLimiter is used on client side to restrict the rate at which requests are made. RateLimitConfiguration takes a Permit, the rate at which requests are made, and TimeOut.

*Example*
```
$rateLimitOption = new RateLimitConfigurationOnRequests([
"rateLimitToken" => "...",
"rateLimitTokenLimit" => "...",
"waitTimeOutInMilliSeconds" => "..."
]);
```

## Resources
This package features Mustache templates designed for use with [openapi generator](https://openapi-generator.tech/).
When you build Selling Partner API OpenAPI models with these templates, they help generate a rich SDK with functionality to invoke Selling Partner APIs built in. The templates are located in *resources/openapi-generator*.

Dependencies are declared in the composer.json file.

## License
OpenAPI Generator templates are subject to the [OpenAPI Generator License](https://github.com/OpenAPITools/openapi-generator/blob/v5.2.1/LICENSE).

All other work licensed as follows:

Copyright 2019 Amazon.com, Inc

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this library except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
53 changes: 53 additions & 0 deletions clients/sellingpartner-api-aa-php/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"name": "spapi/auth-and-auth",
"description": "Amazon Selling Partner APIs official client library.",
"type": "library",
"keywords": [
"sp-api",
"amazon",
"sdk",
"openapi-generator",
"php",
"rest"
],
"homepage": "https://developer-docs.amazon.com/sp-api",
"license": "Apache-2.0",
"authors": [
{
"name": "Amazon API Services",
"homepage": "https://developer-docs.amazon.com/sp-api"
}
],
"require": {
"php": "^8.3",
"ext-json": "*",
"guzzlehttp/guzzle": "^8.3",
"guzzlehttp/psr7": "^2.0",
"aws/aws-sdk-php": "^3.228",
"symfony/http-kernel": "^7.2",
"symfony/rate-limiter": "^7.2",
"vlucas/phpdotenv": "^5.6",
"ext-openssl": "*"
},
"require-dev": {
"phpunit/phpunit": "^9.6",
"squizlabs/php_codesniffer": "^3.7"
},
"autoload": {
"psr-4": {
"SpApi\\AuthAndAuth\\": "src/authandauth/",
"OpenAPI\\Client\\": "sdk/lib/",
"OpenAPI\\Client\\Test\\" : "sdk/test/"
}
},
"autoload-dev": {
"psr-4": {
"SpApi\\Test\\AuthAndAuth\\" : "tests/authandauth/",
"OpenAPI\\Client\\": "sdk/lib/"
}
},
"scripts": {
"fix": "phpcs --standard=PSR12 src/authandauth tests/authandauth",
"test": "phpunit tests/authandauth"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"additionalProperties": {
"customTemplateDir": "./resources/openapi-generator/templates",
"common_header" : "/**\n * Selling Partner API\n *\n * The Selling Partner API enables developers to programmatically retrieve information from various domains.\n * These APIs provide tools for building fast, flexible, and custom applications,\n * as well as demand-based decision support systems.\n *\n * The version of the OpenAPI document: v0\n * Generated by: https://openapi-generator.tech\n * Generator version: 7.9.0\n */"
},
"files": {
"ModelInterface.mustache": {
"templateFile": "ModelInterface.mustache",
"destinationFilename": "lib/Model/ModelInterface.php"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

/**
* ApiException
* PHP version 7.4
*
* @category Class
* @package {{invokerPackage}}
* @author OpenAPI Generator team
* @link https://openapi-generator.tech
*/

{{common_header}}
/**
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/

namespace {{invokerPackage}};

use Exception;
use stdClass;

/**
* ApiException Class Doc Comment
*
* @category Class
* @package {{invokerPackage}}
* @author OpenAPI Generator team
* @link https://openapi-generator.tech
*/
class ApiException extends Exception
{
/**
* The HTTP body of the server response either as Json or string.
*
* @var stdClass|string|null
*/
protected stdClass|string|null $responseBody;

/**
* The HTTP header of the server response.
*
* @var string[][]|null
*/
protected ?array $responseHeaders;

/**
* The deserialized response object
*
* @var stdClass|string|null
*/
protected stdClass|string|null $responseObject;

/**
* Constructor
*
* @param string $message Error message
* @param int $code HTTP status code
* @param string[][]|null $responseHeaders HTTP response header
* @param string|stdClass|null $responseBody
* HTTP decoded body of the server response either as stdClass or string
*/
public function __construct(
string $message = "",
int $code = 0,
$responseHeaders = [],
string|stdClass|null $responseBody = null
) {
parent::__construct($message, $code);
$this->responseHeaders = $responseHeaders;
$this->responseBody = $responseBody;
}

/**
* Gets the HTTP response header
*
* @return string[][]|null HTTP response header
*/
public function getResponseHeaders(): ?array
{
return $this->responseHeaders;
}

/**
* Gets the HTTP body of the server response either as Json or string
*
* @return stdClass|string|null HTTP body of the server response either as \stdClass or string
*/
public function getResponseBody(): string|stdClass|null
{
return $this->responseBody;
}

/**
* Sets the deserialized response object (during deserialization)
*
* @param mixed $obj Deserialized response object
*
* @return void
*/
public function setResponseObject(mixed $obj): void
{
$this->responseObject = $obj;
}

/**
* Gets the deserialized response object (during deserialization)
*
* @return stdClass|string|null the deserialized response object
*/
public function getResponseObject(): stdClass|string|null
{
return $this->responseObject;
}
}
Loading