Welcome to mirror list, hosted at ThFree Co, Russian Federation.

cryptowrapper.php « session « private « lib - github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8bc41ac12422f877fc6cd9c5f959faed1e4becf8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
/**
 * @author Joas Schilling <nickvergessen@owncloud.com>
 *
 * @copyright Copyright (c) 2015, ownCloud, Inc.
 * @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\IConfig;
use OCP\ISession;
use OCP\Security\ICrypto;
use OCP\Security\ISecureRandom;

class CryptoWrapper {
	const COOKIE_NAME = 'oc_sessionPassphrase';

	/** @var ISession */
	protected $session;

	/** @var \OCP\Security\ICrypto */
	protected $crypto;

	/** @var ISecureRandom */
	protected $random;

	/**
	 * @param IConfig $config
	 * @param ICrypto $crypto
	 * @param ISecureRandom $random
	 */
	public function __construct(IConfig $config, ICrypto $crypto, ISecureRandom $random) {
		$this->crypto = $crypto;
		$this->config = $config;
		$this->random = $random;

		if (isset($_COOKIE[self::COOKIE_NAME])) {
			// TODO circular dependency
//			$request = \OC::$server->getRequest();
//			$this->passphrase = $request->getCookie(self::COOKIE_NAME);
			$this->passphrase = $_COOKIE[self::COOKIE_NAME];
		} else {
			$this->passphrase = $this->random->getMediumStrengthGenerator()->generate(128);

			// TODO circular dependency
			// $secureCookie = \OC::$server->getRequest()->getServerProtocol() === 'https';
			$secureCookie = false;
			$expires = time() + $this->config->getSystemValue('remember_login_cookie_lifetime', 60 * 60 * 24 * 15);

			if (!defined('PHPUNIT_RUN')) {
				setcookie(self::COOKIE_NAME, $this->passphrase, $expires, \OC::$WEBROOT, '', $secureCookie);
			}
		}
	}

	/**
	 * @param ISession $session
	 * @return ISession
	 */
	public function wrapSession(ISession $session) {
		if (!($session instanceof CryptoSessionData) &&  $this->config->getSystemValue('encrypt.session', false)) {
			return new \OC\Session\CryptoSessionData($session, $this->crypto, $this->passphrase);
		}

		return $session;
	}
}