Passed
Push — master ( e88490...575397 )
by Alexandre
04:15
created

HybridFlow::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 4
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\Flows;
10
11
12
use OAuth2\Endpoints\AuthorizationEndpoint;
13
use OAuth2\Endpoints\TokenEndpoint;
14
use OAuth2\Exceptions\OAuthException;
15
use OAuth2\Extensions\OpenID\Credentials\AuthorizationCodeInterface;
16
use OAuth2\Extensions\OpenID\IdTokenManager;
17
use OAuth2\Extensions\OpenID\Storages\AuthorizationCodeStorageInterface;
18
use OAuth2\Flows\FlowInterface;
19
use OAuth2\Storages\AccessTokenStorageInterface;
20
use OAuth2\Storages\RefreshTokenStorageInterface;
21
use OAuth2\Tests\Storages\RefreshTokenStorage;
22
23
class HybridFlow implements FlowInterface
24
{
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
        $this->authorizationCodeStorage = $authorizationCodeStorage;
45
        $this->accessTokenStorage = $accessTokenStorage;
46
        $this->idTokenManager = $idTokenManager;
47
    }
48
49
    public function getResponseTypes(): array
50
    {
51
        return ['code id_token', 'code token', 'code id_token token'];
52
    }
53
54
    /**
55
     * @param AuthorizationEndpoint $authorizationEndpoint
56
     * @param array $requestData
57
     */
58
    public function verifyAuthorizationRequest(AuthorizationEndpoint $authorizationEndpoint, array $requestData)
59
    {
60
    }
61
62
    public function handleAuthorizationRequest(AuthorizationEndpoint $authorizationEndpoint, array $requestData): array
63
    {
64
        if (!$authorizationEndpoint instanceof \OAuth2\Extensions\OpenID\Endpoints\AuthorizationEndpoint) {
65
            throw new \InvalidArgumentException();
66
        }
67
68
        $result = [];
69
        $idTokenClaims = [];
70
        $responseTypes = explode(' ', $requestData['response_type']);
71
        if (in_array('code', $responseTypes)) {
72
//            $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...
73
//            $idTokenTokenEndpoint = $this->idTokenManager->issueIdToken(
74
//                $authorizationEndpoint->getClient(),
75
//                $authorizationEndpoint->getResourceOwner(),
76
//                $resourceOwnerClaims
77
//            );
78
79
            $authorizationCode = $this->authorizationCodeStorage->generate(
80
                implode(' ', $authorizationEndpoint->getScopes()),
81
                $authorizationEndpoint->getClient()->getIdentifier(),
82
                $authorizationEndpoint->getResourceOwner()->getIdentifier(),
83
                $requestData['scope'] ?? null,
84
                $requestData['redirect_uri'] ?? null
85
//                $idTokenTokenEndpoint
86
            );
87
88
            if (!$authorizationCode instanceof AuthorizationCodeInterface) {
89
                throw new \InvalidArgumentException();
90
            }
91
92
            $idTokenClaims['c_hash'] = $this->idTokenManager->getCodeHash(
93
                $authorizationEndpoint->getClient(), $authorizationCode);
94
            $result['code'] = $authorizationCode->getCode();
95
        }
96
97
        if (in_array('token', $responseTypes)) {
98
            $accessToken = $this->accessTokenStorage->generate(
99
                implode(' ', $authorizationEndpoint->getScopes()),
100
                $authorizationEndpoint->getClient()->getIdentifier(),
101
                $authorizationEndpoint->getResourceOwner()->getIdentifier()
102
            );
103
104
            $idTokenClaims['at_hash'] = $this->idTokenManager->getAccessTokenHash(
105
                $authorizationEndpoint->getClient(), $accessToken);
106
            $result['access_token'] = $accessToken->getToken();
107
        }
108
109
        if (in_array('id_token', $responseTypes)) {
110
            $result['id_token'] = $this->idTokenManager->issueIdToken(
111
                $authorizationEndpoint->getClient(),
112
                $authorizationEndpoint->getResourceOwner(),
113
                $idTokenClaims
114
            );
115
        }
116
117
        return $result;
118
    }
119
120
    public function getDefaultResponseMode(): string
121
    {
122
        return 'fragment';
123
    }
124
125
    public function getUnsupportedResponseModes(): array
126
    {
127
        return ['query'];
128
    }
129
130
    public function getGrantTypes(): array
131
    {
132
        return [];
133
    }
134
135
    public function handleAccessTokenRequest(TokenEndpoint $tokenEndpoint, array $requestData): array
136
    {
137
        return [];
138
    }
139
}