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