Completed
Pull Request — master (#705)
by
unknown
64:42
created

OAuthMiddleware::validateCombinations()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 15
ccs 0
cts 3
cp 0
rs 9.2
cc 4
eloc 8
nc 4
nop 1
crap 20
1
<?php
2
3
/*
4
 * This file is part of OAuth 2.0 Laravel.
5
 *
6
 * (c) Luca Degasperi <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace LucaDegasperi\OAuth2Server\Middleware;
13
14
use Closure;
15
use League\OAuth2\Server\Exception\InvalidScopeException;
16
use LucaDegasperi\OAuth2Server\Authorizer;
17
18
/**
19
 * This is the oauth middleware class.
20
 *
21
 * @author Luca Degasperi <[email protected]>
22
 */
23
class OAuthMiddleware
24
{
25
    /**
26
     * The Authorizer instance.
27
     *
28
     * @var \LucaDegasperi\OAuth2Server\Authorizer
29
     */
30
    protected $authorizer;
31
32
    /**
33
     * Whether or not to check the http headers only for an access token.
34
     *
35
     * @var bool
36
     */
37
    protected $httpHeadersOnly = false;
38
39
    /**
40
     * Create a new oauth middleware instance.
41
     *
42
     * @param \LucaDegasperi\OAuth2Server\Authorizer $authorizer
43
     * @param bool $httpHeadersOnly
44
     */
45
    public function __construct(Authorizer $authorizer, $httpHeadersOnly = false)
46
    {
47
        $this->authorizer = $authorizer;
48
        $this->httpHeadersOnly = $httpHeadersOnly;
49
    }
50
51
    /**
52
     * Handle an incoming request.
53
     *
54
     * @param \Illuminate\Http\Request $request
55
     * @param \Closure $next
56
     * @param string|null $scopesString
57
     *
58
     * @throws \League\OAuth2\Server\Exception\InvalidScopeException
59
     *
60
     * @return mixed
61
     */
62
    public function handle($request, Closure $next, $scopesString = null)
63
    {
64
        $scopes = [];
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...
65
66
        if (!is_null($scopesString)) {
67
68
            $scopeCombinations = explode('|', $scopesString);
69
        }
70
71
        $this->authorizer->setRequest($request);
72
73
        $this->authorizer->validateAccessToken($this->httpHeadersOnly);
74
        $this->validateCombinations($scopeCombinations);
0 ignored issues
show
Bug introduced by
The variable $scopeCombinations does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
75
76
        return $next($request);
77
    }
78
79
    /**
80
     * Validate the scopes.
81
     *
82
     * @param $scopes
83
     *
84
     * @throws \League\OAuth2\Server\Exception\InvalidScopeException
85
     */
86
    public function validateCombinations($combinations)
87
    {
88
        if (!empty($combinations)) {
89
            $combinationMeet = false;
0 ignored issues
show
Unused Code introduced by
$combinationMeet 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...
90
            foreach ($combinations as $index => $combination) {
91
                $scopes = explode('+', $combination);
92
                if($this->validateScopes($scopes))
93
                {
94
                    return true;
95
                }
96
            }
97
98
            throw new InvalidScopeException(implode(',', $combinations));
99
        }
100
    }
101
102
    /**
103
     * Validate the scopes.
104
     *
105
     * @param $scopes
106
     *
107
     * @throws \League\OAuth2\Server\Exception\InvalidScopeException
108
     */
109
    public function validateScopes($scopes)
110
    {
111
        if (!empty($scopes) && !$this->authorizer->hasScope($scopes)) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return !(!empty($scopes)...er->hasScope($scopes));.
Loading history...
112
            return false;
113
        }
114
        return true;
115
    }
116
}
117