1 | <?php |
||
15 | class Authorization implements MiddlewareInterface |
||
16 | { |
||
17 | /** |
||
18 | * OAuth2 Server |
||
19 | * |
||
20 | * @var OAuth2\Server |
||
21 | */ |
||
22 | private $server; |
||
23 | |||
24 | /** |
||
25 | * Array of scopes required for authorization. |
||
26 | * |
||
27 | * @var array |
||
28 | */ |
||
29 | private $scopes; |
||
30 | |||
31 | /** |
||
32 | * Container for token. |
||
33 | * |
||
34 | * @var ArrayAccess|ContainerInterface |
||
35 | */ |
||
36 | private $container; |
||
37 | |||
38 | /** |
||
39 | * Create a new instance of the Authroization middleware. |
||
40 | * |
||
41 | * @param OAuth2\Server $server The configured OAuth2 server. |
||
42 | * @param ArrayAccess|ContainerInterface $container A container object in which to store the token from the request. |
||
43 | * @param array $scopes Scopes required for authorization. $scopes can be given as an array of |
||
44 | * arrays. OR logic will use with each grouping. |
||
45 | * Example: |
||
46 | * Given ['superUser', ['basicUser', 'aPermission']], the request will be |
||
47 | * verified if the request token has 'superUser' scope OR 'basicUser' and |
||
48 | * 'aPermission' as its scope. |
||
49 | * |
||
50 | * @throws \InvalidArgumentException Thrown if $container is not an instance of ArrayAccess or ContainerInterface. |
||
51 | */ |
||
52 | public function __construct(OAuth2\Server $server, $container, array $scopes = []) |
||
64 | |||
65 | /** |
||
66 | * Execute this middleware. |
||
67 | * |
||
68 | * @param ServerRequestInterface $request The PSR7 request. |
||
69 | * @param ResponseInterface $response The PSR7 response. |
||
70 | * @param callable $next The Next middleware. |
||
71 | * |
||
72 | * @return ResponseInterface |
||
73 | */ |
||
74 | public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) |
||
92 | |||
93 | /** |
||
94 | * Helper method to set the token value in the container instance. |
||
95 | * |
||
96 | * @param array $token The token from the incoming request. |
||
97 | * |
||
98 | * @return void |
||
99 | */ |
||
100 | private function setToken(array $token) |
||
109 | |||
110 | /** |
||
111 | * Returns a callable function to be used as a authorization middleware with a specified scope. |
||
112 | * |
||
113 | * @param array $scopes Scopes require for authorization. |
||
114 | * |
||
115 | * @return Authorization |
||
116 | */ |
||
117 | public function withRequiredScope(array $scopes) |
||
123 | |||
124 | /** |
||
125 | * Helper method to ensure given scopes are formatted properly. |
||
126 | * |
||
127 | * @param array $scopes Scopes required for authorization. |
||
128 | * |
||
129 | * @return array The formatted scopes array. |
||
130 | */ |
||
131 | private function formatScopes(array $scopes) |
||
148 | } |
||
149 |