-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0fbc014
Showing
14 changed files
with
641 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
name: Tests | ||
on: [push, pull_request] | ||
jobs: | ||
symfony: | ||
name: PHP ${{ matrix.php-versions }} | ||
runs-on: ubuntu-latest | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
php-versions: | ||
- '7.2' | ||
- '7.3' | ||
- '7.4' | ||
include: | ||
- php-versions: '8.0' | ||
composer-flags: '--ignore-platform-reqs' | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v2 | ||
- name: Setup PHP, with composer and extensions | ||
uses: shivammathur/setup-php@v2 | ||
with: | ||
php-version: ${{ matrix.php-versions }} | ||
extensions: mbstring, xml, ctype, iconv, curl | ||
- name: Get composer cache directory | ||
id: composer-cache | ||
run: echo "::set-output name=dir::$(composer config cache-files-dir)" | ||
- name: Cache composer dependencies | ||
uses: actions/cache@v1 | ||
with: | ||
path: ${{ steps.composer-cache.outputs.dir }} | ||
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }} | ||
restore-keys: ${{ runner.os }}-composer- | ||
- name: Install Composer dependencies | ||
run: | | ||
composer install -n --prefer-dist ${{ matrix.composer-flags }} | ||
- name: Run Tests | ||
run: vendor/bin/phpunit | ||
- name: Upload coverage to Codecov | ||
uses: codecov/codecov-action@v1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/vendor/ | ||
/composer.lock | ||
# Ignore local PHPUnit configuration | ||
/phpunit.xml | ||
/coverage.xml | ||
|
||
# Ignore PHP-CS-Fixer files | ||
/.php_cs | ||
/.php_cs.cache | ||
/cs_fixer_tmp_* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
return PhpCsFixer\Config::create() | ||
->setRules(array( | ||
'@Symfony' => true, | ||
'@Symfony:risky' => true, | ||
'@PHP71Migration' => true, | ||
'@PHP71Migration:risky' => true, | ||
'ordered_imports' => true, | ||
)) | ||
->setRiskyAllowed(true) | ||
->setFinder(PhpCsFixer\Finder::create()->in(['src', 'tests'])) | ||
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# Alibaba Cloud API Gateway HTTP Request Signer for PHP | ||
|
||
[](https://packagist.org/packages/ion-bazan/aliyun-http-signer) | ||
[](https://github.com/IonBazan/aliyun-http-signer/actions) | ||
[](https://packagist.org/packages/ion-bazan/aliyun-http-signer) | ||
[](https://codecov.io/gh/IonBazan/aliyun-http-signer) | ||
[](https://packagist.org/packages/ion-bazan/aliyun-http-signer) | ||
[](https://packagist.org/packages/ion-bazan/aliyun-http-signer) | ||
|
||
This library implements [Alibaba Cloud API Gateway request signature](https://www.alibabacloud.com/help/doc-detail/29475.htm) calculation for [PSR-7](https://www.php-fig.org/psr/psr-7/) compatible requests. | ||
It integrates with [Guzzle](https://github.com/guzzle/guzzle) by providing a simple [Middleware](http://docs.guzzlephp.org/en/stable/handlers-and-middleware.html#middleware). | ||
|
||
# Installation | ||
Use [Composer](https://getcomposer.org/) to install the package using: | ||
|
||
```bash | ||
composer require ion-bazan/aliyun-http-signer | ||
``` | ||
|
||
# Usage | ||
|
||
## Sign PSR-7-compatible API request | ||
|
||
```php | ||
<?php | ||
|
||
require_once 'vendor/autoload.php'; | ||
|
||
use IonBazan\AliyunSigner\Key; | ||
use IonBazan\AliyunSigner\RequestSigner; | ||
use Psr\Http\Message\RequestInterface; | ||
|
||
function signRequest(RequestInterface $request): RequestInterface | ||
{ | ||
// Provide credentials | ||
$appId = '12345678'; | ||
$secret = base64_encode('secret'); | ||
|
||
// Create signer | ||
$signer = new RequestSigner(new Key($appId, $secret)); | ||
|
||
return $signer->signRequest($request); | ||
} | ||
``` | ||
|
||
## Sign an API request using Guzzle middleware | ||
|
||
```php | ||
<?php | ||
|
||
require_once 'vendor/autoload.php'; | ||
|
||
use GuzzleHttp\Client; | ||
use GuzzleHttp\HandlerStack; | ||
use IonBazan\AliyunSigner\Key; | ||
use IonBazan\AliyunSigner\RequestSigner; | ||
use IonBazan\AliyunSigner\Guzzle\RequestSignerMiddleware; | ||
|
||
// Provide credentials | ||
$appId = '12345678'; | ||
$secret = base64_encode('secret'); | ||
|
||
// Create signer and middleware | ||
$signer = new RequestSigner(new Key($appId, $secret)); | ||
$middleware = new RequestSignerMiddleware($signer); | ||
$stack = HandlerStack::create(); | ||
$stack->push($middleware); | ||
|
||
$client = new Client(['handler' => $stack]); | ||
$response = $client->get('https://example.com/api/v1/test'); | ||
``` | ||
|
||
# Bugs & issues | ||
|
||
If you found a bug or security vulnerability, please [open an issue](https://github.com/IonBazan/aliyun-http-signer/issues/new) | ||
|
||
# Contributing | ||
|
||
Please feel free to submit Pull Requests adding new features or fixing bugs. | ||
Please note that code must follow PSR-1, PSR-2, PSR-4 and PSR-7. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
{ | ||
"name": "ion-bazan/aliyun-http-signer", | ||
"type": "library", | ||
"description": "An implementation of the Aliyun HTTP HMAC Spec in PHP that integrates with Guzzle.", | ||
"keywords": ["Aliyun", "Alibaba", "Alibaba Cloud", "API Gateway", "Guzzle", "PSR-7", "HMAC", "Signature"], | ||
"homepage": "https://github.com/IonBazan/aliyun-http-signer", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Ion Bazan", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"support": { | ||
"issues": "https://github.com/IonBazan/aliyun-http-signer/issues" | ||
}, | ||
"require": { | ||
"php": ">=7.2", | ||
"psr/http-message": "^1.0", | ||
"ramsey/uuid": "^3.0|^4.0" | ||
}, | ||
"require-dev": { | ||
"friendsofphp/php-cs-fixer": "^2.16", | ||
"guzzlehttp/guzzle": "^6.0|^7.0", | ||
"laminas/laminas-diactoros": "^2.2", | ||
"phpunit/phpunit": "^8.0|^9.0" | ||
}, | ||
"suggest": { | ||
"guzzlehttp/guzzle": "^6.0|^7.0", | ||
"laminas/laminas-diactoros": "^2.2" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"IonBazan\\AliyunSigner\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"IonBazan\\AliyunSigner\\Tests\\": "tests/" | ||
} | ||
}, | ||
"config": { | ||
"sort-packages": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd" | ||
bootstrap="vendor/autoload.php" | ||
colors="true" | ||
verbose="true" | ||
> | ||
<testsuites> | ||
<testsuite name="Aliyun Request Signer Test Suite"> | ||
<directory>tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<filter> | ||
<whitelist processUncoveredFilesFromWhitelist="true"> | ||
<directory suffix=".php">src</directory> | ||
</whitelist> | ||
</filter> | ||
|
||
<logging> | ||
<log type="coverage-clover" target="coverage.xml"/> | ||
</logging> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace IonBazan\AliyunSigner\Digest; | ||
|
||
class Digest implements DigestInterface | ||
{ | ||
public function sign(string $message, string $secret): string | ||
{ | ||
return base64_encode(hash_hmac('sha256', $message, $secret, true)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace IonBazan\AliyunSigner\Digest; | ||
|
||
interface DigestInterface | ||
{ | ||
public function sign(string $message, string $secret): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace IonBazan\AliyunSigner\Guzzle; | ||
|
||
use Closure; | ||
use IonBazan\AliyunSigner\RequestSigner; | ||
use Psr\Http\Message\RequestInterface; | ||
|
||
class RequestSignerMiddleware | ||
{ | ||
/** @var RequestSigner */ | ||
protected $requestSigner; | ||
|
||
public function __construct(RequestSigner $requestSigner) | ||
{ | ||
$this->requestSigner = $requestSigner; | ||
} | ||
|
||
public function __invoke(callable $handler): Closure | ||
{ | ||
return function (RequestInterface $request, array $options) use ($handler) { | ||
$request = $this->requestSigner->signRequest($request); | ||
|
||
return $handler($request, $options); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace IonBazan\AliyunSigner; | ||
|
||
class Key | ||
{ | ||
/** @var string */ | ||
protected $id; | ||
|
||
/** @var string */ | ||
protected $secret; | ||
|
||
public function __construct(string $id, string $secret) | ||
{ | ||
$this->id = $id; | ||
$this->secret = $secret; | ||
} | ||
|
||
public function getId(): string | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getSecret(): string | ||
{ | ||
return $this->secret; | ||
} | ||
} |
Oops, something went wrong.