Failed Conditions
Push — master ( 323120...a399af )
by Florent
05:26
created

ConsentEndpoint::getAuthorizationId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 1
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\MessageFactory;
17
use OAuth2Framework\Component\AuthorizationEndpoint\AuthorizationRequest\AuthorizationRequest;
18
use OAuth2Framework\Component\Core\Message\OAuth2Error;
19
use Psr\Http\Message\ResponseInterface;
20
use Psr\Http\Message\ServerRequestInterface;
21
use Psr\Http\Server\RequestHandlerInterface;
22
use Symfony\Component\HttpFoundation\Session\SessionInterface;
23
use Symfony\Component\Routing\RouterInterface;
24
25
abstract class ConsentEndpoint extends AbstractEndpoint
26
{
27
    private $router;
28
29
    public function __construct(MessageFactory $messageFactory, SessionInterface $session, RouterInterface $router)
30
    {
31
        parent::__construct($messageFactory, $session);
32
        $this->router = $router;
33
    }
34
35
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
36
    {
37
        try {
38
            $authorizationId = $this->getAuthorizationId($request);
39
            $authorization = $this->getAuthorization($authorizationId);
40
            if ($this->processConsent($authorization)) {
41
                $redirectTo = $this->router->generate('authorization_process_endpoint', ['authorization_id' => $authorizationId]);
42
43
                return $this->createRedirectResponse($redirectTo);
44
            }
45
46
            throw $this->buildOAuth2Error($authorization, OAuth2Error::ERROR_LOGIN_REQUIRED, 'The resource owner is not logged in.');
47
        } catch (OAuth2Error $e) {
48
            throw $e;
49
        } catch (\Exception $e) {
50
            throw new OAuth2Error(400, OAuth2Error::ERROR_INVALID_REQUEST, null);
51
        }
52
    }
53
54
    private function getAuthorizationId(ServerRequestInterface $request): string
55
    {
56
        $authorizationId = $request->getAttribute('authorization_id');
57
        if (null === $authorizationId) {
58
            throw new \InvalidArgumentException('Invalid authorization ID.');
59
        }
60
61
        return $authorizationId;
62
    }
63
64
    abstract protected function processConsent(AuthorizationRequest $authorizationRequest): bool;
65
}
66