2013-05-28 21:46:57 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\User;
|
|
|
|
|
|
|
|
use OC\Hooks\Emitter;
|
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 {
|
2013-05-28 21:46:57 +00:00
|
|
|
/**
|
|
|
|
* @var \OC\User\Manager $manager
|
|
|
|
*/
|
|
|
|
private $manager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \OC\Session\Session $session
|
|
|
|
*/
|
|
|
|
private $session;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \OC\User\User $activeUser
|
|
|
|
*/
|
|
|
|
protected $activeUser;
|
|
|
|
|
|
|
|
/**
|
2014-07-16 17:40:22 +00:00
|
|
|
* @param \OCP\IUserManager $manager
|
|
|
|
* @param \OCP\ISession $session
|
2013-05-28 21:46:57 +00:00
|
|
|
*/
|
2014-07-16 17:40:22 +00:00
|
|
|
public function __construct(\OCP\IUserManager $manager, \OCP\ISession $session) {
|
2013-05-28 21:46:57 +00:00
|
|
|
$this->manager = $manager;
|
|
|
|
$this->session = $session;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $scope
|
|
|
|
* @param string $method
|
|
|
|
* @param callable $callback
|
|
|
|
*/
|
|
|
|
public function listen($scope, $method, $callback) {
|
|
|
|
$this->manager->listen($scope, $method, $callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $scope optional
|
|
|
|
* @param string $method optional
|
|
|
|
* @param callable $callback optional
|
|
|
|
*/
|
|
|
|
public function removeListener($scope = null, $method = null, $callback = null) {
|
|
|
|
$this->manager->removeListener($scope, $method, $callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-03 11:33:56 +00:00
|
|
|
* get the manager object
|
|
|
|
*
|
2013-05-28 21:46:57 +00:00
|
|
|
* @return \OC\User\Manager
|
|
|
|
*/
|
|
|
|
public function getManager() {
|
|
|
|
return $this->manager;
|
|
|
|
}
|
|
|
|
|
2014-07-16 17:40:22 +00:00
|
|
|
/**
|
|
|
|
* get the session object
|
|
|
|
*
|
|
|
|
* @return \OCP\ISession
|
|
|
|
*/
|
|
|
|
public function getSession() {
|
|
|
|
return $this->session;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set the session object
|
|
|
|
*
|
|
|
|
* @param \OCP\ISession $session
|
|
|
|
*/
|
|
|
|
public function setSession(\OCP\ISession $session) {
|
|
|
|
if ($this->session instanceof \OCP\ISession) {
|
|
|
|
$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
|
|
|
|
*
|
2014-05-12 22:27:36 +00:00
|
|
|
* @param \OC\User\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
|
|
|
|
*
|
2014-12-19 13:14:38 +00:00
|
|
|
* @return \OCP\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
|
|
|
|
if (\OC_User::isIncognitoMode()) {
|
|
|
|
return null;
|
|
|
|
}
|
2013-05-28 21:46:57 +00:00
|
|
|
if ($this->activeUser) {
|
|
|
|
return $this->activeUser;
|
|
|
|
} else {
|
|
|
|
$uid = $this->session->get('user_id');
|
2014-08-04 12:47:14 +00:00
|
|
|
if ($uid !== null) {
|
2013-05-28 21:46:57 +00:00
|
|
|
$this->activeUser = $this->manager->get($uid);
|
|
|
|
return $this->activeUser;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
return $this->getUser() !== null;
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
$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 {
|
|
|
|
throw new LoginException('Login canceled by app');
|
|
|
|
}
|
2013-09-16 12:15:35 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2013-05-28 21:46:57 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
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
|
2014-12-04 15:48:07 +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
|
2014-12-04 15:48:07 +00:00
|
|
|
\OC::$server->getConfig()->deleteUserValue($uid, 'login_token', $currentToken);
|
2014-09-03 15:46:48 +00:00
|
|
|
$newToken = \OC::$server->getSecureRandom()->getMediumStrengthGenerator()->generate(32);
|
2014-12-04 15:48:07 +00:00
|
|
|
\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');
|
|
|
|
$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) {
|
|
|
|
$secure_cookie = \OC_Config::getValue("forcessl", false); //TODO: DI for cookies and OC_Config
|
|
|
|
$expires = time() + \OC_Config::getValue('remember_login_cookie_lifetime', 60 * 60 * 24 * 15);
|
2013-05-31 22:35:16 +00:00
|
|
|
setcookie("oc_username", $username, $expires, \OC::$WEBROOT, '', $secure_cookie);
|
|
|
|
setcookie("oc_token", $token, $expires, \OC::$WEBROOT, '', $secure_cookie, true);
|
2014-05-12 22:27:36 +00:00
|
|
|
setcookie("oc_remember_login", "1", $expires, \OC::$WEBROOT, '', $secure_cookie);
|
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() {
|
|
|
|
unset($_COOKIE["oc_username"]); //TODO: DI
|
|
|
|
unset($_COOKIE["oc_token"]);
|
|
|
|
unset($_COOKIE["oc_remember_login"]);
|
2014-10-13 11:11:48 +00:00
|
|
|
setcookie('oc_username', '', time() - 3600, \OC::$WEBROOT);
|
|
|
|
setcookie('oc_token', '', time() - 3600, \OC::$WEBROOT);
|
|
|
|
setcookie('oc_remember_login', '', time() - 3600, \OC::$WEBROOT);
|
2013-11-07 17:40:15 +00:00
|
|
|
// old cookies might be stored under /webroot/ instead of /webroot
|
|
|
|
// and Firefox doesn't like it!
|
2014-10-13 11:11:48 +00:00
|
|
|
setcookie('oc_username', '', time() - 3600, \OC::$WEBROOT . '/');
|
|
|
|
setcookie('oc_token', '', time() - 3600, \OC::$WEBROOT . '/');
|
|
|
|
setcookie('oc_remember_login', '', time() - 3600, \OC::$WEBROOT . '/');
|
2013-05-28 21:46:57 +00:00
|
|
|
}
|
|
|
|
}
|