1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* Copyright (c) Phauthentic (https://github.com/Phauthentic) |
5
|
|
|
* |
6
|
|
|
* Licensed under The MIT License |
7
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
8
|
|
|
* Redistributions of files must retain the above copyright notice. |
9
|
|
|
* |
10
|
|
|
* @copyright Copyright (c) Phauthentic (https://github.com/Phauthentic) |
11
|
|
|
* @link https://github.com/Phauthentic |
12
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License |
13
|
|
|
*/ |
14
|
|
|
namespace Phauthentic\Authentication\Middleware; |
15
|
|
|
|
16
|
|
|
use Phauthentic\Authentication\AuthenticationServiceProviderInterface; |
17
|
|
|
use Phauthentic\Authentication\Authenticator\Exception\AuthenticationExceptionInterface; |
18
|
|
|
use Phauthentic\Authentication\Authenticator\Exception\UnauthorizedException; |
19
|
|
|
use Psr\Http\Message\ResponseFactoryInterface; |
20
|
|
|
use Psr\Http\Message\ResponseInterface; |
21
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
22
|
|
|
use Psr\Http\Message\StreamFactoryInterface; |
23
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
24
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
25
|
|
|
use RuntimeException; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Handles the case when the authentication middleware has thrown an exception |
29
|
|
|
*/ |
30
|
|
|
class AuthenticationFailureHandlerMiddleware implements MiddlewareInterface |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* Response factory |
34
|
|
|
* |
35
|
|
|
* @var \Psr\Http\Message\ResponseFactoryInterface |
36
|
|
|
*/ |
37
|
|
|
protected $responseFactory; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* PSR Stream Interface |
41
|
|
|
* |
42
|
|
|
* @var \Psr\Http\Message\StreamFactoryInterface |
43
|
|
|
*/ |
44
|
|
|
protected $streamFactory; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Constructor. |
48
|
|
|
* |
49
|
|
|
* @param \Psr\Http\Message\ResponseFactoryInterface $responseFactory Factory. |
50
|
|
|
* @param \Psr\Http\Message\StreamFactoryInterface $streamFactory Factory. |
51
|
|
|
*/ |
52
|
|
|
public function __construct( |
53
|
|
|
ResponseFactoryInterface $responseFactory, |
54
|
|
|
StreamFactoryInterface $streamFactory |
55
|
|
|
) { |
56
|
|
|
$this->responseFactory = $responseFactory; |
57
|
|
|
$this->streamFactory = $streamFactory; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritDoc} |
62
|
|
|
*/ |
63
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
64
|
|
|
{ |
65
|
|
|
try { |
66
|
|
|
return $handler->handle($request); |
67
|
|
|
} catch (UnauthorizedException $e) { |
68
|
|
|
return $this->createUnauthorizedResponse($e); |
69
|
|
|
} catch (AuthenticationException $e) { |
|
|
|
|
70
|
|
|
return $this->createErrorResponse($e); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Creates an unauthorized response. |
76
|
|
|
* |
77
|
|
|
* @param UnauthorizedException $e Exception. |
78
|
|
|
* @return ResponseInterface |
79
|
|
|
*/ |
80
|
|
|
protected function createUnauthorizedResponse(UnauthorizedException $e): ResponseInterface |
81
|
|
|
{ |
82
|
|
|
$body = $this->streamFactory->createStream(); |
83
|
|
|
$body->write($e->getBody()); |
84
|
|
|
|
85
|
|
|
$response = $this |
86
|
|
|
->responseFactory |
87
|
|
|
->createResponse($e->getCode()) |
88
|
|
|
->withBody($body); |
89
|
|
|
|
90
|
|
|
foreach ($e->getHeaders() as $header => $value) { |
91
|
|
|
$response = $response->withHeader($header, $value); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $response; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Creates an error response. |
99
|
|
|
* |
100
|
|
|
* @param UnauthorizedException $e Exception. |
101
|
|
|
* @return ResponseInterface |
102
|
|
|
*/ |
103
|
|
|
protected function createErrorResponse(AuthenticationExceptionInterface $e, int $responseCode = 500): ResponseInterface |
104
|
|
|
{ |
105
|
|
|
$body = $this->streamFactory->createStream(); |
106
|
|
|
$body->write($e->getMessage()); |
|
|
|
|
107
|
|
|
|
108
|
|
|
$response = $this |
109
|
|
|
->responseFactory |
110
|
|
|
->createResponse($responseCode) |
111
|
|
|
->withBody($body); |
112
|
|
|
|
113
|
|
|
return $response; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
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