Completed
Push — master ( 289dee...aec877 )
by Alexandre
03:35
created

CodeResponseType::verifyRequest()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 15
c 0
b 0
f 0
ccs 0
cts 2
cp 0
rs 8.8571
cc 5
eloc 8
nc 4
nop 1
crap 30
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: GCC-MED
5
 * Date: 19/01/2018
6
 * Time: 15:36
7
 */
8
9
namespace OAuth2\OpenID\ResponseTypes;
10
11
12
use OAuth2\Config;
13
use OAuth2\Exceptions\OAuthException;
14
use OAuth2\Repositories\ConfigurationRepository;
15
use OAuth2\Roles\Clients\RegisteredClient;
16
use OAuth2\Roles\ResourceOwnerInterface;
17
use OAuth2\OpenID\Storages\AuthorizationCodeStorageInterface;
18
use Psr\Http\Message\ServerRequestInterface;
19
20
class CodeResponseType extends \OAuth2\ResponseTypes\CodeResponseType implements ResponseTypeInterface
21
{
22
23
    /**
24
     * @var ConfigurationRepository
25
     */
26
    private $configurationRepository;
27
28
    public function __construct(ConfigurationRepository $configurationRepository, AuthorizationCodeStorageInterface $authorizationCodeStorage)
29
    {
30
        parent::__construct($authorizationCodeStorage);
31
        $this->configurationRepository = $configurationRepository;
32
    }
33
34
    /**
35
     * @param ServerRequestInterface $request
36
     * @param ResourceOwnerInterface $resourceOwner
37
     * @param RegisteredClient $client
38
     * @param array|null $scope
39
     * @param array|null $extendedResponseTypes
40
     * @return array
41
     * @throws OAuthException
42
     */
43
    public function handle(ServerRequestInterface $request, ResourceOwnerInterface $resourceOwner,
44
                           RegisteredClient $client, ?array $scope = null, ?array $extendedResponseTypes = null): array
45
    {
46
        $data = $request->getMethod() === 'GET' ? $request->getQueryParams() : $request->getParsedBody();
47
48
        if (is_array($client->getSupportedGrantTypes()) && !in_array('authorization_code', $client->getSupportedGrantTypes())) {
49
            throw new OAuthException('unauthorized_client',
50
                'Client is not authorized to request an authorization code with this method',
51
                'https://tools.ietf.org/html/rfc6749#section-5.2');
52
        }
53
54
        $redirectUri = $request->getQueryParams()['redirect_uri'] ?? $request->getParsedBody()['redirect_uri'] ?? null;
0 ignored issues
show
Unused Code introduced by
The assignment to $redirectUri is dead and can be removed.
Loading history...
55
56
        $requestedScopes = $request->getQueryParams()['scope'] ?? $request->getParsedBody()['scope'] ?? null;
57
        $requestedScopes = $requestedScopes ? explode(' ', $requestedScopes) : [];
58
59
        $scopeRequestedIsIdentical = true;
60
        if ((empty($requestedScopes) && !is_null($scope)) || (is_array($scope) && !empty(array_diff($requestedScopes, $scope)))) {
61
            $scopeRequestedIsIdentical = false;
62
        }
63
64
        $scope = is_array($scope) ? implode(' ', $scope) : null;
65
        $authorizationCode = $this->authorizationCodeStorage->create(
66
            $client->getIdentifier(), $resourceOwner->getIdentifier(), $data['redirect_uri'], $scope, $scopeRequestedIsIdentical, $idToken);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $idToken seems to be never defined.
Loading history...
Unused Code introduced by
The call to OAuth2\Storages\Authoriz...rageInterface::create() has too many arguments starting with $idToken. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

66
        /** @scrutinizer ignore-call */ 
67
        $authorizationCode = $this->authorizationCodeStorage->create(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
67
68
        return [
69
            'code' => $authorizationCode->getCode()
70
        ];
71
    }
72
73
74
    /**
75
     * @param ServerRequestInterface $request
76
     * @throws OAuthException
77
     * @throws \Exception
78
     */
79
    public function verifyRequest(ServerRequestInterface $request): void
80
    {
81
        parent::verifyRequest($request);
82
83
        $scope = explode(' ', $request->getQueryParams()['scope'] ?? $request->getParsedBody()['scope'] ?? null);
84
        $state = $request->getQueryParams()['state'] ?? $request->getParsedBody()['state'] ?? null;
85
86
        if (is_array($scope) && in_array('openid', $scope)) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
87
88
        }
89
90
        if(!$state && $this->configurationRepository->getConfig(Config::ENFORCE_STATE)) {
91
            throw new OAuthException('invalid_request',
92
                'Missing a required parameter : state',
93
                'http://openid.net/specs/openid-connect-core-1_0.html#AuthorizationEndpoint'
94
            );
95
        }
96
    }
97
98
}