1 | <?php |
||
2 | /** |
||
3 | * Nextcloud - Gallery |
||
4 | * |
||
5 | * This file is licensed under the Affero General Public License version 3 or |
||
6 | * later. See the COPYING file. |
||
7 | * |
||
8 | * @author Olivier Paroz <[email protected]> |
||
9 | * @author Bernhard Posselt <[email protected]> |
||
10 | * |
||
11 | * @copyright Olivier Paroz 2017 |
||
12 | * @copyright Bernhard Posselt 2017 |
||
13 | */ |
||
14 | |||
15 | namespace OCA\Gallery\Middleware; |
||
16 | |||
17 | use OCP\IURLGenerator; |
||
0 ignored issues
–
show
|
|||
18 | use OCP\IRequest; |
||
0 ignored issues
–
show
The type
OCP\IRequest was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
19 | use OCP\ILogger; |
||
0 ignored issues
–
show
The type
OCP\ILogger was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
20 | |||
21 | use OCP\AppFramework\Http\JSONResponse; |
||
0 ignored issues
–
show
The type
OCP\AppFramework\Http\JSONResponse was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
22 | use OCP\AppFramework\Http\RedirectResponse; |
||
0 ignored issues
–
show
The type
OCP\AppFramework\Http\RedirectResponse was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
23 | use OCP\AppFramework\Http\TemplateResponse; |
||
0 ignored issues
–
show
The type
OCP\AppFramework\Http\TemplateResponse was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
24 | use OCP\AppFramework\Middleware; |
||
0 ignored issues
–
show
The type
OCP\AppFramework\Middleware was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
25 | |||
26 | /** |
||
27 | * Checks that we have a valid token linked to a valid resource and that the |
||
28 | * user is authorised to access it |
||
29 | * |
||
30 | * @package OCA\Gallery\Middleware |
||
31 | */ |
||
32 | abstract class CheckMiddleware extends Middleware { |
||
33 | |||
34 | /** @var string */ |
||
35 | protected $appName; |
||
36 | /** @var IRequest */ |
||
37 | protected $request; |
||
38 | /** @var IURLGenerator */ |
||
39 | private $urlGenerator; |
||
40 | /** @var ILogger */ |
||
41 | protected $logger; |
||
42 | |||
43 | /*** |
||
44 | * Constructor |
||
45 | * |
||
46 | * @param string $appName |
||
47 | * @param IRequest $request |
||
48 | * @param IURLGenerator $urlGenerator |
||
49 | * @param ILogger $logger |
||
50 | */ |
||
51 | public function __construct( |
||
52 | $appName, |
||
53 | IRequest $request, |
||
54 | IURLGenerator $urlGenerator, |
||
55 | ILogger $logger |
||
56 | ) { |
||
57 | $this->appName = $appName; |
||
58 | $this->request = $request; |
||
59 | $this->urlGenerator = $urlGenerator; |
||
60 | $this->logger = $logger; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * If a CheckException is being caught, clients who sent an ajax requests |
||
65 | * get a JSON error response while the others are redirected to an error |
||
66 | * page |
||
67 | * |
||
68 | * @inheritDoc |
||
69 | */ |
||
70 | public function afterException($controller, $methodName, \Exception $exception) { |
||
71 | if ($exception instanceof CheckException) { |
||
72 | $message = $exception->getMessage(); |
||
73 | $code = $exception->getCode(); |
||
74 | |||
75 | $this->logger->debug("[TokenCheckException] {exception}", ['exception' => $message]); |
||
76 | |||
77 | return $this->computeResponse($message, $code); |
||
78 | } |
||
79 | |||
80 | throw $exception; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Decides which type of response to send |
||
85 | * |
||
86 | * @param string $message |
||
87 | * @param int $code |
||
88 | * |
||
89 | * @return JSONResponse|RedirectResponse|TemplateResponse |
||
90 | */ |
||
91 | private function computeResponse($message, $code) { |
||
92 | $acceptHtml = stripos($this->request->getHeader('Accept'), 'html'); |
||
93 | if ($acceptHtml === false) { |
||
94 | $response = $this->sendJsonResponse($message, $code); |
||
95 | } else { |
||
96 | $response = $this->sendHtmlResponse($message, $code); |
||
97 | } |
||
98 | |||
99 | return $response; |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * Redirects the client to an error page or shows an authentication form |
||
104 | * |
||
105 | * @param string $message |
||
106 | * @param int $code |
||
107 | * |
||
108 | * @return RedirectResponse|TemplateResponse |
||
109 | */ |
||
110 | private function sendHtmlResponse($message, $code) { |
||
111 | $this->logger->debug("[CheckException] HTML response"); |
||
112 | |||
113 | /** |
||
114 | * We need to render a template for 401 or we'll have an endless loop as |
||
115 | * this is called before the controller gets a chance to render anything |
||
116 | */ |
||
117 | if ($code === 401) { |
||
118 | $response = $this->sendHtml401(); |
||
119 | } else { |
||
120 | $response = $this->redirectToErrorPage($message, $code); |
||
121 | } |
||
122 | |||
123 | return $response; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Shows an authentication form |
||
128 | * |
||
129 | * @return TemplateResponse |
||
130 | */ |
||
131 | private function sendHtml401() { |
||
132 | $params = $this->request->getParams(); |
||
133 | |||
134 | $this->logger->debug( |
||
135 | '[CheckException] Unauthorised Request params: {params}', |
||
136 | ['params' => $params] |
||
137 | ); |
||
138 | |||
139 | return new TemplateResponse($this->appName, 'authenticate', $params, 'guest'); |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * Redirects the client to an error page |
||
144 | * |
||
145 | * @param string $message |
||
146 | * @param int $code |
||
147 | * |
||
148 | * @return RedirectResponse |
||
149 | */ |
||
150 | private function redirectToErrorPage($message, $code) { |
||
151 | $url = $this->urlGenerator->linkToRoute( |
||
152 | $this->appName . '.page.error_page', ['code' => $code] |
||
153 | ); |
||
154 | |||
155 | $response = new RedirectResponse($url); |
||
156 | $response->addCookie('galleryErrorMessage', $message); |
||
157 | |||
158 | return $response; |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * Returns a JSON response to the client |
||
163 | * |
||
164 | * @param string $message |
||
165 | * @param int $code |
||
166 | * |
||
167 | * @return JSONResponse |
||
168 | */ |
||
169 | private function sendJsonResponse($message, $code) { |
||
170 | $this->logger->debug("[TokenCheckException] JSON response"); |
||
171 | |||
172 | $jsonData = [ |
||
173 | 'message' => $message, |
||
174 | 'success' => false |
||
175 | ]; |
||
176 | |||
177 | return new JSONResponse($jsonData, $code); |
||
178 | } |
||
179 | |||
180 | } |
||
181 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths