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 | * Create a new instance of the Authroization middleware. |
||
33 | * |
||
34 | * @param OAuth2\Server $server The configured OAuth2 server. |
||
35 | * @param array $scopes Scopes required for authorization. $scopes can be given as an array of arrays. |
||
36 | * OR logic will use with each grouping. |
||
37 | * Example: Given ['superUser', ['basicUser', 'aPermission']], the request will be |
||
38 | * verified if the request token has 'superUser' scope OR 'basicUser' and |
||
39 | * 'aPermission' as its scope. |
||
40 | */ |
||
41 | public function __construct(OAuth2\Server $server, array $scopes = []) |
||
46 | |||
47 | /** |
||
48 | * Execute this middleware as a function. |
||
49 | * |
||
50 | * @param ServerRequestInterface $request The PSR7 request. |
||
51 | * @param ResponseInterface $response The PSR7 response. |
||
52 | * @param callable $next The Next middleware. |
||
53 | * |
||
54 | * @return ResponseInterface |
||
55 | */ |
||
56 | public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) |
||
57 | { |
||
58 | $handler = new class implements RequestHandlerInterface |
||
59 | { |
||
60 | public $next; |
||
61 | public $response; |
||
62 | |||
63 | /** |
||
64 | * Handle the request and return a response. |
||
65 | * |
||
66 | * @param ServerRequestInterface $request The request to handle. |
||
67 | * |
||
68 | * @return ResponseInterface |
||
69 | */ |
||
70 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
81 | |||
82 | /** |
||
83 | * Execute this middleware. |
||
84 | * |
||
85 | * @param ServerRequestInterface $request The PSR-7 request. |
||
86 | * @param RequestHandlerInterface $handler The PSR-15 request handler. |
||
87 | * |
||
88 | * @return ResponseInterface |
||
89 | */ |
||
90 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface |
||
108 | |||
109 | /** |
||
110 | * Returns a callable function to be used as a authorization middleware with a specified scope. |
||
111 | * |
||
112 | * @param array $scopes Scopes require for authorization. |
||
113 | * |
||
114 | * @return Authorization |
||
115 | */ |
||
116 | public function withRequiredScope(array $scopes) |
||
122 | |||
123 | /** |
||
124 | * Helper method to ensure given scopes are formatted properly. |
||
125 | * |
||
126 | * @param array $scopes Scopes required for authorization. |
||
127 | * |
||
128 | * @return array The formatted scopes array. |
||
129 | */ |
||
130 | private function formatScopes(array $scopes) |
||
147 | } |
||
148 |