@@ -59,358 +59,358 @@ |
||
59 | 59 | |
60 | 60 | class LoginController extends Controller { |
61 | 61 | |
62 | - const LOGIN_MSG_INVALIDPASSWORD = 'invalidpassword'; |
|
63 | - const LOGIN_MSG_USERDISABLED = 'userdisabled'; |
|
64 | - |
|
65 | - /** @var IUserManager */ |
|
66 | - private $userManager; |
|
67 | - /** @var IConfig */ |
|
68 | - private $config; |
|
69 | - /** @var ISession */ |
|
70 | - private $session; |
|
71 | - /** @var IUserSession|Session */ |
|
72 | - private $userSession; |
|
73 | - /** @var IURLGenerator */ |
|
74 | - private $urlGenerator; |
|
75 | - /** @var ILogger */ |
|
76 | - private $logger; |
|
77 | - /** @var Manager */ |
|
78 | - private $twoFactorManager; |
|
79 | - /** @var Defaults */ |
|
80 | - private $defaults; |
|
81 | - /** @var Throttler */ |
|
82 | - private $throttler; |
|
83 | - |
|
84 | - /** |
|
85 | - * @param string $appName |
|
86 | - * @param IRequest $request |
|
87 | - * @param IUserManager $userManager |
|
88 | - * @param IConfig $config |
|
89 | - * @param ISession $session |
|
90 | - * @param IUserSession $userSession |
|
91 | - * @param IURLGenerator $urlGenerator |
|
92 | - * @param ILogger $logger |
|
93 | - * @param Manager $twoFactorManager |
|
94 | - * @param Defaults $defaults |
|
95 | - * @param Throttler $throttler |
|
96 | - */ |
|
97 | - public function __construct($appName, |
|
98 | - IRequest $request, |
|
99 | - IUserManager $userManager, |
|
100 | - IConfig $config, |
|
101 | - ISession $session, |
|
102 | - IUserSession $userSession, |
|
103 | - IURLGenerator $urlGenerator, |
|
104 | - ILogger $logger, |
|
105 | - Manager $twoFactorManager, |
|
106 | - Defaults $defaults, |
|
107 | - Throttler $throttler) { |
|
108 | - parent::__construct($appName, $request); |
|
109 | - $this->userManager = $userManager; |
|
110 | - $this->config = $config; |
|
111 | - $this->session = $session; |
|
112 | - $this->userSession = $userSession; |
|
113 | - $this->urlGenerator = $urlGenerator; |
|
114 | - $this->logger = $logger; |
|
115 | - $this->twoFactorManager = $twoFactorManager; |
|
116 | - $this->defaults = $defaults; |
|
117 | - $this->throttler = $throttler; |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * @NoAdminRequired |
|
122 | - * @UseSession |
|
123 | - * |
|
124 | - * @return RedirectResponse |
|
125 | - */ |
|
126 | - public function logout() { |
|
127 | - $loginToken = $this->request->getCookie('nc_token'); |
|
128 | - if (!is_null($loginToken)) { |
|
129 | - $this->config->deleteUserValue($this->userSession->getUser()->getUID(), 'login_token', $loginToken); |
|
130 | - } |
|
131 | - $this->userSession->logout(); |
|
132 | - |
|
133 | - $response = new RedirectResponse($this->urlGenerator->linkToRouteAbsolute('core.login.showLoginForm')); |
|
134 | - $response->addHeader('Clear-Site-Data', '"cache", "storage", "executionContexts"'); |
|
135 | - return $response; |
|
136 | - } |
|
137 | - |
|
138 | - /** |
|
139 | - * @PublicPage |
|
140 | - * @NoCSRFRequired |
|
141 | - * @UseSession |
|
142 | - * |
|
143 | - * @param string $user |
|
144 | - * @param string $redirect_url |
|
145 | - * |
|
146 | - * @return TemplateResponse|RedirectResponse |
|
147 | - */ |
|
148 | - public function showLoginForm(string $user = null, string $redirect_url = null): Http\Response { |
|
149 | - |
|
150 | - if ($this->userSession->isLoggedIn()) { |
|
151 | - return new RedirectResponse(OC_Util::getDefaultPageUrl()); |
|
152 | - } |
|
153 | - |
|
154 | - $parameters = array(); |
|
155 | - $loginMessages = $this->session->get('loginMessages'); |
|
156 | - $errors = []; |
|
157 | - $messages = []; |
|
158 | - if (is_array($loginMessages)) { |
|
159 | - list($errors, $messages) = $loginMessages; |
|
160 | - } |
|
161 | - $this->session->remove('loginMessages'); |
|
162 | - foreach ($errors as $value) { |
|
163 | - $parameters[$value] = true; |
|
164 | - } |
|
165 | - |
|
166 | - $parameters['messages'] = $messages; |
|
167 | - if ($user !== null && $user !== '') { |
|
168 | - $parameters['loginName'] = $user; |
|
169 | - $parameters['user_autofocus'] = false; |
|
170 | - } else { |
|
171 | - $parameters['loginName'] = ''; |
|
172 | - $parameters['user_autofocus'] = true; |
|
173 | - } |
|
174 | - if (!empty($redirect_url)) { |
|
175 | - $parameters['redirect_url'] = $redirect_url; |
|
176 | - } |
|
177 | - |
|
178 | - $parameters = $this->setPasswordResetParameters($user, $parameters); |
|
179 | - $parameters['alt_login'] = OC_App::getAlternativeLogIns(); |
|
180 | - |
|
181 | - if ($user !== null && $user !== '') { |
|
182 | - $parameters['loginName'] = $user; |
|
183 | - $parameters['user_autofocus'] = false; |
|
184 | - } else { |
|
185 | - $parameters['loginName'] = ''; |
|
186 | - $parameters['user_autofocus'] = true; |
|
187 | - } |
|
188 | - |
|
189 | - $parameters['throttle_delay'] = $this->throttler->getDelay($this->request->getRemoteAddress()); |
|
190 | - |
|
191 | - // OpenGraph Support: http://ogp.me/ |
|
192 | - Util::addHeader('meta', ['property' => 'og:title', 'content' => Util::sanitizeHTML($this->defaults->getName())]); |
|
193 | - Util::addHeader('meta', ['property' => 'og:description', 'content' => Util::sanitizeHTML($this->defaults->getSlogan())]); |
|
194 | - Util::addHeader('meta', ['property' => 'og:site_name', 'content' => Util::sanitizeHTML($this->defaults->getName())]); |
|
195 | - Util::addHeader('meta', ['property' => 'og:url', 'content' => $this->urlGenerator->getAbsoluteURL('/')]); |
|
196 | - Util::addHeader('meta', ['property' => 'og:type', 'content' => 'website']); |
|
197 | - Util::addHeader('meta', ['property' => 'og:image', 'content' => $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'favicon-touch.png'))]); |
|
198 | - |
|
199 | - return new TemplateResponse( |
|
200 | - $this->appName, 'login', $parameters, 'guest' |
|
201 | - ); |
|
202 | - } |
|
203 | - |
|
204 | - /** |
|
205 | - * Sets the password reset params. |
|
206 | - * |
|
207 | - * Users may not change their passwords if: |
|
208 | - * - The account is disabled |
|
209 | - * - The backend doesn't support password resets |
|
210 | - * - The password reset function is disabled |
|
211 | - * |
|
212 | - * @param string $user |
|
213 | - * @param array $parameters |
|
214 | - * @return array |
|
215 | - */ |
|
216 | - private function setPasswordResetParameters( |
|
217 | - string $user = null, array $parameters): array { |
|
218 | - if ($user !== null && $user !== '') { |
|
219 | - $userObj = $this->userManager->get($user); |
|
220 | - } else { |
|
221 | - $userObj = null; |
|
222 | - } |
|
223 | - |
|
224 | - $parameters['resetPasswordLink'] = $this->config |
|
225 | - ->getSystemValue('lost_password_link', ''); |
|
226 | - |
|
227 | - if (!$parameters['resetPasswordLink'] && $userObj !== null) { |
|
228 | - $parameters['canResetPassword'] = $userObj->canChangePassword(); |
|
229 | - } else if ($userObj !== null && $userObj->isEnabled() === false) { |
|
230 | - $parameters['canResetPassword'] = false; |
|
231 | - } else { |
|
232 | - $parameters['canResetPassword'] = true; |
|
233 | - } |
|
234 | - |
|
235 | - return $parameters; |
|
236 | - } |
|
237 | - |
|
238 | - /** |
|
239 | - * @param string $redirectUrl |
|
240 | - * @return RedirectResponse |
|
241 | - */ |
|
242 | - private function generateRedirect($redirectUrl) { |
|
243 | - if (!is_null($redirectUrl) && $this->userSession->isLoggedIn()) { |
|
244 | - $location = $this->urlGenerator->getAbsoluteURL(urldecode($redirectUrl)); |
|
245 | - // Deny the redirect if the URL contains a @ |
|
246 | - // This prevents unvalidated redirects like ?redirect_url=:[email protected] |
|
247 | - if (strpos($location, '@') === false) { |
|
248 | - return new RedirectResponse($location); |
|
249 | - } |
|
250 | - } |
|
251 | - return new RedirectResponse(OC_Util::getDefaultPageUrl()); |
|
252 | - } |
|
253 | - |
|
254 | - /** |
|
255 | - * @PublicPage |
|
256 | - * @UseSession |
|
257 | - * @NoCSRFRequired |
|
258 | - * @BruteForceProtection(action=login) |
|
259 | - * |
|
260 | - * @param string $user |
|
261 | - * @param string $password |
|
262 | - * @param string $redirect_url |
|
263 | - * @param boolean $remember_login |
|
264 | - * @param string $timezone |
|
265 | - * @param string $timezone_offset |
|
266 | - * @return RedirectResponse |
|
267 | - */ |
|
268 | - public function tryLogin($user, $password, $redirect_url, $remember_login = true, $timezone = '', $timezone_offset = '') { |
|
269 | - if(!is_string($user)) { |
|
270 | - throw new \InvalidArgumentException('Username must be string'); |
|
271 | - } |
|
272 | - |
|
273 | - // If the user is already logged in and the CSRF check does not pass then |
|
274 | - // simply redirect the user to the correct page as required. This is the |
|
275 | - // case when an user has already logged-in, in another tab. |
|
276 | - if(!$this->request->passesCSRFCheck()) { |
|
277 | - return $this->generateRedirect($redirect_url); |
|
278 | - } |
|
279 | - |
|
280 | - if ($this->userManager instanceof PublicEmitter) { |
|
281 | - $this->userManager->emit('\OC\User', 'preLogin', array($user, $password)); |
|
282 | - } |
|
283 | - |
|
284 | - $originalUser = $user; |
|
285 | - |
|
286 | - $userObj = $this->userManager->get($user); |
|
287 | - |
|
288 | - if ($userObj !== null && $userObj->isEnabled() === false) { |
|
289 | - $this->logger->warning('Login failed: \''. $user . '\' disabled' . |
|
290 | - ' (Remote IP: \''. $this->request->getRemoteAddress(). '\')', |
|
291 | - ['app' => 'core']); |
|
292 | - return $this->createLoginFailedResponse($user, $originalUser, |
|
293 | - $redirect_url, self::LOGIN_MSG_USERDISABLED); |
|
294 | - } |
|
295 | - |
|
296 | - // TODO: Add all the insane error handling |
|
297 | - /* @var $loginResult IUser */ |
|
298 | - $loginResult = $this->userManager->checkPasswordNoLogging($user, $password); |
|
299 | - if ($loginResult === false) { |
|
300 | - $users = $this->userManager->getByEmail($user); |
|
301 | - // we only allow login by email if unique |
|
302 | - if (count($users) === 1) { |
|
303 | - $previousUser = $user; |
|
304 | - $user = $users[0]->getUID(); |
|
305 | - if($user !== $previousUser) { |
|
306 | - $loginResult = $this->userManager->checkPassword($user, $password); |
|
307 | - } |
|
308 | - } |
|
309 | - } |
|
310 | - |
|
311 | - if ($loginResult === false) { |
|
312 | - $this->logger->warning('Login failed: \''. $user . |
|
313 | - '\' (Remote IP: \''. $this->request->getRemoteAddress(). '\')', |
|
314 | - ['app' => 'core']); |
|
315 | - return $this->createLoginFailedResponse($user, $originalUser, |
|
316 | - $redirect_url, self::LOGIN_MSG_INVALIDPASSWORD); |
|
317 | - } |
|
318 | - |
|
319 | - // TODO: remove password checks from above and let the user session handle failures |
|
320 | - // requires https://github.com/owncloud/core/pull/24616 |
|
321 | - $this->userSession->completeLogin($loginResult, ['loginName' => $user, 'password' => $password]); |
|
322 | - $this->userSession->createSessionToken($this->request, $loginResult->getUID(), $user, $password, IToken::REMEMBER); |
|
323 | - $this->userSession->updateTokens($loginResult->getUID(), $password); |
|
324 | - |
|
325 | - // User has successfully logged in, now remove the password reset link, when it is available |
|
326 | - $this->config->deleteUserValue($loginResult->getUID(), 'core', 'lostpassword'); |
|
327 | - |
|
328 | - $this->session->set('last-password-confirm', $loginResult->getLastLogin()); |
|
329 | - |
|
330 | - if ($timezone_offset !== '') { |
|
331 | - $this->config->setUserValue($loginResult->getUID(), 'core', 'timezone', $timezone); |
|
332 | - $this->session->set('timezone', $timezone_offset); |
|
333 | - } |
|
334 | - |
|
335 | - if ($this->twoFactorManager->isTwoFactorAuthenticated($loginResult)) { |
|
336 | - $this->twoFactorManager->prepareTwoFactorLogin($loginResult, $remember_login); |
|
337 | - |
|
338 | - $providers = $this->twoFactorManager->getProviderSet($loginResult)->getPrimaryProviders(); |
|
339 | - if (count($providers) === 1) { |
|
340 | - // Single provider, hence we can redirect to that provider's challenge page directly |
|
341 | - /* @var $provider IProvider */ |
|
342 | - $provider = array_pop($providers); |
|
343 | - $url = 'core.TwoFactorChallenge.showChallenge'; |
|
344 | - $urlParams = [ |
|
345 | - 'challengeProviderId' => $provider->getId(), |
|
346 | - ]; |
|
347 | - } else { |
|
348 | - $url = 'core.TwoFactorChallenge.selectChallenge'; |
|
349 | - $urlParams = []; |
|
350 | - } |
|
351 | - |
|
352 | - if (!is_null($redirect_url)) { |
|
353 | - $urlParams['redirect_url'] = $redirect_url; |
|
354 | - } |
|
355 | - |
|
356 | - return new RedirectResponse($this->urlGenerator->linkToRoute($url, $urlParams)); |
|
357 | - } |
|
358 | - |
|
359 | - if ($remember_login) { |
|
360 | - $this->userSession->createRememberMeToken($loginResult); |
|
361 | - } |
|
362 | - |
|
363 | - return $this->generateRedirect($redirect_url); |
|
364 | - } |
|
365 | - |
|
366 | - /** |
|
367 | - * Creates a login failed response. |
|
368 | - * |
|
369 | - * @param string $user |
|
370 | - * @param string $originalUser |
|
371 | - * @param string $redirect_url |
|
372 | - * @param string $loginMessage |
|
373 | - * @return RedirectResponse |
|
374 | - */ |
|
375 | - private function createLoginFailedResponse( |
|
376 | - $user, $originalUser, $redirect_url, string $loginMessage) { |
|
377 | - // Read current user and append if possible we need to |
|
378 | - // return the unmodified user otherwise we will leak the login name |
|
379 | - $args = !is_null($user) ? ['user' => $originalUser] : []; |
|
380 | - if (!is_null($redirect_url)) { |
|
381 | - $args['redirect_url'] = $redirect_url; |
|
382 | - } |
|
383 | - $response = new RedirectResponse( |
|
384 | - $this->urlGenerator->linkToRoute('core.login.showLoginForm', $args) |
|
385 | - ); |
|
386 | - $response->throttle(['user' => substr($user, 0, 64)]); |
|
387 | - $this->session->set('loginMessages', [ |
|
388 | - [$loginMessage], [] |
|
389 | - ]); |
|
390 | - return $response; |
|
391 | - } |
|
392 | - |
|
393 | - /** |
|
394 | - * @NoAdminRequired |
|
395 | - * @UseSession |
|
396 | - * @BruteForceProtection(action=sudo) |
|
397 | - * |
|
398 | - * @license GNU AGPL version 3 or any later version |
|
399 | - * |
|
400 | - * @param string $password |
|
401 | - * @return DataResponse |
|
402 | - */ |
|
403 | - public function confirmPassword($password) { |
|
404 | - $loginName = $this->userSession->getLoginName(); |
|
405 | - $loginResult = $this->userManager->checkPassword($loginName, $password); |
|
406 | - if ($loginResult === false) { |
|
407 | - $response = new DataResponse([], Http::STATUS_FORBIDDEN); |
|
408 | - $response->throttle(); |
|
409 | - return $response; |
|
410 | - } |
|
411 | - |
|
412 | - $confirmTimestamp = time(); |
|
413 | - $this->session->set('last-password-confirm', $confirmTimestamp); |
|
414 | - return new DataResponse(['lastLogin' => $confirmTimestamp], Http::STATUS_OK); |
|
415 | - } |
|
62 | + const LOGIN_MSG_INVALIDPASSWORD = 'invalidpassword'; |
|
63 | + const LOGIN_MSG_USERDISABLED = 'userdisabled'; |
|
64 | + |
|
65 | + /** @var IUserManager */ |
|
66 | + private $userManager; |
|
67 | + /** @var IConfig */ |
|
68 | + private $config; |
|
69 | + /** @var ISession */ |
|
70 | + private $session; |
|
71 | + /** @var IUserSession|Session */ |
|
72 | + private $userSession; |
|
73 | + /** @var IURLGenerator */ |
|
74 | + private $urlGenerator; |
|
75 | + /** @var ILogger */ |
|
76 | + private $logger; |
|
77 | + /** @var Manager */ |
|
78 | + private $twoFactorManager; |
|
79 | + /** @var Defaults */ |
|
80 | + private $defaults; |
|
81 | + /** @var Throttler */ |
|
82 | + private $throttler; |
|
83 | + |
|
84 | + /** |
|
85 | + * @param string $appName |
|
86 | + * @param IRequest $request |
|
87 | + * @param IUserManager $userManager |
|
88 | + * @param IConfig $config |
|
89 | + * @param ISession $session |
|
90 | + * @param IUserSession $userSession |
|
91 | + * @param IURLGenerator $urlGenerator |
|
92 | + * @param ILogger $logger |
|
93 | + * @param Manager $twoFactorManager |
|
94 | + * @param Defaults $defaults |
|
95 | + * @param Throttler $throttler |
|
96 | + */ |
|
97 | + public function __construct($appName, |
|
98 | + IRequest $request, |
|
99 | + IUserManager $userManager, |
|
100 | + IConfig $config, |
|
101 | + ISession $session, |
|
102 | + IUserSession $userSession, |
|
103 | + IURLGenerator $urlGenerator, |
|
104 | + ILogger $logger, |
|
105 | + Manager $twoFactorManager, |
|
106 | + Defaults $defaults, |
|
107 | + Throttler $throttler) { |
|
108 | + parent::__construct($appName, $request); |
|
109 | + $this->userManager = $userManager; |
|
110 | + $this->config = $config; |
|
111 | + $this->session = $session; |
|
112 | + $this->userSession = $userSession; |
|
113 | + $this->urlGenerator = $urlGenerator; |
|
114 | + $this->logger = $logger; |
|
115 | + $this->twoFactorManager = $twoFactorManager; |
|
116 | + $this->defaults = $defaults; |
|
117 | + $this->throttler = $throttler; |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * @NoAdminRequired |
|
122 | + * @UseSession |
|
123 | + * |
|
124 | + * @return RedirectResponse |
|
125 | + */ |
|
126 | + public function logout() { |
|
127 | + $loginToken = $this->request->getCookie('nc_token'); |
|
128 | + if (!is_null($loginToken)) { |
|
129 | + $this->config->deleteUserValue($this->userSession->getUser()->getUID(), 'login_token', $loginToken); |
|
130 | + } |
|
131 | + $this->userSession->logout(); |
|
132 | + |
|
133 | + $response = new RedirectResponse($this->urlGenerator->linkToRouteAbsolute('core.login.showLoginForm')); |
|
134 | + $response->addHeader('Clear-Site-Data', '"cache", "storage", "executionContexts"'); |
|
135 | + return $response; |
|
136 | + } |
|
137 | + |
|
138 | + /** |
|
139 | + * @PublicPage |
|
140 | + * @NoCSRFRequired |
|
141 | + * @UseSession |
|
142 | + * |
|
143 | + * @param string $user |
|
144 | + * @param string $redirect_url |
|
145 | + * |
|
146 | + * @return TemplateResponse|RedirectResponse |
|
147 | + */ |
|
148 | + public function showLoginForm(string $user = null, string $redirect_url = null): Http\Response { |
|
149 | + |
|
150 | + if ($this->userSession->isLoggedIn()) { |
|
151 | + return new RedirectResponse(OC_Util::getDefaultPageUrl()); |
|
152 | + } |
|
153 | + |
|
154 | + $parameters = array(); |
|
155 | + $loginMessages = $this->session->get('loginMessages'); |
|
156 | + $errors = []; |
|
157 | + $messages = []; |
|
158 | + if (is_array($loginMessages)) { |
|
159 | + list($errors, $messages) = $loginMessages; |
|
160 | + } |
|
161 | + $this->session->remove('loginMessages'); |
|
162 | + foreach ($errors as $value) { |
|
163 | + $parameters[$value] = true; |
|
164 | + } |
|
165 | + |
|
166 | + $parameters['messages'] = $messages; |
|
167 | + if ($user !== null && $user !== '') { |
|
168 | + $parameters['loginName'] = $user; |
|
169 | + $parameters['user_autofocus'] = false; |
|
170 | + } else { |
|
171 | + $parameters['loginName'] = ''; |
|
172 | + $parameters['user_autofocus'] = true; |
|
173 | + } |
|
174 | + if (!empty($redirect_url)) { |
|
175 | + $parameters['redirect_url'] = $redirect_url; |
|
176 | + } |
|
177 | + |
|
178 | + $parameters = $this->setPasswordResetParameters($user, $parameters); |
|
179 | + $parameters['alt_login'] = OC_App::getAlternativeLogIns(); |
|
180 | + |
|
181 | + if ($user !== null && $user !== '') { |
|
182 | + $parameters['loginName'] = $user; |
|
183 | + $parameters['user_autofocus'] = false; |
|
184 | + } else { |
|
185 | + $parameters['loginName'] = ''; |
|
186 | + $parameters['user_autofocus'] = true; |
|
187 | + } |
|
188 | + |
|
189 | + $parameters['throttle_delay'] = $this->throttler->getDelay($this->request->getRemoteAddress()); |
|
190 | + |
|
191 | + // OpenGraph Support: http://ogp.me/ |
|
192 | + Util::addHeader('meta', ['property' => 'og:title', 'content' => Util::sanitizeHTML($this->defaults->getName())]); |
|
193 | + Util::addHeader('meta', ['property' => 'og:description', 'content' => Util::sanitizeHTML($this->defaults->getSlogan())]); |
|
194 | + Util::addHeader('meta', ['property' => 'og:site_name', 'content' => Util::sanitizeHTML($this->defaults->getName())]); |
|
195 | + Util::addHeader('meta', ['property' => 'og:url', 'content' => $this->urlGenerator->getAbsoluteURL('/')]); |
|
196 | + Util::addHeader('meta', ['property' => 'og:type', 'content' => 'website']); |
|
197 | + Util::addHeader('meta', ['property' => 'og:image', 'content' => $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'favicon-touch.png'))]); |
|
198 | + |
|
199 | + return new TemplateResponse( |
|
200 | + $this->appName, 'login', $parameters, 'guest' |
|
201 | + ); |
|
202 | + } |
|
203 | + |
|
204 | + /** |
|
205 | + * Sets the password reset params. |
|
206 | + * |
|
207 | + * Users may not change their passwords if: |
|
208 | + * - The account is disabled |
|
209 | + * - The backend doesn't support password resets |
|
210 | + * - The password reset function is disabled |
|
211 | + * |
|
212 | + * @param string $user |
|
213 | + * @param array $parameters |
|
214 | + * @return array |
|
215 | + */ |
|
216 | + private function setPasswordResetParameters( |
|
217 | + string $user = null, array $parameters): array { |
|
218 | + if ($user !== null && $user !== '') { |
|
219 | + $userObj = $this->userManager->get($user); |
|
220 | + } else { |
|
221 | + $userObj = null; |
|
222 | + } |
|
223 | + |
|
224 | + $parameters['resetPasswordLink'] = $this->config |
|
225 | + ->getSystemValue('lost_password_link', ''); |
|
226 | + |
|
227 | + if (!$parameters['resetPasswordLink'] && $userObj !== null) { |
|
228 | + $parameters['canResetPassword'] = $userObj->canChangePassword(); |
|
229 | + } else if ($userObj !== null && $userObj->isEnabled() === false) { |
|
230 | + $parameters['canResetPassword'] = false; |
|
231 | + } else { |
|
232 | + $parameters['canResetPassword'] = true; |
|
233 | + } |
|
234 | + |
|
235 | + return $parameters; |
|
236 | + } |
|
237 | + |
|
238 | + /** |
|
239 | + * @param string $redirectUrl |
|
240 | + * @return RedirectResponse |
|
241 | + */ |
|
242 | + private function generateRedirect($redirectUrl) { |
|
243 | + if (!is_null($redirectUrl) && $this->userSession->isLoggedIn()) { |
|
244 | + $location = $this->urlGenerator->getAbsoluteURL(urldecode($redirectUrl)); |
|
245 | + // Deny the redirect if the URL contains a @ |
|
246 | + // This prevents unvalidated redirects like ?redirect_url=:[email protected] |
|
247 | + if (strpos($location, '@') === false) { |
|
248 | + return new RedirectResponse($location); |
|
249 | + } |
|
250 | + } |
|
251 | + return new RedirectResponse(OC_Util::getDefaultPageUrl()); |
|
252 | + } |
|
253 | + |
|
254 | + /** |
|
255 | + * @PublicPage |
|
256 | + * @UseSession |
|
257 | + * @NoCSRFRequired |
|
258 | + * @BruteForceProtection(action=login) |
|
259 | + * |
|
260 | + * @param string $user |
|
261 | + * @param string $password |
|
262 | + * @param string $redirect_url |
|
263 | + * @param boolean $remember_login |
|
264 | + * @param string $timezone |
|
265 | + * @param string $timezone_offset |
|
266 | + * @return RedirectResponse |
|
267 | + */ |
|
268 | + public function tryLogin($user, $password, $redirect_url, $remember_login = true, $timezone = '', $timezone_offset = '') { |
|
269 | + if(!is_string($user)) { |
|
270 | + throw new \InvalidArgumentException('Username must be string'); |
|
271 | + } |
|
272 | + |
|
273 | + // If the user is already logged in and the CSRF check does not pass then |
|
274 | + // simply redirect the user to the correct page as required. This is the |
|
275 | + // case when an user has already logged-in, in another tab. |
|
276 | + if(!$this->request->passesCSRFCheck()) { |
|
277 | + return $this->generateRedirect($redirect_url); |
|
278 | + } |
|
279 | + |
|
280 | + if ($this->userManager instanceof PublicEmitter) { |
|
281 | + $this->userManager->emit('\OC\User', 'preLogin', array($user, $password)); |
|
282 | + } |
|
283 | + |
|
284 | + $originalUser = $user; |
|
285 | + |
|
286 | + $userObj = $this->userManager->get($user); |
|
287 | + |
|
288 | + if ($userObj !== null && $userObj->isEnabled() === false) { |
|
289 | + $this->logger->warning('Login failed: \''. $user . '\' disabled' . |
|
290 | + ' (Remote IP: \''. $this->request->getRemoteAddress(). '\')', |
|
291 | + ['app' => 'core']); |
|
292 | + return $this->createLoginFailedResponse($user, $originalUser, |
|
293 | + $redirect_url, self::LOGIN_MSG_USERDISABLED); |
|
294 | + } |
|
295 | + |
|
296 | + // TODO: Add all the insane error handling |
|
297 | + /* @var $loginResult IUser */ |
|
298 | + $loginResult = $this->userManager->checkPasswordNoLogging($user, $password); |
|
299 | + if ($loginResult === false) { |
|
300 | + $users = $this->userManager->getByEmail($user); |
|
301 | + // we only allow login by email if unique |
|
302 | + if (count($users) === 1) { |
|
303 | + $previousUser = $user; |
|
304 | + $user = $users[0]->getUID(); |
|
305 | + if($user !== $previousUser) { |
|
306 | + $loginResult = $this->userManager->checkPassword($user, $password); |
|
307 | + } |
|
308 | + } |
|
309 | + } |
|
310 | + |
|
311 | + if ($loginResult === false) { |
|
312 | + $this->logger->warning('Login failed: \''. $user . |
|
313 | + '\' (Remote IP: \''. $this->request->getRemoteAddress(). '\')', |
|
314 | + ['app' => 'core']); |
|
315 | + return $this->createLoginFailedResponse($user, $originalUser, |
|
316 | + $redirect_url, self::LOGIN_MSG_INVALIDPASSWORD); |
|
317 | + } |
|
318 | + |
|
319 | + // TODO: remove password checks from above and let the user session handle failures |
|
320 | + // requires https://github.com/owncloud/core/pull/24616 |
|
321 | + $this->userSession->completeLogin($loginResult, ['loginName' => $user, 'password' => $password]); |
|
322 | + $this->userSession->createSessionToken($this->request, $loginResult->getUID(), $user, $password, IToken::REMEMBER); |
|
323 | + $this->userSession->updateTokens($loginResult->getUID(), $password); |
|
324 | + |
|
325 | + // User has successfully logged in, now remove the password reset link, when it is available |
|
326 | + $this->config->deleteUserValue($loginResult->getUID(), 'core', 'lostpassword'); |
|
327 | + |
|
328 | + $this->session->set('last-password-confirm', $loginResult->getLastLogin()); |
|
329 | + |
|
330 | + if ($timezone_offset !== '') { |
|
331 | + $this->config->setUserValue($loginResult->getUID(), 'core', 'timezone', $timezone); |
|
332 | + $this->session->set('timezone', $timezone_offset); |
|
333 | + } |
|
334 | + |
|
335 | + if ($this->twoFactorManager->isTwoFactorAuthenticated($loginResult)) { |
|
336 | + $this->twoFactorManager->prepareTwoFactorLogin($loginResult, $remember_login); |
|
337 | + |
|
338 | + $providers = $this->twoFactorManager->getProviderSet($loginResult)->getPrimaryProviders(); |
|
339 | + if (count($providers) === 1) { |
|
340 | + // Single provider, hence we can redirect to that provider's challenge page directly |
|
341 | + /* @var $provider IProvider */ |
|
342 | + $provider = array_pop($providers); |
|
343 | + $url = 'core.TwoFactorChallenge.showChallenge'; |
|
344 | + $urlParams = [ |
|
345 | + 'challengeProviderId' => $provider->getId(), |
|
346 | + ]; |
|
347 | + } else { |
|
348 | + $url = 'core.TwoFactorChallenge.selectChallenge'; |
|
349 | + $urlParams = []; |
|
350 | + } |
|
351 | + |
|
352 | + if (!is_null($redirect_url)) { |
|
353 | + $urlParams['redirect_url'] = $redirect_url; |
|
354 | + } |
|
355 | + |
|
356 | + return new RedirectResponse($this->urlGenerator->linkToRoute($url, $urlParams)); |
|
357 | + } |
|
358 | + |
|
359 | + if ($remember_login) { |
|
360 | + $this->userSession->createRememberMeToken($loginResult); |
|
361 | + } |
|
362 | + |
|
363 | + return $this->generateRedirect($redirect_url); |
|
364 | + } |
|
365 | + |
|
366 | + /** |
|
367 | + * Creates a login failed response. |
|
368 | + * |
|
369 | + * @param string $user |
|
370 | + * @param string $originalUser |
|
371 | + * @param string $redirect_url |
|
372 | + * @param string $loginMessage |
|
373 | + * @return RedirectResponse |
|
374 | + */ |
|
375 | + private function createLoginFailedResponse( |
|
376 | + $user, $originalUser, $redirect_url, string $loginMessage) { |
|
377 | + // Read current user and append if possible we need to |
|
378 | + // return the unmodified user otherwise we will leak the login name |
|
379 | + $args = !is_null($user) ? ['user' => $originalUser] : []; |
|
380 | + if (!is_null($redirect_url)) { |
|
381 | + $args['redirect_url'] = $redirect_url; |
|
382 | + } |
|
383 | + $response = new RedirectResponse( |
|
384 | + $this->urlGenerator->linkToRoute('core.login.showLoginForm', $args) |
|
385 | + ); |
|
386 | + $response->throttle(['user' => substr($user, 0, 64)]); |
|
387 | + $this->session->set('loginMessages', [ |
|
388 | + [$loginMessage], [] |
|
389 | + ]); |
|
390 | + return $response; |
|
391 | + } |
|
392 | + |
|
393 | + /** |
|
394 | + * @NoAdminRequired |
|
395 | + * @UseSession |
|
396 | + * @BruteForceProtection(action=sudo) |
|
397 | + * |
|
398 | + * @license GNU AGPL version 3 or any later version |
|
399 | + * |
|
400 | + * @param string $password |
|
401 | + * @return DataResponse |
|
402 | + */ |
|
403 | + public function confirmPassword($password) { |
|
404 | + $loginName = $this->userSession->getLoginName(); |
|
405 | + $loginResult = $this->userManager->checkPassword($loginName, $password); |
|
406 | + if ($loginResult === false) { |
|
407 | + $response = new DataResponse([], Http::STATUS_FORBIDDEN); |
|
408 | + $response->throttle(); |
|
409 | + return $response; |
|
410 | + } |
|
411 | + |
|
412 | + $confirmTimestamp = time(); |
|
413 | + $this->session->set('last-password-confirm', $confirmTimestamp); |
|
414 | + return new DataResponse(['lastLogin' => $confirmTimestamp], Http::STATUS_OK); |
|
415 | + } |
|
416 | 416 | } |