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 ArrayAccess; |
||
| 5 | use Chadicus\Slim\OAuth2\Http\RequestBridge; |
||
| 6 | use Chadicus\Slim\OAuth2\Http\ResponseBridge; |
||
| 7 | use Psr\Http\Message\ServerRequestInterface; |
||
| 8 | use Psr\Http\Message\ResponseInterface; |
||
| 9 | use Psr\Http\Server\MiddlewareInterface; |
||
| 10 | use Psr\Http\Server\RequestHandlerInterface; |
||
| 11 | use Interop\Container\ContainerInterface; |
||
| 12 | use OAuth2; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Slim Middleware to handle OAuth2 Authorization. |
||
| 16 | */ |
||
| 17 | class Authorization implements MiddlewareInterface |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * OAuth2 Server |
||
| 21 | * |
||
| 22 | * @var OAuth2\Server |
||
| 23 | */ |
||
| 24 | private $server; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Array of scopes required for authorization. |
||
| 28 | * |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | private $scopes; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Container for token. |
||
| 35 | * |
||
| 36 | * @var ArrayAccess|ContainerInterface |
||
| 37 | */ |
||
| 38 | private $container; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Create a new instance of the Authroization middleware. |
||
| 42 | * |
||
| 43 | * @param OAuth2\Server $server The configured OAuth2 server. |
||
| 44 | * @param ArrayAccess|ContainerInterface $container A container object in which to store the token from the |
||
| 45 | * request. |
||
| 46 | * @param array $scopes Scopes required for authorization. $scopes can be given as an |
||
| 47 | * array of arrays. OR logic will use with each grouping. |
||
| 48 | * Example: |
||
| 49 | * Given ['superUser', ['basicUser', 'aPermission']], the request |
||
| 50 | * will be verified if the request token has 'superUser' scope |
||
| 51 | * OR 'basicUser' and 'aPermission' as its scope. |
||
| 52 | * |
||
| 53 | * @throws \InvalidArgumentException Thrown if $container is not an instance of ArrayAccess or ContainerInterface. |
||
| 54 | */ |
||
| 55 | public function __construct(OAuth2\Server $server, $container, array $scopes = []) |
||
| 56 | { |
||
| 57 | $this->server = $server; |
||
| 58 | if (!is_a($container, '\\ArrayAccess') && !is_a($container, '\\Interop\\Container\\ContainerInterface')) { |
||
| 59 | throw new \InvalidArgumentException( |
||
| 60 | '$container does not implement \\ArrayAccess or \\Interop\\Container\\ContainerInterface' |
||
| 61 | ); |
||
| 62 | } |
||
| 63 | |||
| 64 | $this->container = $container; |
||
| 65 | $this->scopes = $this->formatScopes($scopes); |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Execute this middleware as a function. |
||
| 70 | * |
||
| 71 | * @param ServerRequestInterface $request The PSR7 request. |
||
| 72 | * @param ResponseInterface $response The PSR7 response. |
||
| 73 | * @param callable $next The Next middleware. |
||
| 74 | * |
||
| 75 | * @return ResponseInterface |
||
| 76 | */ |
||
| 77 | public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) |
||
| 78 | { |
||
| 79 | $handler = new class implements RequestHandlerInterface |
||
|
0 ignored issues
–
show
|
|||
| 80 | { |
||
| 81 | public $next; |
||
| 82 | public $response; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Handle the request and return a response. |
||
| 86 | * |
||
| 87 | * @param ServerRequestInterface $request The request to handle. |
||
| 88 | * |
||
| 89 | * @return ResponseInterface |
||
| 90 | */ |
||
| 91 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
| 92 | { |
||
| 93 | return call_user_func_array($this->next, [$request, $this->response]); |
||
| 94 | } |
||
| 95 | }; |
||
| 96 | |||
| 97 | $handler->next = $next; |
||
| 98 | $handler->response = $response; |
||
| 99 | |||
| 100 | return $this->process($request, $handler); |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Execute this middleware. |
||
| 105 | * |
||
| 106 | * @param ServerRequestInterface $request The PSR-7 request. |
||
| 107 | * @param RequestHandlerInterface $handler The PSR-15 request handler. |
||
| 108 | * |
||
| 109 | * @return ResponseInterface |
||
| 110 | */ |
||
| 111 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface |
||
| 112 | { |
||
| 113 | $oauth2Request = RequestBridge::toOAuth2($request); |
||
| 114 | foreach ($this->scopes as $scope) { |
||
| 115 | if ($this->server->verifyResourceRequest($oauth2Request, null, $scope)) { |
||
| 116 | $this->setToken($this->server->getResourceController()->getToken()); |
||
| 117 | return $handler->handle($request); |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | $response = ResponseBridge::fromOauth2($this->server->getResponse()); |
||
| 122 | |||
| 123 | if ($response->hasHeader('Content-Type')) { |
||
| 124 | return $response; |
||
| 125 | } |
||
| 126 | |||
| 127 | return $response->withHeader('Content-Type', 'application/json'); |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Helper method to set the token value in the container instance. |
||
| 132 | * |
||
| 133 | * @param array $token The token from the incoming request. |
||
| 134 | * |
||
| 135 | * @return void |
||
| 136 | */ |
||
| 137 | private function setToken(array $token) |
||
| 138 | { |
||
| 139 | if (is_a($this->container, '\\ArrayAccess')) { |
||
| 140 | $this->container['token'] = $token; |
||
| 141 | return; |
||
| 142 | } |
||
| 143 | |||
| 144 | $this->container->set('token', $token); |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Returns a callable function to be used as a authorization middleware with a specified scope. |
||
| 149 | * |
||
| 150 | * @param array $scopes Scopes require for authorization. |
||
| 151 | * |
||
| 152 | * @return Authorization |
||
| 153 | */ |
||
| 154 | public function withRequiredScope(array $scopes) |
||
| 155 | { |
||
| 156 | $clone = clone $this; |
||
| 157 | $clone->scopes = $clone->formatScopes($scopes); |
||
| 158 | return $clone; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Helper method to ensure given scopes are formatted properly. |
||
| 163 | * |
||
| 164 | * @param array $scopes Scopes required for authorization. |
||
| 165 | * |
||
| 166 | * @return array The formatted scopes array. |
||
| 167 | */ |
||
| 168 | private function formatScopes(array $scopes) |
||
| 169 | { |
||
| 170 | if (empty($scopes)) { |
||
| 171 | return [null]; //use at least 1 null scope |
||
| 172 | } |
||
| 173 | |||
| 174 | array_walk( |
||
| 175 | $scopes, |
||
| 176 | function (&$scope) { |
||
| 177 | if (is_array($scope)) { |
||
| 178 | $scope = implode(' ', $scope); |
||
| 179 | } |
||
| 180 | } |
||
| 181 | ); |
||
| 182 | |||
| 183 | return $scopes; |
||
| 184 | } |
||
| 185 | } |
||
| 186 |
Classes in PHP are usually named in CamelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.
Thus the name database provider becomes
DatabaseProvider.