Skip to content

Commit b07ce15

Browse files
authored
Merge pull request #5 from middlewares/php84-or-improvements
Add support for php8.4 and other improvements
2 parents 4fbe28a + 747236b commit b07ce15

15 files changed

+173
-60
lines changed

.gitattributes

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
* text=auto eol=lf
22

3-
/tests export-ignore
4-
.editorconfig export-ignore
5-
.gitattributes export-ignore
6-
.gitignore export-ignore
7-
.php_cs export-ignore
8-
.travis.yml export-ignore
9-
phpcs.xml.dist export-ignore
10-
phpunit.xml.dist export-ignore
3+
/tests export-ignore
4+
.editorconfig export-ignore
5+
.gitattributes export-ignore
6+
.gitignore export-ignore
7+
.php-cs-fixer.php export-ignore
8+
phpcs.xml.dist export-ignore
9+
phpunit.xml.dist export-ignore
10+
.phpstan.neon export-ignore

.github/workflows/main.yaml

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
name: "testing"
22

33
on:
4-
push:
5-
branches: [ master ]
6-
pull_request:
7-
branches: [ master ]
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: [ master ]
88

99
jobs:
1010
qa:
@@ -13,19 +13,19 @@ jobs:
1313

1414
steps:
1515
- name: Checkout
16-
uses: actions/checkout@v2
16+
uses: actions/checkout@v4
1717

1818
- name: Validate composer.json and composer.lock
1919
run: composer validate
2020

2121
- name: Cache Composer packages
2222
id: composer-cache
23-
uses: actions/cache@v2
23+
uses: actions/cache@v4
2424
with:
25-
path: vendor
26-
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
27-
restore-keys: |
28-
${{ runner.os }}-php-
25+
path: vendor
26+
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
27+
restore-keys: |
28+
${{ runner.os }}-php-
2929
3030
- name: Install dependencies
3131
if: steps.composer-cache.outputs.cache-hit != 'true'
@@ -40,27 +40,27 @@ jobs:
4040

4141
strategy:
4242
matrix:
43-
php:
44-
- 7.2
45-
- 7.3
46-
- 7.4
47-
- 8.0
48-
- 8.1
49-
- 8.2
50-
- 8.3
51-
fail-fast: false
43+
php:
44+
- 7.2
45+
- 7.3
46+
- 7.4
47+
- 8.0
48+
- 8.1
49+
- 8.2
50+
- 8.3
51+
- 8.4
5252

5353
steps:
5454
- name: Checkout
55-
uses: actions/checkout@v2
55+
uses: actions/checkout@v4
5656

5757
- name: Install PHP
5858
uses: shivammathur/setup-php@v2
5959
with:
6060
php-version: ${{ matrix.php }}
6161

6262
- name: Cache PHP dependencies
63-
uses: actions/cache@v2
63+
uses: actions/cache@v4
6464
with:
6565
path: vendor
6666
key: ${{ runner.os }}-php-${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }}
@@ -71,6 +71,3 @@ jobs:
7171

7272
- name: Tests
7373
run: composer test
74-
75-
- name: Tests coverage
76-
run: composer coverage

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ vendor
22
composer.lock
33
coverage
44
*.cache
5+
.idea
6+
kit

.php-cs-fixer.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
return My\PhpCsFixerConfig::create()
4+
->setFinder(
5+
PhpCsFixer\Finder::create()
6+
->files()
7+
->name('*.php')
8+
->in(__DIR__.'/src')
9+
->in(__DIR__.'/tests')
10+
);

.phpstan.neon

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
parameters:
2+
level: 8
3+
paths:
4+
- src
5+
- tests
6+
treatPhpDocTypesAsCertain: false

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/)
55
and this project adheres to [Semantic Versioning](http://semver.org/).
66

7+
## [2.2.0] - 2025-03-21
8+
### Added
9+
- Support for PHP 8.4
10+
711
## [2.1.2] - 2024-01-12
812
### Fixed
913
- Updated dependencies.
@@ -72,6 +76,7 @@ First version
7276

7377
[#2]: https://github.com/middlewares/http-authentication/issues/2
7478

79+
[2.2.0]: https://github.com/middlewares/http-authentication/compare/v2.1.2...v2.2.0
7580
[2.1.2]: https://github.com/middlewares/http-authentication/compare/v2.1.1...v2.1.2
7681
[2.1.1]: https://github.com/middlewares/http-authentication/compare/v2.1.0...v2.1.1
7782
[2.1.0]: https://github.com/middlewares/http-authentication/compare/v2.0.0...v2.1.0

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2019
3+
Copyright (c) 2019-2025
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

composer.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@
1919
},
2020
"require": {
2121
"php": "^7.2 || ^8.0",
22-
"middlewares/utils": "^3.0 || ^4.0",
23-
"psr/http-server-middleware": "^1.0"
22+
"middlewares/utils": "^2 || ^3 || ^4",
23+
"psr/http-server-middleware": "^1"
2424
},
2525
"require-dev": {
2626
"phpunit/phpunit": "^8 || ^9",
27-
"laminas/laminas-diactoros": "^2.2 || ^3.0",
28-
"friendsofphp/php-cs-fixer": "^2.0",
29-
"squizlabs/php_codesniffer": "^3.0",
30-
"oscarotero/php-cs-fixer-config": "^1.0",
31-
"phpstan/phpstan": "^0.12"
27+
"laminas/laminas-diactoros": "^2 || ^3",
28+
"friendsofphp/php-cs-fixer": "^3",
29+
"squizlabs/php_codesniffer": "^3",
30+
"oscarotero/php-cs-fixer-config": "^2",
31+
"phpstan/phpstan": "^1 || ^2"
3232
},
3333
"autoload": {
3434
"psr-4": {
@@ -48,4 +48,4 @@
4848
"coverage": "phpunit --coverage-text",
4949
"coverage-html": "phpunit --coverage-html=coverage"
5050
}
51-
}
51+
}

phpcs.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="Middlewares coding standard">
3+
<description>Middlewares coding standard</description>
4+
5+
<!-- display progress -->
6+
<arg value="p"/>
7+
<arg name="report" value="full"/>
8+
<arg name="colors"/>
9+
10+
<!-- coding standard -->
11+
<rule ref="PSR2"/>
12+
13+
<!-- Paths to check -->
14+
<file>src</file>
15+
<file>tests</file>
16+
</ruleset>

phpunit.xml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
5+
backupGlobals="false"
6+
backupStaticAttributes="false"
7+
beStrictAboutOutputDuringTests="true"
8+
beStrictAboutTestsThatDoNotTestAnything="true"
9+
beStrictAboutTodoAnnotatedTests="true"
10+
colors="true"
11+
verbose="true"
12+
convertErrorsToExceptions="true"
13+
convertNoticesToExceptions="true"
14+
convertWarningsToExceptions="true"
15+
processIsolation="false"
16+
stopOnFailure="false"
17+
failOnWarning="true">
18+
<testsuites>
19+
<testsuite name="HttpAuthentication test suite">
20+
<directory>tests</directory>
21+
</testsuite>
22+
</testsuites>
23+
24+
<filter>
25+
<whitelist addUncoveredFilesFromWhitelist="true" processUncoveredFilesFromWhitelist="true">
26+
<directory>./src</directory>
27+
<exclude>
28+
<directory>./tests</directory>
29+
<directory>./vendor</directory>
30+
</exclude>
31+
</whitelist>
32+
</filter>
33+
</phpunit>

src/BasicAuthentication.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
class BasicAuthentication extends HttpAuthentication implements MiddlewareInterface
1212
{
13+
/** @var bool */
1314
private $verifyHash = false;
1415

1516
/**
@@ -31,7 +32,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
3132
return $handler->handle($request);
3233
}
3334

34-
public function verifyHash($verifyHash = true): self
35+
public function verifyHash(bool $verifyHash = true): self
3536
{
3637
$this->verifyHash = $verifyHash;
3738

@@ -50,8 +51,8 @@ private function login(ServerRequestInterface $request): ?string
5051
return null;
5152
}
5253

53-
//Check the user
54-
if (!isset($this->users[$authorization['username']])) {
54+
//Check the user and password
55+
if (!isset($this->users[$authorization['username']]) || !isset($authorization['password'])) {
5556
return null;
5657
}
5758

@@ -68,14 +69,22 @@ private function login(ServerRequestInterface $request): ?string
6869

6970
/**
7071
* Parses the authorization header for a basic authentication.
72+
*
73+
* @return ?array<string, string>
7174
*/
7275
private function parseHeader(string $header): ?array
7376
{
7477
if (strpos($header, 'Basic') !== 0) {
7578
return null;
7679
}
7780

78-
$header = base64_decode(substr($header, 6));
81+
$userAndPassword = substr($header, 6);
82+
if (!$userAndPassword) {
83+
return null;
84+
}
85+
86+
/** @var string|false $header */
87+
$header = base64_decode($userAndPassword);
7988

8089
if ($header === false) {
8190
return null;
@@ -85,7 +94,7 @@ private function parseHeader(string $header): ?array
8594

8695
return [
8796
'username' => $header[0],
88-
'password' => isset($header[1]) ? $header[1] : null,
97+
'password' => $header[1],
8998
];
9099
}
91100
}

src/DigestAuthentication.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ private function login(ServerRequestInterface $request): ?string
7878

7979
/**
8080
* Validates the authorization.
81+
*
82+
* @param array<string,string> $authorization
8183
*/
8284
private function isValid(array $authorization, string $method, string $password): bool
8385
{
@@ -96,6 +98,8 @@ private function isValid(array $authorization, string $method, string $password)
9698

9799
/**
98100
* Parses the authorization header for a basic authentication.
101+
*
102+
* @return array<string, string>|null $header
99103
*/
100104
private function parseHeader(string $header): ?array
101105
{
@@ -120,6 +124,7 @@ private function parseHeader(string $header): ?array
120124

121125
if ($matches) {
122126
foreach ($matches as $m) {
127+
// @phpstan-ignore-next-line
123128
$data[$m[1]] = $m[3] ?: $m[4];
124129
unset($needed_parts[$m[1]]);
125130
}

src/HttpAuthentication.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
abstract class HttpAuthentication
1212
{
1313
/**
14-
* @var array|ArrayAccess The available users
14+
* @var array<string, string>|ArrayAccess<string, string> The available users
1515
*/
1616
protected $users;
1717

@@ -31,11 +31,11 @@ abstract class HttpAuthentication
3131
protected $responseFactory;
3232

3333
/**
34-
* @param array|ArrayAccess $users [username => password]
34+
* @param array<string, string>|ArrayAccess<string, string> $users [username => password]
3535
*/
36-
public function __construct($users, ResponseFactoryInterface $responseFactory = null)
36+
public function __construct($users, ?ResponseFactoryInterface $responseFactory = null)
3737
{
38-
if (!is_array($users) && !($users instanceof ArrayAccess)) {
38+
if (!is_array($users) && !$users instanceof ArrayAccess) {
3939
throw new InvalidArgumentException(
4040
'The users argument must be an array or implement the ArrayAccess interface'
4141
);

0 commit comments

Comments
 (0)