2013-05-29 01:46:57 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2017-02-07 03:25:39 +03:00
|
|
|
* @copyright Copyright (c) 2017, Sandro Lutz <sandro.lutz@temparus.ch>
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Bernhard Posselt <dev@bernhard-posselt.com>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Bjoern Schiessle <bjoern@schiessle.org>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Felix Rupp <github@felixrupp.com>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Greta Doci <gretadoci@gmail.com>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 19:13:36 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-01-12 17:02:16 +03:00
|
|
|
* @author Robin McCorkell <robin@mccorkell.me.uk>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
* @author Sandro Lutz <sandro.lutz@temparus.ch>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-03-26 13:44:34 +03:00
|
|
|
*
|
2013-05-29 01:46:57 +04:00
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2013-05-29 01:46:57 +04:00
|
|
|
namespace OC\User;
|
|
|
|
|
2016-04-25 15:10:55 +03:00
|
|
|
use OC;
|
2018-10-30 15:19:59 +03:00
|
|
|
use OC\Authentication\Exceptions\ExpiredTokenException;
|
2016-04-25 15:10:55 +03:00
|
|
|
use OC\Authentication\Exceptions\InvalidTokenException;
|
2016-05-31 11:48:14 +03:00
|
|
|
use OC\Authentication\Exceptions\PasswordlessTokenException;
|
2016-06-17 12:01:35 +03:00
|
|
|
use OC\Authentication\Exceptions\PasswordLoginForbiddenException;
|
2016-04-25 15:10:55 +03:00
|
|
|
use OC\Authentication\Token\IProvider;
|
2016-04-27 10:38:30 +03:00
|
|
|
use OC\Authentication\Token\IToken;
|
2013-05-29 01:46:57 +04:00
|
|
|
use OC\Hooks\Emitter;
|
2016-11-30 15:28:36 +03:00
|
|
|
use OC\Hooks\PublicEmitter;
|
2016-04-25 15:10:55 +03:00
|
|
|
use OC_User;
|
2016-05-02 20:58:19 +03:00
|
|
|
use OC_Util;
|
2016-04-25 15:10:55 +03:00
|
|
|
use OCA\DAV\Connector\Sabre\Auth;
|
2016-05-02 20:58:19 +03:00
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
2019-07-27 13:56:27 +03:00
|
|
|
use OCP\EventDispatcher\IEventDispatcher;
|
2017-05-05 15:44:51 +03:00
|
|
|
use OCP\Files\NotPermittedException;
|
2016-05-24 15:08:42 +03:00
|
|
|
use OCP\IConfig;
|
2018-01-23 11:41:44 +03:00
|
|
|
use OCP\ILogger;
|
2016-04-25 15:10:55 +03:00
|
|
|
use OCP\IRequest;
|
2016-04-07 16:39:34 +03:00
|
|
|
use OCP\ISession;
|
2016-04-25 15:10:55 +03:00
|
|
|
use OCP\IUser;
|
2014-07-09 17:32:33 +04:00
|
|
|
use OCP\IUserSession;
|
2017-02-17 17:40:20 +03:00
|
|
|
use OCP\Lockdown\ILockdownManager;
|
2016-10-31 11:36:55 +03:00
|
|
|
use OCP\Security\ISecureRandom;
|
2016-05-03 17:35:00 +03:00
|
|
|
use OCP\Session\Exceptions\SessionNotAvailableException;
|
2019-11-20 17:12:52 +03:00
|
|
|
use OCP\User\Events\PostLoginEvent;
|
2016-05-31 11:48:14 +03:00
|
|
|
use OCP\Util;
|
2016-09-23 18:21:07 +03:00
|
|
|
use Symfony\Component\EventDispatcher\GenericEvent;
|
2013-05-29 01:46:57 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Session
|
|
|
|
*
|
|
|
|
* Hooks available in scope \OC\User:
|
|
|
|
* - preSetPassword(\OC\User\User $user, string $password, string $recoverPassword)
|
|
|
|
* - postSetPassword(\OC\User\User $user, string $password, string $recoverPassword)
|
|
|
|
* - preDelete(\OC\User\User $user)
|
|
|
|
* - postDelete(\OC\User\User $user)
|
|
|
|
* - preCreateUser(string $uid, string $password)
|
|
|
|
* - postCreateUser(\OC\User\User $user)
|
2018-03-19 17:23:30 +03:00
|
|
|
* - assignedUserId(string $uid)
|
|
|
|
* - preUnassignedUserId(string $uid)
|
|
|
|
* - postUnassignedUserId(string $uid)
|
2013-05-29 01:46:57 +04:00
|
|
|
* - preLogin(string $user, string $password)
|
2014-05-26 15:53:26 +04:00
|
|
|
* - postLogin(\OC\User\User $user, string $password)
|
|
|
|
* - preRememberedLogin(string $uid)
|
|
|
|
* - postRememberedLogin(\OC\User\User $user)
|
2013-05-29 01:46:57 +04:00
|
|
|
* - logout()
|
2017-02-06 19:05:11 +03:00
|
|
|
* - postLogout()
|
2013-05-29 01:46:57 +04:00
|
|
|
*
|
|
|
|
* @package OC\User
|
|
|
|
*/
|
2014-07-09 17:32:33 +04:00
|
|
|
class Session implements IUserSession, Emitter {
|
2016-05-24 15:08:42 +03:00
|
|
|
|
2018-01-23 11:58:46 +03:00
|
|
|
/** @var Manager|PublicEmitter $manager */
|
2013-05-29 01:46:57 +04:00
|
|
|
private $manager;
|
|
|
|
|
2016-05-17 18:20:54 +03:00
|
|
|
/** @var ISession $session */
|
2013-05-29 01:46:57 +04:00
|
|
|
private $session;
|
|
|
|
|
2016-05-17 18:20:54 +03:00
|
|
|
/** @var ITimeFactory */
|
2017-02-17 17:40:20 +03:00
|
|
|
private $timeFactory;
|
2016-05-02 20:58:19 +03:00
|
|
|
|
2016-05-17 18:20:54 +03:00
|
|
|
/** @var IProvider */
|
2016-04-25 15:10:55 +03:00
|
|
|
private $tokenProvider;
|
|
|
|
|
2016-05-24 15:08:42 +03:00
|
|
|
/** @var IConfig */
|
|
|
|
private $config;
|
|
|
|
|
2016-05-17 18:20:54 +03:00
|
|
|
/** @var User $activeUser */
|
2013-05-29 01:46:57 +04:00
|
|
|
protected $activeUser;
|
|
|
|
|
2016-10-31 11:36:55 +03:00
|
|
|
/** @var ISecureRandom */
|
|
|
|
private $random;
|
|
|
|
|
2017-02-17 17:40:20 +03:00
|
|
|
/** @var ILockdownManager */
|
|
|
|
private $lockdownManager;
|
|
|
|
|
2018-01-23 11:41:44 +03:00
|
|
|
/** @var ILogger */
|
|
|
|
private $logger;
|
2019-07-27 13:56:27 +03:00
|
|
|
/** @var IEventDispatcher */
|
|
|
|
private $dispatcher;
|
2018-01-23 11:41:44 +03:00
|
|
|
|
2013-05-29 01:46:57 +04:00
|
|
|
/**
|
2018-01-23 11:58:46 +03:00
|
|
|
* @param Manager $manager
|
2016-04-07 16:39:34 +03:00
|
|
|
* @param ISession $session
|
2017-02-17 17:40:20 +03:00
|
|
|
* @param ITimeFactory $timeFactory
|
2016-05-06 17:31:40 +03:00
|
|
|
* @param IProvider $tokenProvider
|
2016-05-24 15:08:42 +03:00
|
|
|
* @param IConfig $config
|
2016-10-31 11:36:55 +03:00
|
|
|
* @param ISecureRandom $random
|
2017-02-17 17:40:20 +03:00
|
|
|
* @param ILockdownManager $lockdownManager
|
2018-01-23 11:41:44 +03:00
|
|
|
* @param ILogger $logger
|
2013-05-29 01:46:57 +04:00
|
|
|
*/
|
2018-01-23 11:58:46 +03:00
|
|
|
public function __construct(Manager $manager,
|
2016-07-20 19:36:15 +03:00
|
|
|
ISession $session,
|
2017-02-17 17:40:20 +03:00
|
|
|
ITimeFactory $timeFactory,
|
2016-07-20 19:36:15 +03:00
|
|
|
$tokenProvider,
|
2016-10-31 11:36:55 +03:00
|
|
|
IConfig $config,
|
2017-02-17 17:40:20 +03:00
|
|
|
ISecureRandom $random,
|
2018-01-23 11:41:44 +03:00
|
|
|
ILockdownManager $lockdownManager,
|
2019-07-27 13:56:27 +03:00
|
|
|
ILogger $logger,
|
|
|
|
IEventDispatcher $dispatcher) {
|
2013-05-29 01:46:57 +04:00
|
|
|
$this->manager = $manager;
|
|
|
|
$this->session = $session;
|
2017-02-17 17:40:20 +03:00
|
|
|
$this->timeFactory = $timeFactory;
|
2016-04-25 15:10:55 +03:00
|
|
|
$this->tokenProvider = $tokenProvider;
|
2016-05-24 15:08:42 +03:00
|
|
|
$this->config = $config;
|
2016-10-31 11:36:55 +03:00
|
|
|
$this->random = $random;
|
2017-02-17 17:40:20 +03:00
|
|
|
$this->lockdownManager = $lockdownManager;
|
2018-01-23 11:41:44 +03:00
|
|
|
$this->logger = $logger;
|
2019-07-27 13:56:27 +03:00
|
|
|
$this->dispatcher = $dispatcher;
|
2013-05-29 01:46:57 +04:00
|
|
|
}
|
|
|
|
|
2016-04-27 13:01:13 +03:00
|
|
|
/**
|
2016-05-17 18:20:54 +03:00
|
|
|
* @param IProvider $provider
|
2016-04-27 13:01:13 +03:00
|
|
|
*/
|
2016-05-17 18:20:54 +03:00
|
|
|
public function setTokenProvider(IProvider $provider) {
|
2016-04-27 13:01:13 +03:00
|
|
|
$this->tokenProvider = $provider;
|
|
|
|
}
|
|
|
|
|
2013-05-29 01:46:57 +04:00
|
|
|
/**
|
|
|
|
* @param string $scope
|
|
|
|
* @param string $method
|
|
|
|
* @param callable $callback
|
|
|
|
*/
|
2015-05-08 15:27:22 +03:00
|
|
|
public function listen($scope, $method, callable $callback) {
|
2013-05-29 01:46:57 +04:00
|
|
|
$this->manager->listen($scope, $method, $callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $scope optional
|
|
|
|
* @param string $method optional
|
|
|
|
* @param callable $callback optional
|
|
|
|
*/
|
2016-04-26 12:32:35 +03:00
|
|
|
public function removeListener($scope = null, $method = null, callable $callback = null) {
|
2013-05-29 01:46:57 +04:00
|
|
|
$this->manager->removeListener($scope, $method, $callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-03 15:33:56 +04:00
|
|
|
* get the manager object
|
|
|
|
*
|
2016-11-30 15:28:36 +03:00
|
|
|
* @return Manager|PublicEmitter
|
2013-05-29 01:46:57 +04:00
|
|
|
*/
|
|
|
|
public function getManager() {
|
|
|
|
return $this->manager;
|
|
|
|
}
|
|
|
|
|
2014-07-16 21:40:22 +04:00
|
|
|
/**
|
|
|
|
* get the session object
|
|
|
|
*
|
2016-04-07 16:39:34 +03:00
|
|
|
* @return ISession
|
2014-07-16 21:40:22 +04:00
|
|
|
*/
|
|
|
|
public function getSession() {
|
|
|
|
return $this->session;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set the session object
|
|
|
|
*
|
2016-04-07 16:39:34 +03:00
|
|
|
* @param ISession $session
|
2014-07-16 21:40:22 +04:00
|
|
|
*/
|
2016-04-07 16:39:34 +03:00
|
|
|
public function setSession(ISession $session) {
|
|
|
|
if ($this->session instanceof ISession) {
|
2014-07-16 21:40:22 +04:00
|
|
|
$this->session->close();
|
|
|
|
}
|
|
|
|
$this->session = $session;
|
2014-10-13 15:11:48 +04:00
|
|
|
$this->activeUser = null;
|
2014-07-16 21:40:22 +04:00
|
|
|
}
|
|
|
|
|
2013-05-29 01:46:57 +04:00
|
|
|
/**
|
|
|
|
* set the currently active user
|
|
|
|
*
|
2016-08-11 11:10:32 +03:00
|
|
|
* @param IUser|null $user
|
2013-05-29 01:46:57 +04:00
|
|
|
*/
|
|
|
|
public function setUser($user) {
|
|
|
|
if (is_null($user)) {
|
|
|
|
$this->session->remove('user_id');
|
|
|
|
} else {
|
|
|
|
$this->session->set('user_id', $user->getUID());
|
|
|
|
}
|
|
|
|
$this->activeUser = $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the current active user
|
|
|
|
*
|
2016-04-25 15:10:55 +03:00
|
|
|
* @return IUser|null Current user, otherwise null
|
2013-05-29 01:46:57 +04:00
|
|
|
*/
|
|
|
|
public function getUser() {
|
2014-12-17 23:53:43 +03:00
|
|
|
// FIXME: This is a quick'n dirty work-around for the incognito mode as
|
|
|
|
// described at https://github.com/owncloud/core/pull/12912#issuecomment-67391155
|
2016-04-25 15:10:55 +03:00
|
|
|
if (OC_User::isIncognitoMode()) {
|
2014-12-17 23:53:43 +03:00
|
|
|
return null;
|
|
|
|
}
|
2016-04-28 11:52:28 +03:00
|
|
|
if (is_null($this->activeUser)) {
|
2013-05-29 01:46:57 +04:00
|
|
|
$uid = $this->session->get('user_id');
|
2016-04-28 11:52:28 +03:00
|
|
|
if (is_null($uid)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$this->activeUser = $this->manager->get($uid);
|
|
|
|
if (is_null($this->activeUser)) {
|
2013-05-29 01:46:57 +04:00
|
|
|
return null;
|
|
|
|
}
|
2016-06-17 14:59:15 +03:00
|
|
|
$this->validateSession();
|
2013-05-29 01:46:57 +04:00
|
|
|
}
|
2016-04-28 11:52:28 +03:00
|
|
|
return $this->activeUser;
|
2013-05-29 01:46:57 +04:00
|
|
|
}
|
|
|
|
|
2016-06-17 16:41:32 +03:00
|
|
|
/**
|
|
|
|
* Validate whether the current session is valid
|
|
|
|
*
|
|
|
|
* - For token-authenticated clients, the token validity is checked
|
|
|
|
* - For browsers, the session token validity is checked
|
|
|
|
*/
|
2016-06-17 14:59:15 +03:00
|
|
|
protected function validateSession() {
|
2016-06-17 16:41:32 +03:00
|
|
|
$token = null;
|
|
|
|
$appPassword = $this->session->get('app_password');
|
2016-04-26 12:32:35 +03:00
|
|
|
|
2016-06-17 16:41:32 +03:00
|
|
|
if (is_null($appPassword)) {
|
2016-05-08 20:31:42 +03:00
|
|
|
try {
|
2016-06-17 16:41:32 +03:00
|
|
|
$token = $this->session->getId();
|
|
|
|
} catch (SessionNotAvailableException $ex) {
|
2016-05-06 17:31:40 +03:00
|
|
|
return;
|
2016-04-26 12:32:35 +03:00
|
|
|
}
|
2016-06-17 16:41:32 +03:00
|
|
|
} else {
|
|
|
|
$token = $appPassword;
|
2016-04-26 12:32:35 +03:00
|
|
|
}
|
|
|
|
|
2016-06-17 16:41:32 +03:00
|
|
|
if (!$this->validateToken($token)) {
|
2016-04-28 11:52:28 +03:00
|
|
|
// Session was invalidated
|
2016-04-26 12:32:35 +03:00
|
|
|
$this->logout();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-16 21:07:14 +03:00
|
|
|
/**
|
2014-12-19 16:36:00 +03:00
|
|
|
* Checks whether the user is logged in
|
2014-12-16 21:07:14 +03:00
|
|
|
*
|
|
|
|
* @return bool if logged in
|
|
|
|
*/
|
|
|
|
public function isLoggedIn() {
|
2016-04-07 16:39:34 +03:00
|
|
|
$user = $this->getUser();
|
|
|
|
if (is_null($user)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $user->isEnabled();
|
2014-12-16 21:07:14 +03:00
|
|
|
}
|
|
|
|
|
2013-12-11 16:56:45 +04:00
|
|
|
/**
|
|
|
|
* set the login name
|
|
|
|
*
|
2014-05-13 02:27:36 +04:00
|
|
|
* @param string|null $loginName for the logged in user
|
2013-12-11 16:56:45 +04:00
|
|
|
*/
|
2014-01-09 13:27:47 +04:00
|
|
|
public function setLoginName($loginName) {
|
|
|
|
if (is_null($loginName)) {
|
2013-12-11 16:56:45 +04:00
|
|
|
$this->session->remove('loginname');
|
|
|
|
} else {
|
2014-01-09 13:27:47 +04:00
|
|
|
$this->session->set('loginname', $loginName);
|
2013-12-11 16:56:45 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the login name of the current user
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2014-01-09 13:27:47 +04:00
|
|
|
public function getLoginName() {
|
2013-12-11 16:56:45 +04:00
|
|
|
if ($this->activeUser) {
|
|
|
|
return $this->session->get('loginname');
|
|
|
|
}
|
2018-01-26 01:01:03 +03:00
|
|
|
|
|
|
|
$uid = $this->session->get('user_id');
|
|
|
|
if ($uid) {
|
|
|
|
$this->activeUser = $this->manager->get($uid);
|
|
|
|
return $this->session->get('loginname');
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2013-12-11 16:56:45 +04:00
|
|
|
}
|
|
|
|
|
2019-06-12 15:26:01 +03:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getImpersonatingUserID(): ?string {
|
|
|
|
|
|
|
|
return $this->session->get('oldUserId');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setImpersonatingUserID(bool $useCurrentUser = true): void {
|
|
|
|
if ($useCurrentUser === false) {
|
|
|
|
$this->session->remove('oldUserId');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$currentUser = $this->getUser();
|
|
|
|
|
|
|
|
if ($currentUser === null) {
|
|
|
|
throw new \OC\User\NoUserException();
|
|
|
|
}
|
|
|
|
$this->session->set('oldUserId', $currentUser->getUID());
|
|
|
|
|
|
|
|
}
|
2017-01-26 13:08:26 +03:00
|
|
|
/**
|
|
|
|
* set the token id
|
|
|
|
*
|
|
|
|
* @param int|null $token that was used to log in
|
|
|
|
*/
|
|
|
|
protected function setToken($token) {
|
|
|
|
if ($token === null) {
|
|
|
|
$this->session->remove('token-id');
|
|
|
|
} else {
|
|
|
|
$this->session->set('token-id', $token);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-03 15:33:56 +04:00
|
|
|
/**
|
2016-05-24 15:08:42 +03:00
|
|
|
* try to log in with the provided credentials
|
2013-06-03 15:33:56 +04:00
|
|
|
*
|
|
|
|
* @param string $uid
|
|
|
|
* @param string $password
|
2014-02-06 19:30:58 +04:00
|
|
|
* @return boolean|null
|
2015-01-22 16:13:17 +03:00
|
|
|
* @throws LoginException
|
2013-06-03 15:33:56 +04:00
|
|
|
*/
|
2013-05-29 01:46:57 +04:00
|
|
|
public function login($uid, $password) {
|
2016-01-04 17:00:58 +03:00
|
|
|
$this->session->regenerateId();
|
2016-06-27 23:16:22 +03:00
|
|
|
if ($this->validateToken($password, $uid)) {
|
|
|
|
return $this->loginWithToken($password);
|
2013-05-29 01:46:57 +04:00
|
|
|
}
|
2016-08-11 10:54:36 +03:00
|
|
|
return $this->loginWithPassword($uid, $password);
|
2013-05-29 01:46:57 +04:00
|
|
|
}
|
|
|
|
|
2016-11-30 15:28:36 +03:00
|
|
|
/**
|
|
|
|
* @param IUser $user
|
|
|
|
* @param array $loginDetails
|
2016-12-02 13:16:33 +03:00
|
|
|
* @param bool $regenerateSessionId
|
2017-04-13 20:16:12 +03:00
|
|
|
* @return true returns true if login successful or an exception otherwise
|
2016-11-30 15:28:36 +03:00
|
|
|
* @throws LoginException
|
|
|
|
*/
|
2016-12-02 13:16:33 +03:00
|
|
|
public function completeLogin(IUser $user, array $loginDetails, $regenerateSessionId = true) {
|
2016-11-30 15:28:36 +03:00
|
|
|
if (!$user->isEnabled()) {
|
|
|
|
// disabled users can not log in
|
|
|
|
// injecting l10n does not work - there is a circular dependency between session and \OCP\L10N\IFactory
|
|
|
|
$message = \OC::$server->getL10N('lib')->t('User disabled');
|
|
|
|
throw new LoginException($message);
|
|
|
|
}
|
|
|
|
|
2016-12-02 13:16:33 +03:00
|
|
|
if($regenerateSessionId) {
|
|
|
|
$this->session->regenerateId();
|
|
|
|
}
|
2016-12-01 16:06:22 +03:00
|
|
|
|
2016-11-30 15:28:36 +03:00
|
|
|
$this->setUser($user);
|
|
|
|
$this->setLoginName($loginDetails['loginName']);
|
|
|
|
|
2019-01-23 21:47:47 +03:00
|
|
|
$isToken = isset($loginDetails['token']) && $loginDetails['token'] instanceof IToken;
|
|
|
|
if ($isToken) {
|
2016-11-30 15:28:36 +03:00
|
|
|
$this->setToken($loginDetails['token']->getId());
|
2017-04-06 16:27:30 +03:00
|
|
|
$this->lockdownManager->setToken($loginDetails['token']);
|
2016-11-30 15:28:36 +03:00
|
|
|
$firstTimeLogin = false;
|
|
|
|
} else {
|
|
|
|
$this->setToken(null);
|
|
|
|
$firstTimeLogin = $user->updateLastLoginTimestamp();
|
|
|
|
}
|
2019-07-27 13:56:27 +03:00
|
|
|
|
2019-11-20 17:12:52 +03:00
|
|
|
$this->dispatcher->dispatchTyped(new PostLoginEvent(
|
2019-07-27 13:56:27 +03:00
|
|
|
$user,
|
|
|
|
$loginDetails['password'],
|
|
|
|
$isToken
|
2019-11-20 17:12:52 +03:00
|
|
|
));
|
2019-01-23 21:47:47 +03:00
|
|
|
$this->manager->emit('\OC\User', 'postLogin', [
|
|
|
|
$user,
|
|
|
|
$loginDetails['password'],
|
|
|
|
$isToken,
|
|
|
|
]);
|
2016-11-30 15:28:36 +03:00
|
|
|
if($this->isLoggedIn()) {
|
Fix failing csp/nonce check due to timed out session
The CSP nonce is based on the CSRF token. This token does not change,
unless you log in (or out). In case of the session data being lost,
e.g. because php gets rid of old sessions, a new CSRF token is gen-
erated. While this is fine in theory, it actually caused some annoying
problems where the browser restored a tab and Nextcloud js was blocked
due to an outdated nonce.
The main problem here is that, while processing the request, we write
out security headers relatively early. At that point the CSRF token
is known/generated and transformed into a CSP nonce. During this request,
however, we also log the user in because the session information was
lost. At that point we also refresh the CSRF token, which eventually
causes the browser to block any scripts as the nonce in the header
does not match the one which is used to include scripts.
This patch adds a flag to indicate whether the CSRF token should be
refreshed or not. It is assumed that refreshing is only necessary
if we want to re-generate the session id too. To my knowledge, this
case only happens on fresh logins, not when we recover from a deleted
session file.
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
2017-09-04 18:29:26 +03:00
|
|
|
$this->prepareUserLogin($firstTimeLogin, $regenerateSessionId);
|
2016-11-30 15:28:36 +03:00
|
|
|
return true;
|
|
|
|
}
|
2018-01-26 01:01:03 +03:00
|
|
|
|
|
|
|
$message = \OC::$server->getL10N('lib')->t('Login canceled by app');
|
|
|
|
throw new LoginException($message);
|
2016-11-30 15:28:36 +03:00
|
|
|
}
|
|
|
|
|
2016-05-24 15:08:42 +03:00
|
|
|
/**
|
|
|
|
* Tries to log in a client
|
|
|
|
*
|
|
|
|
* Checks token auth enforced
|
|
|
|
* Checks 2FA enabled
|
|
|
|
*
|
|
|
|
* @param string $user
|
|
|
|
* @param string $password
|
2016-06-13 16:38:34 +03:00
|
|
|
* @param IRequest $request
|
2016-07-20 19:36:15 +03:00
|
|
|
* @param OC\Security\Bruteforce\Throttler $throttler
|
2016-05-24 15:08:42 +03:00
|
|
|
* @throws LoginException
|
2016-06-17 12:01:35 +03:00
|
|
|
* @throws PasswordLoginForbiddenException
|
2016-05-24 15:08:42 +03:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2016-07-20 19:36:15 +03:00
|
|
|
public function logClientIn($user,
|
|
|
|
$password,
|
|
|
|
IRequest $request,
|
|
|
|
OC\Security\Bruteforce\Throttler $throttler) {
|
2017-01-17 19:21:27 +03:00
|
|
|
$currentDelay = $throttler->sleepDelay($request->getRemoteAddress(), 'login');
|
2016-07-20 19:36:15 +03:00
|
|
|
|
2017-02-01 20:13:41 +03:00
|
|
|
if ($this->manager instanceof PublicEmitter) {
|
|
|
|
$this->manager->emit('\OC\User', 'preLogin', array($user, $password));
|
|
|
|
}
|
2017-01-31 22:21:58 +03:00
|
|
|
|
2018-10-30 15:19:59 +03:00
|
|
|
try {
|
|
|
|
$isTokenPassword = $this->isTokenPassword($password);
|
|
|
|
} catch (ExpiredTokenException $e) {
|
|
|
|
// Just return on an expired token no need to check further or record a failed login
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-05-24 15:08:42 +03:00
|
|
|
if (!$isTokenPassword && $this->isTokenAuthEnforced()) {
|
2016-06-17 12:01:35 +03:00
|
|
|
throw new PasswordLoginForbiddenException();
|
2016-05-24 15:08:42 +03:00
|
|
|
}
|
|
|
|
if (!$isTokenPassword && $this->isTwoFactorEnforced($user)) {
|
2016-06-17 12:01:35 +03:00
|
|
|
throw new PasswordLoginForbiddenException();
|
2016-05-24 15:08:42 +03:00
|
|
|
}
|
2018-01-23 11:41:44 +03:00
|
|
|
|
|
|
|
// Try to login with this username and password
|
2016-06-07 14:41:44 +03:00
|
|
|
if (!$this->login($user, $password) ) {
|
2018-01-23 11:41:44 +03:00
|
|
|
|
|
|
|
// Failed, maybe the user used their email address
|
2016-06-07 14:41:44 +03:00
|
|
|
$users = $this->manager->getByEmail($user);
|
2018-01-23 11:41:44 +03:00
|
|
|
if (!(\count($users) === 1 && $this->login($users[0]->getUID(), $password))) {
|
2016-07-20 19:36:15 +03:00
|
|
|
|
2018-01-23 11:41:44 +03:00
|
|
|
$this->logger->warning('Login failed: \'' . $user . '\' (Remote IP: \'' . \OC::$server->getRequest()->getRemoteAddress() . '\')', ['app' => 'core']);
|
|
|
|
|
2019-01-04 23:45:44 +03:00
|
|
|
$throttler->registerAttempt('login', $request->getRemoteAddress(), ['user' => $user]);
|
2018-01-23 11:41:44 +03:00
|
|
|
if ($currentDelay === 0) {
|
|
|
|
$throttler->sleepDelay($request->getRemoteAddress(), 'login');
|
|
|
|
}
|
|
|
|
return false;
|
2016-07-21 00:09:27 +03:00
|
|
|
}
|
2016-06-07 14:41:44 +03:00
|
|
|
}
|
2016-06-13 16:38:34 +03:00
|
|
|
|
2016-06-17 16:41:32 +03:00
|
|
|
if ($isTokenPassword) {
|
|
|
|
$this->session->set('app_password', $password);
|
|
|
|
} else if($this->supportsCookies($request)) {
|
|
|
|
// Password login, but cookies supported -> create (browser) session token
|
2016-06-13 17:00:49 +03:00
|
|
|
$this->createSessionToken($request, $this->getUser()->getUID(), $user, $password);
|
|
|
|
}
|
2016-06-13 16:38:34 +03:00
|
|
|
|
2016-06-07 14:41:44 +03:00
|
|
|
return true;
|
2016-05-24 15:08:42 +03:00
|
|
|
}
|
|
|
|
|
2016-06-13 17:00:49 +03:00
|
|
|
protected function supportsCookies(IRequest $request) {
|
|
|
|
if (!is_null($request->getCookie('cookie_test'))) {
|
|
|
|
return true;
|
|
|
|
}
|
2017-02-17 17:40:20 +03:00
|
|
|
setcookie('cookie_test', 'test', $this->timeFactory->getTime() + 3600);
|
2016-06-13 17:00:49 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-05-24 15:08:42 +03:00
|
|
|
private function isTokenAuthEnforced() {
|
|
|
|
return $this->config->getSystemValue('token_auth_enforced', false);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function isTwoFactorEnforced($username) {
|
2016-05-31 11:48:14 +03:00
|
|
|
Util::emitHook(
|
2016-05-24 15:08:42 +03:00
|
|
|
'\OCA\Files_Sharing\API\Server2Server',
|
|
|
|
'preLoginNameUsedAsUserName',
|
|
|
|
array('uid' => &$username)
|
|
|
|
);
|
|
|
|
$user = $this->manager->get($username);
|
2016-05-24 17:18:27 +03:00
|
|
|
if (is_null($user)) {
|
2016-06-07 14:41:44 +03:00
|
|
|
$users = $this->manager->getByEmail($username);
|
2016-10-07 17:49:57 +03:00
|
|
|
if (empty($users)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-07 14:41:44 +03:00
|
|
|
if (count($users) !== 1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
$user = $users[0];
|
2016-05-24 17:18:27 +03:00
|
|
|
}
|
2016-05-24 15:08:42 +03:00
|
|
|
// DI not possible due to cyclic dependencies :'-/
|
|
|
|
return OC::$server->getTwoFactorAuthManager()->isTwoFactorAuthenticated($user);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the given 'password' is actually a device token
|
|
|
|
*
|
2016-06-07 14:41:44 +03:00
|
|
|
* @param string $password
|
2016-05-24 15:08:42 +03:00
|
|
|
* @return boolean
|
2018-10-30 15:19:59 +03:00
|
|
|
* @throws ExpiredTokenException
|
2016-05-24 15:08:42 +03:00
|
|
|
*/
|
|
|
|
public function isTokenPassword($password) {
|
|
|
|
try {
|
|
|
|
$this->tokenProvider->getToken($password);
|
|
|
|
return true;
|
2018-10-30 15:19:59 +03:00
|
|
|
} catch (ExpiredTokenException $e) {
|
|
|
|
throw $e;
|
2016-05-24 15:08:42 +03:00
|
|
|
} catch (InvalidTokenException $ex) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Fix failing csp/nonce check due to timed out session
The CSP nonce is based on the CSRF token. This token does not change,
unless you log in (or out). In case of the session data being lost,
e.g. because php gets rid of old sessions, a new CSRF token is gen-
erated. While this is fine in theory, it actually caused some annoying
problems where the browser restored a tab and Nextcloud js was blocked
due to an outdated nonce.
The main problem here is that, while processing the request, we write
out security headers relatively early. At that point the CSRF token
is known/generated and transformed into a CSP nonce. During this request,
however, we also log the user in because the session information was
lost. At that point we also refresh the CSRF token, which eventually
causes the browser to block any scripts as the nonce in the header
does not match the one which is used to include scripts.
This patch adds a flag to indicate whether the CSRF token should be
refreshed or not. It is assumed that refreshing is only necessary
if we want to re-generate the session id too. To my knowledge, this
case only happens on fresh logins, not when we recover from a deleted
session file.
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
2017-09-04 18:29:26 +03:00
|
|
|
protected function prepareUserLogin($firstTimeLogin, $refreshCsrfToken = true) {
|
|
|
|
if ($refreshCsrfToken) {
|
|
|
|
// TODO: mock/inject/use non-static
|
|
|
|
// Refresh the token
|
|
|
|
\OC::$server->getCsrfTokenManager()->refreshToken();
|
|
|
|
}
|
|
|
|
|
2016-04-28 11:52:28 +03:00
|
|
|
//we need to pass the user name, which may differ from login name
|
|
|
|
$user = $this->getUser()->getUID();
|
2016-05-02 20:58:19 +03:00
|
|
|
OC_Util::setupFS($user);
|
2016-09-23 18:21:07 +03:00
|
|
|
|
|
|
|
if ($firstTimeLogin) {
|
|
|
|
// TODO: lock necessary?
|
|
|
|
//trigger creation of user home and /files folder
|
|
|
|
$userFolder = \OC::$server->getUserFolder($user);
|
|
|
|
|
2017-05-05 15:44:51 +03:00
|
|
|
try {
|
|
|
|
// copy skeleton
|
|
|
|
\OC_Util::copySkeleton($user, $userFolder);
|
|
|
|
} catch (NotPermittedException $ex) {
|
|
|
|
// read only uses
|
|
|
|
}
|
2016-09-23 18:21:07 +03:00
|
|
|
|
|
|
|
// trigger any other initialization
|
|
|
|
\OC::$server->getEventDispatcher()->dispatch(IUser::class . '::firstLogin', new GenericEvent($this->getUser()));
|
|
|
|
}
|
2016-04-28 11:52:28 +03:00
|
|
|
}
|
|
|
|
|
2016-04-25 15:10:55 +03:00
|
|
|
/**
|
|
|
|
* Tries to login the user with HTTP Basic Authentication
|
2016-04-29 15:00:07 +03:00
|
|
|
*
|
2016-05-11 12:23:25 +03:00
|
|
|
* @todo do not allow basic auth if the user is 2FA enforced
|
2016-04-29 15:00:07 +03:00
|
|
|
* @param IRequest $request
|
2016-12-05 22:57:15 +03:00
|
|
|
* @param OC\Security\Bruteforce\Throttler $throttler
|
2016-04-27 10:38:30 +03:00
|
|
|
* @return boolean if the login was successful
|
2016-04-25 15:10:55 +03:00
|
|
|
*/
|
2016-07-20 19:36:15 +03:00
|
|
|
public function tryBasicAuthLogin(IRequest $request,
|
|
|
|
OC\Security\Bruteforce\Throttler $throttler) {
|
2016-05-06 17:31:40 +03:00
|
|
|
if (!empty($request->server['PHP_AUTH_USER']) && !empty($request->server['PHP_AUTH_PW'])) {
|
2016-06-17 12:01:35 +03:00
|
|
|
try {
|
2016-07-20 19:36:15 +03:00
|
|
|
if ($this->logClientIn($request->server['PHP_AUTH_USER'], $request->server['PHP_AUTH_PW'], $request, $throttler)) {
|
2016-06-17 12:01:35 +03:00
|
|
|
/**
|
|
|
|
* Add DAV authenticated. This should in an ideal world not be
|
|
|
|
* necessary but the iOS App reads cookies from anywhere instead
|
|
|
|
* only the DAV endpoint.
|
|
|
|
* This makes sure that the cookies will be valid for the whole scope
|
|
|
|
* @see https://github.com/owncloud/core/issues/22893
|
|
|
|
*/
|
|
|
|
$this->session->set(
|
|
|
|
Auth::DAV_AUTHENTICATED, $this->getUser()->getUID()
|
|
|
|
);
|
2016-12-05 22:57:15 +03:00
|
|
|
|
|
|
|
// Set the last-password-confirm session to make the sudo mode work
|
2017-02-17 17:40:20 +03:00
|
|
|
$this->session->set('last-password-confirm', $this->timeFactory->getTime());
|
2016-12-05 22:57:15 +03:00
|
|
|
|
2016-06-17 12:01:35 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} catch (PasswordLoginForbiddenException $ex) {
|
|
|
|
// Nothing to do
|
2016-04-25 15:10:55 +03:00
|
|
|
}
|
|
|
|
}
|
2016-04-27 10:38:30 +03:00
|
|
|
return false;
|
2016-04-25 15:10:55 +03:00
|
|
|
}
|
|
|
|
|
2016-06-27 23:16:22 +03:00
|
|
|
/**
|
|
|
|
* Log an user in via login name and password
|
|
|
|
*
|
|
|
|
* @param string $uid
|
|
|
|
* @param string $password
|
|
|
|
* @return boolean
|
|
|
|
* @throws LoginException if an app canceld the login process or the user is not enabled
|
|
|
|
*/
|
|
|
|
private function loginWithPassword($uid, $password) {
|
2018-01-23 11:41:44 +03:00
|
|
|
$user = $this->manager->checkPasswordNoLogging($uid, $password);
|
2016-06-27 23:16:22 +03:00
|
|
|
if ($user === false) {
|
|
|
|
// Password check failed
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-02 13:16:33 +03:00
|
|
|
return $this->completeLogin($user, ['loginName' => $uid, 'password' => $password], false);
|
2016-06-27 23:16:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Log an user in with a given token (id)
|
|
|
|
*
|
|
|
|
* @param string $token
|
|
|
|
* @return boolean
|
2017-01-26 13:08:26 +03:00
|
|
|
* @throws LoginException if an app canceled the login process or the user is not enabled
|
2016-06-27 23:16:22 +03:00
|
|
|
*/
|
2016-06-17 14:59:15 +03:00
|
|
|
private function loginWithToken($token) {
|
|
|
|
try {
|
|
|
|
$dbToken = $this->tokenProvider->getToken($token);
|
|
|
|
} catch (InvalidTokenException $ex) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$uid = $dbToken->getUID();
|
|
|
|
|
2016-06-27 23:16:22 +03:00
|
|
|
// When logging in with token, the password must be decrypted first before passing to login hook
|
2016-06-20 10:13:47 +03:00
|
|
|
$password = '';
|
2016-06-17 14:59:15 +03:00
|
|
|
try {
|
|
|
|
$password = $this->tokenProvider->getPassword($dbToken, $token);
|
|
|
|
} catch (PasswordlessTokenException $ex) {
|
2016-06-20 10:13:47 +03:00
|
|
|
// Ignore and use empty string instead
|
2016-06-17 14:59:15 +03:00
|
|
|
}
|
2016-06-27 23:16:22 +03:00
|
|
|
|
2016-12-01 16:06:22 +03:00
|
|
|
$this->manager->emit('\OC\User', 'preLogin', array($uid, $password));
|
|
|
|
|
2016-04-25 15:10:55 +03:00
|
|
|
$user = $this->manager->get($uid);
|
|
|
|
if (is_null($user)) {
|
|
|
|
// user does not exist
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-27 23:16:22 +03:00
|
|
|
|
2017-04-06 16:04:00 +03:00
|
|
|
return $this->completeLogin(
|
|
|
|
$user,
|
|
|
|
[
|
|
|
|
'loginName' => $dbToken->getLoginName(),
|
|
|
|
'password' => $password,
|
|
|
|
'token' => $dbToken
|
|
|
|
],
|
|
|
|
false);
|
2016-04-25 15:10:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new session token for the given user credentials
|
|
|
|
*
|
2016-04-27 10:38:30 +03:00
|
|
|
* @param IRequest $request
|
2016-04-25 15:10:55 +03:00
|
|
|
* @param string $uid user UID
|
2016-05-24 11:50:18 +03:00
|
|
|
* @param string $loginName login name
|
2016-04-25 15:10:55 +03:00
|
|
|
* @param string $password
|
2016-09-06 22:41:15 +03:00
|
|
|
* @param int $remember
|
2016-04-25 15:10:55 +03:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2016-09-06 22:41:15 +03:00
|
|
|
public function createSessionToken(IRequest $request, $uid, $loginName, $password = null, $remember = IToken::DO_NOT_REMEMBER) {
|
2016-04-25 15:10:55 +03:00
|
|
|
if (is_null($this->manager->get($uid))) {
|
|
|
|
// User does not exist
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-26 13:45:08 +03:00
|
|
|
$name = isset($request->server['HTTP_USER_AGENT']) ? $request->server['HTTP_USER_AGENT'] : 'unknown browser';
|
2016-06-08 16:03:15 +03:00
|
|
|
try {
|
|
|
|
$sessionId = $this->session->getId();
|
|
|
|
$pwd = $this->getPassword($password);
|
2018-06-11 11:45:19 +03:00
|
|
|
// Make sure the current sessionId has no leftover tokens
|
|
|
|
$this->tokenProvider->invalidateToken($sessionId);
|
2016-11-27 15:59:46 +03:00
|
|
|
$this->tokenProvider->generateToken($sessionId, $uid, $loginName, $pwd, $name, IToken::TEMPORARY_TOKEN, $remember);
|
2016-06-08 16:03:15 +03:00
|
|
|
return true;
|
|
|
|
} catch (SessionNotAvailableException $ex) {
|
|
|
|
// This can happen with OCC, where a memory session is used
|
|
|
|
// if a memory session is used, we shouldn't create a session token anyway
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-03 09:55:00 +03:00
|
|
|
}
|
2016-05-20 18:54:46 +03:00
|
|
|
|
2016-06-03 09:55:00 +03:00
|
|
|
/**
|
|
|
|
* Checks if the given password is a token.
|
|
|
|
* If yes, the password is extracted from the token.
|
|
|
|
* If no, the same password is returned.
|
|
|
|
*
|
|
|
|
* @param string $password either the login password or a device token
|
|
|
|
* @return string|null the password or null if none was set in the token
|
|
|
|
*/
|
|
|
|
private function getPassword($password) {
|
|
|
|
if (is_null($password)) {
|
|
|
|
// This is surely no token ;-)
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
$token = $this->tokenProvider->getToken($password);
|
|
|
|
try {
|
|
|
|
return $this->tokenProvider->getPassword($token, $password);
|
|
|
|
} catch (PasswordlessTokenException $ex) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} catch (InvalidTokenException $ex) {
|
|
|
|
return $password;
|
2016-04-27 13:01:13 +03:00
|
|
|
}
|
2016-04-25 15:10:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-17 14:59:15 +03:00
|
|
|
* @param IToken $dbToken
|
2016-04-25 15:10:55 +03:00
|
|
|
* @param string $token
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2016-06-17 14:59:15 +03:00
|
|
|
private function checkTokenCredentials(IToken $dbToken, $token) {
|
|
|
|
// Check whether login credentials are still valid and the user was not disabled
|
|
|
|
// This check is performed each 5 minutes
|
|
|
|
$lastCheck = $dbToken->getLastCheck() ? : 0;
|
2017-02-17 17:40:20 +03:00
|
|
|
$now = $this->timeFactory->getTime();
|
2016-06-17 14:59:15 +03:00
|
|
|
if ($lastCheck > ($now - 60 * 5)) {
|
|
|
|
// Checked performed recently, nothing to do now
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-17 18:20:54 +03:00
|
|
|
try {
|
2016-06-17 14:59:15 +03:00
|
|
|
$pwd = $this->tokenProvider->getPassword($dbToken, $token);
|
2016-05-17 18:20:54 +03:00
|
|
|
} catch (InvalidTokenException $ex) {
|
2016-06-17 14:59:15 +03:00
|
|
|
// An invalid token password was used -> log user out
|
|
|
|
return false;
|
|
|
|
} catch (PasswordlessTokenException $ex) {
|
|
|
|
// Token has no password
|
2016-05-17 18:20:54 +03:00
|
|
|
|
2016-06-17 14:59:15 +03:00
|
|
|
if (!is_null($this->activeUser) && !$this->activeUser->isEnabled()) {
|
|
|
|
$this->tokenProvider->invalidateToken($token);
|
|
|
|
return false;
|
2016-04-25 15:10:55 +03:00
|
|
|
}
|
2016-06-17 14:59:15 +03:00
|
|
|
|
|
|
|
$dbToken->setLastCheck($now);
|
|
|
|
return true;
|
2016-04-25 15:10:55 +03:00
|
|
|
}
|
2016-06-17 14:59:15 +03:00
|
|
|
|
2018-09-26 14:10:17 +03:00
|
|
|
// Invalidate token if the user is no longer active
|
|
|
|
if (!is_null($this->activeUser) && !$this->activeUser->isEnabled()) {
|
2016-06-17 14:59:15 +03:00
|
|
|
$this->tokenProvider->invalidateToken($token);
|
|
|
|
return false;
|
|
|
|
}
|
2018-09-26 14:10:17 +03:00
|
|
|
|
|
|
|
// If the token password is no longer valid mark it as such
|
|
|
|
if ($this->manager->checkPassword($dbToken->getLoginName(), $pwd) === false) {
|
|
|
|
$this->tokenProvider->markPasswordInvalid($dbToken, $token);
|
|
|
|
// User is logged out
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-06-17 14:59:15 +03:00
|
|
|
$dbToken->setLastCheck($now);
|
|
|
|
return true;
|
2016-04-25 15:10:55 +03:00
|
|
|
}
|
|
|
|
|
2016-04-27 10:38:30 +03:00
|
|
|
/**
|
2016-06-17 14:59:15 +03:00
|
|
|
* Check if the given token exists and performs password/user-enabled checks
|
|
|
|
*
|
|
|
|
* Invalidates the token if checks fail
|
|
|
|
*
|
|
|
|
* @param string $token
|
2016-06-24 14:57:09 +03:00
|
|
|
* @param string $user login name
|
2016-06-17 14:59:15 +03:00
|
|
|
* @return boolean
|
2016-04-27 10:38:30 +03:00
|
|
|
*/
|
2016-06-24 14:57:09 +03:00
|
|
|
private function validateToken($token, $user = null) {
|
2016-06-17 14:59:15 +03:00
|
|
|
try {
|
|
|
|
$dbToken = $this->tokenProvider->getToken($token);
|
2016-05-17 18:20:54 +03:00
|
|
|
} catch (InvalidTokenException $ex) {
|
2016-06-17 14:59:15 +03:00
|
|
|
return false;
|
|
|
|
}
|
2016-05-17 18:20:54 +03:00
|
|
|
|
2016-06-24 14:57:09 +03:00
|
|
|
// Check if login names match
|
|
|
|
if (!is_null($user) && $dbToken->getLoginName() !== $user) {
|
|
|
|
// TODO: this makes it imposssible to use different login names on browser and client
|
|
|
|
// e.g. login by e-mail 'user@example.com' on browser for generating the token will not
|
|
|
|
// allow to use the client token with the login name 'user'.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-06-17 14:59:15 +03:00
|
|
|
if (!$this->checkTokenCredentials($dbToken, $token)) {
|
|
|
|
return false;
|
2016-04-27 10:38:30 +03:00
|
|
|
}
|
2016-06-17 14:59:15 +03:00
|
|
|
|
2018-08-14 10:45:52 +03:00
|
|
|
// Update token scope
|
|
|
|
$this->lockdownManager->setToken($dbToken);
|
|
|
|
|
2016-06-17 16:41:32 +03:00
|
|
|
$this->tokenProvider->updateTokenActivity($dbToken);
|
|
|
|
|
2016-06-17 14:59:15 +03:00
|
|
|
return true;
|
2016-04-27 10:38:30 +03:00
|
|
|
}
|
|
|
|
|
2016-04-25 15:10:55 +03:00
|
|
|
/**
|
|
|
|
* Tries to login the user with auth token header
|
|
|
|
*
|
2016-08-11 11:55:39 +03:00
|
|
|
* @param IRequest $request
|
2016-04-25 15:10:55 +03:00
|
|
|
* @todo check remember me cookie
|
2016-05-09 16:33:56 +03:00
|
|
|
* @return boolean
|
2016-04-25 15:10:55 +03:00
|
|
|
*/
|
2016-04-29 10:40:33 +03:00
|
|
|
public function tryTokenLogin(IRequest $request) {
|
2016-04-25 15:10:55 +03:00
|
|
|
$authHeader = $request->getHeader('Authorization');
|
2017-05-05 00:46:59 +03:00
|
|
|
if (strpos($authHeader, 'Bearer ') === false) {
|
2016-04-25 15:10:55 +03:00
|
|
|
// No auth header, let's try session id
|
2016-05-03 17:35:00 +03:00
|
|
|
try {
|
2016-06-20 11:41:23 +03:00
|
|
|
$token = $this->session->getId();
|
2016-05-03 17:35:00 +03:00
|
|
|
} catch (SessionNotAvailableException $ex) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-25 15:10:55 +03:00
|
|
|
} else {
|
2017-05-05 00:46:59 +03:00
|
|
|
$token = substr($authHeader, 7);
|
2016-04-25 15:10:55 +03:00
|
|
|
}
|
2016-06-20 11:41:23 +03:00
|
|
|
|
|
|
|
if (!$this->loginWithToken($token)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(!$this->validateToken($token)) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-20 11:23:57 +03:00
|
|
|
|
|
|
|
// Set the session variable so we know this is an app password
|
|
|
|
$this->session->set('app_password', $token);
|
|
|
|
|
2016-06-20 11:41:23 +03:00
|
|
|
return true;
|
2016-04-25 15:10:55 +03:00
|
|
|
}
|
|
|
|
|
2014-05-21 20:03:37 +04:00
|
|
|
/**
|
|
|
|
* perform login using the magic cookie (remember login)
|
|
|
|
*
|
|
|
|
* @param string $uid the username
|
|
|
|
* @param string $currentToken
|
2016-09-06 22:41:15 +03:00
|
|
|
* @param string $oldSessionId
|
2014-05-21 20:03:37 +04:00
|
|
|
* @return bool
|
|
|
|
*/
|
2016-09-06 22:41:15 +03:00
|
|
|
public function loginWithCookie($uid, $currentToken, $oldSessionId) {
|
2016-01-04 17:00:58 +03:00
|
|
|
$this->session->regenerateId();
|
2014-05-26 15:53:26 +04:00
|
|
|
$this->manager->emit('\OC\User', 'preRememberedLogin', array($uid));
|
2014-05-21 20:03:37 +04:00
|
|
|
$user = $this->manager->get($uid);
|
2014-10-13 15:11:48 +04:00
|
|
|
if (is_null($user)) {
|
2014-05-21 20:03:37 +04:00
|
|
|
// user does not exist
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get stored tokens
|
2016-09-06 22:41:15 +03:00
|
|
|
$tokens = $this->config->getUserKeys($uid, 'login_token');
|
2014-05-21 20:03:37 +04:00
|
|
|
// test cookies token against stored tokens
|
2014-10-13 15:11:48 +04:00
|
|
|
if (!in_array($currentToken, $tokens, true)) {
|
2014-05-21 20:03:37 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// replace successfully used token with a new one
|
2016-09-06 22:41:15 +03:00
|
|
|
$this->config->deleteUserValue($uid, 'login_token', $currentToken);
|
2016-10-31 11:36:55 +03:00
|
|
|
$newToken = $this->random->generate(32);
|
2017-02-17 17:40:20 +03:00
|
|
|
$this->config->setUserValue($uid, 'login_token', $newToken, $this->timeFactory->getTime());
|
2016-09-06 22:41:15 +03:00
|
|
|
|
|
|
|
try {
|
|
|
|
$sessionId = $this->session->getId();
|
2019-10-08 12:01:53 +03:00
|
|
|
$token = $this->tokenProvider->renewSessionToken($oldSessionId, $sessionId);
|
2016-09-06 22:41:15 +03:00
|
|
|
} catch (SessionNotAvailableException $ex) {
|
|
|
|
return false;
|
|
|
|
} catch (InvalidTokenException $ex) {
|
|
|
|
\OC::$server->getLogger()->warning('Renewing session token failed', ['app' => 'core']);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-21 20:03:37 +04:00
|
|
|
$this->setMagicInCookie($user->getUID(), $newToken);
|
|
|
|
|
|
|
|
//login
|
|
|
|
$this->setUser($user);
|
2017-01-26 13:08:26 +03:00
|
|
|
$this->setLoginName($token->getLoginName());
|
|
|
|
$this->setToken($token->getId());
|
2017-02-17 17:40:20 +03:00
|
|
|
$this->lockdownManager->setToken($token);
|
2016-09-23 18:21:07 +03:00
|
|
|
$user->updateLastLoginTimestamp();
|
2017-05-16 09:41:11 +03:00
|
|
|
$password = null;
|
|
|
|
try {
|
|
|
|
$password = $this->tokenProvider->getPassword($token, $sessionId);
|
|
|
|
} catch (PasswordlessTokenException $ex) {
|
|
|
|
// Ignore
|
|
|
|
}
|
|
|
|
$this->manager->emit('\OC\User', 'postRememberedLogin', [$user, $password]);
|
2014-05-21 20:03:37 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-06 22:41:15 +03:00
|
|
|
/**
|
|
|
|
* @param IUser $user
|
|
|
|
*/
|
|
|
|
public function createRememberMeToken(IUser $user) {
|
2016-10-31 11:36:55 +03:00
|
|
|
$token = $this->random->generate(32);
|
2017-02-17 17:40:20 +03:00
|
|
|
$this->config->setUserValue($user->getUID(), 'login_token', $token, $this->timeFactory->getTime());
|
2016-09-06 22:41:15 +03:00
|
|
|
$this->setMagicInCookie($user->getUID(), $token);
|
|
|
|
}
|
|
|
|
|
2013-06-03 15:33:56 +04:00
|
|
|
/**
|
|
|
|
* logout the user from the session
|
|
|
|
*/
|
2013-05-29 01:46:57 +04:00
|
|
|
public function logout() {
|
2016-04-25 17:40:41 +03:00
|
|
|
$user = $this->getUser();
|
2019-12-11 11:38:23 +03:00
|
|
|
$this->manager->emit('\OC\User', 'logout', [$user]);
|
|
|
|
if ($user !== null) {
|
2016-05-03 17:35:00 +03:00
|
|
|
try {
|
|
|
|
$this->tokenProvider->invalidateToken($this->session->getId());
|
|
|
|
} catch (SessionNotAvailableException $ex) {
|
2017-01-17 19:21:27 +03:00
|
|
|
|
2016-05-03 17:35:00 +03:00
|
|
|
}
|
2016-04-25 17:40:41 +03:00
|
|
|
}
|
2013-05-29 01:46:57 +04:00
|
|
|
$this->setUser(null);
|
2014-01-09 13:27:47 +04:00
|
|
|
$this->setLoginName(null);
|
2017-01-26 13:08:26 +03:00
|
|
|
$this->setToken(null);
|
2013-05-29 01:46:57 +04:00
|
|
|
$this->unsetMagicInCookie();
|
2014-10-30 14:10:39 +03:00
|
|
|
$this->session->clear();
|
2019-12-11 11:38:23 +03:00
|
|
|
$this->manager->emit('\OC\User', 'postLogout', [$user]);
|
2013-05-29 01:46:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set cookie value to use in next page load
|
|
|
|
*
|
|
|
|
* @param string $username username to be set
|
|
|
|
* @param string $token
|
|
|
|
*/
|
|
|
|
public function setMagicInCookie($username, $token) {
|
2016-04-25 15:10:55 +03:00
|
|
|
$secureCookie = OC::$server->getRequest()->getServerProtocol() === 'https';
|
2016-09-06 22:41:15 +03:00
|
|
|
$webRoot = \OC::$WEBROOT;
|
|
|
|
if ($webRoot === '') {
|
|
|
|
$webRoot = '/';
|
|
|
|
}
|
|
|
|
|
2018-09-28 16:06:48 +03:00
|
|
|
$maxAge = $this->config->getSystemValue('remember_login_cookie_lifetime', 60 * 60 * 24 * 15);
|
|
|
|
\OC\Http\CookieHelper::setCookie(
|
|
|
|
'nc_username',
|
|
|
|
$username,
|
|
|
|
$maxAge,
|
|
|
|
$webRoot,
|
|
|
|
'',
|
|
|
|
$secureCookie,
|
|
|
|
true,
|
|
|
|
\OC\Http\CookieHelper::SAMESITE_LAX
|
|
|
|
);
|
|
|
|
\OC\Http\CookieHelper::setCookie(
|
|
|
|
'nc_token',
|
|
|
|
$token,
|
|
|
|
$maxAge,
|
|
|
|
$webRoot,
|
|
|
|
'',
|
|
|
|
$secureCookie,
|
|
|
|
true,
|
|
|
|
\OC\Http\CookieHelper::SAMESITE_LAX
|
|
|
|
);
|
2016-09-06 22:41:15 +03:00
|
|
|
try {
|
2018-09-28 16:06:48 +03:00
|
|
|
\OC\Http\CookieHelper::setCookie(
|
|
|
|
'nc_session_id',
|
|
|
|
$this->session->getId(),
|
|
|
|
$maxAge,
|
|
|
|
$webRoot,
|
|
|
|
'',
|
|
|
|
$secureCookie,
|
|
|
|
true,
|
|
|
|
\OC\Http\CookieHelper::SAMESITE_LAX
|
|
|
|
);
|
2016-09-06 22:41:15 +03:00
|
|
|
} catch (SessionNotAvailableException $ex) {
|
|
|
|
// ignore
|
|
|
|
}
|
2013-05-29 01:46:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-03 15:33:56 +04:00
|
|
|
* Remove cookie for "remember username"
|
2013-05-29 01:46:57 +04:00
|
|
|
*/
|
|
|
|
public function unsetMagicInCookie() {
|
2015-08-26 15:29:36 +03:00
|
|
|
//TODO: DI for cookies and IRequest
|
2016-04-25 15:10:55 +03:00
|
|
|
$secureCookie = OC::$server->getRequest()->getServerProtocol() === 'https';
|
2015-01-14 13:20:53 +03:00
|
|
|
|
2016-09-06 22:41:15 +03:00
|
|
|
unset($_COOKIE['nc_username']); //TODO: DI
|
|
|
|
unset($_COOKIE['nc_token']);
|
|
|
|
unset($_COOKIE['nc_session_id']);
|
2017-02-17 17:40:20 +03:00
|
|
|
setcookie('nc_username', '', $this->timeFactory->getTime() - 3600, OC::$WEBROOT, '', $secureCookie, true);
|
|
|
|
setcookie('nc_token', '', $this->timeFactory->getTime() - 3600, OC::$WEBROOT, '', $secureCookie, true);
|
|
|
|
setcookie('nc_session_id', '', $this->timeFactory->getTime() - 3600, OC::$WEBROOT, '', $secureCookie, true);
|
2013-11-07 21:40:15 +04:00
|
|
|
// old cookies might be stored under /webroot/ instead of /webroot
|
|
|
|
// and Firefox doesn't like it!
|
2017-02-17 17:40:20 +03:00
|
|
|
setcookie('nc_username', '', $this->timeFactory->getTime() - 3600, OC::$WEBROOT . '/', '', $secureCookie, true);
|
|
|
|
setcookie('nc_token', '', $this->timeFactory->getTime() - 3600, OC::$WEBROOT . '/', '', $secureCookie, true);
|
|
|
|
setcookie('nc_session_id', '', $this->timeFactory->getTime() - 3600, OC::$WEBROOT . '/', '', $secureCookie, true);
|
2013-05-29 01:46:57 +04:00
|
|
|
}
|
2016-04-25 15:10:55 +03:00
|
|
|
|
2016-06-21 11:23:50 +03:00
|
|
|
/**
|
|
|
|
* Update password of the browser session token if there is one
|
|
|
|
*
|
|
|
|
* @param string $password
|
|
|
|
*/
|
|
|
|
public function updateSessionTokenPassword($password) {
|
|
|
|
try {
|
|
|
|
$sessionId = $this->session->getId();
|
|
|
|
$token = $this->tokenProvider->getToken($sessionId);
|
|
|
|
$this->tokenProvider->setPassword($token, $sessionId, $password);
|
|
|
|
} catch (SessionNotAvailableException $ex) {
|
|
|
|
// Nothing to do
|
|
|
|
} catch (InvalidTokenException $ex) {
|
|
|
|
// Nothing to do
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 14:36:04 +03:00
|
|
|
public function updateTokens(string $uid, string $password) {
|
|
|
|
$this->tokenProvider->updatePasswords($uid, $password);
|
|
|
|
}
|
|
|
|
|
2016-09-06 22:41:15 +03:00
|
|
|
|
2013-05-29 01:46:57 +04:00
|
|
|
}
|