Passed
Push — master ( 0c0a9d...5256fe )
by Alexandre
01:49
created

TokenResponseType::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
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Alexandre
5
 * Date: 07/01/2018
6
 * Time: 13:33
7
 */
8
9
namespace OAuth2\ResponseTypes;
10
11
12
use OAuth2\OpenID\ResponseModes\ResponseModeInterface;
13
use OAuth2\Roles\Clients\RegisteredClient;
14
use OAuth2\Roles\ResourceOwnerInterface;
15
use OAuth2\Storages\AccessTokenStorageInterface;
16
use OAuthException;
17
use Psr\Http\Message\ServerRequestInterface;
18
19
20
class TokenResponseType implements ResponseTypeInterface
21
{
22
    /**
23
     * @var AccessTokenStorageInterface
24
     */
25
    private $accessTokenStorage;
26
27
    public function __construct(AccessTokenStorageInterface $accessTokenStorage)
28
    {
29
        $this->accessTokenStorage = $accessTokenStorage;
30
    }
31
32
    public function getResponseType(): string
33
    {
34
        return 'token';
35
    }
36
37 1
    /**
38
     * @param ServerRequestInterface $request
39
     * @param ResourceOwnerInterface $resourceOwner
40 1
     * @param RegisteredClient $client
41
     * @param array|null $scope
42
     * @param array|null $extendedResponseTypes
43
     * @return array
44
     * @throws OAuthException
45
     */
46 1
    public function handle(ServerRequestInterface $request, ResourceOwnerInterface $resourceOwner, RegisteredClient $client,
47 1
                           ?array $scope = null, ?array $extendedResponseTypes = null): array
48
    {
49
        if (!$client->isImplicitAllowed()) {
50 1
            throw new OAuthException('unauthorized_client',
51 1
                'Client is not authorized to request an authorization code with this method',
0 ignored issues
show
Bug introduced by
'Client is not authorize... code with this method' of type string is incompatible with the type integer expected by parameter $code of OAuthException::__construct(). ( Ignorable by Annotation )

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

51
                /** @scrutinizer ignore-type */ 'Client is not authorized to request an authorization code with this method',
Loading history...
52
                'https://tools.ietf.org/html/rfc6749#section-5.2');
0 ignored issues
show
Bug introduced by
'https://tools.ietf.org/html/rfc6749#section-5.2' of type string is incompatible with the type Throwable|null expected by parameter $previous of OAuthException::__construct(). ( Ignorable by Annotation )

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

52
                /** @scrutinizer ignore-type */ 'https://tools.ietf.org/html/rfc6749#section-5.2');
Loading history...
53
        }
54 1
55 1
        $accessToken = $this->accessTokenStorage->create(
56
            $client->getIdentifier(), $resourceOwner->getIdentifier(), implode(' ', $scope));
0 ignored issues
show
Bug introduced by
It seems like $scope can also be of type null; however, parameter $pieces of implode() does only seem to accept array, 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

56
            $client->getIdentifier(), $resourceOwner->getIdentifier(), implode(' ', /** @scrutinizer ignore-type */ $scope));
Loading history...
57
58 1
        $data = [
59 1
            'access_token' => $accessToken->getToken(),
60
            'token_type' => $accessToken->getType(),
61 1
        ];
62 1
63
        if ($accessToken->getExpiresAt()) {
64
            $data['expires_in'] = $accessToken->getExpiresAt() - time();
65 1
        }
66
67
        $requestedScopes = $request->getQueryParams()['scope'] ?? $request->getParsedBody()['scope'] ?? null;
68 1
        $requestedScopes = $requestedScopes ? explode(' ', $requestedScopes) : [];
69
70 1
        if ((empty($requestedScopes) && !is_null($scope)) || (is_array($scope) && !empty(array_diff($requestedScopes, $scope)))) {
71
            $data['scope'] = implode(' ', $scope);
72
        }
73 1
74
        return $data;
75 1
    }
76
77
    public function getDefaultResponseMode(): string
78 1
    {
79
        return ResponseModeInterface::RESPONSE_MODE_FRAGMENT;
80
    }
81 1
82
    public function isImplicit(): bool
83 1
    {
84
        return true;
85 1
    }
86
87
    public function verifyRequest(ServerRequestInterface $request): void
88
    {
89
90
    }
91
92
    public function requireTLS(): bool
93
    {
94
        return true;
95
    }
96
97
    public function isMultiValuedResponseTypeSupported(): bool
98
    {
99
        return true;
100
    }
101
102
    public function getExtendedResponseTypes(): ?array
103
    {
104
        return null;
105
    }
106
107
    public function isQueryResponseModeSupported(): bool
108
    {
109
        return false;
110
    }
111
}