@@ -90,944 +90,944 @@ |
||
90 | 90 | */ |
91 | 91 | class Session implements IUserSession, Emitter { |
92 | 92 | |
93 | - /** @var Manager $manager */ |
|
94 | - private $manager; |
|
95 | - |
|
96 | - /** @var ISession $session */ |
|
97 | - private $session; |
|
98 | - |
|
99 | - /** @var ITimeFactory */ |
|
100 | - private $timeFactory; |
|
101 | - |
|
102 | - /** @var IProvider */ |
|
103 | - private $tokenProvider; |
|
104 | - |
|
105 | - /** @var IConfig */ |
|
106 | - private $config; |
|
107 | - |
|
108 | - /** @var User $activeUser */ |
|
109 | - protected $activeUser; |
|
110 | - |
|
111 | - /** @var ISecureRandom */ |
|
112 | - private $random; |
|
113 | - |
|
114 | - /** @var ILockdownManager */ |
|
115 | - private $lockdownManager; |
|
116 | - |
|
117 | - private LoggerInterface $logger; |
|
118 | - /** @var IEventDispatcher */ |
|
119 | - private $dispatcher; |
|
120 | - |
|
121 | - public function __construct(Manager $manager, |
|
122 | - ISession $session, |
|
123 | - ITimeFactory $timeFactory, |
|
124 | - ?IProvider $tokenProvider, |
|
125 | - IConfig $config, |
|
126 | - ISecureRandom $random, |
|
127 | - ILockdownManager $lockdownManager, |
|
128 | - LoggerInterface $logger, |
|
129 | - IEventDispatcher $dispatcher |
|
130 | - ) { |
|
131 | - $this->manager = $manager; |
|
132 | - $this->session = $session; |
|
133 | - $this->timeFactory = $timeFactory; |
|
134 | - $this->tokenProvider = $tokenProvider; |
|
135 | - $this->config = $config; |
|
136 | - $this->random = $random; |
|
137 | - $this->lockdownManager = $lockdownManager; |
|
138 | - $this->logger = $logger; |
|
139 | - $this->dispatcher = $dispatcher; |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * @param IProvider $provider |
|
144 | - */ |
|
145 | - public function setTokenProvider(IProvider $provider) { |
|
146 | - $this->tokenProvider = $provider; |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * @param string $scope |
|
151 | - * @param string $method |
|
152 | - * @param callable $callback |
|
153 | - */ |
|
154 | - public function listen($scope, $method, callable $callback) { |
|
155 | - $this->manager->listen($scope, $method, $callback); |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * @param string $scope optional |
|
160 | - * @param string $method optional |
|
161 | - * @param callable $callback optional |
|
162 | - */ |
|
163 | - public function removeListener($scope = null, $method = null, callable $callback = null) { |
|
164 | - $this->manager->removeListener($scope, $method, $callback); |
|
165 | - } |
|
166 | - |
|
167 | - /** |
|
168 | - * get the manager object |
|
169 | - * |
|
170 | - * @return Manager|PublicEmitter |
|
171 | - */ |
|
172 | - public function getManager() { |
|
173 | - return $this->manager; |
|
174 | - } |
|
175 | - |
|
176 | - /** |
|
177 | - * get the session object |
|
178 | - * |
|
179 | - * @return ISession |
|
180 | - */ |
|
181 | - public function getSession() { |
|
182 | - return $this->session; |
|
183 | - } |
|
184 | - |
|
185 | - /** |
|
186 | - * set the session object |
|
187 | - * |
|
188 | - * @param ISession $session |
|
189 | - */ |
|
190 | - public function setSession(ISession $session) { |
|
191 | - if ($this->session instanceof ISession) { |
|
192 | - $this->session->close(); |
|
193 | - } |
|
194 | - $this->session = $session; |
|
195 | - $this->activeUser = null; |
|
196 | - } |
|
197 | - |
|
198 | - /** |
|
199 | - * set the currently active user |
|
200 | - * |
|
201 | - * @param IUser|null $user |
|
202 | - */ |
|
203 | - public function setUser($user) { |
|
204 | - if (is_null($user)) { |
|
205 | - $this->session->remove('user_id'); |
|
206 | - } else { |
|
207 | - $this->session->set('user_id', $user->getUID()); |
|
208 | - } |
|
209 | - $this->activeUser = $user; |
|
210 | - } |
|
211 | - |
|
212 | - /** |
|
213 | - * get the current active user |
|
214 | - * |
|
215 | - * @return IUser|null Current user, otherwise null |
|
216 | - */ |
|
217 | - public function getUser() { |
|
218 | - // FIXME: This is a quick'n dirty work-around for the incognito mode as |
|
219 | - // described at https://github.com/owncloud/core/pull/12912#issuecomment-67391155 |
|
220 | - if (OC_User::isIncognitoMode()) { |
|
221 | - return null; |
|
222 | - } |
|
223 | - if (is_null($this->activeUser)) { |
|
224 | - $uid = $this->session->get('user_id'); |
|
225 | - if (is_null($uid)) { |
|
226 | - return null; |
|
227 | - } |
|
228 | - $this->activeUser = $this->manager->get($uid); |
|
229 | - if (is_null($this->activeUser)) { |
|
230 | - return null; |
|
231 | - } |
|
232 | - $this->validateSession(); |
|
233 | - } |
|
234 | - return $this->activeUser; |
|
235 | - } |
|
236 | - |
|
237 | - /** |
|
238 | - * Validate whether the current session is valid |
|
239 | - * |
|
240 | - * - For token-authenticated clients, the token validity is checked |
|
241 | - * - For browsers, the session token validity is checked |
|
242 | - */ |
|
243 | - protected function validateSession() { |
|
244 | - $token = null; |
|
245 | - $appPassword = $this->session->get('app_password'); |
|
246 | - |
|
247 | - if (is_null($appPassword)) { |
|
248 | - try { |
|
249 | - $token = $this->session->getId(); |
|
250 | - } catch (SessionNotAvailableException $ex) { |
|
251 | - return; |
|
252 | - } |
|
253 | - } else { |
|
254 | - $token = $appPassword; |
|
255 | - } |
|
256 | - |
|
257 | - if (!$this->validateToken($token)) { |
|
258 | - // Session was invalidated |
|
259 | - $this->logout(); |
|
260 | - } |
|
261 | - } |
|
262 | - |
|
263 | - /** |
|
264 | - * Checks whether the user is logged in |
|
265 | - * |
|
266 | - * @return bool if logged in |
|
267 | - */ |
|
268 | - public function isLoggedIn() { |
|
269 | - $user = $this->getUser(); |
|
270 | - if (is_null($user)) { |
|
271 | - return false; |
|
272 | - } |
|
273 | - |
|
274 | - return $user->isEnabled(); |
|
275 | - } |
|
276 | - |
|
277 | - /** |
|
278 | - * set the login name |
|
279 | - * |
|
280 | - * @param string|null $loginName for the logged in user |
|
281 | - */ |
|
282 | - public function setLoginName($loginName) { |
|
283 | - if (is_null($loginName)) { |
|
284 | - $this->session->remove('loginname'); |
|
285 | - } else { |
|
286 | - $this->session->set('loginname', $loginName); |
|
287 | - } |
|
288 | - } |
|
289 | - |
|
290 | - /** |
|
291 | - * Get the login name of the current user |
|
292 | - * |
|
293 | - * @return ?string |
|
294 | - */ |
|
295 | - public function getLoginName() { |
|
296 | - if ($this->activeUser) { |
|
297 | - return $this->session->get('loginname'); |
|
298 | - } |
|
299 | - |
|
300 | - $uid = $this->session->get('user_id'); |
|
301 | - if ($uid) { |
|
302 | - $this->activeUser = $this->manager->get($uid); |
|
303 | - return $this->session->get('loginname'); |
|
304 | - } |
|
305 | - |
|
306 | - return null; |
|
307 | - } |
|
308 | - |
|
309 | - /** |
|
310 | - * @return null|string |
|
311 | - */ |
|
312 | - public function getImpersonatingUserID(): ?string { |
|
313 | - return $this->session->get('oldUserId'); |
|
314 | - } |
|
315 | - |
|
316 | - public function setImpersonatingUserID(bool $useCurrentUser = true): void { |
|
317 | - if ($useCurrentUser === false) { |
|
318 | - $this->session->remove('oldUserId'); |
|
319 | - return; |
|
320 | - } |
|
321 | - |
|
322 | - $currentUser = $this->getUser(); |
|
323 | - |
|
324 | - if ($currentUser === null) { |
|
325 | - throw new \OC\User\NoUserException(); |
|
326 | - } |
|
327 | - $this->session->set('oldUserId', $currentUser->getUID()); |
|
328 | - } |
|
329 | - /** |
|
330 | - * set the token id |
|
331 | - * |
|
332 | - * @param int|null $token that was used to log in |
|
333 | - */ |
|
334 | - protected function setToken($token) { |
|
335 | - if ($token === null) { |
|
336 | - $this->session->remove('token-id'); |
|
337 | - } else { |
|
338 | - $this->session->set('token-id', $token); |
|
339 | - } |
|
340 | - } |
|
341 | - |
|
342 | - /** |
|
343 | - * try to log in with the provided credentials |
|
344 | - * |
|
345 | - * @param string $uid |
|
346 | - * @param string $password |
|
347 | - * @return boolean|null |
|
348 | - * @throws LoginException |
|
349 | - */ |
|
350 | - public function login($uid, $password) { |
|
351 | - $this->session->regenerateId(); |
|
352 | - if ($this->validateToken($password, $uid)) { |
|
353 | - return $this->loginWithToken($password); |
|
354 | - } |
|
355 | - return $this->loginWithPassword($uid, $password); |
|
356 | - } |
|
357 | - |
|
358 | - /** |
|
359 | - * @param IUser $user |
|
360 | - * @param array $loginDetails |
|
361 | - * @param bool $regenerateSessionId |
|
362 | - * @return true returns true if login successful or an exception otherwise |
|
363 | - * @throws LoginException |
|
364 | - */ |
|
365 | - public function completeLogin(IUser $user, array $loginDetails, $regenerateSessionId = true) { |
|
366 | - if (!$user->isEnabled()) { |
|
367 | - // disabled users can not log in |
|
368 | - // injecting l10n does not work - there is a circular dependency between session and \OCP\L10N\IFactory |
|
369 | - $message = \OC::$server->getL10N('lib')->t('User disabled'); |
|
370 | - throw new LoginException($message); |
|
371 | - } |
|
372 | - |
|
373 | - if ($regenerateSessionId) { |
|
374 | - $this->session->regenerateId(); |
|
375 | - } |
|
376 | - |
|
377 | - $this->setUser($user); |
|
378 | - $this->setLoginName($loginDetails['loginName']); |
|
379 | - |
|
380 | - $isToken = isset($loginDetails['token']) && $loginDetails['token'] instanceof IToken; |
|
381 | - if ($isToken) { |
|
382 | - $this->setToken($loginDetails['token']->getId()); |
|
383 | - $this->lockdownManager->setToken($loginDetails['token']); |
|
384 | - $firstTimeLogin = false; |
|
385 | - } else { |
|
386 | - $this->setToken(null); |
|
387 | - $firstTimeLogin = $user->updateLastLoginTimestamp(); |
|
388 | - } |
|
389 | - |
|
390 | - $this->dispatcher->dispatchTyped(new PostLoginEvent( |
|
391 | - $user, |
|
392 | - $loginDetails['loginName'], |
|
393 | - $loginDetails['password'], |
|
394 | - $isToken |
|
395 | - )); |
|
396 | - $this->manager->emit('\OC\User', 'postLogin', [ |
|
397 | - $user, |
|
398 | - $loginDetails['loginName'], |
|
399 | - $loginDetails['password'], |
|
400 | - $isToken, |
|
401 | - ]); |
|
402 | - if ($this->isLoggedIn()) { |
|
403 | - $this->prepareUserLogin($firstTimeLogin, $regenerateSessionId); |
|
404 | - return true; |
|
405 | - } |
|
406 | - |
|
407 | - $message = \OC::$server->getL10N('lib')->t('Login canceled by app'); |
|
408 | - throw new LoginException($message); |
|
409 | - } |
|
410 | - |
|
411 | - /** |
|
412 | - * Tries to log in a client |
|
413 | - * |
|
414 | - * Checks token auth enforced |
|
415 | - * Checks 2FA enabled |
|
416 | - * |
|
417 | - * @param string $user |
|
418 | - * @param string $password |
|
419 | - * @param IRequest $request |
|
420 | - * @param OC\Security\Bruteforce\Throttler $throttler |
|
421 | - * @throws LoginException |
|
422 | - * @throws PasswordLoginForbiddenException |
|
423 | - * @return boolean |
|
424 | - */ |
|
425 | - public function logClientIn($user, |
|
426 | - $password, |
|
427 | - IRequest $request, |
|
428 | - OC\Security\Bruteforce\Throttler $throttler) { |
|
429 | - $currentDelay = $throttler->sleepDelay($request->getRemoteAddress(), 'login'); |
|
430 | - |
|
431 | - if ($this->manager instanceof PublicEmitter) { |
|
432 | - $this->manager->emit('\OC\User', 'preLogin', [$user, $password]); |
|
433 | - } |
|
434 | - |
|
435 | - try { |
|
436 | - $isTokenPassword = $this->isTokenPassword($password); |
|
437 | - } catch (ExpiredTokenException $e) { |
|
438 | - // Just return on an expired token no need to check further or record a failed login |
|
439 | - return false; |
|
440 | - } |
|
441 | - |
|
442 | - if (!$isTokenPassword && $this->isTokenAuthEnforced()) { |
|
443 | - throw new PasswordLoginForbiddenException(); |
|
444 | - } |
|
445 | - if (!$isTokenPassword && $this->isTwoFactorEnforced($user)) { |
|
446 | - throw new PasswordLoginForbiddenException(); |
|
447 | - } |
|
448 | - |
|
449 | - // Try to login with this username and password |
|
450 | - if (!$this->login($user, $password)) { |
|
451 | - |
|
452 | - // Failed, maybe the user used their email address |
|
453 | - if (!filter_var($user, FILTER_VALIDATE_EMAIL)) { |
|
454 | - return false; |
|
455 | - } |
|
456 | - $users = $this->manager->getByEmail($user); |
|
457 | - if (!(\count($users) === 1 && $this->login($users[0]->getUID(), $password))) { |
|
458 | - $this->logger->warning('Login failed: \'' . $user . '\' (Remote IP: \'' . \OC::$server->getRequest()->getRemoteAddress() . '\')', ['app' => 'core']); |
|
459 | - |
|
460 | - $throttler->registerAttempt('login', $request->getRemoteAddress(), ['user' => $user]); |
|
461 | - |
|
462 | - $this->dispatcher->dispatchTyped(new OC\Authentication\Events\LoginFailed($user)); |
|
463 | - |
|
464 | - if ($currentDelay === 0) { |
|
465 | - $throttler->sleepDelay($request->getRemoteAddress(), 'login'); |
|
466 | - } |
|
467 | - return false; |
|
468 | - } |
|
469 | - } |
|
470 | - |
|
471 | - if ($isTokenPassword) { |
|
472 | - $this->session->set('app_password', $password); |
|
473 | - } elseif ($this->supportsCookies($request)) { |
|
474 | - // Password login, but cookies supported -> create (browser) session token |
|
475 | - $this->createSessionToken($request, $this->getUser()->getUID(), $user, $password); |
|
476 | - } |
|
477 | - |
|
478 | - return true; |
|
479 | - } |
|
480 | - |
|
481 | - protected function supportsCookies(IRequest $request) { |
|
482 | - if (!is_null($request->getCookie('cookie_test'))) { |
|
483 | - return true; |
|
484 | - } |
|
485 | - setcookie('cookie_test', 'test', $this->timeFactory->getTime() + 3600); |
|
486 | - return false; |
|
487 | - } |
|
488 | - |
|
489 | - private function isTokenAuthEnforced() { |
|
490 | - return $this->config->getSystemValue('token_auth_enforced', false); |
|
491 | - } |
|
492 | - |
|
493 | - protected function isTwoFactorEnforced($username) { |
|
494 | - Util::emitHook( |
|
495 | - '\OCA\Files_Sharing\API\Server2Server', |
|
496 | - 'preLoginNameUsedAsUserName', |
|
497 | - ['uid' => &$username] |
|
498 | - ); |
|
499 | - $user = $this->manager->get($username); |
|
500 | - if (is_null($user)) { |
|
501 | - $users = $this->manager->getByEmail($username); |
|
502 | - if (empty($users)) { |
|
503 | - return false; |
|
504 | - } |
|
505 | - if (count($users) !== 1) { |
|
506 | - return true; |
|
507 | - } |
|
508 | - $user = $users[0]; |
|
509 | - } |
|
510 | - // DI not possible due to cyclic dependencies :'-/ |
|
511 | - return OC::$server->getTwoFactorAuthManager()->isTwoFactorAuthenticated($user); |
|
512 | - } |
|
513 | - |
|
514 | - /** |
|
515 | - * Check if the given 'password' is actually a device token |
|
516 | - * |
|
517 | - * @param string $password |
|
518 | - * @return boolean |
|
519 | - * @throws ExpiredTokenException |
|
520 | - */ |
|
521 | - public function isTokenPassword($password) { |
|
522 | - try { |
|
523 | - $this->tokenProvider->getToken($password); |
|
524 | - return true; |
|
525 | - } catch (ExpiredTokenException $e) { |
|
526 | - throw $e; |
|
527 | - } catch (InvalidTokenException $ex) { |
|
528 | - $this->logger->debug('Token is not valid: ' . $ex->getMessage(), [ |
|
529 | - 'exception' => $ex, |
|
530 | - ]); |
|
531 | - return false; |
|
532 | - } |
|
533 | - } |
|
534 | - |
|
535 | - protected function prepareUserLogin($firstTimeLogin, $refreshCsrfToken = true) { |
|
536 | - if ($refreshCsrfToken) { |
|
537 | - // TODO: mock/inject/use non-static |
|
538 | - // Refresh the token |
|
539 | - \OC::$server->getCsrfTokenManager()->refreshToken(); |
|
540 | - } |
|
541 | - |
|
542 | - if ($firstTimeLogin) { |
|
543 | - //we need to pass the user name, which may differ from login name |
|
544 | - $user = $this->getUser()->getUID(); |
|
545 | - OC_Util::setupFS($user); |
|
546 | - |
|
547 | - // TODO: lock necessary? |
|
548 | - //trigger creation of user home and /files folder |
|
549 | - $userFolder = \OC::$server->getUserFolder($user); |
|
550 | - |
|
551 | - try { |
|
552 | - // copy skeleton |
|
553 | - \OC_Util::copySkeleton($user, $userFolder); |
|
554 | - } catch (NotPermittedException $ex) { |
|
555 | - // read only uses |
|
556 | - } |
|
557 | - |
|
558 | - // trigger any other initialization |
|
559 | - \OC::$server->getEventDispatcher()->dispatch(IUser::class . '::firstLogin', new GenericEvent($this->getUser())); |
|
560 | - } |
|
561 | - } |
|
562 | - |
|
563 | - /** |
|
564 | - * Tries to login the user with HTTP Basic Authentication |
|
565 | - * |
|
566 | - * @todo do not allow basic auth if the user is 2FA enforced |
|
567 | - * @param IRequest $request |
|
568 | - * @param OC\Security\Bruteforce\Throttler $throttler |
|
569 | - * @return boolean if the login was successful |
|
570 | - */ |
|
571 | - public function tryBasicAuthLogin(IRequest $request, |
|
572 | - OC\Security\Bruteforce\Throttler $throttler) { |
|
573 | - if (!empty($request->server['PHP_AUTH_USER']) && !empty($request->server['PHP_AUTH_PW'])) { |
|
574 | - try { |
|
575 | - if ($this->logClientIn($request->server['PHP_AUTH_USER'], $request->server['PHP_AUTH_PW'], $request, $throttler)) { |
|
576 | - /** |
|
577 | - * Add DAV authenticated. This should in an ideal world not be |
|
578 | - * necessary but the iOS App reads cookies from anywhere instead |
|
579 | - * only the DAV endpoint. |
|
580 | - * This makes sure that the cookies will be valid for the whole scope |
|
581 | - * @see https://github.com/owncloud/core/issues/22893 |
|
582 | - */ |
|
583 | - $this->session->set( |
|
584 | - Auth::DAV_AUTHENTICATED, $this->getUser()->getUID() |
|
585 | - ); |
|
586 | - |
|
587 | - // Set the last-password-confirm session to make the sudo mode work |
|
588 | - $this->session->set('last-password-confirm', $this->timeFactory->getTime()); |
|
589 | - |
|
590 | - return true; |
|
591 | - } |
|
592 | - // If credentials were provided, they need to be valid, otherwise we do boom |
|
593 | - throw new LoginException(); |
|
594 | - } catch (PasswordLoginForbiddenException $ex) { |
|
595 | - // Nothing to do |
|
596 | - } |
|
597 | - } |
|
598 | - return false; |
|
599 | - } |
|
600 | - |
|
601 | - /** |
|
602 | - * Log an user in via login name and password |
|
603 | - * |
|
604 | - * @param string $uid |
|
605 | - * @param string $password |
|
606 | - * @return boolean |
|
607 | - * @throws LoginException if an app canceld the login process or the user is not enabled |
|
608 | - */ |
|
609 | - private function loginWithPassword($uid, $password) { |
|
610 | - $user = $this->manager->checkPasswordNoLogging($uid, $password); |
|
611 | - if ($user === false) { |
|
612 | - // Password check failed |
|
613 | - return false; |
|
614 | - } |
|
615 | - |
|
616 | - return $this->completeLogin($user, ['loginName' => $uid, 'password' => $password], false); |
|
617 | - } |
|
618 | - |
|
619 | - /** |
|
620 | - * Log an user in with a given token (id) |
|
621 | - * |
|
622 | - * @param string $token |
|
623 | - * @return boolean |
|
624 | - * @throws LoginException if an app canceled the login process or the user is not enabled |
|
625 | - */ |
|
626 | - private function loginWithToken($token) { |
|
627 | - try { |
|
628 | - $dbToken = $this->tokenProvider->getToken($token); |
|
629 | - } catch (InvalidTokenException $ex) { |
|
630 | - return false; |
|
631 | - } |
|
632 | - $uid = $dbToken->getUID(); |
|
633 | - |
|
634 | - // When logging in with token, the password must be decrypted first before passing to login hook |
|
635 | - $password = ''; |
|
636 | - try { |
|
637 | - $password = $this->tokenProvider->getPassword($dbToken, $token); |
|
638 | - } catch (PasswordlessTokenException $ex) { |
|
639 | - // Ignore and use empty string instead |
|
640 | - } |
|
641 | - |
|
642 | - $this->manager->emit('\OC\User', 'preLogin', [$dbToken->getLoginName(), $password]); |
|
643 | - |
|
644 | - $user = $this->manager->get($uid); |
|
645 | - if (is_null($user)) { |
|
646 | - // user does not exist |
|
647 | - return false; |
|
648 | - } |
|
649 | - |
|
650 | - return $this->completeLogin( |
|
651 | - $user, |
|
652 | - [ |
|
653 | - 'loginName' => $dbToken->getLoginName(), |
|
654 | - 'password' => $password, |
|
655 | - 'token' => $dbToken |
|
656 | - ], |
|
657 | - false); |
|
658 | - } |
|
659 | - |
|
660 | - /** |
|
661 | - * Create a new session token for the given user credentials |
|
662 | - * |
|
663 | - * @param IRequest $request |
|
664 | - * @param string $uid user UID |
|
665 | - * @param string $loginName login name |
|
666 | - * @param string $password |
|
667 | - * @param int $remember |
|
668 | - * @return boolean |
|
669 | - */ |
|
670 | - public function createSessionToken(IRequest $request, $uid, $loginName, $password = null, $remember = IToken::DO_NOT_REMEMBER) { |
|
671 | - if (is_null($this->manager->get($uid))) { |
|
672 | - // User does not exist |
|
673 | - return false; |
|
674 | - } |
|
675 | - $name = isset($request->server['HTTP_USER_AGENT']) ? utf8_encode($request->server['HTTP_USER_AGENT']) : 'unknown browser'; |
|
676 | - try { |
|
677 | - $sessionId = $this->session->getId(); |
|
678 | - $pwd = $this->getPassword($password); |
|
679 | - // Make sure the current sessionId has no leftover tokens |
|
680 | - $this->tokenProvider->invalidateToken($sessionId); |
|
681 | - $this->tokenProvider->generateToken($sessionId, $uid, $loginName, $pwd, $name, IToken::TEMPORARY_TOKEN, $remember); |
|
682 | - return true; |
|
683 | - } catch (SessionNotAvailableException $ex) { |
|
684 | - // This can happen with OCC, where a memory session is used |
|
685 | - // if a memory session is used, we shouldn't create a session token anyway |
|
686 | - return false; |
|
687 | - } |
|
688 | - } |
|
689 | - |
|
690 | - /** |
|
691 | - * Checks if the given password is a token. |
|
692 | - * If yes, the password is extracted from the token. |
|
693 | - * If no, the same password is returned. |
|
694 | - * |
|
695 | - * @param string $password either the login password or a device token |
|
696 | - * @return string|null the password or null if none was set in the token |
|
697 | - */ |
|
698 | - private function getPassword($password) { |
|
699 | - if (is_null($password)) { |
|
700 | - // This is surely no token ;-) |
|
701 | - return null; |
|
702 | - } |
|
703 | - try { |
|
704 | - $token = $this->tokenProvider->getToken($password); |
|
705 | - try { |
|
706 | - return $this->tokenProvider->getPassword($token, $password); |
|
707 | - } catch (PasswordlessTokenException $ex) { |
|
708 | - return null; |
|
709 | - } |
|
710 | - } catch (InvalidTokenException $ex) { |
|
711 | - return $password; |
|
712 | - } |
|
713 | - } |
|
714 | - |
|
715 | - /** |
|
716 | - * @param IToken $dbToken |
|
717 | - * @param string $token |
|
718 | - * @return boolean |
|
719 | - */ |
|
720 | - private function checkTokenCredentials(IToken $dbToken, $token) { |
|
721 | - // Check whether login credentials are still valid and the user was not disabled |
|
722 | - // This check is performed each 5 minutes |
|
723 | - $lastCheck = $dbToken->getLastCheck() ? : 0; |
|
724 | - $now = $this->timeFactory->getTime(); |
|
725 | - if ($lastCheck > ($now - 60 * 5)) { |
|
726 | - // Checked performed recently, nothing to do now |
|
727 | - return true; |
|
728 | - } |
|
729 | - |
|
730 | - try { |
|
731 | - $pwd = $this->tokenProvider->getPassword($dbToken, $token); |
|
732 | - } catch (InvalidTokenException $ex) { |
|
733 | - // An invalid token password was used -> log user out |
|
734 | - return false; |
|
735 | - } catch (PasswordlessTokenException $ex) { |
|
736 | - // Token has no password |
|
737 | - |
|
738 | - if (!is_null($this->activeUser) && !$this->activeUser->isEnabled()) { |
|
739 | - $this->tokenProvider->invalidateToken($token); |
|
740 | - return false; |
|
741 | - } |
|
742 | - |
|
743 | - $dbToken->setLastCheck($now); |
|
744 | - $this->tokenProvider->updateToken($dbToken); |
|
745 | - return true; |
|
746 | - } |
|
747 | - |
|
748 | - // Invalidate token if the user is no longer active |
|
749 | - if (!is_null($this->activeUser) && !$this->activeUser->isEnabled()) { |
|
750 | - $this->tokenProvider->invalidateToken($token); |
|
751 | - return false; |
|
752 | - } |
|
753 | - |
|
754 | - // If the token password is no longer valid mark it as such |
|
755 | - if ($this->manager->checkPassword($dbToken->getLoginName(), $pwd) === false) { |
|
756 | - $this->tokenProvider->markPasswordInvalid($dbToken, $token); |
|
757 | - // User is logged out |
|
758 | - return false; |
|
759 | - } |
|
760 | - |
|
761 | - $dbToken->setLastCheck($now); |
|
762 | - $this->tokenProvider->updateToken($dbToken); |
|
763 | - return true; |
|
764 | - } |
|
765 | - |
|
766 | - /** |
|
767 | - * Check if the given token exists and performs password/user-enabled checks |
|
768 | - * |
|
769 | - * Invalidates the token if checks fail |
|
770 | - * |
|
771 | - * @param string $token |
|
772 | - * @param string $user login name |
|
773 | - * @return boolean |
|
774 | - */ |
|
775 | - private function validateToken($token, $user = null) { |
|
776 | - try { |
|
777 | - $dbToken = $this->tokenProvider->getToken($token); |
|
778 | - } catch (InvalidTokenException $ex) { |
|
779 | - return false; |
|
780 | - } |
|
781 | - |
|
782 | - // Check if login names match |
|
783 | - if (!is_null($user) && $dbToken->getLoginName() !== $user) { |
|
784 | - // TODO: this makes it impossible to use different login names on browser and client |
|
785 | - // e.g. login by e-mail '[email protected]' on browser for generating the token will not |
|
786 | - // allow to use the client token with the login name 'user'. |
|
787 | - $this->logger->error('App token login name does not match', [ |
|
788 | - 'tokenLoginName' => $dbToken->getLoginName(), |
|
789 | - 'sessionLoginName' => $user, |
|
790 | - ]); |
|
791 | - |
|
792 | - return false; |
|
793 | - } |
|
794 | - |
|
795 | - if (!$this->checkTokenCredentials($dbToken, $token)) { |
|
796 | - return false; |
|
797 | - } |
|
798 | - |
|
799 | - // Update token scope |
|
800 | - $this->lockdownManager->setToken($dbToken); |
|
801 | - |
|
802 | - $this->tokenProvider->updateTokenActivity($dbToken); |
|
803 | - |
|
804 | - return true; |
|
805 | - } |
|
806 | - |
|
807 | - /** |
|
808 | - * Tries to login the user with auth token header |
|
809 | - * |
|
810 | - * @param IRequest $request |
|
811 | - * @todo check remember me cookie |
|
812 | - * @return boolean |
|
813 | - */ |
|
814 | - public function tryTokenLogin(IRequest $request) { |
|
815 | - $authHeader = $request->getHeader('Authorization'); |
|
816 | - if (strpos($authHeader, 'Bearer ') === 0) { |
|
817 | - $token = substr($authHeader, 7); |
|
818 | - } else { |
|
819 | - // No auth header, let's try session id |
|
820 | - try { |
|
821 | - $token = $this->session->getId(); |
|
822 | - } catch (SessionNotAvailableException $ex) { |
|
823 | - return false; |
|
824 | - } |
|
825 | - } |
|
826 | - |
|
827 | - if (!$this->loginWithToken($token)) { |
|
828 | - return false; |
|
829 | - } |
|
830 | - if (!$this->validateToken($token)) { |
|
831 | - return false; |
|
832 | - } |
|
833 | - |
|
834 | - try { |
|
835 | - $dbToken = $this->tokenProvider->getToken($token); |
|
836 | - } catch (InvalidTokenException $e) { |
|
837 | - // Can't really happen but better save than sorry |
|
838 | - return true; |
|
839 | - } |
|
840 | - |
|
841 | - // Remember me tokens are not app_passwords |
|
842 | - if ($dbToken->getRemember() === IToken::DO_NOT_REMEMBER) { |
|
843 | - // Set the session variable so we know this is an app password |
|
844 | - $this->session->set('app_password', $token); |
|
845 | - } |
|
846 | - |
|
847 | - return true; |
|
848 | - } |
|
849 | - |
|
850 | - /** |
|
851 | - * perform login using the magic cookie (remember login) |
|
852 | - * |
|
853 | - * @param string $uid the username |
|
854 | - * @param string $currentToken |
|
855 | - * @param string $oldSessionId |
|
856 | - * @return bool |
|
857 | - */ |
|
858 | - public function loginWithCookie($uid, $currentToken, $oldSessionId) { |
|
859 | - $this->session->regenerateId(); |
|
860 | - $this->manager->emit('\OC\User', 'preRememberedLogin', [$uid]); |
|
861 | - $user = $this->manager->get($uid); |
|
862 | - if (is_null($user)) { |
|
863 | - // user does not exist |
|
864 | - return false; |
|
865 | - } |
|
866 | - |
|
867 | - // get stored tokens |
|
868 | - $tokens = $this->config->getUserKeys($uid, 'login_token'); |
|
869 | - // test cookies token against stored tokens |
|
870 | - if (!in_array($currentToken, $tokens, true)) { |
|
871 | - $this->logger->info('Tried to log in {uid} but could not verify token', [ |
|
872 | - 'app' => 'core', |
|
873 | - 'uid' => $uid, |
|
874 | - ]); |
|
875 | - return false; |
|
876 | - } |
|
877 | - // replace successfully used token with a new one |
|
878 | - $this->config->deleteUserValue($uid, 'login_token', $currentToken); |
|
879 | - $newToken = $this->random->generate(32); |
|
880 | - $this->config->setUserValue($uid, 'login_token', $newToken, (string)$this->timeFactory->getTime()); |
|
881 | - |
|
882 | - try { |
|
883 | - $sessionId = $this->session->getId(); |
|
884 | - $token = $this->tokenProvider->renewSessionToken($oldSessionId, $sessionId); |
|
885 | - } catch (SessionNotAvailableException $ex) { |
|
886 | - $this->logger->warning('Could not renew session token for {uid} because the session is unavailable', [ |
|
887 | - 'app' => 'core', |
|
888 | - 'uid' => $uid, |
|
889 | - ]); |
|
890 | - return false; |
|
891 | - } catch (InvalidTokenException $ex) { |
|
892 | - $this->logger->warning('Renewing session token failed', ['app' => 'core']); |
|
893 | - return false; |
|
894 | - } |
|
895 | - |
|
896 | - $this->setMagicInCookie($user->getUID(), $newToken); |
|
897 | - |
|
898 | - //login |
|
899 | - $this->setUser($user); |
|
900 | - $this->setLoginName($token->getLoginName()); |
|
901 | - $this->setToken($token->getId()); |
|
902 | - $this->lockdownManager->setToken($token); |
|
903 | - $user->updateLastLoginTimestamp(); |
|
904 | - $password = null; |
|
905 | - try { |
|
906 | - $password = $this->tokenProvider->getPassword($token, $sessionId); |
|
907 | - } catch (PasswordlessTokenException $ex) { |
|
908 | - // Ignore |
|
909 | - } |
|
910 | - $this->manager->emit('\OC\User', 'postRememberedLogin', [$user, $password]); |
|
911 | - return true; |
|
912 | - } |
|
913 | - |
|
914 | - /** |
|
915 | - * @param IUser $user |
|
916 | - */ |
|
917 | - public function createRememberMeToken(IUser $user) { |
|
918 | - $token = $this->random->generate(32); |
|
919 | - $this->config->setUserValue($user->getUID(), 'login_token', $token, (string)$this->timeFactory->getTime()); |
|
920 | - $this->setMagicInCookie($user->getUID(), $token); |
|
921 | - } |
|
922 | - |
|
923 | - /** |
|
924 | - * logout the user from the session |
|
925 | - */ |
|
926 | - public function logout() { |
|
927 | - $user = $this->getUser(); |
|
928 | - $this->manager->emit('\OC\User', 'logout', [$user]); |
|
929 | - if ($user !== null) { |
|
930 | - try { |
|
931 | - $this->tokenProvider->invalidateToken($this->session->getId()); |
|
932 | - } catch (SessionNotAvailableException $ex) { |
|
933 | - } |
|
934 | - } |
|
935 | - $this->setUser(null); |
|
936 | - $this->setLoginName(null); |
|
937 | - $this->setToken(null); |
|
938 | - $this->unsetMagicInCookie(); |
|
939 | - $this->session->clear(); |
|
940 | - $this->manager->emit('\OC\User', 'postLogout', [$user]); |
|
941 | - } |
|
942 | - |
|
943 | - /** |
|
944 | - * Set cookie value to use in next page load |
|
945 | - * |
|
946 | - * @param string $username username to be set |
|
947 | - * @param string $token |
|
948 | - */ |
|
949 | - public function setMagicInCookie($username, $token) { |
|
950 | - $secureCookie = OC::$server->getRequest()->getServerProtocol() === 'https'; |
|
951 | - $webRoot = \OC::$WEBROOT; |
|
952 | - if ($webRoot === '') { |
|
953 | - $webRoot = '/'; |
|
954 | - } |
|
955 | - |
|
956 | - $maxAge = $this->config->getSystemValue('remember_login_cookie_lifetime', 60 * 60 * 24 * 15); |
|
957 | - \OC\Http\CookieHelper::setCookie( |
|
958 | - 'nc_username', |
|
959 | - $username, |
|
960 | - $maxAge, |
|
961 | - $webRoot, |
|
962 | - '', |
|
963 | - $secureCookie, |
|
964 | - true, |
|
965 | - \OC\Http\CookieHelper::SAMESITE_LAX |
|
966 | - ); |
|
967 | - \OC\Http\CookieHelper::setCookie( |
|
968 | - 'nc_token', |
|
969 | - $token, |
|
970 | - $maxAge, |
|
971 | - $webRoot, |
|
972 | - '', |
|
973 | - $secureCookie, |
|
974 | - true, |
|
975 | - \OC\Http\CookieHelper::SAMESITE_LAX |
|
976 | - ); |
|
977 | - try { |
|
978 | - \OC\Http\CookieHelper::setCookie( |
|
979 | - 'nc_session_id', |
|
980 | - $this->session->getId(), |
|
981 | - $maxAge, |
|
982 | - $webRoot, |
|
983 | - '', |
|
984 | - $secureCookie, |
|
985 | - true, |
|
986 | - \OC\Http\CookieHelper::SAMESITE_LAX |
|
987 | - ); |
|
988 | - } catch (SessionNotAvailableException $ex) { |
|
989 | - // ignore |
|
990 | - } |
|
991 | - } |
|
992 | - |
|
993 | - /** |
|
994 | - * Remove cookie for "remember username" |
|
995 | - */ |
|
996 | - public function unsetMagicInCookie() { |
|
997 | - //TODO: DI for cookies and IRequest |
|
998 | - $secureCookie = OC::$server->getRequest()->getServerProtocol() === 'https'; |
|
999 | - |
|
1000 | - unset($_COOKIE['nc_username']); //TODO: DI |
|
1001 | - unset($_COOKIE['nc_token']); |
|
1002 | - unset($_COOKIE['nc_session_id']); |
|
1003 | - setcookie('nc_username', '', $this->timeFactory->getTime() - 3600, OC::$WEBROOT, '', $secureCookie, true); |
|
1004 | - setcookie('nc_token', '', $this->timeFactory->getTime() - 3600, OC::$WEBROOT, '', $secureCookie, true); |
|
1005 | - setcookie('nc_session_id', '', $this->timeFactory->getTime() - 3600, OC::$WEBROOT, '', $secureCookie, true); |
|
1006 | - // old cookies might be stored under /webroot/ instead of /webroot |
|
1007 | - // and Firefox doesn't like it! |
|
1008 | - setcookie('nc_username', '', $this->timeFactory->getTime() - 3600, OC::$WEBROOT . '/', '', $secureCookie, true); |
|
1009 | - setcookie('nc_token', '', $this->timeFactory->getTime() - 3600, OC::$WEBROOT . '/', '', $secureCookie, true); |
|
1010 | - setcookie('nc_session_id', '', $this->timeFactory->getTime() - 3600, OC::$WEBROOT . '/', '', $secureCookie, true); |
|
1011 | - } |
|
1012 | - |
|
1013 | - /** |
|
1014 | - * Update password of the browser session token if there is one |
|
1015 | - * |
|
1016 | - * @param string $password |
|
1017 | - */ |
|
1018 | - public function updateSessionTokenPassword($password) { |
|
1019 | - try { |
|
1020 | - $sessionId = $this->session->getId(); |
|
1021 | - $token = $this->tokenProvider->getToken($sessionId); |
|
1022 | - $this->tokenProvider->setPassword($token, $sessionId, $password); |
|
1023 | - } catch (SessionNotAvailableException $ex) { |
|
1024 | - // Nothing to do |
|
1025 | - } catch (InvalidTokenException $ex) { |
|
1026 | - // Nothing to do |
|
1027 | - } |
|
1028 | - } |
|
1029 | - |
|
1030 | - public function updateTokens(string $uid, string $password) { |
|
1031 | - $this->tokenProvider->updatePasswords($uid, $password); |
|
1032 | - } |
|
93 | + /** @var Manager $manager */ |
|
94 | + private $manager; |
|
95 | + |
|
96 | + /** @var ISession $session */ |
|
97 | + private $session; |
|
98 | + |
|
99 | + /** @var ITimeFactory */ |
|
100 | + private $timeFactory; |
|
101 | + |
|
102 | + /** @var IProvider */ |
|
103 | + private $tokenProvider; |
|
104 | + |
|
105 | + /** @var IConfig */ |
|
106 | + private $config; |
|
107 | + |
|
108 | + /** @var User $activeUser */ |
|
109 | + protected $activeUser; |
|
110 | + |
|
111 | + /** @var ISecureRandom */ |
|
112 | + private $random; |
|
113 | + |
|
114 | + /** @var ILockdownManager */ |
|
115 | + private $lockdownManager; |
|
116 | + |
|
117 | + private LoggerInterface $logger; |
|
118 | + /** @var IEventDispatcher */ |
|
119 | + private $dispatcher; |
|
120 | + |
|
121 | + public function __construct(Manager $manager, |
|
122 | + ISession $session, |
|
123 | + ITimeFactory $timeFactory, |
|
124 | + ?IProvider $tokenProvider, |
|
125 | + IConfig $config, |
|
126 | + ISecureRandom $random, |
|
127 | + ILockdownManager $lockdownManager, |
|
128 | + LoggerInterface $logger, |
|
129 | + IEventDispatcher $dispatcher |
|
130 | + ) { |
|
131 | + $this->manager = $manager; |
|
132 | + $this->session = $session; |
|
133 | + $this->timeFactory = $timeFactory; |
|
134 | + $this->tokenProvider = $tokenProvider; |
|
135 | + $this->config = $config; |
|
136 | + $this->random = $random; |
|
137 | + $this->lockdownManager = $lockdownManager; |
|
138 | + $this->logger = $logger; |
|
139 | + $this->dispatcher = $dispatcher; |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * @param IProvider $provider |
|
144 | + */ |
|
145 | + public function setTokenProvider(IProvider $provider) { |
|
146 | + $this->tokenProvider = $provider; |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * @param string $scope |
|
151 | + * @param string $method |
|
152 | + * @param callable $callback |
|
153 | + */ |
|
154 | + public function listen($scope, $method, callable $callback) { |
|
155 | + $this->manager->listen($scope, $method, $callback); |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * @param string $scope optional |
|
160 | + * @param string $method optional |
|
161 | + * @param callable $callback optional |
|
162 | + */ |
|
163 | + public function removeListener($scope = null, $method = null, callable $callback = null) { |
|
164 | + $this->manager->removeListener($scope, $method, $callback); |
|
165 | + } |
|
166 | + |
|
167 | + /** |
|
168 | + * get the manager object |
|
169 | + * |
|
170 | + * @return Manager|PublicEmitter |
|
171 | + */ |
|
172 | + public function getManager() { |
|
173 | + return $this->manager; |
|
174 | + } |
|
175 | + |
|
176 | + /** |
|
177 | + * get the session object |
|
178 | + * |
|
179 | + * @return ISession |
|
180 | + */ |
|
181 | + public function getSession() { |
|
182 | + return $this->session; |
|
183 | + } |
|
184 | + |
|
185 | + /** |
|
186 | + * set the session object |
|
187 | + * |
|
188 | + * @param ISession $session |
|
189 | + */ |
|
190 | + public function setSession(ISession $session) { |
|
191 | + if ($this->session instanceof ISession) { |
|
192 | + $this->session->close(); |
|
193 | + } |
|
194 | + $this->session = $session; |
|
195 | + $this->activeUser = null; |
|
196 | + } |
|
197 | + |
|
198 | + /** |
|
199 | + * set the currently active user |
|
200 | + * |
|
201 | + * @param IUser|null $user |
|
202 | + */ |
|
203 | + public function setUser($user) { |
|
204 | + if (is_null($user)) { |
|
205 | + $this->session->remove('user_id'); |
|
206 | + } else { |
|
207 | + $this->session->set('user_id', $user->getUID()); |
|
208 | + } |
|
209 | + $this->activeUser = $user; |
|
210 | + } |
|
211 | + |
|
212 | + /** |
|
213 | + * get the current active user |
|
214 | + * |
|
215 | + * @return IUser|null Current user, otherwise null |
|
216 | + */ |
|
217 | + public function getUser() { |
|
218 | + // FIXME: This is a quick'n dirty work-around for the incognito mode as |
|
219 | + // described at https://github.com/owncloud/core/pull/12912#issuecomment-67391155 |
|
220 | + if (OC_User::isIncognitoMode()) { |
|
221 | + return null; |
|
222 | + } |
|
223 | + if (is_null($this->activeUser)) { |
|
224 | + $uid = $this->session->get('user_id'); |
|
225 | + if (is_null($uid)) { |
|
226 | + return null; |
|
227 | + } |
|
228 | + $this->activeUser = $this->manager->get($uid); |
|
229 | + if (is_null($this->activeUser)) { |
|
230 | + return null; |
|
231 | + } |
|
232 | + $this->validateSession(); |
|
233 | + } |
|
234 | + return $this->activeUser; |
|
235 | + } |
|
236 | + |
|
237 | + /** |
|
238 | + * Validate whether the current session is valid |
|
239 | + * |
|
240 | + * - For token-authenticated clients, the token validity is checked |
|
241 | + * - For browsers, the session token validity is checked |
|
242 | + */ |
|
243 | + protected function validateSession() { |
|
244 | + $token = null; |
|
245 | + $appPassword = $this->session->get('app_password'); |
|
246 | + |
|
247 | + if (is_null($appPassword)) { |
|
248 | + try { |
|
249 | + $token = $this->session->getId(); |
|
250 | + } catch (SessionNotAvailableException $ex) { |
|
251 | + return; |
|
252 | + } |
|
253 | + } else { |
|
254 | + $token = $appPassword; |
|
255 | + } |
|
256 | + |
|
257 | + if (!$this->validateToken($token)) { |
|
258 | + // Session was invalidated |
|
259 | + $this->logout(); |
|
260 | + } |
|
261 | + } |
|
262 | + |
|
263 | + /** |
|
264 | + * Checks whether the user is logged in |
|
265 | + * |
|
266 | + * @return bool if logged in |
|
267 | + */ |
|
268 | + public function isLoggedIn() { |
|
269 | + $user = $this->getUser(); |
|
270 | + if (is_null($user)) { |
|
271 | + return false; |
|
272 | + } |
|
273 | + |
|
274 | + return $user->isEnabled(); |
|
275 | + } |
|
276 | + |
|
277 | + /** |
|
278 | + * set the login name |
|
279 | + * |
|
280 | + * @param string|null $loginName for the logged in user |
|
281 | + */ |
|
282 | + public function setLoginName($loginName) { |
|
283 | + if (is_null($loginName)) { |
|
284 | + $this->session->remove('loginname'); |
|
285 | + } else { |
|
286 | + $this->session->set('loginname', $loginName); |
|
287 | + } |
|
288 | + } |
|
289 | + |
|
290 | + /** |
|
291 | + * Get the login name of the current user |
|
292 | + * |
|
293 | + * @return ?string |
|
294 | + */ |
|
295 | + public function getLoginName() { |
|
296 | + if ($this->activeUser) { |
|
297 | + return $this->session->get('loginname'); |
|
298 | + } |
|
299 | + |
|
300 | + $uid = $this->session->get('user_id'); |
|
301 | + if ($uid) { |
|
302 | + $this->activeUser = $this->manager->get($uid); |
|
303 | + return $this->session->get('loginname'); |
|
304 | + } |
|
305 | + |
|
306 | + return null; |
|
307 | + } |
|
308 | + |
|
309 | + /** |
|
310 | + * @return null|string |
|
311 | + */ |
|
312 | + public function getImpersonatingUserID(): ?string { |
|
313 | + return $this->session->get('oldUserId'); |
|
314 | + } |
|
315 | + |
|
316 | + public function setImpersonatingUserID(bool $useCurrentUser = true): void { |
|
317 | + if ($useCurrentUser === false) { |
|
318 | + $this->session->remove('oldUserId'); |
|
319 | + return; |
|
320 | + } |
|
321 | + |
|
322 | + $currentUser = $this->getUser(); |
|
323 | + |
|
324 | + if ($currentUser === null) { |
|
325 | + throw new \OC\User\NoUserException(); |
|
326 | + } |
|
327 | + $this->session->set('oldUserId', $currentUser->getUID()); |
|
328 | + } |
|
329 | + /** |
|
330 | + * set the token id |
|
331 | + * |
|
332 | + * @param int|null $token that was used to log in |
|
333 | + */ |
|
334 | + protected function setToken($token) { |
|
335 | + if ($token === null) { |
|
336 | + $this->session->remove('token-id'); |
|
337 | + } else { |
|
338 | + $this->session->set('token-id', $token); |
|
339 | + } |
|
340 | + } |
|
341 | + |
|
342 | + /** |
|
343 | + * try to log in with the provided credentials |
|
344 | + * |
|
345 | + * @param string $uid |
|
346 | + * @param string $password |
|
347 | + * @return boolean|null |
|
348 | + * @throws LoginException |
|
349 | + */ |
|
350 | + public function login($uid, $password) { |
|
351 | + $this->session->regenerateId(); |
|
352 | + if ($this->validateToken($password, $uid)) { |
|
353 | + return $this->loginWithToken($password); |
|
354 | + } |
|
355 | + return $this->loginWithPassword($uid, $password); |
|
356 | + } |
|
357 | + |
|
358 | + /** |
|
359 | + * @param IUser $user |
|
360 | + * @param array $loginDetails |
|
361 | + * @param bool $regenerateSessionId |
|
362 | + * @return true returns true if login successful or an exception otherwise |
|
363 | + * @throws LoginException |
|
364 | + */ |
|
365 | + public function completeLogin(IUser $user, array $loginDetails, $regenerateSessionId = true) { |
|
366 | + if (!$user->isEnabled()) { |
|
367 | + // disabled users can not log in |
|
368 | + // injecting l10n does not work - there is a circular dependency between session and \OCP\L10N\IFactory |
|
369 | + $message = \OC::$server->getL10N('lib')->t('User disabled'); |
|
370 | + throw new LoginException($message); |
|
371 | + } |
|
372 | + |
|
373 | + if ($regenerateSessionId) { |
|
374 | + $this->session->regenerateId(); |
|
375 | + } |
|
376 | + |
|
377 | + $this->setUser($user); |
|
378 | + $this->setLoginName($loginDetails['loginName']); |
|
379 | + |
|
380 | + $isToken = isset($loginDetails['token']) && $loginDetails['token'] instanceof IToken; |
|
381 | + if ($isToken) { |
|
382 | + $this->setToken($loginDetails['token']->getId()); |
|
383 | + $this->lockdownManager->setToken($loginDetails['token']); |
|
384 | + $firstTimeLogin = false; |
|
385 | + } else { |
|
386 | + $this->setToken(null); |
|
387 | + $firstTimeLogin = $user->updateLastLoginTimestamp(); |
|
388 | + } |
|
389 | + |
|
390 | + $this->dispatcher->dispatchTyped(new PostLoginEvent( |
|
391 | + $user, |
|
392 | + $loginDetails['loginName'], |
|
393 | + $loginDetails['password'], |
|
394 | + $isToken |
|
395 | + )); |
|
396 | + $this->manager->emit('\OC\User', 'postLogin', [ |
|
397 | + $user, |
|
398 | + $loginDetails['loginName'], |
|
399 | + $loginDetails['password'], |
|
400 | + $isToken, |
|
401 | + ]); |
|
402 | + if ($this->isLoggedIn()) { |
|
403 | + $this->prepareUserLogin($firstTimeLogin, $regenerateSessionId); |
|
404 | + return true; |
|
405 | + } |
|
406 | + |
|
407 | + $message = \OC::$server->getL10N('lib')->t('Login canceled by app'); |
|
408 | + throw new LoginException($message); |
|
409 | + } |
|
410 | + |
|
411 | + /** |
|
412 | + * Tries to log in a client |
|
413 | + * |
|
414 | + * Checks token auth enforced |
|
415 | + * Checks 2FA enabled |
|
416 | + * |
|
417 | + * @param string $user |
|
418 | + * @param string $password |
|
419 | + * @param IRequest $request |
|
420 | + * @param OC\Security\Bruteforce\Throttler $throttler |
|
421 | + * @throws LoginException |
|
422 | + * @throws PasswordLoginForbiddenException |
|
423 | + * @return boolean |
|
424 | + */ |
|
425 | + public function logClientIn($user, |
|
426 | + $password, |
|
427 | + IRequest $request, |
|
428 | + OC\Security\Bruteforce\Throttler $throttler) { |
|
429 | + $currentDelay = $throttler->sleepDelay($request->getRemoteAddress(), 'login'); |
|
430 | + |
|
431 | + if ($this->manager instanceof PublicEmitter) { |
|
432 | + $this->manager->emit('\OC\User', 'preLogin', [$user, $password]); |
|
433 | + } |
|
434 | + |
|
435 | + try { |
|
436 | + $isTokenPassword = $this->isTokenPassword($password); |
|
437 | + } catch (ExpiredTokenException $e) { |
|
438 | + // Just return on an expired token no need to check further or record a failed login |
|
439 | + return false; |
|
440 | + } |
|
441 | + |
|
442 | + if (!$isTokenPassword && $this->isTokenAuthEnforced()) { |
|
443 | + throw new PasswordLoginForbiddenException(); |
|
444 | + } |
|
445 | + if (!$isTokenPassword && $this->isTwoFactorEnforced($user)) { |
|
446 | + throw new PasswordLoginForbiddenException(); |
|
447 | + } |
|
448 | + |
|
449 | + // Try to login with this username and password |
|
450 | + if (!$this->login($user, $password)) { |
|
451 | + |
|
452 | + // Failed, maybe the user used their email address |
|
453 | + if (!filter_var($user, FILTER_VALIDATE_EMAIL)) { |
|
454 | + return false; |
|
455 | + } |
|
456 | + $users = $this->manager->getByEmail($user); |
|
457 | + if (!(\count($users) === 1 && $this->login($users[0]->getUID(), $password))) { |
|
458 | + $this->logger->warning('Login failed: \'' . $user . '\' (Remote IP: \'' . \OC::$server->getRequest()->getRemoteAddress() . '\')', ['app' => 'core']); |
|
459 | + |
|
460 | + $throttler->registerAttempt('login', $request->getRemoteAddress(), ['user' => $user]); |
|
461 | + |
|
462 | + $this->dispatcher->dispatchTyped(new OC\Authentication\Events\LoginFailed($user)); |
|
463 | + |
|
464 | + if ($currentDelay === 0) { |
|
465 | + $throttler->sleepDelay($request->getRemoteAddress(), 'login'); |
|
466 | + } |
|
467 | + return false; |
|
468 | + } |
|
469 | + } |
|
470 | + |
|
471 | + if ($isTokenPassword) { |
|
472 | + $this->session->set('app_password', $password); |
|
473 | + } elseif ($this->supportsCookies($request)) { |
|
474 | + // Password login, but cookies supported -> create (browser) session token |
|
475 | + $this->createSessionToken($request, $this->getUser()->getUID(), $user, $password); |
|
476 | + } |
|
477 | + |
|
478 | + return true; |
|
479 | + } |
|
480 | + |
|
481 | + protected function supportsCookies(IRequest $request) { |
|
482 | + if (!is_null($request->getCookie('cookie_test'))) { |
|
483 | + return true; |
|
484 | + } |
|
485 | + setcookie('cookie_test', 'test', $this->timeFactory->getTime() + 3600); |
|
486 | + return false; |
|
487 | + } |
|
488 | + |
|
489 | + private function isTokenAuthEnforced() { |
|
490 | + return $this->config->getSystemValue('token_auth_enforced', false); |
|
491 | + } |
|
492 | + |
|
493 | + protected function isTwoFactorEnforced($username) { |
|
494 | + Util::emitHook( |
|
495 | + '\OCA\Files_Sharing\API\Server2Server', |
|
496 | + 'preLoginNameUsedAsUserName', |
|
497 | + ['uid' => &$username] |
|
498 | + ); |
|
499 | + $user = $this->manager->get($username); |
|
500 | + if (is_null($user)) { |
|
501 | + $users = $this->manager->getByEmail($username); |
|
502 | + if (empty($users)) { |
|
503 | + return false; |
|
504 | + } |
|
505 | + if (count($users) !== 1) { |
|
506 | + return true; |
|
507 | + } |
|
508 | + $user = $users[0]; |
|
509 | + } |
|
510 | + // DI not possible due to cyclic dependencies :'-/ |
|
511 | + return OC::$server->getTwoFactorAuthManager()->isTwoFactorAuthenticated($user); |
|
512 | + } |
|
513 | + |
|
514 | + /** |
|
515 | + * Check if the given 'password' is actually a device token |
|
516 | + * |
|
517 | + * @param string $password |
|
518 | + * @return boolean |
|
519 | + * @throws ExpiredTokenException |
|
520 | + */ |
|
521 | + public function isTokenPassword($password) { |
|
522 | + try { |
|
523 | + $this->tokenProvider->getToken($password); |
|
524 | + return true; |
|
525 | + } catch (ExpiredTokenException $e) { |
|
526 | + throw $e; |
|
527 | + } catch (InvalidTokenException $ex) { |
|
528 | + $this->logger->debug('Token is not valid: ' . $ex->getMessage(), [ |
|
529 | + 'exception' => $ex, |
|
530 | + ]); |
|
531 | + return false; |
|
532 | + } |
|
533 | + } |
|
534 | + |
|
535 | + protected function prepareUserLogin($firstTimeLogin, $refreshCsrfToken = true) { |
|
536 | + if ($refreshCsrfToken) { |
|
537 | + // TODO: mock/inject/use non-static |
|
538 | + // Refresh the token |
|
539 | + \OC::$server->getCsrfTokenManager()->refreshToken(); |
|
540 | + } |
|
541 | + |
|
542 | + if ($firstTimeLogin) { |
|
543 | + //we need to pass the user name, which may differ from login name |
|
544 | + $user = $this->getUser()->getUID(); |
|
545 | + OC_Util::setupFS($user); |
|
546 | + |
|
547 | + // TODO: lock necessary? |
|
548 | + //trigger creation of user home and /files folder |
|
549 | + $userFolder = \OC::$server->getUserFolder($user); |
|
550 | + |
|
551 | + try { |
|
552 | + // copy skeleton |
|
553 | + \OC_Util::copySkeleton($user, $userFolder); |
|
554 | + } catch (NotPermittedException $ex) { |
|
555 | + // read only uses |
|
556 | + } |
|
557 | + |
|
558 | + // trigger any other initialization |
|
559 | + \OC::$server->getEventDispatcher()->dispatch(IUser::class . '::firstLogin', new GenericEvent($this->getUser())); |
|
560 | + } |
|
561 | + } |
|
562 | + |
|
563 | + /** |
|
564 | + * Tries to login the user with HTTP Basic Authentication |
|
565 | + * |
|
566 | + * @todo do not allow basic auth if the user is 2FA enforced |
|
567 | + * @param IRequest $request |
|
568 | + * @param OC\Security\Bruteforce\Throttler $throttler |
|
569 | + * @return boolean if the login was successful |
|
570 | + */ |
|
571 | + public function tryBasicAuthLogin(IRequest $request, |
|
572 | + OC\Security\Bruteforce\Throttler $throttler) { |
|
573 | + if (!empty($request->server['PHP_AUTH_USER']) && !empty($request->server['PHP_AUTH_PW'])) { |
|
574 | + try { |
|
575 | + if ($this->logClientIn($request->server['PHP_AUTH_USER'], $request->server['PHP_AUTH_PW'], $request, $throttler)) { |
|
576 | + /** |
|
577 | + * Add DAV authenticated. This should in an ideal world not be |
|
578 | + * necessary but the iOS App reads cookies from anywhere instead |
|
579 | + * only the DAV endpoint. |
|
580 | + * This makes sure that the cookies will be valid for the whole scope |
|
581 | + * @see https://github.com/owncloud/core/issues/22893 |
|
582 | + */ |
|
583 | + $this->session->set( |
|
584 | + Auth::DAV_AUTHENTICATED, $this->getUser()->getUID() |
|
585 | + ); |
|
586 | + |
|
587 | + // Set the last-password-confirm session to make the sudo mode work |
|
588 | + $this->session->set('last-password-confirm', $this->timeFactory->getTime()); |
|
589 | + |
|
590 | + return true; |
|
591 | + } |
|
592 | + // If credentials were provided, they need to be valid, otherwise we do boom |
|
593 | + throw new LoginException(); |
|
594 | + } catch (PasswordLoginForbiddenException $ex) { |
|
595 | + // Nothing to do |
|
596 | + } |
|
597 | + } |
|
598 | + return false; |
|
599 | + } |
|
600 | + |
|
601 | + /** |
|
602 | + * Log an user in via login name and password |
|
603 | + * |
|
604 | + * @param string $uid |
|
605 | + * @param string $password |
|
606 | + * @return boolean |
|
607 | + * @throws LoginException if an app canceld the login process or the user is not enabled |
|
608 | + */ |
|
609 | + private function loginWithPassword($uid, $password) { |
|
610 | + $user = $this->manager->checkPasswordNoLogging($uid, $password); |
|
611 | + if ($user === false) { |
|
612 | + // Password check failed |
|
613 | + return false; |
|
614 | + } |
|
615 | + |
|
616 | + return $this->completeLogin($user, ['loginName' => $uid, 'password' => $password], false); |
|
617 | + } |
|
618 | + |
|
619 | + /** |
|
620 | + * Log an user in with a given token (id) |
|
621 | + * |
|
622 | + * @param string $token |
|
623 | + * @return boolean |
|
624 | + * @throws LoginException if an app canceled the login process or the user is not enabled |
|
625 | + */ |
|
626 | + private function loginWithToken($token) { |
|
627 | + try { |
|
628 | + $dbToken = $this->tokenProvider->getToken($token); |
|
629 | + } catch (InvalidTokenException $ex) { |
|
630 | + return false; |
|
631 | + } |
|
632 | + $uid = $dbToken->getUID(); |
|
633 | + |
|
634 | + // When logging in with token, the password must be decrypted first before passing to login hook |
|
635 | + $password = ''; |
|
636 | + try { |
|
637 | + $password = $this->tokenProvider->getPassword($dbToken, $token); |
|
638 | + } catch (PasswordlessTokenException $ex) { |
|
639 | + // Ignore and use empty string instead |
|
640 | + } |
|
641 | + |
|
642 | + $this->manager->emit('\OC\User', 'preLogin', [$dbToken->getLoginName(), $password]); |
|
643 | + |
|
644 | + $user = $this->manager->get($uid); |
|
645 | + if (is_null($user)) { |
|
646 | + // user does not exist |
|
647 | + return false; |
|
648 | + } |
|
649 | + |
|
650 | + return $this->completeLogin( |
|
651 | + $user, |
|
652 | + [ |
|
653 | + 'loginName' => $dbToken->getLoginName(), |
|
654 | + 'password' => $password, |
|
655 | + 'token' => $dbToken |
|
656 | + ], |
|
657 | + false); |
|
658 | + } |
|
659 | + |
|
660 | + /** |
|
661 | + * Create a new session token for the given user credentials |
|
662 | + * |
|
663 | + * @param IRequest $request |
|
664 | + * @param string $uid user UID |
|
665 | + * @param string $loginName login name |
|
666 | + * @param string $password |
|
667 | + * @param int $remember |
|
668 | + * @return boolean |
|
669 | + */ |
|
670 | + public function createSessionToken(IRequest $request, $uid, $loginName, $password = null, $remember = IToken::DO_NOT_REMEMBER) { |
|
671 | + if (is_null($this->manager->get($uid))) { |
|
672 | + // User does not exist |
|
673 | + return false; |
|
674 | + } |
|
675 | + $name = isset($request->server['HTTP_USER_AGENT']) ? utf8_encode($request->server['HTTP_USER_AGENT']) : 'unknown browser'; |
|
676 | + try { |
|
677 | + $sessionId = $this->session->getId(); |
|
678 | + $pwd = $this->getPassword($password); |
|
679 | + // Make sure the current sessionId has no leftover tokens |
|
680 | + $this->tokenProvider->invalidateToken($sessionId); |
|
681 | + $this->tokenProvider->generateToken($sessionId, $uid, $loginName, $pwd, $name, IToken::TEMPORARY_TOKEN, $remember); |
|
682 | + return true; |
|
683 | + } catch (SessionNotAvailableException $ex) { |
|
684 | + // This can happen with OCC, where a memory session is used |
|
685 | + // if a memory session is used, we shouldn't create a session token anyway |
|
686 | + return false; |
|
687 | + } |
|
688 | + } |
|
689 | + |
|
690 | + /** |
|
691 | + * Checks if the given password is a token. |
|
692 | + * If yes, the password is extracted from the token. |
|
693 | + * If no, the same password is returned. |
|
694 | + * |
|
695 | + * @param string $password either the login password or a device token |
|
696 | + * @return string|null the password or null if none was set in the token |
|
697 | + */ |
|
698 | + private function getPassword($password) { |
|
699 | + if (is_null($password)) { |
|
700 | + // This is surely no token ;-) |
|
701 | + return null; |
|
702 | + } |
|
703 | + try { |
|
704 | + $token = $this->tokenProvider->getToken($password); |
|
705 | + try { |
|
706 | + return $this->tokenProvider->getPassword($token, $password); |
|
707 | + } catch (PasswordlessTokenException $ex) { |
|
708 | + return null; |
|
709 | + } |
|
710 | + } catch (InvalidTokenException $ex) { |
|
711 | + return $password; |
|
712 | + } |
|
713 | + } |
|
714 | + |
|
715 | + /** |
|
716 | + * @param IToken $dbToken |
|
717 | + * @param string $token |
|
718 | + * @return boolean |
|
719 | + */ |
|
720 | + private function checkTokenCredentials(IToken $dbToken, $token) { |
|
721 | + // Check whether login credentials are still valid and the user was not disabled |
|
722 | + // This check is performed each 5 minutes |
|
723 | + $lastCheck = $dbToken->getLastCheck() ? : 0; |
|
724 | + $now = $this->timeFactory->getTime(); |
|
725 | + if ($lastCheck > ($now - 60 * 5)) { |
|
726 | + // Checked performed recently, nothing to do now |
|
727 | + return true; |
|
728 | + } |
|
729 | + |
|
730 | + try { |
|
731 | + $pwd = $this->tokenProvider->getPassword($dbToken, $token); |
|
732 | + } catch (InvalidTokenException $ex) { |
|
733 | + // An invalid token password was used -> log user out |
|
734 | + return false; |
|
735 | + } catch (PasswordlessTokenException $ex) { |
|
736 | + // Token has no password |
|
737 | + |
|
738 | + if (!is_null($this->activeUser) && !$this->activeUser->isEnabled()) { |
|
739 | + $this->tokenProvider->invalidateToken($token); |
|
740 | + return false; |
|
741 | + } |
|
742 | + |
|
743 | + $dbToken->setLastCheck($now); |
|
744 | + $this->tokenProvider->updateToken($dbToken); |
|
745 | + return true; |
|
746 | + } |
|
747 | + |
|
748 | + // Invalidate token if the user is no longer active |
|
749 | + if (!is_null($this->activeUser) && !$this->activeUser->isEnabled()) { |
|
750 | + $this->tokenProvider->invalidateToken($token); |
|
751 | + return false; |
|
752 | + } |
|
753 | + |
|
754 | + // If the token password is no longer valid mark it as such |
|
755 | + if ($this->manager->checkPassword($dbToken->getLoginName(), $pwd) === false) { |
|
756 | + $this->tokenProvider->markPasswordInvalid($dbToken, $token); |
|
757 | + // User is logged out |
|
758 | + return false; |
|
759 | + } |
|
760 | + |
|
761 | + $dbToken->setLastCheck($now); |
|
762 | + $this->tokenProvider->updateToken($dbToken); |
|
763 | + return true; |
|
764 | + } |
|
765 | + |
|
766 | + /** |
|
767 | + * Check if the given token exists and performs password/user-enabled checks |
|
768 | + * |
|
769 | + * Invalidates the token if checks fail |
|
770 | + * |
|
771 | + * @param string $token |
|
772 | + * @param string $user login name |
|
773 | + * @return boolean |
|
774 | + */ |
|
775 | + private function validateToken($token, $user = null) { |
|
776 | + try { |
|
777 | + $dbToken = $this->tokenProvider->getToken($token); |
|
778 | + } catch (InvalidTokenException $ex) { |
|
779 | + return false; |
|
780 | + } |
|
781 | + |
|
782 | + // Check if login names match |
|
783 | + if (!is_null($user) && $dbToken->getLoginName() !== $user) { |
|
784 | + // TODO: this makes it impossible to use different login names on browser and client |
|
785 | + // e.g. login by e-mail '[email protected]' on browser for generating the token will not |
|
786 | + // allow to use the client token with the login name 'user'. |
|
787 | + $this->logger->error('App token login name does not match', [ |
|
788 | + 'tokenLoginName' => $dbToken->getLoginName(), |
|
789 | + 'sessionLoginName' => $user, |
|
790 | + ]); |
|
791 | + |
|
792 | + return false; |
|
793 | + } |
|
794 | + |
|
795 | + if (!$this->checkTokenCredentials($dbToken, $token)) { |
|
796 | + return false; |
|
797 | + } |
|
798 | + |
|
799 | + // Update token scope |
|
800 | + $this->lockdownManager->setToken($dbToken); |
|
801 | + |
|
802 | + $this->tokenProvider->updateTokenActivity($dbToken); |
|
803 | + |
|
804 | + return true; |
|
805 | + } |
|
806 | + |
|
807 | + /** |
|
808 | + * Tries to login the user with auth token header |
|
809 | + * |
|
810 | + * @param IRequest $request |
|
811 | + * @todo check remember me cookie |
|
812 | + * @return boolean |
|
813 | + */ |
|
814 | + public function tryTokenLogin(IRequest $request) { |
|
815 | + $authHeader = $request->getHeader('Authorization'); |
|
816 | + if (strpos($authHeader, 'Bearer ') === 0) { |
|
817 | + $token = substr($authHeader, 7); |
|
818 | + } else { |
|
819 | + // No auth header, let's try session id |
|
820 | + try { |
|
821 | + $token = $this->session->getId(); |
|
822 | + } catch (SessionNotAvailableException $ex) { |
|
823 | + return false; |
|
824 | + } |
|
825 | + } |
|
826 | + |
|
827 | + if (!$this->loginWithToken($token)) { |
|
828 | + return false; |
|
829 | + } |
|
830 | + if (!$this->validateToken($token)) { |
|
831 | + return false; |
|
832 | + } |
|
833 | + |
|
834 | + try { |
|
835 | + $dbToken = $this->tokenProvider->getToken($token); |
|
836 | + } catch (InvalidTokenException $e) { |
|
837 | + // Can't really happen but better save than sorry |
|
838 | + return true; |
|
839 | + } |
|
840 | + |
|
841 | + // Remember me tokens are not app_passwords |
|
842 | + if ($dbToken->getRemember() === IToken::DO_NOT_REMEMBER) { |
|
843 | + // Set the session variable so we know this is an app password |
|
844 | + $this->session->set('app_password', $token); |
|
845 | + } |
|
846 | + |
|
847 | + return true; |
|
848 | + } |
|
849 | + |
|
850 | + /** |
|
851 | + * perform login using the magic cookie (remember login) |
|
852 | + * |
|
853 | + * @param string $uid the username |
|
854 | + * @param string $currentToken |
|
855 | + * @param string $oldSessionId |
|
856 | + * @return bool |
|
857 | + */ |
|
858 | + public function loginWithCookie($uid, $currentToken, $oldSessionId) { |
|
859 | + $this->session->regenerateId(); |
|
860 | + $this->manager->emit('\OC\User', 'preRememberedLogin', [$uid]); |
|
861 | + $user = $this->manager->get($uid); |
|
862 | + if (is_null($user)) { |
|
863 | + // user does not exist |
|
864 | + return false; |
|
865 | + } |
|
866 | + |
|
867 | + // get stored tokens |
|
868 | + $tokens = $this->config->getUserKeys($uid, 'login_token'); |
|
869 | + // test cookies token against stored tokens |
|
870 | + if (!in_array($currentToken, $tokens, true)) { |
|
871 | + $this->logger->info('Tried to log in {uid} but could not verify token', [ |
|
872 | + 'app' => 'core', |
|
873 | + 'uid' => $uid, |
|
874 | + ]); |
|
875 | + return false; |
|
876 | + } |
|
877 | + // replace successfully used token with a new one |
|
878 | + $this->config->deleteUserValue($uid, 'login_token', $currentToken); |
|
879 | + $newToken = $this->random->generate(32); |
|
880 | + $this->config->setUserValue($uid, 'login_token', $newToken, (string)$this->timeFactory->getTime()); |
|
881 | + |
|
882 | + try { |
|
883 | + $sessionId = $this->session->getId(); |
|
884 | + $token = $this->tokenProvider->renewSessionToken($oldSessionId, $sessionId); |
|
885 | + } catch (SessionNotAvailableException $ex) { |
|
886 | + $this->logger->warning('Could not renew session token for {uid} because the session is unavailable', [ |
|
887 | + 'app' => 'core', |
|
888 | + 'uid' => $uid, |
|
889 | + ]); |
|
890 | + return false; |
|
891 | + } catch (InvalidTokenException $ex) { |
|
892 | + $this->logger->warning('Renewing session token failed', ['app' => 'core']); |
|
893 | + return false; |
|
894 | + } |
|
895 | + |
|
896 | + $this->setMagicInCookie($user->getUID(), $newToken); |
|
897 | + |
|
898 | + //login |
|
899 | + $this->setUser($user); |
|
900 | + $this->setLoginName($token->getLoginName()); |
|
901 | + $this->setToken($token->getId()); |
|
902 | + $this->lockdownManager->setToken($token); |
|
903 | + $user->updateLastLoginTimestamp(); |
|
904 | + $password = null; |
|
905 | + try { |
|
906 | + $password = $this->tokenProvider->getPassword($token, $sessionId); |
|
907 | + } catch (PasswordlessTokenException $ex) { |
|
908 | + // Ignore |
|
909 | + } |
|
910 | + $this->manager->emit('\OC\User', 'postRememberedLogin', [$user, $password]); |
|
911 | + return true; |
|
912 | + } |
|
913 | + |
|
914 | + /** |
|
915 | + * @param IUser $user |
|
916 | + */ |
|
917 | + public function createRememberMeToken(IUser $user) { |
|
918 | + $token = $this->random->generate(32); |
|
919 | + $this->config->setUserValue($user->getUID(), 'login_token', $token, (string)$this->timeFactory->getTime()); |
|
920 | + $this->setMagicInCookie($user->getUID(), $token); |
|
921 | + } |
|
922 | + |
|
923 | + /** |
|
924 | + * logout the user from the session |
|
925 | + */ |
|
926 | + public function logout() { |
|
927 | + $user = $this->getUser(); |
|
928 | + $this->manager->emit('\OC\User', 'logout', [$user]); |
|
929 | + if ($user !== null) { |
|
930 | + try { |
|
931 | + $this->tokenProvider->invalidateToken($this->session->getId()); |
|
932 | + } catch (SessionNotAvailableException $ex) { |
|
933 | + } |
|
934 | + } |
|
935 | + $this->setUser(null); |
|
936 | + $this->setLoginName(null); |
|
937 | + $this->setToken(null); |
|
938 | + $this->unsetMagicInCookie(); |
|
939 | + $this->session->clear(); |
|
940 | + $this->manager->emit('\OC\User', 'postLogout', [$user]); |
|
941 | + } |
|
942 | + |
|
943 | + /** |
|
944 | + * Set cookie value to use in next page load |
|
945 | + * |
|
946 | + * @param string $username username to be set |
|
947 | + * @param string $token |
|
948 | + */ |
|
949 | + public function setMagicInCookie($username, $token) { |
|
950 | + $secureCookie = OC::$server->getRequest()->getServerProtocol() === 'https'; |
|
951 | + $webRoot = \OC::$WEBROOT; |
|
952 | + if ($webRoot === '') { |
|
953 | + $webRoot = '/'; |
|
954 | + } |
|
955 | + |
|
956 | + $maxAge = $this->config->getSystemValue('remember_login_cookie_lifetime', 60 * 60 * 24 * 15); |
|
957 | + \OC\Http\CookieHelper::setCookie( |
|
958 | + 'nc_username', |
|
959 | + $username, |
|
960 | + $maxAge, |
|
961 | + $webRoot, |
|
962 | + '', |
|
963 | + $secureCookie, |
|
964 | + true, |
|
965 | + \OC\Http\CookieHelper::SAMESITE_LAX |
|
966 | + ); |
|
967 | + \OC\Http\CookieHelper::setCookie( |
|
968 | + 'nc_token', |
|
969 | + $token, |
|
970 | + $maxAge, |
|
971 | + $webRoot, |
|
972 | + '', |
|
973 | + $secureCookie, |
|
974 | + true, |
|
975 | + \OC\Http\CookieHelper::SAMESITE_LAX |
|
976 | + ); |
|
977 | + try { |
|
978 | + \OC\Http\CookieHelper::setCookie( |
|
979 | + 'nc_session_id', |
|
980 | + $this->session->getId(), |
|
981 | + $maxAge, |
|
982 | + $webRoot, |
|
983 | + '', |
|
984 | + $secureCookie, |
|
985 | + true, |
|
986 | + \OC\Http\CookieHelper::SAMESITE_LAX |
|
987 | + ); |
|
988 | + } catch (SessionNotAvailableException $ex) { |
|
989 | + // ignore |
|
990 | + } |
|
991 | + } |
|
992 | + |
|
993 | + /** |
|
994 | + * Remove cookie for "remember username" |
|
995 | + */ |
|
996 | + public function unsetMagicInCookie() { |
|
997 | + //TODO: DI for cookies and IRequest |
|
998 | + $secureCookie = OC::$server->getRequest()->getServerProtocol() === 'https'; |
|
999 | + |
|
1000 | + unset($_COOKIE['nc_username']); //TODO: DI |
|
1001 | + unset($_COOKIE['nc_token']); |
|
1002 | + unset($_COOKIE['nc_session_id']); |
|
1003 | + setcookie('nc_username', '', $this->timeFactory->getTime() - 3600, OC::$WEBROOT, '', $secureCookie, true); |
|
1004 | + setcookie('nc_token', '', $this->timeFactory->getTime() - 3600, OC::$WEBROOT, '', $secureCookie, true); |
|
1005 | + setcookie('nc_session_id', '', $this->timeFactory->getTime() - 3600, OC::$WEBROOT, '', $secureCookie, true); |
|
1006 | + // old cookies might be stored under /webroot/ instead of /webroot |
|
1007 | + // and Firefox doesn't like it! |
|
1008 | + setcookie('nc_username', '', $this->timeFactory->getTime() - 3600, OC::$WEBROOT . '/', '', $secureCookie, true); |
|
1009 | + setcookie('nc_token', '', $this->timeFactory->getTime() - 3600, OC::$WEBROOT . '/', '', $secureCookie, true); |
|
1010 | + setcookie('nc_session_id', '', $this->timeFactory->getTime() - 3600, OC::$WEBROOT . '/', '', $secureCookie, true); |
|
1011 | + } |
|
1012 | + |
|
1013 | + /** |
|
1014 | + * Update password of the browser session token if there is one |
|
1015 | + * |
|
1016 | + * @param string $password |
|
1017 | + */ |
|
1018 | + public function updateSessionTokenPassword($password) { |
|
1019 | + try { |
|
1020 | + $sessionId = $this->session->getId(); |
|
1021 | + $token = $this->tokenProvider->getToken($sessionId); |
|
1022 | + $this->tokenProvider->setPassword($token, $sessionId, $password); |
|
1023 | + } catch (SessionNotAvailableException $ex) { |
|
1024 | + // Nothing to do |
|
1025 | + } catch (InvalidTokenException $ex) { |
|
1026 | + // Nothing to do |
|
1027 | + } |
|
1028 | + } |
|
1029 | + |
|
1030 | + public function updateTokens(string $uid, string $password) { |
|
1031 | + $this->tokenProvider->updatePasswords($uid, $password); |
|
1032 | + } |
|
1033 | 1033 | } |