chadicus /
slim-oauth2-middleware
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 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
|
|||
| 80 | return $next($request, $response); |
||
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 84 | return ResponseBridge::fromOAuth2($this->server->getResponse()); |
||
|
0 ignored issues
–
show
$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 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: