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

IdTokenResponseType::getExtendedResponseTypes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
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 2
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: GCC-MED
5
 * Date: 19/01/2018
6
 * Time: 15:06
7
 */
8
9
namespace OAuth2\OpenID\ResponseTypes;
10
11
12
use OAuth2\Exceptions\OAuthException;
13
use OAuth2\OpenID\Credentials\IdToken;
14
use OAuth2\OpenID\ResponseModes\ResponseModeInterface;
15
use OAuth2\Roles\Clients\RegisteredClient;
16
use OAuth2\Roles\ResourceOwnerInterface;
17
use Psr\Http\Message\ServerRequestInterface;
18
19
class IdTokenResponseType implements ResponseTypeInterface
20
{
21
22
    public function getResponseType(): string
23
    {
24
        return 'id_token';
25
    }
26
27
    /**
28
     * @param ServerRequestInterface $request
29
     * @param ResourceOwnerInterface $resourceOwner
30
     * @param RegisteredClient $client
31
     * @param array|null $scope
32
     * @param array|null $extendedResponseTypes
33
     * @return array
34
     */
35
    public function handle(ServerRequestInterface $request, ResourceOwnerInterface $resourceOwner,
36
                           RegisteredClient $client, ?array $scope = null, ?array $extendedResponseTypes = null): array
37
    {
38
        $claims = [];
39
40
        if(isset($extendedResponseTypes['code'])) {
41
            //c_hash
42
            $code = $extendedResponseTypes['code']->handle()['code'];
43
            $result['code'] = $code;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$result was never initialized. Although not strictly required by PHP, it is generally a good practice to add $result = array(); before regardless.
Loading history...
44
        }
45
        if(isset($extendedResponseTypes['token'])) {
46
            //at_hash
47
            $token = $extendedResponseTypes['token']->handle()['token'];
48
            $result['token'] = $token;
49
        }
50
51
        $idToken = new IdToken($claims);
0 ignored issues
show
Unused Code introduced by
The assignment to $idToken is dead and can be removed.
Loading history...
52
53
        $result['id_token'] = '123aze';
54
        return $result;
55
    }
56
57
    public function getDefaultResponseMode(): string
58
    {
59
        return ResponseModeInterface::RESPONSE_MODE_FRAGMENT;
60
    }
61
62
    public function isImplicit(): bool
63
    {
64
        return true;
65
    }
66
67
    /**
68
     * @param ServerRequestInterface $request
69
     * @throws OAuthException
70
     */
71
    public function verifyRequest(ServerRequestInterface $request): void
72
    {
73
        $scope = explode(' ', $request->getQueryParams()['scope'] ?? $request->getParsedBody()['scope'] ?? null);
74
        if (is_null($scope)) {
0 ignored issues
show
introduced by
The condition is_null($scope) can never be true.
Loading history...
75
            throw new OAuthException('invalid_request',
76
                'Missing a required parameter : scope',
77
                'http://openid.net/specs/openid-connect-core-1_0.html#AuthorizationEndpoint'
78
            );
79
        }
80
        if (!in_array('openid', $scope)) {
81
            throw new OAuthException('invalid_request',
82
                'Invalid scope parameter : openid scope value must be present',
83
                'http://openid.net/specs/openid-connect-core-1_0.html#AuthorizationEndpoint'
84
            );
85
        }
86
    }
87
88
    public function requireTLS(): bool
89
    {
90
        return true;
91
    }
92
93
    public function isMultiValuedResponseTypeSupported(): bool
94
    {
95
        return true;
96
    }
97
98
    public function getExtendedResponseTypes(): ?array {
99
        return ['code', 'token'];
100
    }
101
102
    public function isQueryResponseModeSupported(): bool
103
    {
104
        return false;
105
    }
106
}