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\AuthenticationException; |
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
|
|
|
* @throws RuntimeException When request attribute exists. |
64
|
|
|
*/ |
65
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
66
|
|
|
{ |
67
|
|
|
try { |
68
|
|
|
return $handler->handle($request); |
69
|
|
|
} catch (UnauthorizedException $e) { |
70
|
|
|
return $this->createErrorResponse($e, $e->getCode()); |
71
|
|
|
} catch (AuthenticationException $e) { |
72
|
|
|
return $this->createErrorResponse($e); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Creates an unauthorized response. |
78
|
|
|
* |
79
|
|
|
* @param UnauthorizedException $e Exception. |
80
|
|
|
* @return ResponseInterface |
81
|
|
|
*/ |
82
|
|
|
protected function createErrorResponse(AuthenticationException $e, int $responseCode = 500): ResponseInterface |
83
|
|
|
{ |
84
|
|
|
$body = $this->streamFactory->createStream(); |
85
|
|
|
$body->write($e->getBody()); |
|
|
|
|
86
|
|
|
|
87
|
|
|
$response = $this |
88
|
|
|
->responseFactory |
89
|
|
|
->createResponse($responseCode) |
90
|
|
|
->withBody($body); |
91
|
|
|
|
92
|
|
|
foreach ($e->getHeaders() as $header => $value) { |
|
|
|
|
93
|
|
|
$response = $response->withHeader($header, $value); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $response; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|