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

ScopeParameterChecker   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A check() 0 16 3
A getRequestedScope() 0 13 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\Scope;
15
16
use OAuth2Framework\Component\AuthorizationEndpoint\AuthorizationRequest\AuthorizationRequest;
17
use OAuth2Framework\Component\AuthorizationEndpoint\ParameterChecker\ParameterChecker;
18
use OAuth2Framework\Component\Scope\Policy\ScopePolicyManager;
19
20
class ScopeParameterChecker implements ParameterChecker
21
{
22
    private $scopeRepository;
23
24
    private $scopePolicyManager;
25
26
    public function __construct(ScopeRepository $scopeRepository, ScopePolicyManager $scopePolicyManager)
27
    {
28
        $this->scopeRepository = $scopeRepository;
29
        $this->scopePolicyManager = $scopePolicyManager;
30
    }
31
32
    public function check(AuthorizationRequest $authorization): void
33
    {
34
        $requestedScope = $this->getRequestedScope($authorization);
35
        $requestedScope = $this->scopePolicyManager->apply($requestedScope, $authorization->getClient());
36
        if (empty($requestedScope)) {
37
            return;
38
        }
39
        $scopes = \explode(' ', $requestedScope);
40
41
        $availableScopes = $this->scopeRepository->all();
42
        if (0 !== \count(\array_diff($scopes, $availableScopes))) {
43
            throw new \InvalidArgumentException(\Safe\sprintf('An unsupported scope was requested. Available scopes are %s.', \implode(', ', $availableScopes)));
44
        }
45
        $authorization->getMetadata()->set('scope', \implode(' ', $scopes));
46
        $authorization->setResponseParameter('scope', \implode(' ', $scopes)); //TODO: should be done after consent depending on approved scope
47
    }
48
49
    private function getRequestedScope(AuthorizationRequest $authorization): string
50
    {
51
        if ($authorization->hasQueryParam('scope')) {
52
            $requestedScope = $authorization->getQueryParam('scope');
53
            if (1 !== \Safe\preg_match('/^[\x20\x23-\x5B\x5D-\x7E]+$/', $requestedScope)) {
54
                throw new \InvalidArgumentException('Invalid characters found in the "scope" parameter.');
55
            }
56
        } else {
57
            $requestedScope = '';
58
        }
59
60
        return $requestedScope;
61
    }
62
}
63