Passed
Push — master ( aec877...07a608 )
by Alexandre
01:51
created

CodeResponseType::getExtendedResponseTypes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
crap 2
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\OpenID\ResponseModes\ResponseModeInterface;
15
use OAuth2\Repositories\ConfigurationRepository;
16
use OAuth2\Roles\Clients\RegisteredClient;
17
use OAuth2\Roles\ResourceOwnerInterface;
18
use OAuth2\OpenID\Storages\AuthorizationCodeStorageInterface;
19
use Psr\Http\Message\ServerRequestInterface;
20
21
class CodeResponseType implements ResponseTypeInterface
22
{
23
24
    /**
25
     * @var ConfigurationRepository
26
     */
27
    private $configurationRepository;
28
    /**
29
     * @var AuthorizationCodeStorageInterface
30
     */
31
    private $authorizationCodeStorage;
32
33
    public function __construct(ConfigurationRepository $configurationRepository, AuthorizationCodeStorageInterface $authorizationCodeStorage)
34
    {
35
        $this->configurationRepository = $configurationRepository;
36
        $this->authorizationCodeStorage = $authorizationCodeStorage;
37
    }
38
39
    public function getResponseType(): string
40
    {
41
        return 'code';
42
    }
43
44
    /**
45
     * @param ServerRequestInterface $request
46
     * @param ResourceOwnerInterface $resourceOwner
47
     * @param RegisteredClient $client
48
     * @param array|null $scope
49
     * @param array|null $extendedResponseTypes
50
     * @return array
51
     * @throws OAuthException
52
     */
53
    public function handle(ServerRequestInterface $request, ResourceOwnerInterface $resourceOwner,
54
                           RegisteredClient $client, ?array $scope = null, ?array $extendedResponseTypes = null): array
55
    {
56
        $data = $request->getMethod() === 'GET' ? $request->getQueryParams() : $request->getParsedBody();
57
58
        if (is_array($client->getSupportedGrantTypes()) && !in_array('authorization_code', $client->getSupportedGrantTypes())) {
59
            throw new OAuthException('unauthorized_client',
60
                'Client is not authorized to request an authorization code with this method',
61
                'https://tools.ietf.org/html/rfc6749#section-5.2');
62
        }
63
64
        $idToken =
65
66
        $requestedScopes = $data['scope'] ?? null;
67
        $requestedScopes = $requestedScopes ? explode(' ', $requestedScopes) : [];
68
69
        $scopeRequestedIsIdentical = true;
70
        if ((empty($requestedScopes) && !is_null($scope)) || (is_array($scope) && !empty(array_diff($requestedScopes, $scope)))) {
71
            $scopeRequestedIsIdentical = false;
72
        }
73
74
        $scope = is_array($scope) ? implode(' ', $scope) : null;
75
        $authorizationCode = $this->authorizationCodeStorage->create(
76
            $client->getIdentifier(), $resourceOwner->getIdentifier(), $idToken, $data['redirect_uri'], $scope, $scopeRequestedIsIdentical);
0 ignored issues
show
Bug introduced by
It seems like $idToken can also be of type null; however, parameter $idToken of OAuth2\OpenID\Storages\A...rageInterface::create() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

76
            $client->getIdentifier(), $resourceOwner->getIdentifier(), /** @scrutinizer ignore-type */ $idToken, $data['redirect_uri'], $scope, $scopeRequestedIsIdentical);
Loading history...
77
78
        return [
79
            'code' => $authorizationCode->getCode()
80
        ];
81
    }
82
83
84
    /**
85
     * @param ServerRequestInterface $request
86
     */
87
    public function verifyRequest(ServerRequestInterface $request): void
88
    {
89
    }
90
91
    public function getDefaultResponseMode(): string
92
    {
93
        return ResponseModeInterface::RESPONSE_MODE_QUERY;
94
    }
95
96
    public function isImplicit(): bool
97
    {
98
        return false;
99
    }
100
101
    public function requireTLS(): bool
102
    {
103
        return true;
104
    }
105
106
    protected function generateCode()
107
    {
108
        return bin2hex(random_bytes(8));
109
    }
110
111
    public function verifyRequest(ServerRequestInterface $request): void
112
    {
113
    }
114
115
    public function isMultiValuedResponseTypeSupported(): bool
116
    {
117
        return true;
118
    }
119
120
    public function getExtendedResponseTypes(): ?array
121
    {
122
        return null;
123
    }
124
125
    public function isQueryResponseModeSupported(): bool
126
    {
127
        return true;
128
    }
129
130
}