Completed
Pull Request — master (#774)
by
unknown
12:17
created

OAuthMiddleware::validateCombinations()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 3
cp 0
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
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
        $scopeCombinations = [];
66
67
        if (!is_null($scopesString)) {
68
            // We extract all possible scopes combinations, its required to meet at least one.
69
            $scopeCombinations = explode('|', $scopesString);
70
        }
71
72
        $this->authorizer->setRequest($request);
73
74
        $this->authorizer->validateAccessToken($this->httpHeadersOnly);
75
        $this->validateCombinations($scopeCombinations);
76
77
        return $next($request);
78
    }
79
80
    /**
81
     * Validate the scopes.
82
     *
83
     * @param $scopes
84
     *
85
     * @throws \League\OAuth2\Server\Exception\InvalidScopeException
86
     */
87
    public function validateCombinations($combinations)
88
    {
89
        if (!empty($combinations)) {
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