AuthorizationController::approve()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 4
cp 0
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
namespace CodexShaper\OAuth2\Server\Http\Controllers;
4
5
use CodexShaper\OAuth2\Server\Entities\User as UserEntity;
6
use CodexShaper\OAuth2\Server\Http\Requests\ServerRequest;
7
use CodexShaper\OAuth2\Server\Http\Responses\ServerResponse;
8
use CodexShaper\OAuth2\Server\Manager;
9
use CodexShaper\OAuth2\Server\Model;
10
use CodexShaper\OAuth2\Server\Models\User;
11
use Illuminate\Http\Request;
12
use League\OAuth2\Server\Exception\OAuthServerException;
13
14
class AuthorizationController
15
{
16
    /**
17
     * The server manager.
18
     *
19
     * @var \CodexShaper\OAuth2\Server\Manager
20
     */
21
    protected $manager;
22
23
    /**
24
     * The authorization server.
25
     *
26
     * @var \League\OAuth2\Server\AuthorizationServer
27
     */
28
    protected $server;
29
30
    /**
31
     * The psr7 server request.
32
     *
33
     * @var \CodexShaper\OAuth2\Server\Http\Requests\ServerRequest
34
     */
35
    protected $request;
36
37
    /**
38
     * The psr7 server response.
39
     *
40
     * @var \CodexShaper\OAuth2\Server\Http\Responses\ServerResponse
41
     */
42
    protected $response;
43
44
    /**
45
     * Create a new authorization controller instance.
46
     *
47
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
48
     */
49
    public function __construct()
50
    {
51
        $this->manager = new Manager();
52
        $this->server = $this->manager->makeAuthorizationServer();
53
        $this->request = ServerRequest::getPsrServerRequest();
54
        $this->response = ServerResponse::getPsrServerResponse();
0 ignored issues
show
Documentation Bug introduced by
It seems like \CodexShaper\OAuth2\Serv...:getPsrServerResponse() of type object<Nyholm\Psr7\Response> is incompatible with the declared type object<CodexShaper\OAuth...sponses\ServerResponse> of property $response.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
55
    }
56
57
    /**
58
     * Make authorization.
59
     *
60
     * @param \CodexShaper\OAuth2\Server\Models\User $user
61
     *
62
     * @return \League\OAuth2\Server\ResponseTypes\ResponseTypeInterface|void
63
     */
64
    public function authorize($user)
65
    {
66
        try {
67
68
            // Validate the HTTP request and return an AuthorizationRequest object.
69
            $authRequest = $this->server->validateAuthorizationRequest($this->request);
0 ignored issues
show
Documentation introduced by
$this->request is of type object<CodexShaper\OAuth...Requests\ServerRequest>, but the function expects a object<Psr\Http\Message\ServerRequestInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
70
71
            // Get all validate scopes from psr request
72
            $scopes = $this->filterScopes($authRequest);
0 ignored issues
show
Unused Code introduced by
$scopes is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
73
74
            // Get token for current user and request client id
75
            $token = Model::findToken('clientModel', $authRequest, $user);
76
77
            if (($token) || Model::instance('clientModel')->isSkipsAuthorization()) {
78
                return $this->approve($authRequest, $user);
79
            }
80
81
            return  $authRequest;
82
        } catch (OAuthServerException $exception) {
83
84
            // All instances of OAuthServerException can be formatted into a HTTP response
85
            return $exception->generateHttpResponse($this->response);
0 ignored issues
show
Documentation introduced by
$this->response is of type object<CodexShaper\OAuth...sponses\ServerResponse>, but the function expects a object<Psr\Http\Message\ResponseInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
86
        }
87
    }
88
89
    /**
90
     * Approve the authorization.
91
     *
92
     * @param \League\OAuth2\Server\RequestTypes\AuthorizationRequest $authRequest
93
     * @param \CodexShaper\OAuth2\Server\Models\User                  $user
94
     *
95
     * @return \League\OAuth2\Server\ResponseTypes\ResponseTypeInterface
96
     */
97
    public function approve($authRequest, $user)
98
    {
99
        // Once the user has logged in set the user on the AuthorizationRequest
100
        $authRequest->setUser(new UserEntity($user->getKey())); // an instance of UserEntityInterface
101
102
        // Once the user has approved or denied the client update the status
103
        // (true = approved, false = denied)
104
        $authRequest->setAuthorizationApproved(true);
105
106
        // Return the HTTP redirect response
107
        return $this->server->completeAuthorizationRequest($authRequest, $this->response);
0 ignored issues
show
Documentation introduced by
$this->response is of type object<CodexShaper\OAuth...sponses\ServerResponse>, but the function expects a object<Psr\Http\Message\ResponseInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
108
    }
109
110
    /**
111
     * Deny the authorization request.
112
     *
113
     * @return void
114
     */
115
    public function deny()
116
    {
117
    }
118
119
    /**
120
     * Filter all scopes.
121
     *
122
     * @param \League\OAuth2\Server\RequestTypes\AuthorizationRequest $authRequest
123
     *
124
     * @return array
125
     */
126
    public function filterScopes($authRequest)
127
    {
128
        return array_filter($authRequest->getScopes(), function ($scope) {
129
            if (Manager::isValidateScope($scope->getIdentifier())) {
130
                return $scope->getIdentifier();
131
            }
132
        });
133
    }
134
}
135