AuthorizationController::authorize()   B
last analyzed

Complexity

Conditions 5
Paths 14

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 41
ccs 0
cts 17
cp 0
rs 8.9528
c 0
b 0
f 0
cc 5
nc 14
nop 2
crap 30
1
<?php
2
3
namespace CodexShaper\OAuth2\Server\Http\Controllers;
4
5
use Auth;
6
use CodexShaper\OAuth2\Server\Entities\User as UserEntity;
7
use CodexShaper\OAuth2\Server\Manager;
8
use CodexShaper\OAuth2\Server\Model;
9
use CodexShaper\OAuth2\Server\Models\User;
10
use Illuminate\Http\Request;
11
use League\OAuth2\Server\Exception\OAuthServerException;
12
use Psr\Http\Message\ResponseInterface;
13
use Psr\Http\Message\ServerRequestInterface;
14
15
class AuthorizationController
16
{
17
    public function authorize(ServerRequestInterface $request, ResponseInterface $response)
18
    {
19
        try {
20
            // Get current user
21
            $user = Auth::User();
22
            // If user not loggedin then redirect for authenticate
23
            if (!$user) {
24
                return redirect()->route('login');
25
            }
26
27
            $manager = new Manager();
28
            $server = $manager->makeAuthorizationServer();
29
30
            // Validate the HTTP request and return an AuthorizationRequest object.
31
            $authRequest = $server->validateAuthorizationRequest($request);
32
33
            // Get all validate scopes from psr request
34
            $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...
35
36
            // Get token for current user and request client id
37
            $token = Model::findToken('clientModel', $authRequest, $user);
38
39
            if (($token) || Model::instance('clientModel')->isSkipsAuthorization()) {
40
                // Once the user has logged in set the user on the AuthorizationRequest
41
                $authRequest->setUser(new UserEntity($user->getKey())); // an instance of UserEntityInterface
42
43
                // Once the user has approved or denied the client update the status
44
                // (true = approved, false = denied)
45
                $authRequest->setAuthorizationApproved(true);
46
47
                // Return the HTTP redirect response
48
                return $server->completeAuthorizationRequest($authRequest, $response);
49
            }
50
51
            echo '<p>Hello</p>';
52
        } catch (OAuthServerException $exception) {
53
54
            // All instances of OAuthServerException can be formatted into a HTTP response
55
            return $exception->generateHttpResponse($response);
56
        }
57
    }
58
59
    public function filterScopes($authRequest)
60
    {
61
        return array_filter($authRequest->getScopes(), function ($scope) {
62
            if (Manager::isValidateScope($scope->getIdentifier())) {
63
                return $scope->getIdentifier();
64
            }
65
        });
66
    }
67
}
68