ImplicitFlow::getDefaultResponseMode()   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: 14/03/2018
6
 * Time: 22:13
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
use OAuth2\Exceptions\OAuthException;
16
use OAuth2\Extensions\OpenID\IdTokenManager;
17
use OAuth2\Extensions\OpenID\Roles\Clients\ClientMetadataInterface;
18
use OAuth2\AuthorizationGrantTypes\Flows\FlowInterface;
19
use OAuth2\AuthorizationGrantTypes\AbstractGrantType;
20
use OAuth2\Extensions\OpenID\Roles\ResourceOwnerInterface;
0 ignored issues
show
Bug introduced by
The type OAuth2\Extensions\OpenID...\ResourceOwnerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use OAuth2\Helper;
22
use OAuth2\Storages\AccessTokenStorageInterface;
23
use OAuth2\Storages\RefreshTokenStorageInterface;
24
25
class ImplicitFlow extends AbstractGrantType implements FlowInterface
26
{
27
    /**
28
     * @var IdTokenManager
29
     */
30
    private $idTokenManager;
31
32
    public function __construct(AccessTokenStorageInterface $accessTokenStorage,
33
                                RefreshTokenStorageInterface $refreshTokenStorage,
34
                                IdTokenManager $idTokenManager)
35
    {
36
        parent::__construct($accessTokenStorage, $refreshTokenStorage);
37
        $this->idTokenManager = $idTokenManager;
38
    }
39
40
    /**
41
     * @return string[]
42
     */
43
    public function getResponseTypes(): array
44
    {
45
        return [
46
            'id_token',
47
            'id_token token'
48
        ];
49
    }
50
51
    /**
52
     * @return string[]
53
     */
54
    public function getGrantTypes(): array
55
    {
56
        return [];
57
    }
58
59
    public function handleAccessTokenRequest(TokenEndpoint $tokenEndpoint, array $requestData): array
60
    {
61
        return [];
62
    }
63
64
    /**
65
     * @param AuthorizationEndpoint $authorizationEndpoint
66
     * @param array $requestData
67
     * @throws OAuthException
68
     */
69
    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

69
    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...
70
    {
71
        if (!$authorizationEndpoint instanceof \OAuth2\Extensions\OpenID\Endpoints\AuthorizationEndpoint) {
72
            throw new \InvalidArgumentException();
73
        }
74
        if (!$authorizationEndpoint->getNonce()) {
75
            throw new OAuthException('invalid_request', 'Nonce required');
76
        }
77
    }
78
79
    /**
80
     * @param AuthorizationEndpoint $authorizationEndpoint
81
     * @param array $requestData
82
     * @return array
83
     */
84
    public function handleAuthorizationRequest(AuthorizationRequestInterface $authorizationRequest): array
85
    {
86
        if (!$authorizationEndpoint instanceof \OAuth2\Extensions\OpenID\Endpoints\AuthorizationEndpoint) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $authorizationEndpoint does not exist. Did you maybe mean $authorizationRequest?
Loading history...
87
            throw new \InvalidArgumentException();
88
        }
89
90
        $resourceOwner = $authorizationEndpoint->getResourceOwner();
0 ignored issues
show
Bug introduced by
The method getResourceOwner() does not exist on OAuth2\Extensions\OpenID...s\AuthorizationEndpoint. ( Ignorable by Annotation )

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

90
        /** @scrutinizer ignore-call */ 
91
        $resourceOwner = $authorizationEndpoint->getResourceOwner();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
91
        $idToken = [];
92
93
        if ($resourceOwner instanceof ResourceOwnerInterface) {
94
            if (!is_null($authorizationEndpoint->getMaxAge())) {
95
                $time = $resourceOwner->getLastTimeActivelyAuthenticated();
96
                $idToken['auth_time'] = $time ? $time->getTimestamp() : $authorizationEndpoint->getMaxAge();
97
            }
98
            $acr = $resourceOwner->getAuthenticationContextClassReference();
99
            if (!is_null($acr)) {
100
                $idToken['acr'] = $acr;
101
            }
102
103
            $amr = $resourceOwner->getAuthenticationMethodsReferences();
104
            if (!is_null($amr)) {
105
                $idToken['amr'] = $amr;
106
            }
107
        }
108
109
        if (!is_null($authorizationEndpoint->getNonce())) {
110
            $idToken['nonce'] = $authorizationEndpoint->getNonce();
111
        }
112
113
        $accessToken = $this->issueAccessToken(
114
            $authorizationEndpoint->getScopes(),
0 ignored issues
show
Bug introduced by
The method getScopes() does not exist on OAuth2\Extensions\OpenID...s\AuthorizationEndpoint. ( Ignorable by Annotation )

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

114
            $authorizationEndpoint->/** @scrutinizer ignore-call */ 
115
                                    getScopes(),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
115
            $authorizationEndpoint->getClient()->getIdentifier(),
0 ignored issues
show
Bug introduced by
The method getClient() does not exist on OAuth2\Extensions\OpenID...s\AuthorizationEndpoint. ( Ignorable by Annotation )

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

115
            $authorizationEndpoint->/** @scrutinizer ignore-call */ 
116
                                    getClient()->getIdentifier(),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
116
            $authorizationEndpoint->getResourceOwner()->getIdentifier()
117
        );
118
119
        $alg = 'RS256';
120
        $metadata = $authorizationEndpoint->getClient()->getMetadata();
121
        if ($metadata instanceof ClientMetadataInterface) {
122
            $alg = $metadata->getIdTokenSignedResponseAlg() ?: 'RS256';
123
        }
124
125
        $macAlgorithm = substr($alg, -3);
126
127
        if (!in_array($macAlgorithm, [256, 384, 512])) {
128
            throw new \UnexpectedValueException("Algotihmn '".$macAlgorithm."' not supported");
129
        }
130
        $macAlgorithm = 'sha' . $macAlgorithm;
131
132
133
        $atHash = hash($macAlgorithm, $accessToken['access_token'], true);
134
        $atHash = substr($atHash, 0, strlen($atHash) / 2);
135
        $atHash = Helper::base64url_encode($atHash);
136
        $idToken['at_hash'] = $atHash;
137
138
        $result = [];
139
        $result['id_token'] = $this->idTokenManager->issueIdToken(
140
            $authorizationEndpoint->getClient(),
141
            $authorizationEndpoint->getResourceOwner()
142
        );
143
144
//        $result = array_merge($result, $accessToken);
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% 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...
145
        return $result;
146
    }
147
148
    public function getDefaultResponseMode(): string
149
    {
150
        return 'fragment';
151
    }
152
153
    public function getUnsupportedResponseModes(): array
154
    {
155
        return ['query'];
156
    }
157
158
    public function isRegistrationOfRedirectUriRequired(): bool
159
    {
160
        return true;
161
    }
162
}