Auth::isDavAuthenticated()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 2
eloc 3
nc 2
nop 1
crap 6
1
<?php
2
/**
3
 * @author Arthur Schiwon <[email protected]>
4
 * @author Bart Visscher <[email protected]>
5
 * @author Christian Seiler <[email protected]>
6
 * @author Jakob Sack <[email protected]>
7
 * @author Lukas Reschke <[email protected]>
8
 * @author Markus Goetz <[email protected]>
9
 * @author Michael Gapczynski <[email protected]>
10
 * @author Morris Jobke <[email protected]>
11
 * @author Thomas Müller <[email protected]>
12
 * @author Vincent Petry <[email protected]>
13
 *
14
 * @copyright Copyright (c) 2015, ownCloud, Inc.
15
 * @license AGPL-3.0
16
 *
17
 * This code is free software: you can redistribute it and/or modify
18
 * it under the terms of the GNU Affero General Public License, version 3,
19
 * as published by the Free Software Foundation.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24
 * GNU Affero General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Affero General Public License, version 3,
27
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
28
 *
29
 */
30
namespace OCA\Contacts\Sabre;
31
32
use Exception;
33
use OCP\ISession;
34
use OCP\IUserSession;
35
use Sabre\DAV\Auth\Backend\AbstractBasic;
36
use Sabre\DAV\Exception\NotAuthenticated;
37
use Sabre\DAV\Exception\ServiceUnavailable;
38
39
class Auth extends AbstractBasic {
40
	const DAV_AUTHENTICATED = 'AUTHENTICATED_TO_DAV_BACKEND';
41
42
	/** @var ISession */
43
	private $session;
44
	/** @var IUserSession */
45
	private $userSession;
46
47
	/**
48
	 * @param ISession $session
49
	 * @param IUserSession $userSession
50
	 */
51
	public function __construct(ISession $session,
52
								IUserSession $userSession) {
53
		$this->session = $session;
54
		$this->userSession = $userSession;
55
	}
56
57
	/**
58
	 * Whether the user has initially authenticated via DAV
59
	 *
60
	 * This is required for WebDAV clients that resent the cookies even when the
61
	 * account was changed.
62
	 *
63
	 * @see https://github.com/owncloud/core/issues/13245
64
	 *
65
	 * @param string $username
66
	 * @return bool
67
	 */
68
	protected function isDavAuthenticated($username) {
69
		return !is_null($this->session->get(self::DAV_AUTHENTICATED)) &&
70
		$this->session->get(self::DAV_AUTHENTICATED) === $username;
71
	}
72
73
	/**
74
	 * Validates a username and password
75
	 *
76
	 * This method should return true or false depending on if login
77
	 * succeeded.
78
	 *
79
	 * @param string $username
80
	 * @param string $password
81
	 * @return bool
82
	 */
83
	protected function validateUserPass($username, $password) {
84
		if ($this->userSession->isLoggedIn() &&
85
			$this->isDavAuthenticated($this->userSession->getUser()->getUID())
86
		) {
87
			\OC_Util::setupFS($this->userSession->getUser()->getUID());
88
			$this->session->close();
89
			return true;
90
		} else {
91
			\OC_Util::setUpFS(); //login hooks may need early access to the filesystem
92
			if($this->userSession->login($username, $password)) {
93
				\OC_Util::setUpFS($this->userSession->getUser()->getUID());
94
				$this->session->set(self::DAV_AUTHENTICATED, $this->userSession->getUser()->getUID());
95
				$this->session->close();
96
				return true;
97
			} else {
98
				$this->session->close();
99
				return false;
100
			}
101
		}
102
	}
103
104
	/**
105
	 * Returns information about the currently logged in username.
106
	 *
107
	 * If nobody is currently logged in, this method should return null.
108
	 *
109
	 * @return string|null
110
	 */
111
	public function getCurrentUser() {
112
		$user = $this->userSession->getUser() ? $this->userSession->getUser()->getUID() : null;
113
		if($user !== null && $this->isDavAuthenticated($user)) {
114
			return $user;
115
		}
116
117
		if($user !== null && is_null($this->session->get(self::DAV_AUTHENTICATED))) {
118
			return $user;
119
		}
120
121
		return null;
122
	}
123
124
	/**
125
	 * Override function here. We want to cache authentication cookies
126
	 * in the syncing client to avoid HTTP-401 roundtrips.
127
	 * If the sync client supplies the cookies, then OC_User::isLoggedIn()
128
	 * will return true and we can see this WebDAV request as already authenticated,
129
	 * even if there are no HTTP Basic Auth headers.
130
	 * In other case, just fallback to the parent implementation.
131
	 *
132
	 * @param \Sabre\DAV\Server $server
133
	 * @param string $realm
134
	 * @return bool
135
	 * @throws ServiceUnavailable
136
	 * @throws NotAuthenticated
137
	 */
138
	public function authenticate(\Sabre\DAV\Server $server, $realm) {
139
		try {
140
			$result = $this->auth($server, $realm);
141
			return $result;
142
		} catch (NotAuthenticated $e) {
0 ignored issues
show
Bug introduced by
The class Sabre\DAV\Exception\NotAuthenticated does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
143
			throw $e;
144
		} catch (Exception $e) {
145
			$class = get_class($e);
146
			$msg = $e->getMessage();
147
			throw new ServiceUnavailable("$class: $msg");
148
		}
149
    }
0 ignored issues
show
Coding Style introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
150
151
	/**
152
	 * @param \Sabre\DAV\Server $server
153
	 * @param $realm
154
	 * @return bool
155
	 */
156
	private function auth(\Sabre\DAV\Server $server, $realm) {
0 ignored issues
show
Best Practice introduced by
Using PHP4-style constructors that are named like the class is not recommend; better use the more explicit __construct method.
Loading history...
157
		if (\OC_User::handleApacheAuth() ||
158
			($this->userSession->isLoggedIn() && is_null($this->session->get(self::DAV_AUTHENTICATED)))
159
		) {
160
			$user = $this->userSession->getUser()->getUID();
161
			\OC_Util::setupFS($user);
162
			$this->currentUser = $user;
163
			$this->session->close();
164
			return true;
165
		}
166
167
		return parent::authenticate($server, $realm);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (authenticate() instead of auth()). Are you sure this is correct? If so, you might want to change this to $this->authenticate().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
168
	}
169
}
170