NonePrompt::handle()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 4
eloc 8
c 1
b 1
f 0
nc 5
nop 3
dl 0
loc 16
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2019 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\Hook;
15
16
use OAuth2Framework\Component\AuthorizationEndpoint\AuthorizationRequest\AuthorizationRequest;
17
use OAuth2Framework\Component\AuthorizationEndpoint\Consent\ConsentRepository;
18
use OAuth2Framework\Component\AuthorizationEndpoint\Exception\OAuth2AuthorizationException;
19
use OAuth2Framework\Component\Core\Message\OAuth2Error;
20
use Psr\Http\Message\ResponseInterface;
21
use Psr\Http\Message\ServerRequestInterface;
22
23
final class NonePrompt implements AuthorizationEndpointHook
24
{
25
    /**
26
     * @var null|ConsentRepository
27
     */
28
    private $consentRepository;
29
30
    public function __construct(?ConsentRepository $consentRepository)
31
    {
32
        $this->consentRepository = $consentRepository;
33
    }
34
35
    public function handle(ServerRequestInterface $request, string $authorizationRequestId, AuthorizationRequest $authorizationRequest): ?ResponseInterface
36
    {
37
        if (!$authorizationRequest->hasPrompt('none')) {
38
            return null;
39
        }
40
41
        $isConsentNeeded = null === $this->consentRepository ? true : !$this->consentRepository->hasConsentBeenGiven($authorizationRequest);
42
        if ($authorizationRequest->hasUserAccount()) {
43
            $this->handleWithAuthenticatedUser($authorizationRequest, $isConsentNeeded);
44
45
            return null;
46
        }
47
48
        $this->handleWithUnauthenticatedUser($authorizationRequest, $isConsentNeeded);
49
50
        return null;
51
    }
52
53
    private function handleWithAuthenticatedUser(AuthorizationRequest $authorizationRequest, bool $isConsentNeeded): void
54
    {
55
        if ($isConsentNeeded) {
56
            throw new OAuth2AuthorizationException(OAuth2Error::ERROR_INTERACTION_REQUIRED, 'The resource owner consent is required.', $authorizationRequest);
57
        }
58
        $authorizationRequest->allow();
59
    }
60
61
    private function handleWithUnauthenticatedUser(AuthorizationRequest $authorizationRequest, bool $isConsentNeeded): void
62
    {
63
        if ($isConsentNeeded) {
64
            throw new OAuth2AuthorizationException(OAuth2Error::ERROR_LOGIN_REQUIRED, 'The resource owner is not logged in.', $authorizationRequest);
65
        }
66
        $authorizationRequest->allow();
67
    }
68
}
69