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

SelectAccountEndpoint   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 42
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B process() 0 29 6
getRouteFor() 0 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\ResponseFactory;
17
use OAuth2Framework\Component\AuthorizationEndpoint\Exception\OAuth2AuthorizationException;
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
24
abstract class SelectAccountEndpoint extends AbstractEndpoint
25
{
26
    private $selectAccountHandler;
27
28
    public function __construct(ResponseFactory $responseFactory, SessionInterface $session, SelectAccountHandler $selectAccountHandler)
29
    {
30
        parent::__construct($responseFactory, $session);
31
        $this->selectAccountHandler = $selectAccountHandler;
32
    }
33
34
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
35
    {
36
        $authorizationId = $this->getAuthorizationId($request);
37
        $authorization = $this->getAuthorization($authorizationId);
38
        try {
39
            $this->selectAccountHandler->prepare($request, $authorizationId, $authorization);
40
            if ($this->selectAccountHandler->hasBeenProcessed($request, $authorizationId, $authorization)) {
41
                if (!$this->selectAccountHandler->isValid($request, $authorizationId, $authorization)) {
42
                    throw new OAuth2AuthorizationException(OAuth2Error::ERROR_ACCOUNT_SELECTION_REQUIRED, 'The resource owner account selection failed.', $authorization);
43
                }
44
45
                switch (true) {
46
                    case $authorization->hasPrompt('consent'):
47
                    default:
48
                        $routeName = 'authorization_consent_endpoint';
49
                        break;
50
                }
51
                $redirectTo = $this->getRouteFor($routeName, $authorizationId);
52
53
                return $this->createRedirectResponse($redirectTo);
54
            }
55
56
            return $this->selectAccountHandler->process($request, $authorizationId, $authorization);
57
        } catch (OAuth2Error $e) {
58
            throw new OAuth2AuthorizationException($e->getMessage(), $e->getErrorDescription(), $authorization);
59
        } catch (\Exception $e) {
60
            throw new OAuth2AuthorizationException(OAuth2Error::ERROR_INVALID_REQUEST, $e->getMessage(), $authorization);
61
        }
62
    }
63
64
    abstract protected function getRouteFor(string $action, string $authorizationId): string;
65
}
66