Completed
Push — master ( 74bd2c...830834 )
by Thomas
09:42
created

PublicAuth::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 9.4285
1
<?php
2
/**
3
 * @author Thomas Müller <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2016, ownCloud, Inc.
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
namespace OCA\DAV\DAV;
22
23
use Sabre\DAV\Auth\Backend\BackendInterface;
24
use Sabre\HTTP\RequestInterface;
25
use Sabre\HTTP\ResponseInterface;
26
27
class PublicAuth implements BackendInterface {
28
29
	/** @var string[] */
30
	private $publicURLs;
31
32
	/**
33
	 * @param string[] $publicURLs
0 ignored issues
show
Bug introduced by
There is no parameter named $publicURLs. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
34
	 */
35
	public function __construct() {
36
		$this->publicURLs = [
37
			'public-calendars',
38
			'principals/system/public'
39
		];
40
	}
41
42
	/**
43
	 * When this method is called, the backend must check if authentication was
44
	 * successful.
45
	 *
46
	 * The returned value must be one of the following
47
	 *
48
	 * [true, "principals/username"]
49
	 * [false, "reason for failure"]
50
	 *
51
	 * If authentication was successful, it's expected that the authentication
52
	 * backend returns a so-called principal url.
53
	 *
54
	 * Examples of a principal url:
55
	 *
56
	 * principals/admin
57
	 * principals/user1
58
	 * principals/users/joe
59
	 * principals/uid/123457
60
	 *
61
	 * If you don't use WebDAV ACL (RFC3744) we recommend that you simply
62
	 * return a string such as:
63
	 *
64
	 * principals/users/[username]
65
	 *
66
	 * @param RequestInterface $request
67
	 * @param ResponseInterface $response
68
	 * @return array
69
	 */
70
	function check(RequestInterface $request, ResponseInterface $response) {
71
72
		if ($this->isRequestPublic($request)) {
73
			return [true, "principals/system/public"];
74
		}
75
		return [false, "No public access to this resource."];
76
	}
77
78
	/**
79
	 * @inheritdoc
80
	 */
81
	function challenge(RequestInterface $request, ResponseInterface $response) {
82
	}
83
84
	/**
85
	 * @param RequestInterface $request
86
	 * @return bool
87
	 */
88
	private function isRequestPublic(RequestInterface $request) {
89
		$params = $request->getQueryParameters();
90
		if (isset($params['sabreAction']) && $params['sabreAction'] == 'asset') {
91
			return true;
92
		}
93
		$url = $request->getPath();
94
		$matchingUrls = array_filter($this->publicURLs, function ($publicUrl) use ($url) {
95
			return strpos($url, $publicUrl, 0) === 0;
96
		});
97
		return !empty($matchingUrls);
98
	}
99
}
100