Passed
Push — master ( 88cf60...7ba870 )
by Roeland
56:46 queued 40:34
created
apps/dav/lib/Connector/Sabre/Auth.php 2 patches
Indentation   +192 added lines, -192 removed lines patch added patch discarded remove patch
@@ -49,213 +49,213 @@
 block discarded – undo
49 49
 class Auth extends AbstractBasic {
50 50
 
51 51
 
52
-	const DAV_AUTHENTICATED = 'AUTHENTICATED_TO_DAV_BACKEND';
52
+    const DAV_AUTHENTICATED = 'AUTHENTICATED_TO_DAV_BACKEND';
53 53
 
54
-	/** @var ISession */
55
-	private $session;
56
-	/** @var Session */
57
-	private $userSession;
58
-	/** @var IRequest */
59
-	private $request;
60
-	/** @var string */
61
-	private $currentUser;
62
-	/** @var Manager */
63
-	private $twoFactorManager;
64
-	/** @var Throttler */
65
-	private $throttler;
54
+    /** @var ISession */
55
+    private $session;
56
+    /** @var Session */
57
+    private $userSession;
58
+    /** @var IRequest */
59
+    private $request;
60
+    /** @var string */
61
+    private $currentUser;
62
+    /** @var Manager */
63
+    private $twoFactorManager;
64
+    /** @var Throttler */
65
+    private $throttler;
66 66
 
67
-	/**
68
-	 * @param ISession $session
69
-	 * @param Session $userSession
70
-	 * @param IRequest $request
71
-	 * @param Manager $twoFactorManager
72
-	 * @param Throttler $throttler
73
-	 * @param string $principalPrefix
74
-	 */
75
-	public function __construct(ISession $session,
76
-								Session $userSession,
77
-								IRequest $request,
78
-								Manager $twoFactorManager,
79
-								Throttler $throttler,
80
-								$principalPrefix = 'principals/users/') {
81
-		$this->session = $session;
82
-		$this->userSession = $userSession;
83
-		$this->twoFactorManager = $twoFactorManager;
84
-		$this->request = $request;
85
-		$this->throttler = $throttler;
86
-		$this->principalPrefix = $principalPrefix;
67
+    /**
68
+     * @param ISession $session
69
+     * @param Session $userSession
70
+     * @param IRequest $request
71
+     * @param Manager $twoFactorManager
72
+     * @param Throttler $throttler
73
+     * @param string $principalPrefix
74
+     */
75
+    public function __construct(ISession $session,
76
+                                Session $userSession,
77
+                                IRequest $request,
78
+                                Manager $twoFactorManager,
79
+                                Throttler $throttler,
80
+                                $principalPrefix = 'principals/users/') {
81
+        $this->session = $session;
82
+        $this->userSession = $userSession;
83
+        $this->twoFactorManager = $twoFactorManager;
84
+        $this->request = $request;
85
+        $this->throttler = $throttler;
86
+        $this->principalPrefix = $principalPrefix;
87 87
 
88
-		// setup realm
89
-		$defaults = new \OCP\Defaults();
90
-		$this->realm = $defaults->getName();
91
-	}
88
+        // setup realm
89
+        $defaults = new \OCP\Defaults();
90
+        $this->realm = $defaults->getName();
91
+    }
92 92
 
93
-	/**
94
-	 * Whether the user has initially authenticated via DAV
95
-	 *
96
-	 * This is required for WebDAV clients that resent the cookies even when the
97
-	 * account was changed.
98
-	 *
99
-	 * @see https://github.com/owncloud/core/issues/13245
100
-	 *
101
-	 * @param string $username
102
-	 * @return bool
103
-	 */
104
-	public function isDavAuthenticated($username) {
105
-		return !is_null($this->session->get(self::DAV_AUTHENTICATED)) &&
106
-		$this->session->get(self::DAV_AUTHENTICATED) === $username;
107
-	}
93
+    /**
94
+     * Whether the user has initially authenticated via DAV
95
+     *
96
+     * This is required for WebDAV clients that resent the cookies even when the
97
+     * account was changed.
98
+     *
99
+     * @see https://github.com/owncloud/core/issues/13245
100
+     *
101
+     * @param string $username
102
+     * @return bool
103
+     */
104
+    public function isDavAuthenticated($username) {
105
+        return !is_null($this->session->get(self::DAV_AUTHENTICATED)) &&
106
+        $this->session->get(self::DAV_AUTHENTICATED) === $username;
107
+    }
108 108
 
109
-	/**
110
-	 * Validates a username and password
111
-	 *
112
-	 * This method should return true or false depending on if login
113
-	 * succeeded.
114
-	 *
115
-	 * @param string $username
116
-	 * @param string $password
117
-	 * @return bool
118
-	 * @throws PasswordLoginForbidden
119
-	 */
120
-	protected function validateUserPass($username, $password) {
121
-		if ($this->userSession->isLoggedIn() &&
122
-			$this->isDavAuthenticated($this->userSession->getUser()->getUID())
123
-		) {
124
-			\OC_Util::setupFS($this->userSession->getUser()->getUID());
125
-			$this->session->close();
126
-			return true;
127
-		} else {
128
-			\OC_Util::setupFS(); //login hooks may need early access to the filesystem
129
-			try {
130
-				if ($this->userSession->logClientIn($username, $password, $this->request, $this->throttler)) {
131
-					\OC_Util::setupFS($this->userSession->getUser()->getUID());
132
-					$this->session->set(self::DAV_AUTHENTICATED, $this->userSession->getUser()->getUID());
133
-					$this->session->close();
134
-					return true;
135
-				} else {
136
-					$this->session->close();
137
-					return false;
138
-				}
139
-			} catch (PasswordLoginForbiddenException $ex) {
140
-				$this->session->close();
141
-				throw new PasswordLoginForbidden();
142
-			}
143
-		}
144
-	}
109
+    /**
110
+     * Validates a username and password
111
+     *
112
+     * This method should return true or false depending on if login
113
+     * succeeded.
114
+     *
115
+     * @param string $username
116
+     * @param string $password
117
+     * @return bool
118
+     * @throws PasswordLoginForbidden
119
+     */
120
+    protected function validateUserPass($username, $password) {
121
+        if ($this->userSession->isLoggedIn() &&
122
+            $this->isDavAuthenticated($this->userSession->getUser()->getUID())
123
+        ) {
124
+            \OC_Util::setupFS($this->userSession->getUser()->getUID());
125
+            $this->session->close();
126
+            return true;
127
+        } else {
128
+            \OC_Util::setupFS(); //login hooks may need early access to the filesystem
129
+            try {
130
+                if ($this->userSession->logClientIn($username, $password, $this->request, $this->throttler)) {
131
+                    \OC_Util::setupFS($this->userSession->getUser()->getUID());
132
+                    $this->session->set(self::DAV_AUTHENTICATED, $this->userSession->getUser()->getUID());
133
+                    $this->session->close();
134
+                    return true;
135
+                } else {
136
+                    $this->session->close();
137
+                    return false;
138
+                }
139
+            } catch (PasswordLoginForbiddenException $ex) {
140
+                $this->session->close();
141
+                throw new PasswordLoginForbidden();
142
+            }
143
+        }
144
+    }
145 145
 
146
-	/**
147
-	 * @param RequestInterface $request
148
-	 * @param ResponseInterface $response
149
-	 * @return array
150
-	 * @throws NotAuthenticated
151
-	 * @throws ServiceUnavailable
152
-	 */
153
-	function check(RequestInterface $request, ResponseInterface $response) {
154
-		try {
155
-			return $this->auth($request, $response);
156
-		} catch (NotAuthenticated $e) {
157
-			throw $e;
158
-		} catch (Exception $e) {
159
-			$class = get_class($e);
160
-			$msg = $e->getMessage();
161
-			\OC::$server->getLogger()->logException($e);
162
-			throw new ServiceUnavailable("$class: $msg");
163
-		}
164
-	}
146
+    /**
147
+     * @param RequestInterface $request
148
+     * @param ResponseInterface $response
149
+     * @return array
150
+     * @throws NotAuthenticated
151
+     * @throws ServiceUnavailable
152
+     */
153
+    function check(RequestInterface $request, ResponseInterface $response) {
154
+        try {
155
+            return $this->auth($request, $response);
156
+        } catch (NotAuthenticated $e) {
157
+            throw $e;
158
+        } catch (Exception $e) {
159
+            $class = get_class($e);
160
+            $msg = $e->getMessage();
161
+            \OC::$server->getLogger()->logException($e);
162
+            throw new ServiceUnavailable("$class: $msg");
163
+        }
164
+    }
165 165
 
166
-	/**
167
-	 * Checks whether a CSRF check is required on the request
168
-	 *
169
-	 * @return bool
170
-	 */
171
-	private function requiresCSRFCheck() {
172
-		// GET requires no check at all
173
-		if($this->request->getMethod() === 'GET') {
174
-			return false;
175
-		}
166
+    /**
167
+     * Checks whether a CSRF check is required on the request
168
+     *
169
+     * @return bool
170
+     */
171
+    private function requiresCSRFCheck() {
172
+        // GET requires no check at all
173
+        if($this->request->getMethod() === 'GET') {
174
+            return false;
175
+        }
176 176
 
177
-		// Official Nextcloud clients require no checks
178
-		if($this->request->isUserAgent([
179
-			IRequest::USER_AGENT_CLIENT_DESKTOP,
180
-			IRequest::USER_AGENT_CLIENT_ANDROID,
181
-			IRequest::USER_AGENT_CLIENT_IOS,
182
-		])) {
183
-			return false;
184
-		}
177
+        // Official Nextcloud clients require no checks
178
+        if($this->request->isUserAgent([
179
+            IRequest::USER_AGENT_CLIENT_DESKTOP,
180
+            IRequest::USER_AGENT_CLIENT_ANDROID,
181
+            IRequest::USER_AGENT_CLIENT_IOS,
182
+        ])) {
183
+            return false;
184
+        }
185 185
 
186
-		// If not logged-in no check is required
187
-		if(!$this->userSession->isLoggedIn()) {
188
-			return false;
189
-		}
186
+        // If not logged-in no check is required
187
+        if(!$this->userSession->isLoggedIn()) {
188
+            return false;
189
+        }
190 190
 
191
-		// POST always requires a check
192
-		if($this->request->getMethod() === 'POST') {
193
-			return true;
194
-		}
191
+        // POST always requires a check
192
+        if($this->request->getMethod() === 'POST') {
193
+            return true;
194
+        }
195 195
 
196
-		// If logged-in AND DAV authenticated no check is required
197
-		if($this->userSession->isLoggedIn() &&
198
-			$this->isDavAuthenticated($this->userSession->getUser()->getUID())) {
199
-			return false;
200
-		}
196
+        // If logged-in AND DAV authenticated no check is required
197
+        if($this->userSession->isLoggedIn() &&
198
+            $this->isDavAuthenticated($this->userSession->getUser()->getUID())) {
199
+            return false;
200
+        }
201 201
 
202
-		return true;
203
-	}
202
+        return true;
203
+    }
204 204
 
205
-	/**
206
-	 * @param RequestInterface $request
207
-	 * @param ResponseInterface $response
208
-	 * @return array
209
-	 * @throws NotAuthenticated
210
-	 */
211
-	private function auth(RequestInterface $request, ResponseInterface $response) {
212
-		$forcedLogout = false;
205
+    /**
206
+     * @param RequestInterface $request
207
+     * @param ResponseInterface $response
208
+     * @return array
209
+     * @throws NotAuthenticated
210
+     */
211
+    private function auth(RequestInterface $request, ResponseInterface $response) {
212
+        $forcedLogout = false;
213 213
 
214
-		if(!$this->request->passesCSRFCheck() &&
215
-			$this->requiresCSRFCheck()) {
216
-			// In case of a fail with POST we need to recheck the credentials
217
-			if($this->request->getMethod() === 'POST') {
218
-				$forcedLogout = true;
219
-			} else {
220
-				$response->setStatus(401);
221
-				throw new \Sabre\DAV\Exception\NotAuthenticated('CSRF check not passed.');
222
-			}
223
-		}
214
+        if(!$this->request->passesCSRFCheck() &&
215
+            $this->requiresCSRFCheck()) {
216
+            // In case of a fail with POST we need to recheck the credentials
217
+            if($this->request->getMethod() === 'POST') {
218
+                $forcedLogout = true;
219
+            } else {
220
+                $response->setStatus(401);
221
+                throw new \Sabre\DAV\Exception\NotAuthenticated('CSRF check not passed.');
222
+            }
223
+        }
224 224
 
225
-		if($forcedLogout) {
226
-			$this->userSession->logout();
227
-		} else {
228
-			if($this->twoFactorManager->needsSecondFactor($this->userSession->getUser())) {
229
-				throw new \Sabre\DAV\Exception\NotAuthenticated('2FA challenge not passed.');
230
-			}
231
-			if (
232
-				//Fix for broken webdav clients
233
-				($this->userSession->isLoggedIn() && is_null($this->session->get(self::DAV_AUTHENTICATED))) ||
234
-				//Well behaved clients that only send the cookie are allowed
235
-				($this->userSession->isLoggedIn() && $this->session->get(self::DAV_AUTHENTICATED) === $this->userSession->getUser()->getUID() && $request->getHeader('Authorization') === null) ||
236
-				\OC_User::handleApacheAuth()
237
-			) {
238
-				$user = $this->userSession->getUser()->getUID();
239
-				\OC_Util::setupFS($user);
240
-				$this->currentUser = $user;
241
-				$this->session->close();
242
-				return [true, $this->principalPrefix . $user];
243
-			}
244
-		}
225
+        if($forcedLogout) {
226
+            $this->userSession->logout();
227
+        } else {
228
+            if($this->twoFactorManager->needsSecondFactor($this->userSession->getUser())) {
229
+                throw new \Sabre\DAV\Exception\NotAuthenticated('2FA challenge not passed.');
230
+            }
231
+            if (
232
+                //Fix for broken webdav clients
233
+                ($this->userSession->isLoggedIn() && is_null($this->session->get(self::DAV_AUTHENTICATED))) ||
234
+                //Well behaved clients that only send the cookie are allowed
235
+                ($this->userSession->isLoggedIn() && $this->session->get(self::DAV_AUTHENTICATED) === $this->userSession->getUser()->getUID() && $request->getHeader('Authorization') === null) ||
236
+                \OC_User::handleApacheAuth()
237
+            ) {
238
+                $user = $this->userSession->getUser()->getUID();
239
+                \OC_Util::setupFS($user);
240
+                $this->currentUser = $user;
241
+                $this->session->close();
242
+                return [true, $this->principalPrefix . $user];
243
+            }
244
+        }
245 245
 
246
-		if (!$this->userSession->isLoggedIn() && in_array('XMLHttpRequest', explode(',', $request->getHeader('X-Requested-With')))) {
247
-			// do not re-authenticate over ajax, use dummy auth name to prevent browser popup
248
-			$response->addHeader('WWW-Authenticate','DummyBasic realm="' . $this->realm . '"');
249
-			$response->setStatus(401);
250
-			throw new \Sabre\DAV\Exception\NotAuthenticated('Cannot authenticate over ajax calls');
251
-		}
246
+        if (!$this->userSession->isLoggedIn() && in_array('XMLHttpRequest', explode(',', $request->getHeader('X-Requested-With')))) {
247
+            // do not re-authenticate over ajax, use dummy auth name to prevent browser popup
248
+            $response->addHeader('WWW-Authenticate','DummyBasic realm="' . $this->realm . '"');
249
+            $response->setStatus(401);
250
+            throw new \Sabre\DAV\Exception\NotAuthenticated('Cannot authenticate over ajax calls');
251
+        }
252 252
 
253
-		$data = parent::check($request, $response);
254
-		if($data[0] === true) {
255
-			$startPos = strrpos($data[1], '/') + 1;
256
-			$user = $this->userSession->getUser()->getUID();
257
-			$data[1] = substr_replace($data[1], $user, $startPos);
258
-		}
259
-		return $data;
260
-	}
253
+        $data = parent::check($request, $response);
254
+        if($data[0] === true) {
255
+            $startPos = strrpos($data[1], '/') + 1;
256
+            $user = $this->userSession->getUser()->getUID();
257
+            $data[1] = substr_replace($data[1], $user, $startPos);
258
+        }
259
+        return $data;
260
+    }
261 261
 }
Please login to merge, or discard this patch.
Spacing   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -170,12 +170,12 @@  discard block
 block discarded – undo
170 170
 	 */
171 171
 	private function requiresCSRFCheck() {
172 172
 		// GET requires no check at all
173
-		if($this->request->getMethod() === 'GET') {
173
+		if ($this->request->getMethod() === 'GET') {
174 174
 			return false;
175 175
 		}
176 176
 
177 177
 		// Official Nextcloud clients require no checks
178
-		if($this->request->isUserAgent([
178
+		if ($this->request->isUserAgent([
179 179
 			IRequest::USER_AGENT_CLIENT_DESKTOP,
180 180
 			IRequest::USER_AGENT_CLIENT_ANDROID,
181 181
 			IRequest::USER_AGENT_CLIENT_IOS,
@@ -184,17 +184,17 @@  discard block
 block discarded – undo
184 184
 		}
185 185
 
186 186
 		// If not logged-in no check is required
187
-		if(!$this->userSession->isLoggedIn()) {
187
+		if (!$this->userSession->isLoggedIn()) {
188 188
 			return false;
189 189
 		}
190 190
 
191 191
 		// POST always requires a check
192
-		if($this->request->getMethod() === 'POST') {
192
+		if ($this->request->getMethod() === 'POST') {
193 193
 			return true;
194 194
 		}
195 195
 
196 196
 		// If logged-in AND DAV authenticated no check is required
197
-		if($this->userSession->isLoggedIn() &&
197
+		if ($this->userSession->isLoggedIn() &&
198 198
 			$this->isDavAuthenticated($this->userSession->getUser()->getUID())) {
199 199
 			return false;
200 200
 		}
@@ -211,10 +211,10 @@  discard block
 block discarded – undo
211 211
 	private function auth(RequestInterface $request, ResponseInterface $response) {
212 212
 		$forcedLogout = false;
213 213
 
214
-		if(!$this->request->passesCSRFCheck() &&
214
+		if (!$this->request->passesCSRFCheck() &&
215 215
 			$this->requiresCSRFCheck()) {
216 216
 			// In case of a fail with POST we need to recheck the credentials
217
-			if($this->request->getMethod() === 'POST') {
217
+			if ($this->request->getMethod() === 'POST') {
218 218
 				$forcedLogout = true;
219 219
 			} else {
220 220
 				$response->setStatus(401);
@@ -222,10 +222,10 @@  discard block
 block discarded – undo
222 222
 			}
223 223
 		}
224 224
 
225
-		if($forcedLogout) {
225
+		if ($forcedLogout) {
226 226
 			$this->userSession->logout();
227 227
 		} else {
228
-			if($this->twoFactorManager->needsSecondFactor($this->userSession->getUser())) {
228
+			if ($this->twoFactorManager->needsSecondFactor($this->userSession->getUser())) {
229 229
 				throw new \Sabre\DAV\Exception\NotAuthenticated('2FA challenge not passed.');
230 230
 			}
231 231
 			if (
@@ -239,19 +239,19 @@  discard block
 block discarded – undo
239 239
 				\OC_Util::setupFS($user);
240 240
 				$this->currentUser = $user;
241 241
 				$this->session->close();
242
-				return [true, $this->principalPrefix . $user];
242
+				return [true, $this->principalPrefix.$user];
243 243
 			}
244 244
 		}
245 245
 
246 246
 		if (!$this->userSession->isLoggedIn() && in_array('XMLHttpRequest', explode(',', $request->getHeader('X-Requested-With')))) {
247 247
 			// do not re-authenticate over ajax, use dummy auth name to prevent browser popup
248
-			$response->addHeader('WWW-Authenticate','DummyBasic realm="' . $this->realm . '"');
248
+			$response->addHeader('WWW-Authenticate', 'DummyBasic realm="'.$this->realm.'"');
249 249
 			$response->setStatus(401);
250 250
 			throw new \Sabre\DAV\Exception\NotAuthenticated('Cannot authenticate over ajax calls');
251 251
 		}
252 252
 
253 253
 		$data = parent::check($request, $response);
254
-		if($data[0] === true) {
254
+		if ($data[0] === true) {
255 255
 			$startPos = strrpos($data[1], '/') + 1;
256 256
 			$user = $this->userSession->getUser()->getUID();
257 257
 			$data[1] = substr_replace($data[1], $user, $startPos);
Please login to merge, or discard this patch.