Failed Conditions
Push — ng ( f9780e...ccd5de )
by Florent
11:07
created

TokenEndpointScopeExtension::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
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\Core\AccessToken\AccessToken;
17
use OAuth2Framework\Component\Server\Core\Client\Client;
18
use OAuth2Framework\Component\Server\Core\ResourceOwner\ResourceOwner;
19
use OAuth2Framework\Component\Server\TokenEndpoint\Extension\TokenEndpointExtension;
20
use OAuth2Framework\Component\Server\TokenEndpoint\GrantTypeData;
21
use OAuth2Framework\Component\Server\TokenEndpoint\GrantType;
22
use OAuth2Framework\Component\Server\Scope\Policy\ScopePolicyManager;
23
use OAuth2Framework\Component\Server\Core\Response\OAuth2Exception;
24
use Psr\Http\Message\ServerRequestInterface;
25
26
final class TokenEndpointScopeExtension implements TokenEndpointExtension
27
{
28
    /**
29
     * @var ScopeRepository
30
     */
31
    private $scopeRepository;
32
33
    /**
34
     * @var ScopePolicyManager|null
35
     */
36
    private $scopePolicyManager;
37
38
    /**
39
     * ScopeProcessor constructor.
40
     *
41
     * @param ScopeRepository         $scopeRepository
42
     * @param ScopePolicyManager|null $scopePolicyManager
43
     */
44
    public function __construct(ScopeRepository $scopeRepository, ? ScopePolicyManager $scopePolicyManager)
45
    {
46
        $this->scopeRepository = $scopeRepository;
47
        $this->scopePolicyManager = $scopePolicyManager;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function beforeAccessTokenIssuance(ServerRequestInterface $request, GrantTypeData $grantTypeData, GrantType $grantType, callable $next): GrantTypeData
54
    {
55
        /** @var GrantTypeData $grantTypeData */
56
        $grantTypeData = $next($request, $grantTypeData, $grantType);
57
        $params = $request->getParsedBody() ?? [];
58
        if (!array_key_exists('scope', $params)) {
59
            $scope = $grantTypeData->getAvailableScopes() ?? [];
0 ignored issues
show
Bug introduced by
The method getAvailableScopes() does not seem to exist on object<OAuth2Framework\C...Endpoint\GrantTypeData>.

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...
60
        } else {
61
            $scope = explode(' ', $params['scope']);
62
        }
63
64
        //Modify the scope according to the scope policy
65
        try {
66
            if (null !== $this->scopePolicyManager) {
67
                $scope = $this->scopePolicyManager->apply($scope, $grantTypeData->getClient());
0 ignored issues
show
Bug introduced by
It seems like $grantTypeData->getClient() can be null; however, apply() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
68
            }
69
        } catch (\InvalidArgumentException $e) {
70
            throw new OAuth2Exception(400, OAuth2Exception::ERROR_INVALID_SCOPE, $e->getMessage(), [], $e);
71
        }
72
73
        $availableScope = $grantTypeData->getAvailableScopes() ? $grantTypeData->getAvailableScopes() : $this->scopeRepository->getAvailableScopesForClient($grantTypeData->getClient());
0 ignored issues
show
Bug introduced by
The method getAvailableScopes() does not seem to exist on object<OAuth2Framework\C...Endpoint\GrantTypeData>.

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...
Bug introduced by
It seems like $grantTypeData->getClient() can be null; however, getAvailableScopesForClient() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
74
75
        //Check if requested scope are within the available scope
76
        if (!$this->scopeRepository->areRequestedScopesAvailable($scope, $availableScope)) {
77
            throw new OAuth2Exception(400, OAuth2Exception::ERROR_INVALID_SCOPE, sprintf('An unsupported scope was requested. Available scopes are %s.', implode(', ', $availableScope)));
78
        }
79
80
        $grantTypeData = $grantTypeData->withScopes($scope);
0 ignored issues
show
Bug introduced by
The method withScopes() does not seem to exist on object<OAuth2Framework\C...Endpoint\GrantTypeData>.

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...
81
82
        return $grantTypeData;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function afterAccessTokenIssuance(Client $client, ResourceOwner $resourceOwner, AccessToken $accessToken, callable $next): array
89
    {
90
        return $next($client, $resourceOwner, $accessToken);
91
    }
92
}
93