Failed Conditions
Push — ng ( 935f22...b3431d )
by Florent
04:01
created

ScopeParameterChecker   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 53
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B process() 0 25 5
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\Server\Scope;
15
16
use OAuth2Framework\Component\Server\AuthorizationEndpoint\Authorization;
17
use OAuth2Framework\Component\Server\AuthorizationEndpoint\ParameterChecker\ParameterChecker;
18
use OAuth2Framework\Component\Server\Scope\Policy\ScopePolicyManager;
19
use OAuth2Framework\Component\Server\Core\Response\OAuth2Exception;
20
21
final class ScopeParameterChecker implements ParameterChecker
22
{
23
    /**
24
     * @var ScopeRepository
25
     */
26
    private $scopeRepository;
27
28
    /**
29
     * @var ScopePolicyManager
30
     */
31
    private $scopePolicyManager;
32
33
    /**
34
     * ScopeParameterChecker constructor.
35
     *
36
     * @param ScopeRepository    $scopeRepository
37
     * @param ScopePolicyManager $scopePolicyManager
38
     */
39
    public function __construct(ScopeRepository $scopeRepository, ScopePolicyManager $scopePolicyManager)
40
    {
41
        $this->scopeRepository = $scopeRepository;
42
        $this->scopePolicyManager = $scopePolicyManager;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function process(Authorization $authorization, callable $next): Authorization
49
    {
50
        try {
51
            if ($authorization->hasQueryParam('scope')) {
52
                $requestedScope = $authorization->getQueryParam('scope');
53
                if (1 !== 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
            $requestedScope = $this->scopePolicyManager->apply($requestedScope, $authorization->getClient());
60
            $scopes = explode(' ', $requestedScope);
61
62
            $availableScope = $this->scopeRepository->getAvailableScopesForClient($authorization->getClient());
0 ignored issues
show
Bug introduced by
The method getAvailableScopesForClient() does not seem to exist on object<OAuth2Framework\C...\Scope\ScopeRepository>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
63
            if (!$this->scopeRepository->areRequestedScopesAvailable($scopes, $availableScope)) {
0 ignored issues
show
Bug introduced by
The method areRequestedScopesAvailable() does not seem to exist on object<OAuth2Framework\C...\Scope\ScopeRepository>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
64
                throw new \InvalidArgumentException(sprintf('An unsupported scope was requested. Available scopes for the client are %s.', implode(', ', $availableScope)));
65
            }
66
            $authorization = $authorization->withScopes($scope);
0 ignored issues
show
Bug introduced by
The variable $scope does not exist. Did you mean $scopes?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
Bug introduced by
The method withScopes() does not seem to exist on object<OAuth2Framework\C...Endpoint\Authorization>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
67
68
            return $next($authorization);
69
        } catch (\InvalidArgumentException $e) {
70
            throw new OAuth2Exception(400, OAuth2Exception::ERROR_INVALID_SCOPE, $e->getMessage(), $authorization, $e);
71
        }
72
    }
73
}
74