Skip to content

Commit

Permalink
update code for PHP 8.3 mini
Browse files Browse the repository at this point in the history
  • Loading branch information
achretien committed Jan 13, 2025
1 parent 598f361 commit 2dc755f
Show file tree
Hide file tree
Showing 43 changed files with 819 additions and 1,333 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/composer.lock
/vendor/
.php_cs.cache
.php-cs-fixer.cache
.phpunit.result.cache
doc/build
.tags*
2 changes: 1 addition & 1 deletion .php_cs → .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
->in(__DIR__ . "/src")
->in(__DIR__ . "/tests");

return PhpCsFixer\Config::create()
return (new PhpCsFixer\Config())
->setRules([
'@PSR2' => true,
'@Symfony' => true,
Expand Down
25 changes: 13 additions & 12 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,21 @@
}
},
"require": {
"php": ">=5.6",
"psr/http-message": "^1.0",
"php": ">=8.3",
"psr/http-message": "^1.0 || ^2.0",
"phpseclib/phpseclib": "~3.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2",
"guzzlehttp/psr7": "^1.2",
"phpunit/phpunit": "~5.6",
"symfony/http-foundation": "~2.8|~3.0",
"symfony/psr-http-message-bridge": "^1.0.2",
"zendframework/zend-diactoros": "^1.1",
"nyholm/psr7": "^1.1",
"nyholm/psr7-server": "^0.3.0",
"kriswallsmith/buzz": "^1.0",
"zendframework/zend-httphandlerrunner": "^1.1"
"friendsofphp/php-cs-fixer": "^3.64",
"guzzlehttp/psr7": "^2.7",
"phpunit/phpunit": "^11.5",
"symfony/http-foundation": "^7.2.2",
"symfony/psr-http-message-bridge": "^7.2.0",
"php-http/discovery": "^1.20"
},
"config": {
"allow-plugins": {
"php-http/discovery": true
}
}
}
34 changes: 13 additions & 21 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
beStrictAboutTestsThatDoNotTestAnything="true"
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
beStrictAboutOutputDuringTests="true"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
verbose="true"
>
<testsuites>
<testsuite name="HTTP Signatures Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
<testsuites>
<testsuite name="HTTP Signatures Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>

<source ignoreIndirectDeprecations="true" restrictNotices="true" restrictWarnings="true">
<include>
<directory>src</directory>
</include>
</source>
</phpunit>
46 changes: 12 additions & 34 deletions src/Algorithm.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,20 @@
abstract class Algorithm
{
/**
* @param string $name
*
* @return HmacAlgorithm
*
* @throws Exception
*/
public static function create($name)
public static function create(string $name): AlgorithmInterface
{
switch ($name) {
case 'hmac-sha1':
return new HmacAlgorithm('sha1');
break;
case 'hmac-sha256':
return new HmacAlgorithm('sha256');
break;
case 'rsa-sha1':
return new RsaAlgorithm('sha1');
break;
case 'rsa-sha256':
return new RsaAlgorithm('sha256');
break;
case 'dsa-sha1':
return new DsaAlgorithm('sha1');
break;
case 'dsa-sha256':
return new DsaAlgorithm('sha256');
break;
case 'ec-sha1':
return new EcAlgorithm('sha1');
break;
case 'ec-sha256':
return new EcAlgorithm('sha256');
break;
default:
throw new AlgorithmException("No algorithm named '$name'");
break;
}
return match ($name) {
'hmac-sha1' => new HmacAlgorithm('sha1'),
'hmac-sha256' => new HmacAlgorithm('sha256'),
'rsa-sha1' => new RsaAlgorithm('sha1'),
'rsa-sha256' => new RsaAlgorithm('sha256'),
'dsa-sha1' => new DsaAlgorithm('sha1'),
'dsa-sha256' => new DsaAlgorithm('sha256'),
'ec-sha1' => new EcAlgorithm('sha1'),
'ec-sha256' => new EcAlgorithm('sha256'),
default => throw new AlgorithmException("No algorithm named '$name'"),
};
}
}
13 changes: 2 additions & 11 deletions src/AlgorithmInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,7 @@

interface AlgorithmInterface
{
/**
* @return string
*/
public function name();
public function name(): string;

/**
* @param string $key
* @param string $data
*
* @return string
*/
public function sign($key, $data);
public function sign(string $key, string $data): string;
}
8 changes: 8 additions & 0 deletions src/AsymmetricAlgorithmInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace HttpSignatures;

interface AsymmetricAlgorithmInterface extends AlgorithmInterface
{
public function verify(string $message, string $signature, array|string $verifyingKey): bool;
}
55 changes: 17 additions & 38 deletions src/BodyDigest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,23 @@

namespace HttpSignatures;

use Psr\Http\Message\RequestInterface;

class BodyDigest
{
/** @var string */
const validHashes =
'sha sha1 sha256 sha512';
public const string validHashes = 'sha sha1 sha256 sha512';

/** @var string */
private $hashName;
private string $hashName;

/** @var string */
private $digestHeaderPrefix;
private string $digestHeaderPrefix;

/**
* @param string $hashAlgorithm
*
* @return BodyDigest
*
* @throws DigestException
*/
public function __construct($hashAlgorithm = null)
public function __construct(?string $hashAlgorithm = null)
{
// Default to sha256 if no spec provided
if (is_null($hashAlgorithm) || '' == $hashAlgorithm) {
if (empty($hashAlgorithm)) {
$hashAlgorithm = 'sha256';
}

Expand All @@ -49,7 +43,7 @@ public function __construct($hashAlgorithm = null)
}
}

public function putDigestInHeaderList($headerList)
public function putDigestInHeaderList(HeaderList $headerList): HeaderList
{
if (!array_search('digest', $headerList->names)) {
$headerList->names[] = 'digest';
Expand All @@ -58,18 +52,16 @@ public function putDigestInHeaderList($headerList)
return $headerList;
}

public function setDigestHeader($message)
public function setDigestHeader(RequestInterface $message): RequestInterface
{
$message = $message->withoutHeader('Digest')
return $message->withoutHeader('Digest')
->withHeader(
'Digest',
$this->getDigestHeaderLinefromBody($message->getBody())
$this->getDigestHeaderLineFromBody($message->getBody())
);

return $message;
}

public function getDigestHeaderLinefromBody($messageBody)
public function getDigestHeaderLineFromBody(?string $messageBody): string
{
if (is_null($messageBody)) {
$messageBody = '';
Expand All @@ -79,13 +71,9 @@ public function getDigestHeaderLinefromBody($messageBody)
}

/**
* @param RequestInterface $message
*
* @return BodyDigest
*
* @throws DigestException
*/
public static function fromMessage($message)
public static function fromMessage(RequestInterface $message): BodyDigest
{
$digestLine = $message->getHeader('Digest');
if (!$digestLine) {
Expand All @@ -98,13 +86,9 @@ public static function fromMessage($message)
}

/**
* @param string $digestLine
*
* @return string
*
* @throws DigestException
*/
private static function getDigestAlgorithm($digestLine)
private static function getDigestAlgorithm(string $digestLine): string
{
// simple test if properly delimited, but see below
if (!strpos($digestLine, '=')) {
Expand All @@ -120,20 +104,15 @@ private static function getDigestAlgorithm($digestLine)
return $hashAlgorithm;
}

public function isValid($message)
public function isValid(RequestInterface $message): bool
{
$receivedDigest = $message->getHeader('Digest')[0];
$expectedDigest = $this->getDigestHeaderLinefromBody($message->getBody());
$expectedDigest = $this->getDigestHeaderLineFromBody($message->getBody());

return hash_equals($receivedDigest, $expectedDigest);
}

/**
* @param string $digestSpec
*
* @return bool
*/
public static function isValidDigestSpec($digestSpec)
public static function isValidDigestSpec(string $digestSpec): bool
{
$digestSpec = strtolower(str_replace('-', '', $digestSpec));
$validHashes = explode(' ', self::validHashes);
Expand Down
Loading

0 comments on commit 2dc755f

Please sign in to comment.