1 | <?php |
||||
2 | |||||
3 | declare(strict_types=1); |
||||
4 | |||||
5 | namespace Kafkiansky\SymfonyMiddleware; |
||||
6 | |||||
7 | use Nyholm\Psr7\Response; |
||||
8 | use Psr\Http\Message\ResponseInterface; |
||||
9 | use Psr\Http\Message\ServerRequestInterface; |
||||
10 | use Psr\Http\Server\MiddlewareInterface; |
||||
11 | use Psr\Http\Server\RequestHandlerInterface; |
||||
12 | |||||
13 | final class AuthenticateBasic implements MiddlewareInterface |
||||
14 | { |
||||
15 | public function __construct( |
||||
16 | private string $user, |
||||
17 | private string $password, |
||||
18 | private string $realm, |
||||
19 | private array|null $excludedPaths = null, |
||||
0 ignored issues
–
show
|
|||||
20 | private array|null $excludedPatterns = null, |
||||
21 | ) { |
||||
22 | } |
||||
23 | |||||
24 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
||||
25 | { |
||||
26 | $path = $request->getUri()->getPath(); |
||||
27 | |||||
28 | if ($this->excludedPaths !== null && in_array($path, $this->excludedPaths)) { |
||||
0 ignored issues
–
show
It seems like
$this->excludedPaths can also be of type Kafkiansky\SymfonyMiddleware\null ; however, parameter $haystack of in_array() does only seem to accept array , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
29 | return $handler->handle($request); |
||||
30 | } |
||||
31 | |||||
32 | if ($this->excludedPatterns !== null && $this->isMatchedByPattern($path)) { |
||||
33 | return $handler->handle($request); |
||||
34 | } |
||||
35 | |||||
36 | if ($this->isCredentialsMatched($request)) { |
||||
37 | return $handler->handle($request); |
||||
38 | } |
||||
39 | |||||
40 | return new Response(401, [ |
||||
41 | 'WWW-Authenticate' => 'Basic realm="'.$this->realm.'"' |
||||
42 | ]); |
||||
43 | } |
||||
44 | |||||
45 | private function isCredentialsMatched(ServerRequestInterface $request): bool |
||||
46 | { |
||||
47 | $user = $request->getServerParams()['PHP_AUTH_USER'] ?? null; |
||||
48 | $passwd = $request->getServerParams()['PHP_AUTH_PW'] ?? null; |
||||
49 | |||||
50 | return $user === $this->user && $passwd === $this->password; |
||||
51 | } |
||||
52 | |||||
53 | private function isMatchedByPattern(string $path): bool |
||||
54 | { |
||||
55 | foreach ($this->excludedPatterns as $excludedPattern) { |
||||
56 | if (preg_match($excludedPattern, $path)) { |
||||
57 | return true; |
||||
58 | } |
||||
59 | } |
||||
60 | |||||
61 | return false; |
||||
62 | } |
||||
63 | } |
||||
64 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths