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

SelectAccountEndpoint::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
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 SelectAccountEndpoint 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->processAccountSelection($authorization)) {
41
                switch (true) {
42
                    case $authorization->hasPrompt('consent'):
43
                    default:
44
                        $routeName = 'authorization_consent_endpoint';
45
                        break;
46
                }
47
                $redirectTo = $this->router->generate($routeName, ['authorization_id' => $authorizationId]);
48
49
                return $this->createRedirectResponse($redirectTo);
50
            }
51
52
            throw $this->buildOAuth2Error($authorization, OAuth2Error::ERROR_ACCOUNT_SELECTION_REQUIRED, 'The resource owner account selection failed.');
53
        } catch (OAuth2Error $e) {
54
            throw $e;
55
        } catch (\Exception $e) {
56
            throw new OAuth2Error(400, OAuth2Error::ERROR_INVALID_REQUEST, null);
57
        }
58
    }
59
60
    private function getAuthorizationId(ServerRequestInterface $request): string
61
    {
62
        $authorizationId = $request->getAttribute('authorization_id');
63
        if (null === $authorizationId) {
64
            throw new \InvalidArgumentException('Invalid authorization ID.');
65
        }
66
67
        return $authorizationId;
68
    }
69
70
    abstract protected function processAccountSelection(AuthorizationRequest $authorizationRequest): bool;
71
}
72