Failed Conditions
Push — master ( d35532...b3f3b3 )
by Florent
04:23
created

AuthorizationExceptionMiddleware::process()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 20
nc 3
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\Core\Message\OAuth2Message;
18
use Psr\Http\Server\RequestHandlerInterface;
19
use Psr\Http\Server\MiddlewareInterface;
20
use Psr\Http\Message\ResponseInterface;
21
use Psr\Http\Message\ServerRequestInterface;
22
23
final class AuthorizationExceptionMiddleware implements MiddlewareInterface
24
{
25
    /**
26
     * {@inheritdoc}
27
     * @throws OAuth2Message
28
     */
29
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
30
    {
31
        try {
32
            return $handler->handle($request);
33
        } catch (OAuth2AuthorizationException $e) {
34
            $redirectUri = $e->getAuthorization()->getRedirectUri();
35
            $responseMode = $e->getAuthorization()->getResponseMode();
36
            if (null !== $redirectUri && null !== $responseMode) {
37
                throw new OAuth2Message(
38
                    302,
39
                    $e->getMessage(),
40
                    $e->getErrorDescription(),
41
                    [
42
                        'response_mode' => $responseMode,
43
                        'redirect_uri' => $redirectUri,
44
                    ],
45
                    $e
46
                );
47
            } else {
48
                throw new OAuth2Message(
49
                    400,
50
                    $e->getMessage(),
51
                    $e->getErrorDescription(),
52
                    [],
53
                    $e
54
                );
55
            }
56
        }
57
    }
58
}
59