Authorization   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 86
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A withRequiredScope() 0 6 1
A __invoke() 0 15 3
A formatScopes() 0 13 3
1
<?php
2
3
namespace App\Middleware;
4
5
use App\Model\User;
6
use Awurth\Slim\Helper\Exception\UnauthorizedException;
7
use Cartalyst\Sentinel\Sentinel;
8
use Chadicus\Slim\OAuth2\Http\RequestBridge;
9
use OAuth2\Server;
10
use Slim\Http\Request;
11
use Slim\Http\Response;
12
13
class Authorization implements MiddlewareInterface
14
{
15
    /**
16
     * @var array
17
     */
18
    protected $scopes;
19
20
    /**
21
     * @var Sentinel
22
     */
23
    protected $sentinel;
24
25
    /**
26
     * @var Server;
27
     */
28
    protected $server;
29
30
    /**
31
     * Constructor.
32
     *
33
     * @param Server   $server
34
     * @param Sentinel $sentinel
35
     * @param array    $scopes
36
     */
37
    public function __construct(Server $server, Sentinel $sentinel, array $scopes = [])
38
    {
39
        $this->server = $server;
40
        $this->sentinel = $sentinel;
41
        $this->scopes = $this->formatScopes($scopes);
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function __invoke(Request $request, Response $response, callable $next)
48
    {
49
        $oauth2Request = RequestBridge::toOAuth2($request);
50
        foreach ($this->scopes as $scope) {
51
            if ($this->server->verifyResourceRequest($oauth2Request, null, $scope)) {
52
                $token = $this->server->getResourceController()->getToken();
0 ignored issues
show
Bug introduced by
The method getToken() does not exist on OAuth2\Controller\ResourceControllerInterface. It seems like you code against a sub-type of OAuth2\Controller\ResourceControllerInterface such as OAuth2\Controller\ResourceController. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
                $token = $this->server->getResourceController()->/** @scrutinizer ignore-call */ getToken();
Loading history...
53
                $user = User::find($token['user_id']);
54
55
                $this->sentinel->stateless($user);
0 ignored issues
show
Bug introduced by
It seems like $user can also be of type Illuminate\Database\Eloquent\Model; however, parameter $credentials of Cartalyst\Sentinel\Sentinel::stateless() does only seem to accept Cartalyst\Sentinel\Users\UserInterface|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

55
                $this->sentinel->stateless(/** @scrutinizer ignore-type */ $user);
Loading history...
56
57
                return $next($request, $response);
58
            }
59
        }
60
61
        throw new UnauthorizedException($request, $response);
62
    }
63
64
    /**
65
     * Returns a callable function to be used as a authorization middleware with a specified scope.
66
     *
67
     * @param array $scopes Scopes require for authorization.
68
     *
69
     * @return Authorization
70
     */
71
    public function withRequiredScope(array $scopes)
72
    {
73
        $clone = clone $this;
74
        $clone->scopes = $clone->formatScopes($scopes);
75
76
        return $clone;
77
    }
78
79
    /**
80
     * Helper method to ensure given scopes are formatted properly.
81
     *
82
     * @param array $scopes Scopes required for authorization.
83
     *
84
     * @return array The formatted scopes array.
85
     */
86
    protected function formatScopes(array $scopes)
87
    {
88
        if (empty($scopes)) {
89
            return [null];
90
        }
91
92
        array_walk($scopes, function (&$scope) {
93
            if (is_array($scope)) {
94
                $scope = implode(' ', $scope);
95
            }
96
        });
97
98
        return $scopes;
99
    }
100
}
101