Failed Conditions
Push — master ( 7c3864...930f9b )
by Florent
14:15
created

AuthorizationExceptionMiddleware::process()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.3888
c 0
b 0
f 0
cc 5
nc 4
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2018 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace OAuth2Framework\Component\AuthorizationEndpoint\Middleware;
15
16
use OAuth2Framework\Component\AuthorizationEndpoint\Exception\OAuth2AuthorizationException;
17
use OAuth2Framework\Component\AuthorizationEndpoint\ResponseMode\QueryResponseMode;
18
use OAuth2Framework\Component\Core\Message\OAuth2Error;
19
use Psr\Http\Message\ResponseInterface;
20
use Psr\Http\Message\ServerRequestInterface;
21
use Psr\Http\Server\MiddlewareInterface;
22
use Psr\Http\Server\RequestHandlerInterface;
23
24
final class AuthorizationExceptionMiddleware implements MiddlewareInterface
25
{
26
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
27
    {
28
        try {
29
            return $handler->handle($request);
30
        } catch (OAuth2AuthorizationException $e) {
31
            $redirectUri = $e->getAuthorization()->getRedirectUri();
32
            $responseMode = $e->getAuthorization()->getResponseMode();
33
            switch (true) {
34
                case null !== $redirectUri && null !== $responseMode:
35
                    throw new OAuth2Error(303, $e->getMessage(), $e->getErrorDescription(), ['response_mode' => $responseMode, 'redirect_uri' => $redirectUri], $e);
36
                case null !== $redirectUri:
37
                    throw new OAuth2Error(303, $e->getMessage(), $e->getErrorDescription(), ['response_mode' => new QueryResponseMode(), 'redirect_uri' => $redirectUri], $e);
38
                default:
39
                    throw new OAuth2Error(400, $e->getMessage(), $e->getErrorDescription(), [], $e);
40
            }
41
        }
42
    }
43
}
44