Passed
Push — master ( 0b391b...60c676 )
by Pauli
01:56
created

AmpacheMiddleware::errorResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * ownCloud - Music app
5
 *
6
 * This file is licensed under the Affero General Public License version 3 or
7
 * later. See the COPYING file.
8
 *
9
 * @author Morris Jobke <[email protected]>
10
 * @author Pauli Järvinen <[email protected]>
11
 * @copyright Morris Jobke 2013, 2014
12
 * @copyright Pauli Järvinen 2018 - 2020
13
 */
14
15
namespace OCA\Music\Middleware;
16
17
use \OCP\IRequest;
0 ignored issues
show
Bug introduced by
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. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use \OCP\AppFramework\Middleware;
0 ignored issues
show
Bug introduced by
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. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
20
use \OCA\Music\AppFramework\BusinessLayer\BusinessLayerException;
21
use \OCA\Music\AppFramework\Utility\MethodAnnotationReader;
22
use \OCA\Music\Db\AmpacheSessionMapper;
23
use \OCA\Music\Http\XMLResponse;
24
25
/**
26
 * Used to do the authentication and checking stuff for an ampache controller method
27
 * It reads out the annotations of a controller method and checks which if
28
 * ampache authentification stuff has to be done.
29
 */
30
class AmpacheMiddleware extends Middleware {
31
	private $appname;
32
	private $request;
33
	private $ampacheSessionMapper;
34
	private $isAmpacheCall;
35
	private $ampacheUser;
36
37
	/**
38
	 * @param Request $request an instance of the request
0 ignored issues
show
Bug introduced by
The type OCA\Music\Middleware\Request 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. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
39
	 */
40
	public function __construct($appname, IRequest $request, AmpacheSessionMapper $ampacheSessionMapper, $ampacheUser) {
41
		$this->appname = $appname;
42
		$this->request = $request;
43
		$this->ampacheSessionMapper = $ampacheSessionMapper;
44
45
		// used to share user info with controller
46
		$this->ampacheUser = $ampacheUser;
47
	}
48
49
	/**
50
	 * This runs all the security checks before a method call. The
51
	 * security checks are determined by inspecting the controller method
52
	 * annotations
53
	 * @param Controller $controller the controller that is being called
0 ignored issues
show
Bug introduced by
The type OCA\Music\Middleware\Controller 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. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
54
	 * @param string $methodName the name of the method
55
	 * @throws AmpacheException when a security check fails
56
	 */
57
	public function beforeController($controller, $methodName) {
58
59
		// get annotations from comments
60
		$annotationReader = new MethodAnnotationReader($controller, $methodName);
61
62
		$this->isAmpacheCall = $annotationReader->hasAnnotation('AmpacheAPI');
63
64
		// don't try to authenticate for the handshake request
65
		if ($this->isAmpacheCall && $this->request['action'] !== 'handshake') {
66
			$token = null;
67
			if (!empty($this->request['auth'])) {
68
				$token = $this->request['auth'];
69
			} elseif (!empty($this->request['ssid'])) {
70
				$token = $this->request['ssid'];
71
			}
72
73
			if ($token !== null && $token !== '') {
74
				$user = $this->ampacheSessionMapper->findByToken($token);
75
				if ($user !== false && \array_key_exists('user_id', $user)) {
76
					$this->ampacheUser->setUserId($user['user_id']);
77
					return;
78
				}
79
			} else {
80
				// for ping action without token the version information is provided
81
				if ($this->request['action'] === 'ping') {
82
					return;
83
				}
84
			}
85
			throw new AmpacheException('Invalid Login', 401);
86
		}
87
	}
88
89
	/**
90
	 * If an AmpacheException is being caught, the appropiate ampache
91
	 * exception response is rendered
92
	 * @param Controller $controller the controller that is being called
93
	 * @param string $methodName the name of the method that will be called on
94
	 *                           the controller
95
	 * @param \Exception $exception the thrown exception
96
	 * @throws \Exception the passed in exception if it wasn't handled
97
	 * @return Response a Response object if the exception was handled
0 ignored issues
show
Bug introduced by
The type OCA\Music\Middleware\Response 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. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
98
	 */
99
	public function afterException($controller, $methodName, \Exception $exception) {
100
		if ($this->isAmpacheCall) {
101
			if ($exception instanceof AmpacheException) {
102
				return self::errorResponse($exception->getCode(), $exception->getMessage());
103
			}
104
			elseif ($exception instanceof BusinessLayerException) {
105
				return self::errorResponse(400, 'Entity not found');
106
			}
107
		}
108
		throw $exception;
109
	}
110
111
	private static function errorResponse($code, $message) {
112
		return new XMLResponse(['root' => [
113
			'error' => [
114
				'code' => $code,
115
				'value' => $message
116
			]
117
		]]);
118
	}
119
}
120