2013-05-28 21:46:57 +00:00
|
|
|
<?php
|
2016-04-25 12:10:55 +00:00
|
|
|
|
2013-05-28 21:46:57 +00:00
|
|
|
/**
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Arthur Schiwon <blizzz@owncloud.com>
|
|
|
|
* @author Bernhard Posselt <dev@bernhard-posselt.com>
|
2016-04-25 12:10:55 +00:00
|
|
|
* @author Christoph Wurst <christoph@owncloud.com>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Joas Schilling <nickvergessen@owncloud.com>
|
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
|
|
|
* @author Lukas Reschke <lukas@owncloud.com>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Robin Appelman <icewind@owncloud.com>
|
2016-01-12 14:02:16 +00:00
|
|
|
* @author Robin McCorkell <robin@mccorkell.me.uk>
|
|
|
|
* @author Roeland Jago Douma <rullzer@owncloud.com>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
|
|
|
*
|
2016-01-12 14:02:16 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
2015-03-26 10:44:34 +00:00
|
|
|
* @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,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
2013-05-28 21:46:57 +00:00
|
|
|
*/
|
2015-02-26 10:37:37 +00:00
|
|
|
|
2013-05-28 21:46:57 +00:00
|
|
|
namespace OC\User;
|
|
|
|
|
2016-04-25 12:10:55 +00:00
|
|
|
use OC;
|
|
|
|
use OC\Authentication\Exceptions\InvalidTokenException;
|
|
|
|
use OC\Authentication\Token\DefaultTokenProvider;
|
|
|
|
use OC\Authentication\Token\IProvider;
|
2016-04-27 07:38:30 +00:00
|
|
|
use OC\Authentication\Token\IToken;
|
2013-05-28 21:46:57 +00:00
|
|
|
use OC\Hooks\Emitter;
|
2016-04-25 12:10:55 +00:00
|
|
|
use OC_User;
|
|
|
|
use OCA\DAV\Connector\Sabre\Auth;
|
|
|
|
use OCP\IRequest;
|
2016-04-07 13:39:34 +00:00
|
|
|
use OCP\ISession;
|
2016-04-25 12:10:55 +00:00
|
|
|
use OCP\IUser;
|
2016-04-07 13:39:34 +00:00
|
|
|
use OCP\IUserManager;
|
2014-07-09 13:32:33 +00:00
|
|
|
use OCP\IUserSession;
|
2013-05-28 21:46:57 +00: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)
|
|
|
|
* - preLogin(string $user, string $password)
|
2014-05-26 11:53:26 +00:00
|
|
|
* - postLogin(\OC\User\User $user, string $password)
|
|
|
|
* - preRememberedLogin(string $uid)
|
|
|
|
* - postRememberedLogin(\OC\User\User $user)
|
2013-05-28 21:46:57 +00:00
|
|
|
* - logout()
|
|
|
|
*
|
|
|
|
* @package OC\User
|
|
|
|
*/
|
2014-07-09 13:32:33 +00:00
|
|
|
class Session implements IUserSession, Emitter {
|
2016-04-25 12:10:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* @var Manager $manager
|
|
|
|
*/
|
2013-05-28 21:46:57 +00:00
|
|
|
private $manager;
|
|
|
|
|
2016-04-25 12:10:55 +00:00
|
|
|
/*
|
2016-04-26 10:48:19 +00:00
|
|
|
* @var ISession $session
|
2016-04-25 12:10:55 +00:00
|
|
|
*/
|
2013-05-28 21:46:57 +00:00
|
|
|
private $session;
|
|
|
|
|
2016-04-25 12:10:55 +00:00
|
|
|
/*
|
|
|
|
* @var DefaultTokenProvider
|
|
|
|
*/
|
|
|
|
private $tokenProvider;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var IProvider[]
|
|
|
|
*/
|
|
|
|
private $tokenProviders;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var User $activeUser
|
2013-05-28 21:46:57 +00:00
|
|
|
protected $activeUser;
|
|
|
|
|
|
|
|
/**
|
2016-04-07 13:39:34 +00:00
|
|
|
* @param IUserManager $manager
|
|
|
|
* @param ISession $session
|
2016-04-25 12:10:55 +00:00
|
|
|
* @param IProvider[] $tokenProviders
|
2013-05-28 21:46:57 +00:00
|
|
|
*/
|
2016-04-26 09:32:35 +00:00
|
|
|
public function __construct(IUserManager $manager, ISession $session, DefaultTokenProvider $tokenProvider, array $tokenProviders = []) {
|
2013-05-28 21:46:57 +00:00
|
|
|
$this->manager = $manager;
|
|
|
|
$this->session = $session;
|
2016-04-25 12:10:55 +00:00
|
|
|
$this->tokenProvider = $tokenProvider;
|
|
|
|
$this->tokenProviders = $tokenProviders;
|
2013-05-28 21:46:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $scope
|
|
|
|
* @param string $method
|
|
|
|
* @param callable $callback
|
|
|
|
*/
|
2015-05-08 12:27:22 +00:00
|
|
|
public function listen($scope, $method, callable $callback) {
|
2013-05-28 21:46:57 +00:00
|
|
|
$this->manager->listen($scope, $method, $callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $scope optional
|
|
|
|
* @param string $method optional
|
|
|
|
* @param callable $callback optional
|
|
|
|
*/
|
2016-04-26 09:32:35 +00:00
|
|
|
public function removeListener($scope = null, $method = null, callable $callback = null) {
|
2013-05-28 21:46:57 +00:00
|
|
|
$this->manager->removeListener($scope, $method, $callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-03 11:33:56 +00:00
|
|
|
* get the manager object
|
|
|
|
*
|
2016-04-25 12:10:55 +00:00
|
|
|
* @return Manager
|
2013-05-28 21:46:57 +00:00
|
|
|
*/
|
|
|
|
public function getManager() {
|
|
|
|
return $this->manager;
|
|
|
|
}
|
|
|
|
|
2014-07-16 17:40:22 +00:00
|
|
|
/**
|
|
|
|
* get the session object
|
|
|
|
*
|
2016-04-07 13:39:34 +00:00
|
|
|
* @return ISession
|
2014-07-16 17:40:22 +00:00
|
|
|
*/
|
|
|
|
public function getSession() {
|
|
|
|
return $this->session;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set the session object
|
|
|
|
*
|
2016-04-07 13:39:34 +00:00
|
|
|
* @param ISession $session
|
2014-07-16 17:40:22 +00:00
|
|
|
*/
|
2016-04-07 13:39:34 +00:00
|
|
|
public function setSession(ISession $session) {
|
|
|
|
if ($this->session instanceof ISession) {
|
2014-07-16 17:40:22 +00:00
|
|
|
$this->session->close();
|
|
|
|
}
|
|
|
|
$this->session = $session;
|
2014-10-13 11:11:48 +00:00
|
|
|
$this->activeUser = null;
|
2014-07-16 17:40:22 +00:00
|
|
|
}
|
|
|
|
|
2013-05-28 21:46:57 +00:00
|
|
|
/**
|
|
|
|
* set the currently active user
|
|
|
|
*
|
2016-04-25 12:10:55 +00:00
|
|
|
* @param User|null $user
|
2013-05-28 21:46:57 +00: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 12:10:55 +00:00
|
|
|
* @return IUser|null Current user, otherwise null
|
2013-05-28 21:46:57 +00:00
|
|
|
*/
|
|
|
|
public function getUser() {
|
2014-12-17 20:53:43 +00: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 12:10:55 +00:00
|
|
|
if (OC_User::isIncognitoMode()) {
|
2014-12-17 20:53:43 +00:00
|
|
|
return null;
|
|
|
|
}
|
2013-05-28 21:46:57 +00:00
|
|
|
if ($this->activeUser) {
|
|
|
|
return $this->activeUser;
|
|
|
|
} else {
|
|
|
|
$uid = $this->session->get('user_id');
|
2016-04-26 09:32:35 +00:00
|
|
|
if ($uid !== null && $this->isValidSession($uid)) {
|
2013-05-28 21:46:57 +00:00
|
|
|
return $this->activeUser;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-26 09:32:35 +00:00
|
|
|
private function isValidSession($uid) {
|
|
|
|
$this->activeUser = $this->manager->get($uid);
|
|
|
|
if (is_null($this->activeUser)) {
|
|
|
|
// User does not exist
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// TODO: use ISession::getId(), https://github.com/owncloud/core/pull/24229
|
|
|
|
$sessionId = session_id();
|
|
|
|
try {
|
|
|
|
$token = $this->tokenProvider->getToken($sessionId);
|
|
|
|
} catch (InvalidTokenException $ex) {
|
|
|
|
// Session was inalidated
|
|
|
|
$this->logout();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether login credentials are still valid
|
|
|
|
// This check is performed each 5 minutes
|
|
|
|
$lastCheck = $this->session->get('last_login_check') ? : 0;
|
|
|
|
if ($lastCheck < (time() - 60 * 5)) {
|
|
|
|
$pwd = $this->tokenProvider->getPassword($token, $sessionId);
|
|
|
|
if ($this->manager->checkPassword($uid, $pwd) === false) {
|
|
|
|
// Password has changed -> log user out
|
|
|
|
$this->logout();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$this->session->set('last_login_check', time());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Session is valid, so the token can be refreshed
|
2016-04-27 07:38:30 +00:00
|
|
|
$this->updateToken($this->tokenProvider, $token);
|
2016-04-26 09:32:35 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-12-16 18:07:14 +00:00
|
|
|
/**
|
2014-12-19 13:36:00 +00:00
|
|
|
* Checks whether the user is logged in
|
2014-12-16 18:07:14 +00:00
|
|
|
*
|
|
|
|
* @return bool if logged in
|
|
|
|
*/
|
|
|
|
public function isLoggedIn() {
|
2016-04-07 13:39:34 +00:00
|
|
|
$user = $this->getUser();
|
|
|
|
if (is_null($user)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $user->isEnabled();
|
2014-12-16 18:07:14 +00:00
|
|
|
}
|
|
|
|
|
2013-12-11 12:56:45 +00:00
|
|
|
/**
|
|
|
|
* set the login name
|
|
|
|
*
|
2014-05-12 22:27:36 +00:00
|
|
|
* @param string|null $loginName for the logged in user
|
2013-12-11 12:56:45 +00:00
|
|
|
*/
|
2014-01-09 09:27:47 +00:00
|
|
|
public function setLoginName($loginName) {
|
|
|
|
if (is_null($loginName)) {
|
2013-12-11 12:56:45 +00:00
|
|
|
$this->session->remove('loginname');
|
|
|
|
} else {
|
2014-01-09 09:27:47 +00:00
|
|
|
$this->session->set('loginname', $loginName);
|
2013-12-11 12:56:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the login name of the current user
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2014-01-09 09:27:47 +00:00
|
|
|
public function getLoginName() {
|
2013-12-11 12:56:45 +00:00
|
|
|
if ($this->activeUser) {
|
|
|
|
return $this->session->get('loginname');
|
|
|
|
} else {
|
|
|
|
$uid = $this->session->get('user_id');
|
|
|
|
if ($uid) {
|
|
|
|
$this->activeUser = $this->manager->get($uid);
|
|
|
|
return $this->session->get('loginname');
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-03 11:33:56 +00:00
|
|
|
/**
|
|
|
|
* try to login with the provided credentials
|
|
|
|
*
|
|
|
|
* @param string $uid
|
|
|
|
* @param string $password
|
2014-02-06 15:30:58 +00:00
|
|
|
* @return boolean|null
|
2015-01-22 13:13:17 +00:00
|
|
|
* @throws LoginException
|
2013-06-03 11:33:56 +00:00
|
|
|
*/
|
2013-05-28 21:46:57 +00:00
|
|
|
public function login($uid, $password) {
|
2016-01-04 14:00:58 +00:00
|
|
|
$this->session->regenerateId();
|
2013-05-28 21:46:57 +00:00
|
|
|
$this->manager->emit('\OC\User', 'preLogin', array($uid, $password));
|
2013-09-16 12:15:35 +00:00
|
|
|
$user = $this->manager->checkPassword($uid, $password);
|
2014-10-13 11:11:48 +00:00
|
|
|
if ($user !== false) {
|
2013-09-16 12:15:35 +00:00
|
|
|
if (!is_null($user)) {
|
|
|
|
if ($user->isEnabled()) {
|
|
|
|
$this->setUser($user);
|
2014-01-09 09:27:47 +00:00
|
|
|
$this->setLoginName($uid);
|
2013-09-16 12:15:35 +00:00
|
|
|
$this->manager->emit('\OC\User', 'postLogin', array($user, $password));
|
2015-01-22 13:13:17 +00:00
|
|
|
if ($this->isLoggedIn()) {
|
|
|
|
return true;
|
|
|
|
} else {
|
2016-04-07 13:39:34 +00:00
|
|
|
// injecting l10n does not work - there is a circular dependency between session and \OCP\L10N\IFactory
|
|
|
|
$message = \OC::$server->getL10N('lib')->t('Login canceled by app');
|
|
|
|
throw new LoginException($message);
|
2015-01-22 13:13:17 +00:00
|
|
|
}
|
2013-09-16 12:15:35 +00:00
|
|
|
} else {
|
2016-04-07 13:39:34 +00:00
|
|
|
// 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);
|
2013-09-16 12:15:35 +00:00
|
|
|
}
|
2013-05-28 21:46:57 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-07 13:39:34 +00:00
|
|
|
return false;
|
2013-05-28 21:46:57 +00:00
|
|
|
}
|
|
|
|
|
2016-04-25 12:10:55 +00:00
|
|
|
/**
|
|
|
|
* Tries to login the user with HTTP Basic Authentication
|
2016-04-27 07:38:30 +00:00
|
|
|
* @return boolean if the login was successful
|
2016-04-25 12:10:55 +00:00
|
|
|
*/
|
|
|
|
public function tryBasicAuthLogin() {
|
|
|
|
if (!empty($_SERVER['PHP_AUTH_USER']) && !empty($_SERVER['PHP_AUTH_PW'])) {
|
|
|
|
$result = $this->login($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']);
|
|
|
|
if ($result === true) {
|
|
|
|
/**
|
|
|
|
* 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-04-27 07:38:30 +00:00
|
|
|
return $result;
|
2016-04-25 12:10:55 +00:00
|
|
|
}
|
2016-04-27 07:38:30 +00:00
|
|
|
return false;
|
2016-04-25 12:10:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function loginWithToken($uid) {
|
|
|
|
//$this->manager->emit('\OC\User', 'preTokenLogin', array($uid));
|
|
|
|
$user = $this->manager->get($uid);
|
|
|
|
if (is_null($user)) {
|
|
|
|
// user does not exist
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//login
|
|
|
|
$this->setUser($user);
|
|
|
|
//$this->manager->emit('\OC\User', 'postTokenLogin', array($user));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new session token for the given user credentials
|
|
|
|
*
|
2016-04-27 07:38:30 +00:00
|
|
|
* @param IRequest $request
|
2016-04-25 12:10:55 +00:00
|
|
|
* @param string $uid user UID
|
|
|
|
* @param string $password
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2016-04-27 07:38:30 +00:00
|
|
|
public function createSessionToken(IRequest $request, $uid, $password) {
|
2016-04-25 14:40:41 +00:00
|
|
|
$this->session->regenerateId();
|
2016-04-25 12:10:55 +00:00
|
|
|
if (is_null($this->manager->get($uid))) {
|
|
|
|
// User does not exist
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-26 10:45:08 +00:00
|
|
|
$name = isset($request->server['HTTP_USER_AGENT']) ? $request->server['HTTP_USER_AGENT'] : 'unknown browser';
|
2016-04-25 14:40:41 +00:00
|
|
|
// TODO: use ISession::getId(), https://github.com/owncloud/core/pull/24229
|
|
|
|
$sessionId = session_id();
|
|
|
|
$token = $this->tokenProvider->generateToken($sessionId, $uid, $password, $name);
|
2016-04-25 12:10:55 +00:00
|
|
|
return $this->loginWithToken($uid);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-04-25 14:40:41 +00:00
|
|
|
* @param IRequest $request
|
2016-04-25 12:10:55 +00:00
|
|
|
* @param string $token
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
private function validateToken(IRequest $request, $token) {
|
|
|
|
foreach ($this->tokenProviders as $provider) {
|
|
|
|
try {
|
2016-04-27 07:38:30 +00:00
|
|
|
$token = $provider->validateToken($token);
|
|
|
|
if (!is_null($token)) {
|
|
|
|
$result = $this->loginWithToken($token->getUid());
|
2016-04-25 12:10:55 +00:00
|
|
|
if ($result) {
|
|
|
|
// Login success
|
2016-04-27 07:38:30 +00:00
|
|
|
$this->updateToken($provider, $token);
|
2016-04-25 12:10:55 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (InvalidTokenException $ex) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-27 07:38:30 +00:00
|
|
|
/**
|
|
|
|
* @param IProvider $provider
|
|
|
|
* @param IToken $token
|
|
|
|
*/
|
|
|
|
private function updateToken(IProvider $provider, IToken $token) {
|
|
|
|
// To save unnecessary DB queries, this is only done once a minute
|
|
|
|
$lastTokenUpdate = $this->session->get('last_token_update') ? : 0;
|
|
|
|
if ($lastTokenUpdate < (time () - 60)) {
|
|
|
|
$provider->updateToken($token);
|
|
|
|
$this->session->set('last_token_update', time());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-25 12:10:55 +00:00
|
|
|
/**
|
|
|
|
* Tries to login the user with auth token header
|
|
|
|
*
|
|
|
|
* @todo check remember me cookie
|
|
|
|
*/
|
|
|
|
public function tryTokenLogin() {
|
|
|
|
// TODO: resolve cyclic dependency and inject IRequest somehow
|
|
|
|
$request = \OC::$server->getRequest();
|
|
|
|
$authHeader = $request->getHeader('Authorization');
|
|
|
|
if (strpos($authHeader, 'token ') === false) {
|
|
|
|
// No auth header, let's try session id
|
|
|
|
// TODO: use ISession::getId(), https://github.com/owncloud/core/pull/24229
|
|
|
|
$sessionId = session_id();
|
|
|
|
return $this->validateToken($request, $sessionId);
|
|
|
|
} else {
|
|
|
|
$token = substr($authHeader, 6);
|
|
|
|
return $this->validateToken($request, $token);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-21 16:03:37 +00:00
|
|
|
/**
|
|
|
|
* perform login using the magic cookie (remember login)
|
|
|
|
*
|
|
|
|
* @param string $uid the username
|
|
|
|
* @param string $currentToken
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function loginWithCookie($uid, $currentToken) {
|
2016-01-04 14:00:58 +00:00
|
|
|
$this->session->regenerateId();
|
2014-05-26 11:53:26 +00:00
|
|
|
$this->manager->emit('\OC\User', 'preRememberedLogin', array($uid));
|
2014-05-21 16:03:37 +00:00
|
|
|
$user = $this->manager->get($uid);
|
2014-10-13 11:11:48 +00:00
|
|
|
if (is_null($user)) {
|
2014-05-21 16:03:37 +00:00
|
|
|
// user does not exist
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get stored tokens
|
2016-04-25 12:10:55 +00:00
|
|
|
$tokens = OC::$server->getConfig()->getUserKeys($uid, 'login_token');
|
2014-05-21 16:03:37 +00:00
|
|
|
// test cookies token against stored tokens
|
2014-10-13 11:11:48 +00:00
|
|
|
if (!in_array($currentToken, $tokens, true)) {
|
2014-05-21 16:03:37 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// replace successfully used token with a new one
|
2016-04-25 12:10:55 +00:00
|
|
|
OC::$server->getConfig()->deleteUserValue($uid, 'login_token', $currentToken);
|
|
|
|
$newToken = OC::$server->getSecureRandom()->generate(32);
|
|
|
|
OC::$server->getConfig()->setUserValue($uid, 'login_token', $newToken, time());
|
2014-05-21 16:03:37 +00:00
|
|
|
$this->setMagicInCookie($user->getUID(), $newToken);
|
|
|
|
|
|
|
|
//login
|
|
|
|
$this->setUser($user);
|
|
|
|
$this->manager->emit('\OC\User', 'postRememberedLogin', array($user));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-03 11:33:56 +00:00
|
|
|
/**
|
|
|
|
* logout the user from the session
|
|
|
|
*/
|
2013-05-28 21:46:57 +00:00
|
|
|
public function logout() {
|
|
|
|
$this->manager->emit('\OC\User', 'logout');
|
2016-04-25 14:40:41 +00:00
|
|
|
$user = $this->getUser();
|
|
|
|
if (!is_null($user)) {
|
|
|
|
// TODO: use ISession::getId(), https://github.com/owncloud/core/pull/24229
|
|
|
|
$this->tokenProvider->invalidateToken(session_id());
|
|
|
|
}
|
2013-05-28 21:46:57 +00:00
|
|
|
$this->setUser(null);
|
2014-01-09 09:27:47 +00:00
|
|
|
$this->setLoginName(null);
|
2013-05-28 21:46:57 +00:00
|
|
|
$this->unsetMagicInCookie();
|
2014-10-30 11:10:39 +00:00
|
|
|
$this->session->clear();
|
2013-05-28 21:46:57 +00: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 12:10:55 +00:00
|
|
|
$secureCookie = OC::$server->getRequest()->getServerProtocol() === 'https';
|
|
|
|
$expires = time() + OC::$server->getConfig()->getSystemValue('remember_login_cookie_lifetime', 60 * 60 * 24 * 15);
|
|
|
|
setcookie("oc_username", $username, $expires, OC::$WEBROOT, '', $secureCookie, true);
|
|
|
|
setcookie("oc_token", $token, $expires, OC::$WEBROOT, '', $secureCookie, true);
|
|
|
|
setcookie("oc_remember_login", "1", $expires, OC::$WEBROOT, '', $secureCookie, true);
|
2013-05-28 21:46:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-03 11:33:56 +00:00
|
|
|
* Remove cookie for "remember username"
|
2013-05-28 21:46:57 +00:00
|
|
|
*/
|
|
|
|
public function unsetMagicInCookie() {
|
2015-08-26 12:29:36 +00:00
|
|
|
//TODO: DI for cookies and IRequest
|
2016-04-25 12:10:55 +00:00
|
|
|
$secureCookie = OC::$server->getRequest()->getServerProtocol() === 'https';
|
2015-01-14 10:20:53 +00:00
|
|
|
|
2013-05-28 21:46:57 +00:00
|
|
|
unset($_COOKIE["oc_username"]); //TODO: DI
|
|
|
|
unset($_COOKIE["oc_token"]);
|
|
|
|
unset($_COOKIE["oc_remember_login"]);
|
2016-04-25 12:10:55 +00:00
|
|
|
setcookie('oc_username', '', time() - 3600, OC::$WEBROOT, '', $secureCookie, true);
|
|
|
|
setcookie('oc_token', '', time() - 3600, OC::$WEBROOT, '', $secureCookie, true);
|
|
|
|
setcookie('oc_remember_login', '', time() - 3600, OC::$WEBROOT, '', $secureCookie, true);
|
2013-11-07 17:40:15 +00:00
|
|
|
// old cookies might be stored under /webroot/ instead of /webroot
|
|
|
|
// and Firefox doesn't like it!
|
2016-04-25 12:10:55 +00:00
|
|
|
setcookie('oc_username', '', time() - 3600, OC::$WEBROOT . '/', '', $secureCookie, true);
|
|
|
|
setcookie('oc_token', '', time() - 3600, OC::$WEBROOT . '/', '', $secureCookie, true);
|
|
|
|
setcookie('oc_remember_login', '', time() - 3600, OC::$WEBROOT . '/', '', $secureCookie, true);
|
2013-05-28 21:46:57 +00:00
|
|
|
}
|
2016-04-25 12:10:55 +00:00
|
|
|
|
2013-05-28 21:46:57 +00:00
|
|
|
}
|