Skip to content

Commit

Permalink
Require PHP 8.1+
Browse files Browse the repository at this point in the history
  • Loading branch information
IonBazan committed Oct 25, 2024
1 parent 84bab0c commit 0fb2a06
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 110 deletions.
8 changes: 3 additions & 5 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,12 @@ jobs:
matrix:
dependencies: [highest]
php-versions:
- '7.2'
- '7.3'
- '7.4'
- '8.0'
- '8.1'
- '8.2'
- '8.3'
- '8.4'
include:
- php-versions: '7.2'
- php-versions: '8.1'
dependencies: 'lowest'
- php-versions: '8.5'
dependencies: 'highest'
Expand All @@ -41,6 +37,8 @@ jobs:
run: vendor/bin/phpunit
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
- name: Run mutation tests
if: ${{ matrix.php-versions == 8.4 }}
env:
Expand Down
8 changes: 3 additions & 5 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,15 @@
->setRules([
'@Symfony' => true,
'@Symfony:risky' => true,
'@PHP71Migration' => true,
'@PHP71Migration:risky' => true,
'@PHP81Migration' => true,
'@PHP80Migration:risky' => true,
'@PHPUnit100Migration:risky' => true,
'ordered_imports' => true,
'global_namespace_import' => [
'import_classes' => true,
'import_constants' => true,
'import_functions' => true,
],
'trailing_comma_in_multiline' => [
'elements' => ['arrays', 'array_destructuring'],
],
])
->setRiskyAllowed(true)
->setFinder(
Expand Down
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,23 @@
}
],
"require": {
"php": ">=7.2",
"php": "^8.1",
"psr/http-message": "^1.0 || ^2.0",
"ramsey/uuid": "^3.0 || ^4.0 || ^5.0"
"ramsey/uuid": "^3.0 || ^4.0 || ^5.0",
"symfony/polyfill-php82": "^1.22"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.0",
"friendsofphp/php-cs-fixer": "^3.64",
"guzzlehttp/guzzle": "^6.0 || ^7.0",
"infection/infection": "~0.15",
"laminas/laminas-diactoros": "^2.2 || ^3.0",
"php-http/client-common": "^2.0",
"phpunit/phpunit": "^8 || ^9 || ^10 || ^11"
"phpunit/phpunit": "^10.5 || ^11"
},
"suggest": {
"guzzlehttp/guzzle": "^6.0|^7.0, to use Guzzle Middleware",
"ion-bazan/guzzle-bundle-aliyun-signer-plugin": "to integrate with Guzzle Bundle",
"laminas/laminas-diactoros": "^2.2, to build HTTP requests",
"laminas/laminas-diactoros": "^2.2 || ^3.0, to build HTTP requests",
"php-http/client-common": "^2.0, to use HttPlug plugin"
},
"config": {
Expand Down
38 changes: 17 additions & 21 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.2/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 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true" cacheDirectory=".phpunit.cache">
<coverage>
<report>
<clover outputFile="coverage.xml"/>
</report>
</coverage>
<testsuites>
<testsuite name="Aliyun Request Signer Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<logging/>
<source>
<include>
<directory>src</directory>
</include>
</source>
</phpunit>
6 changes: 1 addition & 5 deletions src/Guzzle/RequestSignerMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@

class RequestSignerMiddleware
{
/** @var RequestSigner */
protected $requestSigner;

public function __construct(RequestSigner $requestSigner)
public function __construct(protected readonly RequestSigner $requestSigner)
{
$this->requestSigner = $requestSigner;
}

public function __invoke(callable $handler): Closure
Expand Down
6 changes: 1 addition & 5 deletions src/HttPlug/RequestSignerPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,8 @@

class RequestSignerPlugin implements Plugin
{
/** @var RequestSigner */
protected $requestSigner;

public function __construct(RequestSigner $requestSigner)
public function __construct(protected readonly RequestSigner $requestSigner)
{
$this->requestSigner = $requestSigner;
}

public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise
Expand Down
27 changes: 7 additions & 20 deletions src/Key.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,14 @@

namespace IonBazan\AliyunSigner;

use SensitiveParameter;

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;
public function __construct(
public readonly string $id,
#[SensitiveParameter]
public readonly string $secret,
) {
}
}
23 changes: 7 additions & 16 deletions src/RequestSigner.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Ramsey\Uuid\Uuid;

use function sprintf;
use function strlen;

class RequestSigner
{
Expand All @@ -26,39 +25,31 @@ class RequestSigner
public const HEADER_DATE = 'Date';
public const HEADER_CONTENT_MD5 = 'Content-MD5';

/** @var Key */
private $key;

/** @var Digest */
private $digest;

private $signatureHeaders = [
private array $signatureHeaders = [
self::HEADER_X_CA_KEY,
self::HEADER_X_CA_NONCE,
self::HEADER_X_CA_SIGNATURE_METHOD,
self::HEADER_X_CA_TIMESTAMP,
self::HEADER_X_CA_STAGE,
];

public function __construct(Key $key, ?DigestInterface $digest = null)
public function __construct(private readonly Key $key, private readonly DigestInterface $digest = new Digest())
{
$this->key = $key;
$this->digest = $digest ?? new Digest();
}

public function signRequest(RequestInterface $request, ?DateTimeInterface $date = null, ?string $nonce = null): RequestInterface
{
$nonce = $nonce ?? Uuid::uuid4()->toString();
$nonce ??= Uuid::uuid4()->toString();

$timestamp = ($date ?? new DateTime())->format('Uv');
$body = $request->getBody()->getContents();
$contentMd5 = strlen($body) ? base64_encode(md5($body, true)) : '';
$contentMd5 = $body !== '' ? base64_encode(md5($body, true)) : '';

$request = $request->withHeader(self::HEADER_DATE, $timestamp)
->withHeader(self::HEADER_CONTENT_MD5, $contentMd5)
->withHeader(self::HEADER_X_CA_SIGNATURE_METHOD, 'HmacSHA256')
->withHeader(self::HEADER_X_CA_TIMESTAMP, $timestamp)
->withHeader(self::HEADER_X_CA_KEY, $this->key->getId())
->withHeader(self::HEADER_X_CA_KEY, $this->key->id)
->withHeader(self::HEADER_X_CA_NONCE, $nonce);

$headers = $this->getHeadersToSign($request);
Expand All @@ -73,7 +64,7 @@ public function signRequest(RequestInterface $request, ?DateTimeInterface $date
$this->getUrlToSign($request),
]);

$signature = $this->digest->sign($textToSign, $this->key->getSecret());
$signature = $this->digest->sign($textToSign, $this->key->secret);

return $request->withHeader(self::HEADER_X_CA_SIGNATURE, $signature)
->withHeader(self::HEADER_X_CA_SIGNATURE_HEADERS, implode(',', array_keys($headers)));
Expand All @@ -96,7 +87,7 @@ protected function getUrlToSign(RequestInterface $request): string
{
$query = urldecode($request->getUri()->getQuery());

return $request->getUri()->getPath().(strlen($query) ? '?'.$query : '');
return $request->getUri()->getPath().($query !== '' ? '?'.$query : '');
}

protected function getHeadersToSign(RequestInterface $request): array
Expand Down
53 changes: 25 additions & 28 deletions tests/RequestSignerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@
use IonBazan\AliyunSigner\RequestSigner;
use Laminas\Diactoros\Request;
use Laminas\Diactoros\StreamFactory;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\RequestInterface;

class RequestSignerTest extends TestCase
{
/**
* @dataProvider requestDataProvider
*/
#[DataProvider('requestDataProvider')]
public function testRequestMessageSignature(RequestInterface $request, string $nonce, string $expectedMessage, string $bodyMd5 = ''): void
{
$digest = $this->createMock(DigestInterface::class);
Expand Down Expand Up @@ -52,18 +51,17 @@ public static function requestDataProvider(): array
),
'test-nonce',
<<<MESSAGE
GET
*/*
application/json
1588032000000
x-ca-key:1234
x-ca-nonce:test-nonce
x-ca-signaturemethod:HmacSHA256
x-ca-timestamp:1588032000000
/v1.0/category/123/products
MESSAGE
,
GET
*/*
application/json
1588032000000
x-ca-key:1234
x-ca-nonce:test-nonce
x-ca-signaturemethod:HmacSHA256
x-ca-timestamp:1588032000000
/v1.0/category/123/products
MESSAGE,
],
'POST with JSON body query string' => [
new Request(
Expand All @@ -74,18 +72,17 @@ public static function requestDataProvider(): array
),
'test-nonce',
<<<MESSAGE
POST
*/*
+8JLzHoXlHWPwTJ/z+va9g==
application/json
1588032000000
x-ca-key:1234
x-ca-nonce:test-nonce
x-ca-signaturemethod:HmacSHA256
x-ca-timestamp:1588032000000
/v1.0/category/123/products?page=10
MESSAGE
,
POST
*/*
+8JLzHoXlHWPwTJ/z+va9g==
application/json
1588032000000
x-ca-key:1234
x-ca-nonce:test-nonce
x-ca-signaturemethod:HmacSHA256
x-ca-timestamp:1588032000000
/v1.0/category/123/products?page=10
MESSAGE,
'+8JLzHoXlHWPwTJ/z+va9g==',
],
];
Expand Down Expand Up @@ -171,7 +168,7 @@ protected function assertSignedRequest(
int $timestamp,
string $signature,
string $contentMd5 = '',
string $signatureHeaders = 'x-ca-key,x-ca-nonce,x-ca-signaturemethod,x-ca-timestamp'
string $signatureHeaders = 'x-ca-key,x-ca-nonce,x-ca-signaturemethod,x-ca-timestamp',
): void {
$this->assertSame($signature, $request->getHeaderLine('X-Ca-Signature'));
$this->assertTrue($request->hasHeader('X-Ca-Nonce'));
Expand Down

0 comments on commit 0fb2a06

Please sign in to comment.