2015-07-20 13:59:04 +03:00
|
|
|
<?php
|
2018-02-27 00:20:21 +03:00
|
|
|
declare(strict_types=1);
|
2015-07-20 13:59:04 +03:00
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Christoph Wurst <christoph@owncloud.com>
|
2016-07-21 18:07:57 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
* @author Victor Dubiniuk <dubiniuk@owncloud.com>
|
2015-07-20 13:59:04 +03: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/>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Session;
|
|
|
|
|
|
|
|
use OCP\ISession;
|
|
|
|
use OCP\Security\ICrypto;
|
2016-04-26 10:29:15 +03:00
|
|
|
use OCP\Session\Exceptions\SessionNotAvailableException;
|
2015-07-20 13:59:04 +03:00
|
|
|
|
2015-08-21 19:27:52 +03:00
|
|
|
/**
|
|
|
|
* Class CryptoSessionData
|
|
|
|
*
|
|
|
|
* @package OC\Session
|
|
|
|
*/
|
2015-07-20 13:59:04 +03:00
|
|
|
class CryptoSessionData implements \ArrayAccess, ISession {
|
|
|
|
/** @var ISession */
|
|
|
|
protected $session;
|
|
|
|
/** @var \OCP\Security\ICrypto */
|
|
|
|
protected $crypto;
|
|
|
|
/** @var string */
|
|
|
|
protected $passphrase;
|
2015-09-08 22:16:11 +03:00
|
|
|
/** @var array */
|
|
|
|
protected $sessionValues;
|
2015-09-08 23:05:36 +03:00
|
|
|
/** @var bool */
|
|
|
|
protected $isModified = false;
|
2015-09-08 22:16:11 +03:00
|
|
|
CONST encryptedSessionName = 'encrypted_session_data';
|
2015-07-20 13:59:04 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ISession $session
|
|
|
|
* @param ICrypto $crypto
|
|
|
|
* @param string $passphrase
|
|
|
|
*/
|
2015-09-08 22:16:11 +03:00
|
|
|
public function __construct(ISession $session,
|
|
|
|
ICrypto $crypto,
|
2018-02-27 00:20:21 +03:00
|
|
|
string $passphrase) {
|
2015-07-20 13:59:04 +03:00
|
|
|
$this->crypto = $crypto;
|
|
|
|
$this->session = $session;
|
|
|
|
$this->passphrase = $passphrase;
|
2015-09-08 22:16:11 +03:00
|
|
|
$this->initializeSession();
|
|
|
|
}
|
|
|
|
|
2015-09-08 23:05:36 +03:00
|
|
|
/**
|
|
|
|
* Close session if class gets destructed
|
|
|
|
*/
|
|
|
|
public function __destruct() {
|
2017-04-12 21:25:22 +03:00
|
|
|
try {
|
|
|
|
$this->close();
|
|
|
|
} catch (SessionNotAvailableException $e){
|
|
|
|
// This exception can occur if session is already closed
|
|
|
|
// So it is safe to ignore it and let the garbage collector to proceed
|
|
|
|
}
|
2015-09-08 23:05:36 +03:00
|
|
|
}
|
|
|
|
|
2015-09-08 22:16:11 +03:00
|
|
|
protected function initializeSession() {
|
2018-01-12 18:31:48 +03:00
|
|
|
$encryptedSessionData = $this->session->get(self::encryptedSessionName) ?: '';
|
2015-09-08 22:16:11 +03:00
|
|
|
try {
|
|
|
|
$this->sessionValues = json_decode(
|
|
|
|
$this->crypto->decrypt($encryptedSessionData, $this->passphrase),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->sessionValues = [];
|
|
|
|
}
|
2015-07-20 13:59:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a value in the session
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
2018-02-27 00:20:21 +03:00
|
|
|
public function set(string $key, $value) {
|
2015-09-08 22:16:11 +03:00
|
|
|
$this->sessionValues[$key] = $value;
|
2015-09-08 23:05:36 +03:00
|
|
|
$this->isModified = true;
|
2015-07-20 13:59:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a value from the session
|
|
|
|
*
|
|
|
|
* @param string $key
|
2015-08-21 19:27:52 +03:00
|
|
|
* @return string|null Either the value or null
|
2015-07-20 13:59:04 +03:00
|
|
|
*/
|
2018-02-27 00:20:21 +03:00
|
|
|
public function get(string $key) {
|
2015-09-08 22:16:11 +03:00
|
|
|
if(isset($this->sessionValues[$key])) {
|
|
|
|
return $this->sessionValues[$key];
|
2015-08-21 19:27:52 +03:00
|
|
|
}
|
2015-09-08 22:16:11 +03:00
|
|
|
|
|
|
|
return null;
|
2015-07-20 13:59:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a named key exists in the session
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @return bool
|
|
|
|
*/
|
2018-02-27 00:20:21 +03:00
|
|
|
public function exists(string $key): bool {
|
2015-09-08 22:16:11 +03:00
|
|
|
return isset($this->sessionValues[$key]);
|
2015-07-20 13:59:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a $key/$value pair from the session
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
*/
|
2018-02-27 00:20:21 +03:00
|
|
|
public function remove(string $key) {
|
2015-09-08 23:05:36 +03:00
|
|
|
$this->isModified = true;
|
2015-09-08 22:16:11 +03:00
|
|
|
unset($this->sessionValues[$key]);
|
|
|
|
$this->session->remove(self::encryptedSessionName);
|
2015-07-20 13:59:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset and recreate the session
|
|
|
|
*/
|
|
|
|
public function clear() {
|
2017-03-14 00:14:11 +03:00
|
|
|
$requesttoken = $this->get('requesttoken');
|
2015-09-08 22:16:11 +03:00
|
|
|
$this->sessionValues = [];
|
2017-03-14 00:14:11 +03:00
|
|
|
if ($requesttoken !== null) {
|
|
|
|
$this->set('requesttoken', $requesttoken);
|
|
|
|
}
|
2015-09-08 23:05:36 +03:00
|
|
|
$this->isModified = true;
|
2015-07-20 13:59:04 +03:00
|
|
|
$this->session->clear();
|
|
|
|
}
|
|
|
|
|
2016-01-04 17:00:58 +03:00
|
|
|
/**
|
|
|
|
* Wrapper around session_regenerate_id
|
|
|
|
*
|
|
|
|
* @param bool $deleteOldSession Whether to delete the old associated session file or not.
|
2018-06-11 11:45:19 +03:00
|
|
|
* @param bool $updateToken Wheater to update the associated auth token
|
2016-01-04 17:00:58 +03:00
|
|
|
* @return void
|
|
|
|
*/
|
2018-06-11 11:45:19 +03:00
|
|
|
public function regenerateId(bool $deleteOldSession = true, bool $updateToken = false) {
|
|
|
|
$this->session->regenerateId($deleteOldSession, $updateToken);
|
2016-01-04 17:00:58 +03:00
|
|
|
}
|
|
|
|
|
2016-04-25 11:23:06 +03:00
|
|
|
/**
|
|
|
|
* Wrapper around session_id
|
|
|
|
*
|
|
|
|
* @return string
|
2016-04-26 10:29:15 +03:00
|
|
|
* @throws SessionNotAvailableException
|
2016-04-25 11:23:06 +03:00
|
|
|
* @since 9.1.0
|
|
|
|
*/
|
2018-02-27 00:20:21 +03:00
|
|
|
public function getId(): string {
|
2016-04-25 11:23:06 +03:00
|
|
|
return $this->session->getId();
|
|
|
|
}
|
|
|
|
|
2015-07-20 13:59:04 +03:00
|
|
|
/**
|
2015-09-08 23:05:36 +03:00
|
|
|
* Close the session and release the lock, also writes all changed data in batch
|
2015-07-20 13:59:04 +03:00
|
|
|
*/
|
|
|
|
public function close() {
|
2015-09-08 23:05:36 +03:00
|
|
|
if($this->isModified) {
|
|
|
|
$encryptedValue = $this->crypto->encrypt(json_encode($this->sessionValues), $this->passphrase);
|
|
|
|
$this->session->set(self::encryptedSessionName, $encryptedValue);
|
|
|
|
$this->isModified = false;
|
|
|
|
}
|
2015-07-20 13:59:04 +03:00
|
|
|
$this->session->close();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $offset
|
|
|
|
* @return bool
|
|
|
|
*/
|
2018-02-27 00:20:21 +03:00
|
|
|
public function offsetExists($offset): bool {
|
2015-07-20 13:59:04 +03:00
|
|
|
return $this->exists($offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $offset
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function offsetGet($offset) {
|
|
|
|
return $this->get($offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $offset
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
|
|
|
public function offsetSet($offset, $value) {
|
|
|
|
$this->set($offset, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $offset
|
|
|
|
*/
|
|
|
|
public function offsetUnset($offset) {
|
|
|
|
$this->remove($offset);
|
|
|
|
}
|
|
|
|
}
|