Completed
Pull Request — stable9 (#47)
by Olivier
08:29
created

CheckMiddleware::sendJsonResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 * ownCloud - galleryplus
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 2014-2015
12
 * @copyright Bernhard Posselt 2012-2015
13
 */
14
15
namespace OCA\GalleryPlus\Middleware;
16
17
use OCP\IURLGenerator;
18
use OCP\IRequest;
19
use OCP\ILogger;
20
21
use OCP\AppFramework\Http\JSONResponse;
22
use OCP\AppFramework\Http\RedirectResponse;
23
use OCP\AppFramework\Http\TemplateResponse;
24
use OCP\AppFramework\Middleware;
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\GalleryPlus\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 62
	public function __construct(
52
		$appName,
53
		IRequest $request,
54
		IURLGenerator $urlGenerator,
55
		ILogger $logger
56
	) {
57 62
		$this->appName = $appName;
58 62
		$this->request = $request;
59 62
		$this->urlGenerator = $urlGenerator;
60 62
		$this->logger = $logger;
61 62
	}
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 11
	public function afterException($controller, $methodName, \Exception $exception) {
71 11
		if ($exception instanceof CheckException) {
72 5
			$message = $exception->getMessage();
73 5
			$code = $exception->getCode();
74
75 5
			$this->logger->debug("[TokenCheckException] {exception}", ['exception' => $message]);
76
77 5
			return $this->computeResponse($message, $code);
78
		}
79
80 6
		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 5
	private function computeResponse($message, $code) {
92 5
		$acceptHtml = stripos($this->request->getHeader('Accept'), 'html');
93 5
		if ($acceptHtml === false) {
94 1
			$response = $this->sendJsonResponse($message, $code);
95
		} else {
96 4
			$response = $this->sendHtmlResponse($message, $code);
97
		}
98
99 5
		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 4
	private function sendHtmlResponse($message, $code) {
111 4
		$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 4
		if ($code === 401) {
118 1
			$response = $this->sendHtml401();
119
		} else {
120 3
			$response = $this->redirectToErrorPage($message, $code);
121
		}
122
123 4
		return $response;
124
	}
125
126
	/**
127
	 * Shows an authentication form
128
	 *
129
	 * @return TemplateResponse
130
	 */
131 1
	private function sendHtml401() {
132 1
		$params = $this->request->getParams();
133
134 1
		$this->logger->debug(
135 1
			'[CheckException] Unauthorised Request params: {params}',
136 1
			['params' => $params]
137
		);
138
139 1
		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 3
	private function redirectToErrorPage($message, $code) {
151 3
		$url = $this->urlGenerator->linkToRoute(
152 3
			$this->appName . '.page.error_page', ['code' => $code]
153
		);
154
155 3
		$response = new RedirectResponse($url);
156 3
		$response->addCookie('galleryErrorMessage', $message);
157
158 3
		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 1
	private function sendJsonResponse($message, $code) {
170 1
		$this->logger->debug("[TokenCheckException] JSON response");
171
172
		$jsonData = [
173 1
			'message' => $message,
174
			'success' => false
175
		];
176
177 1
		return new JSONResponse($jsonData, $code);
178
	}
179
180
}
181