Passed
Push — master ( 5ed34a...340187 )
by Alexandre
02:40
created

AuthorizationEndpoint::beforeConsent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 0
cts 9
cp 0
rs 9.0856
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
crap 6
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: GCC-MED
5
 * Date: 19/01/2018
6
 * Time: 16:02
7
 */
8
9
namespace OAuth2\OpenID\Endpoints;
10
11
12
use GuzzleHttp\Psr7\Response;
13
use GuzzleHttp\Psr7\Uri;
14
use OAuth2\Config;
15
use OAuth2\EndpointMessages\Authorization\AuthorizationResponse;
16
use OAuth2\EndpointMessages\Authorization\ErrorResponse;
17
use OAuth2\Exceptions\OAuthException;
18
use OAuth2\ResponseTypes\ResponseTypeInterface;
19
use OAuth2\Roles\Clients\RegisteredClient;
20
use OAuth2\Roles\ResourceOwnerInterface;
21
use Psr\Http\Message\ResponseInterface;
22
use Psr\Http\Message\ServerRequestInterface;
23
24
class AuthorizationEndpoint extends \OAuth2\Endpoints\AuthorizationEndpoint
25
{
26
    /**
27
     * @param ServerRequestInterface $request
28
     * @param ResourceOwnerInterface $resourceOwner
29
     * @param bool $authorizationDecision
30
     * @param array|null $scopeRestrictedByResourceOwner
31
     * @return ResponseInterface
32
     * @throws \Exception
33
     */
34
    public function handle(ServerRequestInterface $request, ResourceOwnerInterface $resourceOwner, ?bool $authorizationDecision = null,
35
                           ?array $scopeRestrictedByResourceOwner = null): ResponseInterface
36
    {
37
        /**
38
         * @var RegisteredClient $client
39
         */
40
        $result = $this->verify($request);
41
        if ($result instanceof Response) {
42
            return $result;
43
        }
44
45
        list('client' => $client, 'responseTypes' => $responseTypes, 'redirectUri' => $redirectUri) = $result;
0 ignored issues
show
Unused Code introduced by
The assignment to $client is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
Unused Code introduced by
The assignment to $responseTypes is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
46
        $this->verify($request);
47
48
        return new AuthorizationResponse($redirectUri);
49
    }
50
51
    /**
52
     * @param ServerRequestInterface $request
53
     * @return Response
54
     * @throws \Exception
55
     */
56
    public function beforeConsent(ServerRequestInterface $request)
57
    {
58
        /**
59
         * @var RegisteredClient $client
60
         */
61
        $result = $this->verify($request);
62
        if ($result instanceof Response) {
63
            return $result;
64
        }
65
66
        /**
67
         * @var RegisteredClient $client
68
         * @var array $responseTypes
69
         * @var Uri $redirectUri
70
         * @var array|null $scope
71
         * @var string|null $responseMode
72
         * @var bool $isInsecure
73
         */
74
        extract($result);
75
76
77
        return new AuthorizationResponse($redirectUri);
78
    }
79
80
    /**
81
     * @param ServerRequestInterface $request
82
     * @return array|ErrorResponse
83
     * @throws \Exception
84
     */
85
    public function verify(ServerRequestInterface $request)
86
    {
87
        $redirectUri = new Uri();
88
        try {
89
            if ($request->getMethod() == 'GET') {
90
                $data = $request->getQueryParams();
91
            } else if ($request->getMethod() == 'POST') {
92
                $data = $request->getParsedBody();
93
            } else {
94
                throw new OAuthException('invalid_request', 'Support only HTTP GET and POST methods');
95
            }
96
97
            if (!isset($data['scope']) || !$data['scope']) {
98
                throw new OAuthException('invalid_request', 'Missing a required parameter : scope');
99
            }
100
101
            if (!isset($data['response_type']) || !$data['response_type']) {
102
                throw new OAuthException('invalid_request', 'Missing a required parameter : response_type');
103
            }
104
105
            if (!isset($data['client_id']) || !$data['client_id']) {
106
                throw new OAuthException('invalid_request', 'Missing a required parameter : client_id');
107
            }
108
109
            if (!isset($data['redirect_uri']) || !$data['redirect_uri']) {
110
                throw new OAuthException('invalid_request', 'Missing a required parameter : redirect_uri');
111
            }
112
113
            $isStateRequired = $this->server->getConfigurationRepository()->getConfig(Config::ENFORCE_STATE);
114
            if ($isStateRequired && (!isset($data['state']) || !$data['state'])) {
115
                throw new OAuthException('invalid_request', 'Missing a required parameter : state');
116
            }
117
118
            $responseTypes = [];
119
            foreach (explode(' ', $data['response_type']) as $responseTypeName) {
120
                $responseType = $this->server->getResponseTypeRepository()->getResponseType($responseTypeName);
121
                if (!$responseType) {
122
                    throw new OAuthException('invalid_request', 'Unknown response_type : ' . $responseTypeName);
123
                }
124
                $responseType->verifyRequest($request);
125
                $responseTypes[] = $responseType;
126
            }
127
128
            if (empty($responseTypes)) {
129
                throw new OAuthException('invalid_request', 'Invalid response_type parameter');
130
            } else if (count($responseTypes) == 1) {
131
                $defaultResponseMode = $responseTypes[0]->getDefaultResponseMode();
132
            } else {
133
                $defaultResponseMode = ResponseTypeInterface::RESPONSE_MODE_FRAGMENT;
134
            }
135
136
            $responseMode = $data['response_mode'] ?? $defaultResponseMode;
137
            if (count($responseTypes) > 1 && $responseMode === ResponseTypeInterface::RESPONSE_MODE_QUERY) {
138
                throw new OAuthException('invalid_request', 'Invalid response_mode parameter : query mode is not allowed');
139
            }
140
        } catch (OAuthException $e) {
141
            return new ErrorResponse($redirectUri,
142
                $e->getError(), $e->getErrorDescription(), $e->getErrorUri(),
143
                $data['state'] ?? null);
144
        }
145
        $client = '';
146
        return compact('client', 'responseTypes', 'redirectUri');
147
148
//        $requiredParameters = ['scope', 'response_type', 'client_id']
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
149
    }
150
}