Completed
Push — master ( 25f519...aa30c4 )
by Lukas
08:18
created

Session::isReady()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Björn Schießle <[email protected]>
6
 * @author Clark Tomlinson <[email protected]>
7
 * @author Lukas Reschke <[email protected]>
8
 *
9
 * @license AGPL-3.0
10
 *
11
 * This code is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License, version 3,
13
 * as published by the Free Software Foundation.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License, version 3,
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
22
 *
23
 */
24
25
namespace OCA\Encryption;
26
27
use OCA\Encryption\Exceptions\PrivateKeyMissingException;
28
use \OCP\ISession;
29
30
class Session {
31
32
	/** @var ISession */
33
	protected $session;
34
35
	const NOT_INITIALIZED = '0';
36
	const INIT_EXECUTED = '1';
37
	const INIT_SUCCESSFUL = '2';
38
	const RUN_MIGRATION = '3';
39
40
	/**
41
	 * @param ISession $session
42
	 */
43
	public function __construct(ISession $session) {
44
		$this->session = $session;
45
	}
46
47
	/**
48
	 * Sets status of encryption app
49
	 *
50
	 * @param string $status INIT_SUCCESSFUL, INIT_EXECUTED, NOT_INITIALIZED
51
	 */
52
	public function setStatus($status) {
53
		$this->session->set('encryptionInitialized', $status);
54
	}
55
56
	/**
57
	 * Gets status if we already tried to initialize the encryption app
58
	 *
59
	 * @return string init status INIT_SUCCESSFUL, INIT_EXECUTED, NOT_INITIALIZED
60
	 */
61
	public function getStatus() {
62
		$status = $this->session->get('encryptionInitialized');
63
		if (is_null($status)) {
64
			$status = self::NOT_INITIALIZED;
65
		}
66
67
		return $status;
68
	}
69
70
	/**
71
	 * check if encryption was initialized successfully
72
	 *
73
	 * @return bool
74
	 */
75
	public function isReady() {
76
		$status = $this->getStatus();
77
		return $status === self::INIT_SUCCESSFUL;
78
	}
79
80
	/**
81
	 * Gets user or public share private key from session
82
	 *
83
	 * @return string $privateKey The user's plaintext private key
84
	 * @throws Exceptions\PrivateKeyMissingException
85
	 */
86
	public function getPrivateKey() {
87
		$key = $this->session->get('privateKey');
88
		if (is_null($key)) {
89
			throw new Exceptions\PrivateKeyMissingException('please try to log-out and log-in again', 0);
90
		}
91
		return $key;
92
	}
93
94
	/**
95
	 * check if private key is set
96
	 *
97
	 * @return boolean
98
	 */
99
	public function isPrivateKeySet() {
100
		$key = $this->session->get('privateKey');
101
		if (is_null($key)) {
102
			return false;
103
		}
104
105
		return true;
106
	}
107
108
	/**
109
	 * Sets user private key to session
110
	 *
111
	 * @param string $key users private key
112
	 *
113
	 * @note this should only be set on login
114
	 */
115
	public function setPrivateKey($key) {
116
		$this->session->set('privateKey', $key);
117
	}
118
119
	/**
120
	 * store data needed for the decrypt all operation in the session
121
	 *
122
	 * @param string $user
123
	 * @param string $key
124
	 */
125
	public function prepareDecryptAll($user, $key) {
126
		$this->session->set('decryptAll', true);
127
		$this->session->set('decryptAllKey', $key);
128
		$this->session->set('decryptAllUid', $user);
129
	}
130
131
	/**
132
	 * check if we are in decrypt all mode
133
	 *
134
	 * @return bool
135
	 */
136
	public function decryptAllModeActivated() {
137
		$decryptAll = $this->session->get('decryptAll');
138
		return ($decryptAll === true);
139
	}
140
141
	/**
142
	 * get uid used for decrypt all operation
143
	 *
144
	 * @return string
145
	 * @throws \Exception
146
	 */
147
	public function getDecryptAllUid() {
148
		$uid = $this->session->get('decryptAllUid');
149
		if (is_null($uid) && $this->decryptAllModeActivated()) {
150
			throw new \Exception('No uid found while in decrypt all mode');
151
		} elseif (is_null($uid)) {
152
			throw new \Exception('Please activate decrypt all mode first');
153
		}
154
155
		return $uid;
156
	}
157
158
	/**
159
	 * get private key for decrypt all operation
160
	 *
161
	 * @return string
162
	 * @throws PrivateKeyMissingException
163
	 */
164
	public function getDecryptAllKey() {
165
		$privateKey = $this->session->get('decryptAllKey');
166
		if (is_null($privateKey) && $this->decryptAllModeActivated()) {
167
			throw new PrivateKeyMissingException('No private key found while in decrypt all mode');
168
		} elseif (is_null($privateKey)) {
169
			throw new PrivateKeyMissingException('Please activate decrypt all mode first');
170
		}
171
172
		return $privateKey;
173
	}
174
175
	/**
176
	 * remove keys from session
177
	 */
178
	public function clear() {
179
		$this->session->remove('publicSharePrivateKey');
180
		$this->session->remove('privateKey');
181
		$this->session->remove('encryptionInitialized');
182
		$this->session->remove('decryptAll');
183
		$this->session->remove('decryptAllKey');
184
		$this->session->remove('decryptAllUid');
185
	}
186
187
}
188