Completed
Push — stable8.2 ( f4a799...dce584 )
by Thomas
59:22
created

Auth::authenticate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12
Metric Value
dl 0
loc 13
ccs 0
cts 12
cp 0
rs 9.4286
cc 3
eloc 10
nc 3
nop 2
crap 12
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 OC\Connector\Sabre;
31
32
use Exception;
33
use Sabre\DAV\Auth\Backend\AbstractBasic;
34
use Sabre\DAV\Exception\NotAuthenticated;
35
use Sabre\DAV\Exception\ServiceUnavailable;
36
37
class Auth extends AbstractBasic {
38
	const DAV_AUTHENTICATED = 'AUTHENTICATED_TO_DAV_BACKEND';
39
40
	/**
41
	 * Whether the user has initially authenticated via DAV
42
	 *
43
	 * This is required for WebDAV clients that resent the cookies even when the
44
	 * account was changed.
45
	 *
46
	 * @see https://github.com/owncloud/core/issues/13245
47
	 *
48
	 * @param string $username
49
	 * @return bool
50
	 */
51
	protected function isDavAuthenticated($username) {
52
		return !is_null(\OC::$server->getSession()->get(self::DAV_AUTHENTICATED)) &&
53
		\OC::$server->getSession()->get(self::DAV_AUTHENTICATED) === $username;
54
	}
55
56
	/**
57
	 * Validates a username and password
58
	 *
59
	 * This method should return true or false depending on if login
60
	 * succeeded.
61
	 *
62
	 * @param string $username
63
	 * @param string $password
64
	 * @return bool
65
	 */
66
	protected function validateUserPass($username, $password) {
67
		if (\OC_User::isLoggedIn() &&
68
			$this->isDavAuthenticated(\OC_User::getUser())
69
		) {
70
			\OC_Util::setupFS(\OC_User::getUser());
71
			\OC::$server->getSession()->close();
72
			return true;
73
		} else {
74
			\OC_Util::setUpFS(); //login hooks may need early access to the filesystem
75
			if(\OC_User::login($username, $password)) {
76
			        // make sure we use ownCloud's internal username here
77
			        // and not the HTTP auth supplied one, see issue #14048
78
			        $ocUser = \OC_User::getUser();
79
				\OC_Util::setUpFS($ocUser);
80
				\OC::$server->getSession()->set(self::DAV_AUTHENTICATED, $ocUser);
81
				\OC::$server->getSession()->close();
82
				return true;
83
			} else {
84
				\OC::$server->getSession()->close();
85
				return false;
86
			}
87
		}
88
	}
89
90
	/**
91
	 * Returns information about the currently logged in username.
92
	 *
93
	 * If nobody is currently logged in, this method should return null.
94
	 *
95
	 * @return string|null
96
	 */
97
	public function getCurrentUser() {
98
		$user = \OC_User::getUser();
99
		if($user && $this->isDavAuthenticated($user)) {
100
			return $user;
101
		}
102
103
		if($user && is_null(\OC::$server->getSession()->get(self::DAV_AUTHENTICATED))) {
104
			return $user;
105
		}
106
107
		return null;
108
	}
109
110
	/**
111
	 * Override function here. We want to cache authentication cookies
112
	 * in the syncing client to avoid HTTP-401 roundtrips.
113
	 * If the sync client supplies the cookies, then OC_User::isLoggedIn()
114
	 * will return true and we can see this WebDAV request as already authenticated,
115
	 * even if there are no HTTP Basic Auth headers.
116
	 * In other case, just fallback to the parent implementation.
117
	 *
118
	 * @param \Sabre\DAV\Server $server
119
	 * @param string $realm
120
	 * @return bool
121
	 * @throws ServiceUnavailable
122
	 * @throws NotAuthenticated
123
	 */
124
	public function authenticate(\Sabre\DAV\Server $server, $realm) {
125
126
		try {
127
			$result = $this->auth($server, $realm);
128
			return $result;
129
		} 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...
130
			throw $e;
131
		} catch (Exception $e) {
132
			$class = get_class($e);
133
			$msg = $e->getMessage();
134
			throw new ServiceUnavailable("$class: $msg");
135
		}
136
    }
137
138
	/**
139
	 * @param \Sabre\DAV\Server $server
140
	 * @param $realm
141
	 * @return bool
142
	 */
143
	private function auth(\Sabre\DAV\Server $server, $realm) {
144
		if (\OC_User::handleApacheAuth() ||
145
			//Fix for broken webdav clients
146
			(\OC_User::isLoggedIn() && is_null(\OC::$server->getSession()->get(self::DAV_AUTHENTICATED))) ||
147
			//Well behaved clients that only send the cookie are allowed
148
			(\OC_User::isLoggedIn() && \OC::$server->getSession()->get(self::DAV_AUTHENTICATED) === \OC_User::getUser())
149
		) {
150
			$user = \OC_User::getUser();
151
			\OC_Util::setupFS($user);
152
			$this->currentUser = $user;
153
			\OC::$server->getSession()->close();
154
			return true;
155
		}
156
157
		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...
158
	}
159
}
160