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\Authenticator\Exception\UnauthorizedException; |
17
|
|
|
use Psr\Http\Message\ResponseFactoryInterface; |
18
|
|
|
use Psr\Http\Message\ResponseInterface; |
19
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
20
|
|
|
use Psr\Http\Message\StreamFactoryInterface; |
21
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
22
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Handles the case when the authentication middleware has thrown an exception |
26
|
|
|
*/ |
27
|
|
|
class AuthenticationErrorHandlerMiddleware implements MiddlewareInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Response factory |
31
|
|
|
* |
32
|
|
|
* @var \Psr\Http\Message\ResponseFactoryInterface |
33
|
|
|
*/ |
34
|
|
|
protected $responseFactory; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* PSR Stream Interface |
38
|
|
|
* |
39
|
|
|
* @var \Psr\Http\Message\StreamFactoryInterface |
40
|
|
|
*/ |
41
|
|
|
protected $streamFactory; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Constructor. |
45
|
|
|
* |
46
|
|
|
* @param \Psr\Http\Message\ResponseFactoryInterface $responseFactory Factory. |
47
|
|
|
* @param \Psr\Http\Message\StreamFactoryInterface $streamFactory Factory. |
48
|
|
|
*/ |
49
|
|
|
public function __construct( |
50
|
|
|
ResponseFactoryInterface $responseFactory, |
51
|
6 |
|
StreamFactoryInterface $streamFactory |
52
|
|
|
) { |
53
|
|
|
$this->responseFactory = $responseFactory; |
54
|
|
|
$this->streamFactory = $streamFactory; |
55
|
6 |
|
} |
56
|
6 |
|
|
57
|
6 |
|
/** |
58
|
|
|
* {@inheritDoc} |
59
|
|
|
*/ |
60
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
61
|
|
|
{ |
62
|
6 |
|
try { |
63
|
|
|
return $handler->handle($request); |
64
|
|
|
} catch (UnauthorizedException $e) { |
65
|
6 |
|
return $this->createUnauthorizedResponse($e); |
66
|
6 |
|
} |
67
|
3 |
|
} |
68
|
3 |
|
|
69
|
3 |
|
/** |
70
|
|
|
* Creates an unauthorized response. |
71
|
|
|
* |
72
|
|
|
* @param \Phauthentic\Authentication\Authenticator\Exception\UnauthorizedException $e Exception. |
73
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
74
|
|
|
*/ |
75
|
|
|
protected function createUnauthorizedResponse(UnauthorizedException $e): ResponseInterface |
76
|
|
|
{ |
77
|
|
|
$body = $this->streamFactory->createStream(); |
78
|
|
|
$body->write($e->getBody()); |
79
|
3 |
|
|
80
|
|
|
$response = $this |
81
|
3 |
|
->responseFactory |
82
|
3 |
|
->createResponse($e->getCode()) |
83
|
|
|
->withBody($body); |
84
|
|
|
|
85
|
3 |
|
foreach ($e->getHeaders() as $header => $value) { |
86
|
3 |
|
$response = $response->withHeader($header, $value); |
87
|
3 |
|
} |
88
|
|
|
|
89
|
3 |
|
return $response; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|