1 | <?php |
||
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 |
||
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 |
||
95 | }; |
||
96 | |||
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 |
||
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) |
||
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) |
||
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) |
||
185 | } |
||
186 |