From 44526038c0d46e290a8f3500fb927d75ec56139f Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Sun, 5 Jan 2025 12:00:09 +0100 Subject: [PATCH 1/4] Update phpstan dependency version --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index b68c2b6..eaf9ac5 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ }, "require-dev": { "phpunit/phpunit": "^10.0 || ^11.0", - "phpstan/phpstan": "^2", + "phpstan/phpstan": "^2.1", "squizlabs/php_codesniffer": "^3.8", "slevomat/coding-standard": "~8.0" }, From 7f6ad9f6468cfb12137212218c14e59840c6f67d Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Sun, 5 Jan 2025 12:04:28 +0100 Subject: [PATCH 2/4] Remove unused code --- src/Vaults/AbstractVault.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Vaults/AbstractVault.php b/src/Vaults/AbstractVault.php index 25a28f1..9102ea5 100644 --- a/src/Vaults/AbstractVault.php +++ b/src/Vaults/AbstractVault.php @@ -141,7 +141,7 @@ protected function denyAccess(?string $message = null): void $message = empty($message) ? '' . $protocol . ' 401 Unauthorized' : $message; header($protocol . ' 401 Unauthorized'); - header('WWW-Authenticate: ' . $this->type()->value . ' ' . (string) $this->directive()); + header('WWW-Authenticate: ' . $this->type()->value . ' ' . $this->directive()); exit($message); } } From e847419de6a3de67cb657b5c8f546afb7f55d58f Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Sun, 5 Jan 2025 12:14:11 +0100 Subject: [PATCH 3/4] Modernize code --- src/Directive.php | 8 +++++--- src/Tokens/HttpAuthentification.php | 4 ++-- src/Tokens/HttpAuthorization.php | 6 +++--- src/Tokens/PhpAuthDigest.php | 4 ++-- src/Tokens/RedirectHttpAuthorization.php | 4 ++-- src/Vaults/AbstractVault.php | 2 +- src/Vaults/DigestVault.php | 2 +- 7 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/Directive.php b/src/Directive.php index f9874c7..6b71faf 100644 --- a/src/Directive.php +++ b/src/Directive.php @@ -25,8 +25,10 @@ public function __construct(protected array $parameters = []) */ public function __toString(): string { - return implode(', ', array_map(function ($key, $value) { - return sprintf('%s="%s"', $key, $value); - }, array_keys($this->parameters), $this->parameters)); + return implode(', ', array_map( + fn(mixed $key, mixed $value): string => sprintf('%s="%s"', $key, $value), + array_keys($this->parameters), + $this->parameters, + )); } } diff --git a/src/Tokens/HttpAuthentification.php b/src/Tokens/HttpAuthentification.php index 54736d6..6f445f1 100644 --- a/src/Tokens/HttpAuthentification.php +++ b/src/Tokens/HttpAuthentification.php @@ -22,11 +22,11 @@ public function parse(): array throw new AuthentificationException('Failed to parse token.'); } - if (strtolower(substr($value, 0, 5)) !== 'basic') { + if (strtolower(substr((string) $value, 0, 5)) !== 'basic') { throw new AuthentificationException('Failed to parse token.'); } - $data = explode(':', base64_decode(substr($value, 6))); + $data = explode(':', base64_decode(substr((string) $value, 6))); return [ 'username' => $this->getArrayValue($data, 0), diff --git a/src/Tokens/HttpAuthorization.php b/src/Tokens/HttpAuthorization.php index db5595f..04a007d 100644 --- a/src/Tokens/HttpAuthorization.php +++ b/src/Tokens/HttpAuthorization.php @@ -22,16 +22,16 @@ public function parse(): array throw new AuthentificationException('Failed to parse token.'); } - if (strtolower(substr($value, 0, 6)) !== 'digest') { + if (strtolower(substr((string) $value, 0, 6)) !== 'digest') { throw new AuthentificationException('Failed to parse token.'); } - preg_match_all('@(\w+)=(?:(?:")([^"]+)"|([^\s,$]+))@', $value, $matches, PREG_SET_ORDER); + preg_match_all('@(\w+)=(?:(?:")([^"]+)"|([^\s,$]+))@', (string) $value, $matches, PREG_SET_ORDER); $properties = []; foreach ($matches as $m) { $key = $m[1]; - $value = $m[2] ? $m[2] : $m[3]; + $value = $m[2] ?: $m[3]; $properties[$key] = $value; } diff --git a/src/Tokens/PhpAuthDigest.php b/src/Tokens/PhpAuthDigest.php index 5e55c1c..0164229 100644 --- a/src/Tokens/PhpAuthDigest.php +++ b/src/Tokens/PhpAuthDigest.php @@ -22,12 +22,12 @@ public function parse(): array throw new AuthentificationException('Failed to parse token.'); } - preg_match_all('@(\w+)=(?:(?:")([^"]+)"|([^\s,$]+))@', $value, $matches, PREG_SET_ORDER); + preg_match_all('@(\w+)=(?:(?:")([^"]+)"|([^\s,$]+))@', (string) $value, $matches, PREG_SET_ORDER); $properties = []; foreach ($matches as $m) { $key = $m[1]; - $value = $m[2] ? $m[2] : $m[3]; + $value = $m[2] ?: $m[3]; $properties[$key] = $value; } diff --git a/src/Tokens/RedirectHttpAuthorization.php b/src/Tokens/RedirectHttpAuthorization.php index df06cb9..6e58080 100644 --- a/src/Tokens/RedirectHttpAuthorization.php +++ b/src/Tokens/RedirectHttpAuthorization.php @@ -22,11 +22,11 @@ public function parse(): array throw new AuthentificationException('Failed to parse token.'); } - if (strtolower(substr($value, 0, 5)) !== 'basic') { + if (strtolower(substr((string) $value, 0, 5)) !== 'basic') { throw new AuthentificationException('Failed to parse token.'); } - list($username, $password) = explode(':', base64_decode(substr($value, 6))); + [$username, $password] = explode(':', base64_decode(substr((string) $value, 6))); return [ 'username' => $username, diff --git a/src/Vaults/AbstractVault.php b/src/Vaults/AbstractVault.php index 9102ea5..e858fec 100644 --- a/src/Vaults/AbstractVault.php +++ b/src/Vaults/AbstractVault.php @@ -137,7 +137,7 @@ public function setCredentials(string $username, string $password): self */ protected function denyAccess(?string $message = null): void { - $protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1'; + $protocol = $_SERVER['SERVER_PROTOCOL'] ?: 'HTTP/1.1'; $message = empty($message) ? '' . $protocol . ' 401 Unauthorized' : $message; header($protocol . ' 401 Unauthorized'); diff --git a/src/Vaults/DigestVault.php b/src/Vaults/DigestVault.php index e20784f..bf1515c 100644 --- a/src/Vaults/DigestVault.php +++ b/src/Vaults/DigestVault.php @@ -61,7 +61,7 @@ public function directive(): DirectiveInterface */ private function tokenHash(TokenInterface $token): string { - $method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET'; + $method = $_SERVER['REQUEST_METHOD'] ?: 'GET'; return md5(implode(':', [ md5(sprintf('%s:%s:%s', $token->username(), $this->realm(), $this->password())), From b028c2b76021a7acce7f88e78b147012ce140d26 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Sun, 5 Jan 2025 12:18:12 +0100 Subject: [PATCH 4/4] Fix tests --- phpunit.xml.dist | 2 +- src/Vaults/DigestVault.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index e7c9e84..a4942e2 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,5 +1,5 @@ - + ./tests/Unit/ diff --git a/src/Vaults/DigestVault.php b/src/Vaults/DigestVault.php index bf1515c..e20784f 100644 --- a/src/Vaults/DigestVault.php +++ b/src/Vaults/DigestVault.php @@ -61,7 +61,7 @@ public function directive(): DirectiveInterface */ private function tokenHash(TokenInterface $token): string { - $method = $_SERVER['REQUEST_METHOD'] ?: 'GET'; + $method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET'; return md5(implode(':', [ md5(sprintf('%s:%s:%s', $token->username(), $this->realm(), $this->password())),