Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
IonBazan committed Apr 28, 2020
0 parents commit 0fbc014
Show file tree
Hide file tree
Showing 14 changed files with 641 additions and 0 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/php.yml
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
10 changes: 10 additions & 0 deletions .gitignore
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_*
13 changes: 13 additions & 0 deletions .php_cs.dist
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']))
;
80 changes: 80 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Alibaba Cloud API Gateway HTTP Request Signer for PHP

[![Latest version](https://img.shields.io/packagist/v/ion-bazan/aliyun-http-signer.svg)](https://packagist.org/packages/ion-bazan/aliyun-http-signer)
[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/IonBazan/aliyun-http-signer/Tests)](https://github.com/IonBazan/aliyun-http-signer/actions)
[![PHP version](https://img.shields.io/packagist/php-v/ion-bazan/aliyun-http-signer.svg)](https://packagist.org/packages/ion-bazan/aliyun-http-signer)
[![Codecov](https://img.shields.io/codecov/c/gh/IonBazan/aliyun-http-signer)](https://codecov.io/gh/IonBazan/aliyun-http-signer)
[![Downloads](https://img.shields.io/packagist/dt/ion-bazan/aliyun-http-signer.svg)](https://packagist.org/packages/ion-bazan/aliyun-http-signer)
[![License](https://img.shields.io/packagist/l/ion-bazan/aliyun-http-signer.svg)](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.
45 changes: 45 additions & 0 deletions composer.json
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
}
}
23 changes: 23 additions & 0 deletions phpunit.xml.dist
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>
13 changes: 13 additions & 0 deletions src/Digest/Digest.php
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));
}
}
10 changes: 10 additions & 0 deletions src/Digest/DigestInterface.php
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;
}
29 changes: 29 additions & 0 deletions src/Guzzle/RequestSignerMiddleware.php
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);
};
}
}
30 changes: 30 additions & 0 deletions src/Key.php
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;
}
}
Loading

0 comments on commit 0fbc014

Please sign in to comment.