Failed Conditions
Pull Request — master (#17)
by Chad
02:09
created

Authorization::__invoke()   B

Complexity

Conditions 5
Paths 10

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 22
rs 8.6737
cc 5
eloc 12
nc 10
nop 3
1
<?php
2
namespace Chadicus\Slim\OAuth2\Middleware;
3
4
use Chadicus\Slim\OAuth2\Http\RequestBridge;
5
use Chadicus\Slim\OAuth2\Http\ResponseBridge;
6
use Psr\Http\Message\ServerRequestInterface;
7
use Psr\Http\Message\ResponseInterface;
8
use OAuth2;
9
use Slim;
10
11
/**
12
 * Slim Middleware to handle OAuth2 Authorization.
13
 */
14
class Authorization implements MiddlewareInterface
15
{
16
    /**
17
     * Slim App
18
     *
19
     * @var Slim\App
20
     */
21
    private $slim;
22
23
    /**
24
     * OAuth2 Server
25
     *
26
     * @var OAuth2\Server
27
     */
28
    private $server;
29
30
    /**
31
     * Array of scopes required for authorization.
32
     *
33
     * @var array
34
     */
35
    private $scopes;
36
37
    /**
38
     * Create a new instance of the Authroization middleware.
39
     *
40
     * @param Slim\App      $slim   The slim framework application instance.
41
     * @param OAuth2\Server $server The configured OAuth2 server.
42
     * @param array         $scopes Scopes required for authorization. $scopes can be given as an array of arrays. OR
43
     *                              logic will use with each grouping.  Example:
44
     *                              Given ['superUser', ['basicUser', 'aPermission']], the request will be verified if
45
     *                              the request token has 'superUser' scope OR 'basicUser' and 'aPermission' as its
46
     *                              scope.
47
     */
48
    public function __construct(Slim\App $slim, OAuth2\Server $server, array $scopes = [])
49
    {
50
        $this->slim = $slim;
51
        $this->server = $server;
52
        $this->scopes = $scopes;
53
    }
54
55
    /**
56
     * Execute this middleware.
57
     *
58
     * @param  ServerRequestInterface $request  The PSR7 request.
59
     * @param  ResponseInterface      $response The PSR7 response.
60
     * @param  callable               $next     The Next middleware.
61
     *
62
     * @return Slim\Http\Response
63
     */
64
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
65
    {
66
        $oauth2Request = RequestBridge::toOAuth2($request);
67
68
        $scopes = $this->scopes;
69
        if (empty($scopes)) {
70
            $scopes = [null]; //use at least 1 null scope
71
        }
72
73
        foreach ($scopes as $scope) {
74
            if (is_array($scope)) {
75
                $scope = implode(' ', $scope);
76
            }
77
78
            if ($this->server->verifyResourceRequest($oauth2Request, null, $scope)) {
79
                $this->slim->getContainer()->token = $this->server->getResourceController()->getToken();
0 ignored issues
show
Bug introduced by
Accessing token on the interface Interop\Container\ContainerInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
80
                return $next($request, $response);
81
            }
82
        }
83
84
        return ResponseBridge::fromOAuth2($this->server->getResponse());
0 ignored issues
show
Compatibility introduced by
$this->server->getResponse() of type object<OAuth2\ResponseInterface> is not a sub-type of object<OAuth2\Response>. It seems like you assume a concrete implementation of the interface OAuth2\ResponseInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
85
    }
86
87
    /**
88
     * Returns a callable function to be used as a authorization middleware with a specified scope.
89
     *
90
     * @param array $scopes Scopes require for authorization.
91
     *
92
     * @return Authorization
93
     */
94
    public function withRequiredScope(array $scopes)
95
    {
96
        $clone = clone $this;
97
        $clone->scopes = $scopes;
98
        return $clone;
99
    }
100
}
101