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

ProcessEndpoint::process()   A

Complexity

Conditions 4
Paths 15

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
cc 4
nc 15
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;
15
16
use Http\Message\ResponseFactory;
17
use OAuth2Framework\Component\AuthorizationEndpoint\AuthorizationRequest\AuthorizationRequest;
18
use OAuth2Framework\Component\AuthorizationEndpoint\Exception\OAuth2AuthorizationException;
19
use OAuth2Framework\Component\AuthorizationEndpoint\Extension\ExtensionManager;
20
use OAuth2Framework\Component\Core\Message\OAuth2Error;
21
use Psr\Http\Message\ResponseInterface;
22
use Psr\Http\Message\ServerRequestInterface;
23
use Psr\Http\Server\RequestHandlerInterface;
24
use Symfony\Component\HttpFoundation\Session\SessionInterface;
25
26
abstract class ProcessEndpoint extends AbstractEndpoint
27
{
28
    private $extensionManager;
29
30
    public function __construct(ResponseFactory $responseFactory, SessionInterface $session, ExtensionManager $extensionManager)
31
    {
32
        parent::__construct($responseFactory, $session);
33
        $this->extensionManager = $extensionManager;
34
    }
35
36
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
37
    {
38
        $authorizationId = $this->getAuthorizationId($request);
39
        $authorization = $this->getAuthorization($authorizationId);
40
        try {
41
            $this->extensionManager->process($request, $authorization);
42
            if (!$authorization->isAuthorized()) {
43
                throw new OAuth2AuthorizationException(OAuth2Error::ERROR_ACCESS_DENIED, 'The resource owner denied access to your client.', $authorization);
44
            }
45
            $responseType = $authorization->getResponseType();
46
            $responseType->preProcess($authorization);
47
            $responseType->process($authorization);
48
49
            return $this->buildResponse($authorization);
50
        } catch (OAuth2Error $e) {
51
            throw new OAuth2AuthorizationException($e->getMessage(), $e->getErrorDescription(), $authorization);
52
        } catch (\Exception $e) {
53
            throw new OAuth2AuthorizationException(OAuth2Error::ERROR_INVALID_REQUEST, $e->getMessage(), $authorization);
54
        }
55
    }
56
57
    private function buildResponse(AuthorizationRequest $authorization): ResponseInterface
58
    {
59
        $response = $authorization->getResponseMode()->buildResponse(
60
            $this->responseFactory->createResponse(),
61
            $authorization->getRedirectUri(),
62
            $authorization->getResponseParameters()
63
        );
64
        foreach ($authorization->getResponseHeaders() as $k => $v) {
65
            $response = $response->withHeader($k, $v);
66
        }
67
68
        return $response;
69
    }
70
}
71