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
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Handles the case when the authentication middleware has thrown an exception |
28
|
|
|
*/ |
29
|
|
|
class AuthenticationErrorHandlerMiddleware implements MiddlewareInterface |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Response factory |
33
|
|
|
* |
34
|
|
|
* @var \Psr\Http\Message\ResponseFactoryInterface |
35
|
|
|
*/ |
36
|
|
|
protected $responseFactory; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* PSR Stream Interface |
40
|
|
|
* |
41
|
|
|
* @var \Psr\Http\Message\StreamFactoryInterface |
42
|
|
|
*/ |
43
|
|
|
protected $streamFactory; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Constructor. |
47
|
|
|
* |
48
|
|
|
* @param \Psr\Http\Message\ResponseFactoryInterface $responseFactory Factory. |
49
|
|
|
* @param \Psr\Http\Message\StreamFactoryInterface $streamFactory Factory. |
50
|
|
|
*/ |
51
|
6 |
|
public function __construct( |
52
|
|
|
ResponseFactoryInterface $responseFactory, |
53
|
|
|
StreamFactoryInterface $streamFactory |
54
|
|
|
) { |
55
|
6 |
|
$this->responseFactory = $responseFactory; |
56
|
6 |
|
$this->streamFactory = $streamFactory; |
57
|
6 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritDoc} |
61
|
|
|
*/ |
62
|
6 |
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
63
|
|
|
{ |
64
|
|
|
try { |
65
|
6 |
|
return $handler->handle($request); |
66
|
6 |
|
} catch (UnauthorizedException $e) { |
67
|
3 |
|
return $this->createUnauthorizedResponse($e); |
68
|
3 |
|
} catch (AuthenticationExceptionInterface $e) { |
69
|
3 |
|
return $this->createErrorResponse($e); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Creates an unauthorized response. |
75
|
|
|
* |
76
|
|
|
* @param UnauthorizedException $e Exception. |
77
|
|
|
* @return ResponseInterface |
78
|
|
|
*/ |
79
|
3 |
|
protected function createUnauthorizedResponse(UnauthorizedException $e): ResponseInterface |
80
|
|
|
{ |
81
|
3 |
|
$body = $this->streamFactory->createStream(); |
82
|
3 |
|
$body->write($e->getBody()); |
83
|
|
|
|
84
|
|
|
$response = $this |
85
|
3 |
|
->responseFactory |
86
|
3 |
|
->createResponse($e->getCode()) |
87
|
3 |
|
->withBody($body); |
88
|
|
|
|
89
|
3 |
|
foreach ($e->getHeaders() as $header => $value) { |
90
|
|
|
$response = $response->withHeader($header, $value); |
91
|
|
|
} |
92
|
|
|
|
93
|
3 |
|
return $response; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Creates an error response. |
98
|
|
|
* |
99
|
|
|
* @param UnauthorizedException $e Exception. |
100
|
|
|
* @return ResponseInterface |
101
|
|
|
*/ |
102
|
3 |
|
protected function createErrorResponse(AuthenticationExceptionInterface $e, int $responseCode = 500): ResponseInterface |
103
|
|
|
{ |
104
|
3 |
|
$body = $this->streamFactory->createStream(); |
105
|
3 |
|
$body->write($e->getMessage()); |
106
|
|
|
|
107
|
|
|
return $this |
108
|
3 |
|
->responseFactory |
109
|
3 |
|
->createResponse($responseCode) |
110
|
3 |
|
->withBody($body); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|