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

chore: drop eol php 8.0 #384

Merged
merged 8 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
/.gitattributes export-ignore
/.github export-ignore
/.gitignore export-ignore
/.travis.yml export-ignore
/phpunit.xml.dist export-ignore
/.php-cs-fixer.php export-ignore
/README.md export-ignore
/phpstan.neon export-ignore
/phpunit.xml.dist export-ignore
/tests export-ignore
10 changes: 5 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ jobs:
tests:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php: ['8.0', '8.1', '8.2', '8.3']
php: ['8.1', '8.2', '8.3']
ext: ['curl, mbstring, openssl', 'curl, mbstring, openssl, gmp']

name: PHP ${{ matrix.php }} (${{ matrix.ext }})
Expand All @@ -37,7 +38,7 @@ jobs:
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

- name: Cache Composer dependencies
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
Expand All @@ -49,9 +50,9 @@ jobs:
- name: Setup node
uses: actions/setup-node@v4
with:
node-version: '18'
node-version: '20'

- name: Install web-push-testing-service
- name: Install web-push-testing server
run: |
npm install -g web-push-testing

Expand All @@ -65,7 +66,6 @@ jobs:
run: composer test:typing

- name: Run php-cs-fixer
if: ${{ matrix.php != '8.2' }} # Not supported yet.
run: |
composer test:syntax
composer test:syntax_tests
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ cli.log
module.log
.vagrant
Vagrantfile # temp, may be?
/.phpunit.result.cache
.phpunit.cache/*
.php-cs-fixer.cache
.idea/
21 changes: 21 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
// https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/master/doc/config.rst

$config = new PhpCsFixer\Config();
$rules = [
'@PSR12' => true, // The default rule.
'@PHP81Migration' => true, // Must be the same as the min PHP version.
'blank_line_after_opening_tag' => false, // Do not waste space between <?php and declare.
'global_namespace_import' => ['import_classes' => false, 'import_constants' => false, 'import_functions' => false],
'php_unit_test_class_requires_covers' => true,
'php_unit_method_casing' => true,
// Do not enable by default. These rules require review!! (but they are useful)
// '@PHP80Migration:risky' => true,
// '@PHPUnit100Migration:risky' => true,
];

$config->setRules($rules);
$config->setHideProgress(true);
$config->setRiskyAllowed(true);

return $config;
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ As it is standardized, you don't have to worry about what server type it relies

## Requirements

PHP 8.0+ and the following extensions:
PHP 8.1+ and the following extensions:
* gmp (optional but better for performance)
* mbstring
* curl
Expand All @@ -21,6 +21,7 @@ There is no support and maintenance for older PHP versions, however you are free
- PHP 7.1: `v3.x-v5.x`
- PHP 7.2: `v6.x`
- PHP 7.3 7.4: `v7.x`
- PHP 8.0: `v8.x`

This README is only compatible with the latest version. Each version of the library has a git tag where the corresponding README can be read.

Expand Down
23 changes: 12 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,32 @@
"scripts": {
"fix:syntax": "./vendor/bin/php-cs-fixer fix ./src --using-cache=no",
"fix:syntax_tests": "./vendor/bin/php-cs-fixer fix ./tests --using-cache=no",
"test:unit": "./vendor/bin/phpunit --color",
"test:unit": "./vendor/bin/phpunit",
"test:unit_offline": "./vendor/bin/phpunit --exclude-group=online",
"test:typing": "./vendor/bin/phpstan analyse",
"test:syntax": "./vendor/bin/php-cs-fixer fix ./src --dry-run --stop-on-violation --using-cache=no",
"test:syntax_tests": "./vendor/bin/php-cs-fixer fix ./tests --dry-run --stop-on-violation --using-cache=no"
},
"require": {
"php": ">=8.0",
"php": ">=8.1",
"ext-curl": "*",
"ext-json": "*",
"ext-mbstring": "*",
"ext-openssl": "*",
"guzzlehttp/guzzle": "^7.0.1|^6.2",
"web-token/jwt-signature": "^2.0|^3.0.2",
"web-token/jwt-key-mgmt": "^2.0|^3.0.2",
"web-token/jwt-signature-algorithm-ecdsa": "^2.0|^3.0.2",
"web-token/jwt-util-ecc": "^2.0|^3.0.2",
"spomky-labs/base64url": "^2.0"
"guzzlehttp/guzzle": "^7.4.5",
"web-token/jwt-signature": "^3.2.9",
"web-token/jwt-key-mgmt": "^3.2.9",
"web-token/jwt-signature-algorithm-ecdsa": "^3.2.9",
"web-token/jwt-util-ecc": "^3.2.9",
"spomky-labs/base64url": "^2.0.4"
},
"suggest": {
"ext-gmp": "Optional for performance."
},
"require-dev": {
"phpunit/phpunit": "^9.5.27",
"phpstan/phpstan": "^1.9.8",
"friendsofphp/php-cs-fixer": "^v3.13.2"
"phpunit/phpunit": "^10.5.9",
"phpstan/phpstan": "^1.10.57",
"friendsofphp/php-cs-fixer": "^v3.48.0"
},
"autoload": {
"psr-4": {
Expand Down
23 changes: 18 additions & 5 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
backupStaticProperties="false"
bootstrap="vendor/autoload.php"
cacheDirectory=".phpunit.cache"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
stopOnFailure="false"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
>
<source ignoreSuppressionOfDeprecations="true"
ignoreSuppressionOfPhpDeprecations="true"
ignoreSuppressionOfErrors="true"
ignoreSuppressionOfNotices="true"
ignoreSuppressionOfPhpNotices="true"
ignoreSuppressionOfWarnings="true"
ignoreSuppressionOfPhpWarnings="true"
>
<include>
<directory>src</directory>
</include>
</source>
<testsuites>
<testsuite name="WebPush Test Suite">
<directory suffix=".php">./tests/</directory>
Expand Down
72 changes: 9 additions & 63 deletions src/Encryption.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ private static function createLocalKeyObjectUsingPurePhpMethod(): array
'x' => Base64Url::encode(self::addNullPadding($publicKey->getPoint()->getX()->toBytes(false))),
'y' => Base64Url::encode(self::addNullPadding($publicKey->getPoint()->getY()->toBytes(false))),
'd' => Base64Url::encode(self::addNullPadding($privateKey->getSecret()->toBytes(false))),
])
]),
];
}

Expand All @@ -266,7 +266,7 @@ private static function createLocalKeyObjectUsingPurePhpMethod(): array
'x' => Base64Url::encode(self::addNullPadding(hex2bin(gmp_strval($publicKey->getPoint()->getX(), 16)))),
'y' => Base64Url::encode(self::addNullPadding(hex2bin(gmp_strval($publicKey->getPoint()->getY(), 16)))),
'd' => Base64Url::encode(self::addNullPadding(hex2bin(gmp_strval($privateKey->getSecret(), 16)))),
])
]),
];
}

Expand Down Expand Up @@ -294,7 +294,7 @@ private static function createLocalKeyObjectUsingOpenSSL(): array
'x' => Base64Url::encode(self::addNullPadding($details['ec']['x'])),
'y' => Base64Url::encode(self::addNullPadding($details['ec']['y'])),
'd' => Base64Url::encode(self::addNullPadding($details['ec']['d'])),
])
]),
];
}

Expand All @@ -320,68 +320,14 @@ private static function getIKM(string $userAuthToken, string $userPublicKey, str

private static function calculateAgreementKey(JWK $private_key, JWK $public_key): string
{
if (function_exists('openssl_pkey_derive')) {
try {
$publicPem = ECKey::convertPublicKeyToPEM($public_key);
$privatePem = ECKey::convertPrivateKeyToPEM($private_key);

$result = openssl_pkey_derive($publicPem, $privatePem, 256); // @phpstan-ignore-line
if ($result === false) {
throw new \Exception('Unable to compute the agreement key');
}
return $result;
} catch (\Throwable $throwable) {
//Does nothing. Will fallback to the pure PHP function
}
}

$publicPem = ECKey::convertPublicKeyToPEM($public_key);
$privatePem = ECKey::convertPrivateKeyToPEM($private_key);

$curve = NistCurve::curve256();
try {
$rec_x = self::convertBase64ToBigInteger($public_key->get('x'));
$rec_y = self::convertBase64ToBigInteger($public_key->get('y'));
$sen_d = self::convertBase64ToBigInteger($private_key->get('d'));
$priv_key = PrivateKey::create($sen_d);
$pub_key = $curve->getPublicKeyFrom($rec_x, $rec_y);

return hex2bin(str_pad($curve->mul($pub_key->getPoint(), $priv_key->getSecret())->getX()->toBase(16), 64, '0', STR_PAD_LEFT)); // @phpstan-ignore-line
} catch (\Throwable $e) {
$rec_x = self::convertBase64ToGMP($public_key->get('x'));
$rec_y = self::convertBase64ToGMP($public_key->get('y'));
$sen_d = self::convertBase64ToGMP($private_key->get('d'));
$priv_key = PrivateKey::create($sen_d); // @phpstan-ignore-line
$pub_key = $curve->getPublicKeyFrom($rec_x, $rec_y); // @phpstan-ignore-line

return hex2bin(gmp_strval($curve->mul($pub_key->getPoint(), $priv_key->getSecret())->getX(), 16)); // @phpstan-ignore-line
$result = openssl_pkey_derive($publicPem, $privatePem, 256);
if ($result === false) {
throw new \Exception('Unable to compute the agreement key');
}
}

/**
* @throws \ErrorException
*/
private static function convertBase64ToBigInteger(string $value): BigInteger
{
$value = unpack('H*', Base64Url::decode($value));

if ($value === false) {
throw new \ErrorException('Unable to unpack hex value from string');
}

return BigInteger::fromBase($value[1], 16);
}

/**
* @throws \ErrorException
*/
private static function convertBase64ToGMP(string $value): \GMP
{
$value = unpack('H*', Base64Url::decode($value));

if ($value === false) {
throw new \ErrorException('Unable to unpack hex value from string');
}

return gmp_init($value[1], 16);
return $result;
}

private static function addNullPadding(string $data): string
Expand Down
2 changes: 1 addition & 1 deletion src/VAPID.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public static function createVapidKeys(): array

return [
'publicKey' => Base64Url::encode($binaryPublicKey),
'privateKey' => Base64Url::encode($binaryPrivateKey)
'privateKey' => Base64Url::encode($binaryPrivateKey),
];
}
}
14 changes: 7 additions & 7 deletions tests/EncryptionTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
<?php

declare(strict_types=1);

<?php declare(strict_types=1);
/*
* This file is part of the WebPush library.
*
Expand All @@ -15,7 +12,11 @@
use Jose\Component\Core\JWK;
use Minishlink\WebPush\Encryption;
use Minishlink\WebPush\Utils;
use PHPUnit\Framework\Attributes\DataProvider;

/**
* @covers \Minishlink\WebPush\Encryption
*/
final class EncryptionTest extends PHPUnit\Framework\TestCase
{
public function testDeterministicEncrypt(): void
Expand Down Expand Up @@ -75,10 +76,9 @@ public function testGetContentCodingHeader(): void
}

/**
* @dataProvider payloadProvider
*
* @throws ErrorException
*/
#[dataProvider('payloadProvider')]
public function testPadPayload(string $payload, int $maxLengthToPad, int $expectedResLength): void
{
$res = Encryption::padPayload($payload, $maxLengthToPad, "aesgcm");
Expand All @@ -87,7 +87,7 @@ public function testPadPayload(string $payload, int $maxLengthToPad, int $expect
$this->assertEquals($expectedResLength, Utils::safeStrlen($res));
}

public function payloadProvider(): array
public static function payloadProvider(): array
{
return [
['testé', 0, 8],
Expand Down
Loading