Passed
Push — master ( fd01cd...a8d522 )
by Alexandre
02:26
created

AuthorizationEndpoint::handleRequest()   B

Complexity

Conditions 4
Paths 18

Size

Total Lines 99
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 18
nop 1
dl 0
loc 99
rs 8.2098
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Alexandre
5
 * Date: 18/02/2018
6
 * Time: 18:14
7
 */
8
9
namespace OAuth2\Extensions\OpenID\Endpoints;
10
11
12
use GuzzleHttp\Psr7\Response;
13
use OAuth2\Endpoints\EndpointInterface;
14
use OAuth2\Exceptions\OAuthException;
15
use OAuth2\Flows\AuthorizationCodeFlow;
16
use OAuth2\Flows\FlowInterface;
17
use OAuth2\Flows\FlowManager;
18
use OAuth2\ResponseModes\ResponseModeInterface;
19
use OAuth2\ResponseModes\ResponseModeManager;
20
use OAuth2\ResponseTypes\ResponseTypeManager;
21
use Psr\Http\Message\ResponseInterface;
22
use Psr\Http\Message\ServerRequestInterface;
23
24
class AuthorizationEndpoint extends \OAuth2\Endpoints\AuthorizationEndpoint
25
{
26
    function handleRequest(ServerRequestInterface $request): ResponseInterface
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
27
    {
28
        $requestData = $request->getMethod() == 'GET' ? $request->getQueryParams() : $request->getParsedBody();
29
30
31
        try {
32
            /**
33
             * The Authorization Server MUST validate all the OAuth 2.0 parameters according to the OAuth 2.0 specification
34
             */
35
            $this->verifyRequestData($requestData);
0 ignored issues
show
Bug introduced by
It seems like $requestData can also be of type object and null; however, parameter $requestData of OAuth2\Extensions\OpenID...nt::verifyRequestData() 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

35
            $this->verifyRequestData(/** @scrutinizer ignore-type */ $requestData);
Loading history...
36
37
38
            /**
39
             * Verify that a scope parameter is present and contains the openid scope value.
40
             * (If no openid scope value is present, the request may still be a valid OAuth 2.0 request, but is not an OpenID Connect request.)
41
             */
42
            $scope = $requestData['scope'] ?? '';
43
            $scopes = explode(' ', $scope);
44
            if(!in_array('openid', $scopes)) {
45
                return parent::handleRequest($request);
46
            }
47
48
            $responseData = $this->getResponseType()->handleAuthorizationRequest($this, $requestData);
0 ignored issues
show
Bug introduced by
It seems like $requestData can also be of type object and null; however, parameter $requestData of OAuth2\ResponseTypes\Res...eAuthorizationRequest() 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

48
            $responseData = $this->getResponseType()->handleAuthorizationRequest($this, /** @scrutinizer ignore-type */ $requestData);
Loading history...
49
            return $this->getResponseMode()->buildResponse($this, $requestData, $responseData);
0 ignored issues
show
Bug introduced by
It seems like $requestData can also be of type object and null; however, parameter $requestData of OAuth2\ResponseModes\Res...erface::buildResponse() 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

49
            return $this->getResponseMode()->buildResponse($this, /** @scrutinizer ignore-type */ $requestData, $responseData);
Loading history...
50
51
            /**
52
             * If the sub (subject) Claim is requested with a specific value for the ID Token,
53
             * the Authorization Server MUST only send a positive response
54
             * if the End-User identified by that sub value has an active session with the Authorization Server
55
             * or has been Authenticated as a result of the request.
56
             * The Authorization Server MUST NOT reply with an ID Token or Access Token for a different user,
57
             * even if they have an active session with the Authorization Server.
58
             * Such a request can be made either using an id_token_hint parameter
59
             * or by requesting a specific Claim Value as described in Section 5.5.1,
60
             * if the claims parameter is supported by the implementation.
61
             */
62
63
64
            /**
65
             * The Authorization Server MUST verify that all the REQUIRED parameters are present and their usage conforms to this specification.
66
             */
67
68
            /**
69
             * Authorization Server Authenticates the End-User.
70
             *
71
             * If the request is valid, the Authorization Server attempts to Authenticate the End-User
72
             * or determines whether the End-User is Authenticated, depending upon the request parameter values used.
73
             * The methods used by the Authorization Server to Authenticate the End-User
74
             * (e.g. username and password, session cookies, etc.) are beyond the scope of this specification.
75
             * An Authentication user interface MAY be displayed by the Authorization Server,
76
             * depending upon the request parameter values used and the authentication methods used.
77
             *
78
             * The Authorization Server MUST attempt to Authenticate the End-User in the following cases:
79
             *
80
             * The End-User is not already Authenticated.
81
             * The Authentication Request contains the prompt parameter with the value login.
82
             * In this case, the Authorization Server MUST reauthenticate the End-User
83
             * even if the End-User is already authenticated.
84
             *
85
             * The Authorization Server MUST NOT interact with the End-User in the following case:
86
             *
87
             * The Authentication Request contains the prompt parameter with the value none.
88
             * In this case, the Authorization Server MUST return an error if an End-User is not already Authenticated
89
             * or could not be silently Authenticated.
90
             *
91
             * When interacting with the End-User, the Authorization Server MUST employ appropriate measures
92
             * against Cross-Site Request Forgery and Clickjacking as, described in Sections 10.12 and 10.13 of OAuth 2.0 [RFC6749].
93
             */
94
95
            /**
96
             * Authorization Server obtains End-User Consent/Authorization.
97
             *
98
             * Once the End-User is authenticated, the Authorization Server MUST obtain an authorization decision
99
             * before releasing information to the Relying Party.
100
             * When permitted by the request parameters used,
101
             * this MAY be done through an interactive dialogue with the End-User
102
             * that makes it clear what is being consented to or by establishing consent
103
             * via conditions for processing the request or other means (for example, via previous administrative consent).
104
             * Sections 2 and 5.3 describe information release mechanisms.
105
             */
106
107
            /**
108
             * Authorization Server sends the End-User back to the Client with an Authorization Code.
109
             *
110
             * An Authentication Response is an OAuth 2.0 Authorization Response message
111
             * returned from the OP's Authorization Endpoint in response to the Authorization Request message sent by the RP.
112
             *
113
             * When using the Authorization Code Flow, the Authorization Response MUST return the parameters
114
             * defined in Section 4.1.2 of OAuth 2.0 [RFC6749] by adding them as query parameters to the redirect_uri
115
             * specified in the Authorization Request using the application/x-www-form-urlencoded format,
116
             * unless a different Response Mode was specified.
117
             */
118
        }
119
        catch (OAuthException $e) {
120
            /**
121
             * If the Authorization Server encounters any error, it MUST return an error response, per Section 3.1.2.6.
122
             */
123
124
            return new Response(400, ['content-type' => 'application/json'], $e->jsonSerialize());
125
        }
126
    }
127
128
129
130
    /**
131
     * @param array $requestData
132
     * @throws OAuthException
133
     */
134
    public function verifyRequestData(array $requestData)
135
    {
136
        parent::verifyRequestData($requestData); // TODO: Change the autogenerated stub
137
138
        // openid :
139
        // response_mode
140
        // nonce
141
        // display
142
        // prompt
143
        // max_age
144
        // ui_locales
145
        // id_token_hint
146
        // login_hint
147
        // acr_values
148
    }
149
}