These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 PSR7 request |
||
59 | * @param ResponseInterface $response PSR7 response |
||
60 | * @param callable $next Next middleware |
||
61 | * |
||
62 | * @return ResponseInterface |
||
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(); |
||
80 | return $next($request, $response); |
||
81 | } |
||
82 | } |
||
83 | |||
84 | return ResponseBridge::fromOAuth2($this->server->getResponse()); |
||
0 ignored issues
–
show
|
|||
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 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.