HybridFlow::getResponseTypes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Alexandre
5
 * Date: 18/02/2018
6
 * Time: 18:08
7
 */
8
9
namespace OAuth2\Extensions\OpenID\AuthorizationGrantTypes\Flows;
10
11
12
use OAuth2\Endpoints\Authorization\AuthorizationRequestInterface;
13
use OAuth2\Endpoints\AuthorizationEndpoint;
14
use OAuth2\Endpoints\TokenEndpoint;
15
16
use OAuth2\Extensions\OpenID\Credentials\AuthorizationCodeInterface;
17
use OAuth2\Extensions\OpenID\IdTokenManager;
18
use OAuth2\AuthorizationGrantTypes\Flows\FlowInterface;
19
use OAuth2\Storages\AuthorizationCodeStorageInterface;
20
use OAuth2\Storages\AccessTokenStorageInterface;
21
use OAuth2\Storages\RefreshTokenStorageInterface;
22
23
24
class HybridFlow implements FlowInterface
25
{
26
    /**
27
     * @var AuthorizationCodeStorageInterface
28
     */
29
    private $authorizationCodeStorage;
30
    /**
31
     * @var AccessTokenStorageInterface
32
     */
33
    private $accessTokenStorage;
34
    /**
35
     * @var IdTokenManager
36
     */
37
    private $idTokenManager;
38
39
    public function __construct(AuthorizationCodeStorageInterface $authorizationCodeStorage,
40
                                AccessTokenStorageInterface $accessTokenStorage,
41
                                RefreshTokenStorageInterface $refreshTokenStorage,
0 ignored issues
show
Unused Code introduced by
The parameter $refreshTokenStorage is not used and could be removed. ( Ignorable by Annotation )

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

41
                                /** @scrutinizer ignore-unused */ RefreshTokenStorageInterface $refreshTokenStorage,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
42
                                IdTokenManager $idTokenManager)
43
    {
44
        // TODO get authorization code flow in parameter to delegate grant type authorization_code
45
        $this->authorizationCodeStorage = $authorizationCodeStorage;
46
        $this->accessTokenStorage = $accessTokenStorage;
47
        $this->idTokenManager = $idTokenManager;
48
    }
49
50
    public function getResponseTypes(): array
51
    {
52
        return ['code id_token', 'code token', 'code id_token token'];
53
    }
54
55
    /**
56
     * @param AuthorizationEndpoint $authorizationEndpoint
57
     * @param array $requestData
58
     */
59
    public function verifyAuthorizationRequest(AuthorizationEndpoint $authorizationEndpoint, array $requestData)
0 ignored issues
show
Unused Code introduced by
The parameter $requestData is not used and could be removed. ( Ignorable by Annotation )

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

59
    public function verifyAuthorizationRequest(AuthorizationEndpoint $authorizationEndpoint, /** @scrutinizer ignore-unused */ array $requestData)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $authorizationEndpoint is not used and could be removed. ( Ignorable by Annotation )

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

59
    public function verifyAuthorizationRequest(/** @scrutinizer ignore-unused */ AuthorizationEndpoint $authorizationEndpoint, array $requestData)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
60
    {
61
    }
62
63
    public function handleAuthorizationRequest(AuthorizationRequestInterface $authorizationEndpoint): array
64
    {
65
        if (!$authorizationEndpoint instanceof \OAuth2\Extensions\OpenID\Endpoints\AuthorizationEndpoint) {
66
            throw new \InvalidArgumentException();
67
        }
68
69
        $result = [];
70
        $idTokenClaims = [];
71
        $responseTypes = explode(' ', $authorizationEndpoint->getData()['response_type']);
72
        if (in_array('code', $responseTypes)) {
73
//            $resourceOwnerClaims = $authorizationEndpoint->getResourceOwner()->getClaims($authorizationEndpoint->getScopes());
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
74
//            $idTokenTokenEndpoint = $this->idTokenManager->issueIdToken(
75
//                $authorizationEndpoint->getClient(),
76
//                $authorizationEndpoint->getResourceOwner(),
77
//                $resourceOwnerClaims
78
//            );
79
80
            $authorizationCode = $this->authorizationCodeStorage->generate(
81
                $authorizationEndpoint->getScopes(),
82
                $authorizationEndpoint->getClient()->getIdentifier(),
83
                $authorizationEndpoint->getResourceOwner()->getIdentifier(),
84
                $requestData['scope'] ?? null,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $requestData seems to never exist and therefore isset should always be false.
Loading history...
85
                $requestData['redirect_uri'] ?? null
86
//                $idTokenTokenEndpoint
87
            );
88
89
            if (!$authorizationCode instanceof AuthorizationCodeInterface) {
90
                throw new \InvalidArgumentException();
91
            }
92
93
            $idTokenClaims['c_hash'] = $this->idTokenManager->getCodeHash(
94
                $authorizationEndpoint->getClient(), $authorizationCode);
95
            $result['code'] = $authorizationCode->getCode();
96
        }
97
98
        if (in_array('token', $responseTypes)) {
99
            $accessToken = $this->accessTokenStorage->generate(
100
                $authorizationEndpoint->getScopes(),
101
                $authorizationEndpoint->getClient()->getIdentifier(),
102
                $authorizationEndpoint->getResourceOwner()->getIdentifier()
103
            );
104
105
            $idTokenClaims['at_hash'] = $this->idTokenManager->getAccessTokenHash(
106
                $authorizationEndpoint->getClient(), $accessToken);
107
            $result['access_token'] = $accessToken->getToken();
108
        }
109
110
        if (in_array('id_token', $responseTypes)) {
111
            $result['id_token'] = $this->idTokenManager->issueIdToken(
112
                $authorizationEndpoint->getClient(),
113
                $authorizationEndpoint->getResourceOwner(),
0 ignored issues
show
Bug introduced by
$authorizationEndpoint->getResourceOwner() of type OAuth2\Roles\ResourceOwnerInterface is incompatible with the type string expected by parameter $resourceOwnerIdentifier of OAuth2\Extensions\OpenID...Manager::issueIdToken(). ( Ignorable by Annotation )

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

113
                /** @scrutinizer ignore-type */ $authorizationEndpoint->getResourceOwner(),
Loading history...
114
                $idTokenClaims
115
            );
116
        }
117
118
        return $result;
119
    }
120
121
    public function getDefaultResponseMode(): string
122
    {
123
        return 'fragment';
124
    }
125
126
    public function getUnsupportedResponseModes(): array
127
    {
128
        return ['query'];
129
    }
130
131
    public function isRegistrationOfRedirectUriRequired(): bool
132
    {
133
        return true;
134
    }
135
136
    public function getGrantTypes(): array
137
    {
138
        return [];
139
    }
140
141
    public function handleAccessTokenRequest(TokenEndpoint $tokenEndpoint, array $requestData): array
142
    {
143
        return [];
144
    }
145
}