@@ -55,7 +55,6 @@ |
||
55 | 55 | |
56 | 56 | /** |
57 | 57 | * @param array $urlParams |
58 | - * @param bool $encryptionSystemReady |
|
59 | 58 | */ |
60 | 59 | public function __construct($urlParams = array()) { |
61 | 60 | parent::__construct('encryption', $urlParams); |
@@ -48,220 +48,220 @@ |
||
48 | 48 | |
49 | 49 | class Application extends \OCP\AppFramework\App { |
50 | 50 | |
51 | - /** @var IManager */ |
|
52 | - private $encryptionManager; |
|
53 | - /** @var IConfig */ |
|
54 | - private $config; |
|
55 | - |
|
56 | - /** |
|
57 | - * @param array $urlParams |
|
58 | - * @param bool $encryptionSystemReady |
|
59 | - */ |
|
60 | - public function __construct($urlParams = array()) { |
|
61 | - parent::__construct('encryption', $urlParams); |
|
62 | - $this->encryptionManager = \OC::$server->getEncryptionManager(); |
|
63 | - $this->config = \OC::$server->getConfig(); |
|
64 | - $this->registerServices(); |
|
65 | - } |
|
66 | - |
|
67 | - public function setUp() { |
|
68 | - if ($this->encryptionManager->isEnabled()) { |
|
69 | - /** @var Setup $setup */ |
|
70 | - $setup = $this->getContainer()->query('UserSetup'); |
|
71 | - $setup->setupSystem(); |
|
72 | - } |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * register hooks |
|
77 | - */ |
|
78 | - public function registerHooks() { |
|
79 | - if (!$this->config->getSystemValue('maintenance', false)) { |
|
80 | - |
|
81 | - $container = $this->getContainer(); |
|
82 | - $server = $container->getServer(); |
|
83 | - // Register our hooks and fire them. |
|
84 | - $hookManager = new HookManager(); |
|
85 | - |
|
86 | - $hookManager->registerHook([ |
|
87 | - new UserHooks($container->query('KeyManager'), |
|
88 | - $server->getUserManager(), |
|
89 | - $server->getLogger(), |
|
90 | - $container->query('UserSetup'), |
|
91 | - $server->getUserSession(), |
|
92 | - $container->query('Util'), |
|
93 | - $container->query('Session'), |
|
94 | - $container->query('Crypt'), |
|
95 | - $container->query('Recovery')) |
|
96 | - ]); |
|
97 | - |
|
98 | - $hookManager->fireHooks(); |
|
99 | - |
|
100 | - } else { |
|
101 | - // Logout user if we are in maintenance to force re-login |
|
102 | - $this->getContainer()->getServer()->getUserSession()->logout(); |
|
103 | - } |
|
104 | - } |
|
105 | - |
|
106 | - public function registerEncryptionModule() { |
|
107 | - $container = $this->getContainer(); |
|
108 | - |
|
109 | - |
|
110 | - $this->encryptionManager->registerEncryptionModule( |
|
111 | - Encryption::ID, |
|
112 | - Encryption::DISPLAY_NAME, |
|
113 | - function() use ($container) { |
|
114 | - |
|
115 | - return new Encryption( |
|
116 | - $container->query('Crypt'), |
|
117 | - $container->query('KeyManager'), |
|
118 | - $container->query('Util'), |
|
119 | - $container->query('Session'), |
|
120 | - $container->query('EncryptAll'), |
|
121 | - $container->query('DecryptAll'), |
|
122 | - $container->getServer()->getLogger(), |
|
123 | - $container->getServer()->getL10N($container->getAppName()) |
|
124 | - ); |
|
125 | - }); |
|
126 | - |
|
127 | - } |
|
128 | - |
|
129 | - public function registerServices() { |
|
130 | - $container = $this->getContainer(); |
|
131 | - |
|
132 | - $container->registerService('Crypt', |
|
133 | - function (IAppContainer $c) { |
|
134 | - $server = $c->getServer(); |
|
135 | - return new Crypt($server->getLogger(), |
|
136 | - $server->getUserSession(), |
|
137 | - $server->getConfig(), |
|
138 | - $server->getL10N($c->getAppName())); |
|
139 | - }); |
|
140 | - |
|
141 | - $container->registerService('Session', |
|
142 | - function (IAppContainer $c) { |
|
143 | - $server = $c->getServer(); |
|
144 | - return new Session($server->getSession()); |
|
145 | - } |
|
146 | - ); |
|
147 | - |
|
148 | - $container->registerService('KeyManager', |
|
149 | - function (IAppContainer $c) { |
|
150 | - $server = $c->getServer(); |
|
151 | - |
|
152 | - return new KeyManager($server->getEncryptionKeyStorage(), |
|
153 | - $c->query('Crypt'), |
|
154 | - $server->getConfig(), |
|
155 | - $server->getUserSession(), |
|
156 | - new Session($server->getSession()), |
|
157 | - $server->getLogger(), |
|
158 | - $c->query('Util') |
|
159 | - ); |
|
160 | - }); |
|
161 | - |
|
162 | - $container->registerService('Recovery', |
|
163 | - function (IAppContainer $c) { |
|
164 | - $server = $c->getServer(); |
|
165 | - |
|
166 | - return new Recovery( |
|
167 | - $server->getUserSession(), |
|
168 | - $c->query('Crypt'), |
|
169 | - $server->getSecureRandom(), |
|
170 | - $c->query('KeyManager'), |
|
171 | - $server->getConfig(), |
|
172 | - $server->getEncryptionKeyStorage(), |
|
173 | - $server->getEncryptionFilesHelper(), |
|
174 | - new View()); |
|
175 | - }); |
|
176 | - |
|
177 | - $container->registerService('RecoveryController', function (IAppContainer $c) { |
|
178 | - $server = $c->getServer(); |
|
179 | - return new RecoveryController( |
|
180 | - $c->getAppName(), |
|
181 | - $server->getRequest(), |
|
182 | - $server->getConfig(), |
|
183 | - $server->getL10N($c->getAppName()), |
|
184 | - $c->query('Recovery')); |
|
185 | - }); |
|
186 | - |
|
187 | - $container->registerService('StatusController', function (IAppContainer $c) { |
|
188 | - $server = $c->getServer(); |
|
189 | - return new StatusController( |
|
190 | - $c->getAppName(), |
|
191 | - $server->getRequest(), |
|
192 | - $server->getL10N($c->getAppName()), |
|
193 | - $c->query('Session'), |
|
194 | - $server->getEncryptionManager() |
|
195 | - ); |
|
196 | - }); |
|
197 | - |
|
198 | - $container->registerService('SettingsController', function (IAppContainer $c) { |
|
199 | - $server = $c->getServer(); |
|
200 | - return new SettingsController( |
|
201 | - $c->getAppName(), |
|
202 | - $server->getRequest(), |
|
203 | - $server->getL10N($c->getAppName()), |
|
204 | - $server->getUserManager(), |
|
205 | - $server->getUserSession(), |
|
206 | - $c->query('KeyManager'), |
|
207 | - $c->query('Crypt'), |
|
208 | - $c->query('Session'), |
|
209 | - $server->getSession(), |
|
210 | - $c->query('Util') |
|
211 | - ); |
|
212 | - }); |
|
213 | - |
|
214 | - $container->registerService('UserSetup', |
|
215 | - function (IAppContainer $c) { |
|
216 | - $server = $c->getServer(); |
|
217 | - return new Setup($server->getLogger(), |
|
218 | - $server->getUserSession(), |
|
219 | - $c->query('Crypt'), |
|
220 | - $c->query('KeyManager')); |
|
221 | - }); |
|
222 | - |
|
223 | - $container->registerService('Util', |
|
224 | - function (IAppContainer $c) { |
|
225 | - $server = $c->getServer(); |
|
226 | - |
|
227 | - return new Util( |
|
228 | - new View(), |
|
229 | - $c->query('Crypt'), |
|
230 | - $server->getLogger(), |
|
231 | - $server->getUserSession(), |
|
232 | - $server->getConfig(), |
|
233 | - $server->getUserManager()); |
|
234 | - }); |
|
235 | - |
|
236 | - $container->registerService('EncryptAll', |
|
237 | - function (IAppContainer $c) { |
|
238 | - $server = $c->getServer(); |
|
239 | - return new EncryptAll( |
|
240 | - $c->query('UserSetup'), |
|
241 | - $c->getServer()->getUserManager(), |
|
242 | - new View(), |
|
243 | - $c->query('KeyManager'), |
|
244 | - $c->query('Util'), |
|
245 | - $server->getConfig(), |
|
246 | - $server->getMailer(), |
|
247 | - $server->getL10N('encryption'), |
|
248 | - new QuestionHelper(), |
|
249 | - $server->getSecureRandom() |
|
250 | - ); |
|
251 | - } |
|
252 | - ); |
|
253 | - |
|
254 | - $container->registerService('DecryptAll', |
|
255 | - function (IAppContainer $c) { |
|
256 | - return new DecryptAll( |
|
257 | - $c->query('Util'), |
|
258 | - $c->query('KeyManager'), |
|
259 | - $c->query('Crypt'), |
|
260 | - $c->query('Session'), |
|
261 | - new QuestionHelper() |
|
262 | - ); |
|
263 | - } |
|
264 | - ); |
|
265 | - |
|
266 | - } |
|
51 | + /** @var IManager */ |
|
52 | + private $encryptionManager; |
|
53 | + /** @var IConfig */ |
|
54 | + private $config; |
|
55 | + |
|
56 | + /** |
|
57 | + * @param array $urlParams |
|
58 | + * @param bool $encryptionSystemReady |
|
59 | + */ |
|
60 | + public function __construct($urlParams = array()) { |
|
61 | + parent::__construct('encryption', $urlParams); |
|
62 | + $this->encryptionManager = \OC::$server->getEncryptionManager(); |
|
63 | + $this->config = \OC::$server->getConfig(); |
|
64 | + $this->registerServices(); |
|
65 | + } |
|
66 | + |
|
67 | + public function setUp() { |
|
68 | + if ($this->encryptionManager->isEnabled()) { |
|
69 | + /** @var Setup $setup */ |
|
70 | + $setup = $this->getContainer()->query('UserSetup'); |
|
71 | + $setup->setupSystem(); |
|
72 | + } |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * register hooks |
|
77 | + */ |
|
78 | + public function registerHooks() { |
|
79 | + if (!$this->config->getSystemValue('maintenance', false)) { |
|
80 | + |
|
81 | + $container = $this->getContainer(); |
|
82 | + $server = $container->getServer(); |
|
83 | + // Register our hooks and fire them. |
|
84 | + $hookManager = new HookManager(); |
|
85 | + |
|
86 | + $hookManager->registerHook([ |
|
87 | + new UserHooks($container->query('KeyManager'), |
|
88 | + $server->getUserManager(), |
|
89 | + $server->getLogger(), |
|
90 | + $container->query('UserSetup'), |
|
91 | + $server->getUserSession(), |
|
92 | + $container->query('Util'), |
|
93 | + $container->query('Session'), |
|
94 | + $container->query('Crypt'), |
|
95 | + $container->query('Recovery')) |
|
96 | + ]); |
|
97 | + |
|
98 | + $hookManager->fireHooks(); |
|
99 | + |
|
100 | + } else { |
|
101 | + // Logout user if we are in maintenance to force re-login |
|
102 | + $this->getContainer()->getServer()->getUserSession()->logout(); |
|
103 | + } |
|
104 | + } |
|
105 | + |
|
106 | + public function registerEncryptionModule() { |
|
107 | + $container = $this->getContainer(); |
|
108 | + |
|
109 | + |
|
110 | + $this->encryptionManager->registerEncryptionModule( |
|
111 | + Encryption::ID, |
|
112 | + Encryption::DISPLAY_NAME, |
|
113 | + function() use ($container) { |
|
114 | + |
|
115 | + return new Encryption( |
|
116 | + $container->query('Crypt'), |
|
117 | + $container->query('KeyManager'), |
|
118 | + $container->query('Util'), |
|
119 | + $container->query('Session'), |
|
120 | + $container->query('EncryptAll'), |
|
121 | + $container->query('DecryptAll'), |
|
122 | + $container->getServer()->getLogger(), |
|
123 | + $container->getServer()->getL10N($container->getAppName()) |
|
124 | + ); |
|
125 | + }); |
|
126 | + |
|
127 | + } |
|
128 | + |
|
129 | + public function registerServices() { |
|
130 | + $container = $this->getContainer(); |
|
131 | + |
|
132 | + $container->registerService('Crypt', |
|
133 | + function (IAppContainer $c) { |
|
134 | + $server = $c->getServer(); |
|
135 | + return new Crypt($server->getLogger(), |
|
136 | + $server->getUserSession(), |
|
137 | + $server->getConfig(), |
|
138 | + $server->getL10N($c->getAppName())); |
|
139 | + }); |
|
140 | + |
|
141 | + $container->registerService('Session', |
|
142 | + function (IAppContainer $c) { |
|
143 | + $server = $c->getServer(); |
|
144 | + return new Session($server->getSession()); |
|
145 | + } |
|
146 | + ); |
|
147 | + |
|
148 | + $container->registerService('KeyManager', |
|
149 | + function (IAppContainer $c) { |
|
150 | + $server = $c->getServer(); |
|
151 | + |
|
152 | + return new KeyManager($server->getEncryptionKeyStorage(), |
|
153 | + $c->query('Crypt'), |
|
154 | + $server->getConfig(), |
|
155 | + $server->getUserSession(), |
|
156 | + new Session($server->getSession()), |
|
157 | + $server->getLogger(), |
|
158 | + $c->query('Util') |
|
159 | + ); |
|
160 | + }); |
|
161 | + |
|
162 | + $container->registerService('Recovery', |
|
163 | + function (IAppContainer $c) { |
|
164 | + $server = $c->getServer(); |
|
165 | + |
|
166 | + return new Recovery( |
|
167 | + $server->getUserSession(), |
|
168 | + $c->query('Crypt'), |
|
169 | + $server->getSecureRandom(), |
|
170 | + $c->query('KeyManager'), |
|
171 | + $server->getConfig(), |
|
172 | + $server->getEncryptionKeyStorage(), |
|
173 | + $server->getEncryptionFilesHelper(), |
|
174 | + new View()); |
|
175 | + }); |
|
176 | + |
|
177 | + $container->registerService('RecoveryController', function (IAppContainer $c) { |
|
178 | + $server = $c->getServer(); |
|
179 | + return new RecoveryController( |
|
180 | + $c->getAppName(), |
|
181 | + $server->getRequest(), |
|
182 | + $server->getConfig(), |
|
183 | + $server->getL10N($c->getAppName()), |
|
184 | + $c->query('Recovery')); |
|
185 | + }); |
|
186 | + |
|
187 | + $container->registerService('StatusController', function (IAppContainer $c) { |
|
188 | + $server = $c->getServer(); |
|
189 | + return new StatusController( |
|
190 | + $c->getAppName(), |
|
191 | + $server->getRequest(), |
|
192 | + $server->getL10N($c->getAppName()), |
|
193 | + $c->query('Session'), |
|
194 | + $server->getEncryptionManager() |
|
195 | + ); |
|
196 | + }); |
|
197 | + |
|
198 | + $container->registerService('SettingsController', function (IAppContainer $c) { |
|
199 | + $server = $c->getServer(); |
|
200 | + return new SettingsController( |
|
201 | + $c->getAppName(), |
|
202 | + $server->getRequest(), |
|
203 | + $server->getL10N($c->getAppName()), |
|
204 | + $server->getUserManager(), |
|
205 | + $server->getUserSession(), |
|
206 | + $c->query('KeyManager'), |
|
207 | + $c->query('Crypt'), |
|
208 | + $c->query('Session'), |
|
209 | + $server->getSession(), |
|
210 | + $c->query('Util') |
|
211 | + ); |
|
212 | + }); |
|
213 | + |
|
214 | + $container->registerService('UserSetup', |
|
215 | + function (IAppContainer $c) { |
|
216 | + $server = $c->getServer(); |
|
217 | + return new Setup($server->getLogger(), |
|
218 | + $server->getUserSession(), |
|
219 | + $c->query('Crypt'), |
|
220 | + $c->query('KeyManager')); |
|
221 | + }); |
|
222 | + |
|
223 | + $container->registerService('Util', |
|
224 | + function (IAppContainer $c) { |
|
225 | + $server = $c->getServer(); |
|
226 | + |
|
227 | + return new Util( |
|
228 | + new View(), |
|
229 | + $c->query('Crypt'), |
|
230 | + $server->getLogger(), |
|
231 | + $server->getUserSession(), |
|
232 | + $server->getConfig(), |
|
233 | + $server->getUserManager()); |
|
234 | + }); |
|
235 | + |
|
236 | + $container->registerService('EncryptAll', |
|
237 | + function (IAppContainer $c) { |
|
238 | + $server = $c->getServer(); |
|
239 | + return new EncryptAll( |
|
240 | + $c->query('UserSetup'), |
|
241 | + $c->getServer()->getUserManager(), |
|
242 | + new View(), |
|
243 | + $c->query('KeyManager'), |
|
244 | + $c->query('Util'), |
|
245 | + $server->getConfig(), |
|
246 | + $server->getMailer(), |
|
247 | + $server->getL10N('encryption'), |
|
248 | + new QuestionHelper(), |
|
249 | + $server->getSecureRandom() |
|
250 | + ); |
|
251 | + } |
|
252 | + ); |
|
253 | + |
|
254 | + $container->registerService('DecryptAll', |
|
255 | + function (IAppContainer $c) { |
|
256 | + return new DecryptAll( |
|
257 | + $c->query('Util'), |
|
258 | + $c->query('KeyManager'), |
|
259 | + $c->query('Crypt'), |
|
260 | + $c->query('Session'), |
|
261 | + new QuestionHelper() |
|
262 | + ); |
|
263 | + } |
|
264 | + ); |
|
265 | + |
|
266 | + } |
|
267 | 267 | } |
@@ -130,7 +130,7 @@ discard block |
||
130 | 130 | $container = $this->getContainer(); |
131 | 131 | |
132 | 132 | $container->registerService('Crypt', |
133 | - function (IAppContainer $c) { |
|
133 | + function(IAppContainer $c) { |
|
134 | 134 | $server = $c->getServer(); |
135 | 135 | return new Crypt($server->getLogger(), |
136 | 136 | $server->getUserSession(), |
@@ -139,14 +139,14 @@ discard block |
||
139 | 139 | }); |
140 | 140 | |
141 | 141 | $container->registerService('Session', |
142 | - function (IAppContainer $c) { |
|
142 | + function(IAppContainer $c) { |
|
143 | 143 | $server = $c->getServer(); |
144 | 144 | return new Session($server->getSession()); |
145 | 145 | } |
146 | 146 | ); |
147 | 147 | |
148 | 148 | $container->registerService('KeyManager', |
149 | - function (IAppContainer $c) { |
|
149 | + function(IAppContainer $c) { |
|
150 | 150 | $server = $c->getServer(); |
151 | 151 | |
152 | 152 | return new KeyManager($server->getEncryptionKeyStorage(), |
@@ -160,7 +160,7 @@ discard block |
||
160 | 160 | }); |
161 | 161 | |
162 | 162 | $container->registerService('Recovery', |
163 | - function (IAppContainer $c) { |
|
163 | + function(IAppContainer $c) { |
|
164 | 164 | $server = $c->getServer(); |
165 | 165 | |
166 | 166 | return new Recovery( |
@@ -174,7 +174,7 @@ discard block |
||
174 | 174 | new View()); |
175 | 175 | }); |
176 | 176 | |
177 | - $container->registerService('RecoveryController', function (IAppContainer $c) { |
|
177 | + $container->registerService('RecoveryController', function(IAppContainer $c) { |
|
178 | 178 | $server = $c->getServer(); |
179 | 179 | return new RecoveryController( |
180 | 180 | $c->getAppName(), |
@@ -184,7 +184,7 @@ discard block |
||
184 | 184 | $c->query('Recovery')); |
185 | 185 | }); |
186 | 186 | |
187 | - $container->registerService('StatusController', function (IAppContainer $c) { |
|
187 | + $container->registerService('StatusController', function(IAppContainer $c) { |
|
188 | 188 | $server = $c->getServer(); |
189 | 189 | return new StatusController( |
190 | 190 | $c->getAppName(), |
@@ -195,7 +195,7 @@ discard block |
||
195 | 195 | ); |
196 | 196 | }); |
197 | 197 | |
198 | - $container->registerService('SettingsController', function (IAppContainer $c) { |
|
198 | + $container->registerService('SettingsController', function(IAppContainer $c) { |
|
199 | 199 | $server = $c->getServer(); |
200 | 200 | return new SettingsController( |
201 | 201 | $c->getAppName(), |
@@ -212,7 +212,7 @@ discard block |
||
212 | 212 | }); |
213 | 213 | |
214 | 214 | $container->registerService('UserSetup', |
215 | - function (IAppContainer $c) { |
|
215 | + function(IAppContainer $c) { |
|
216 | 216 | $server = $c->getServer(); |
217 | 217 | return new Setup($server->getLogger(), |
218 | 218 | $server->getUserSession(), |
@@ -221,7 +221,7 @@ discard block |
||
221 | 221 | }); |
222 | 222 | |
223 | 223 | $container->registerService('Util', |
224 | - function (IAppContainer $c) { |
|
224 | + function(IAppContainer $c) { |
|
225 | 225 | $server = $c->getServer(); |
226 | 226 | |
227 | 227 | return new Util( |
@@ -234,7 +234,7 @@ discard block |
||
234 | 234 | }); |
235 | 235 | |
236 | 236 | $container->registerService('EncryptAll', |
237 | - function (IAppContainer $c) { |
|
237 | + function(IAppContainer $c) { |
|
238 | 238 | $server = $c->getServer(); |
239 | 239 | return new EncryptAll( |
240 | 240 | $c->query('UserSetup'), |
@@ -252,7 +252,7 @@ discard block |
||
252 | 252 | ); |
253 | 253 | |
254 | 254 | $container->registerService('DecryptAll', |
255 | - function (IAppContainer $c) { |
|
255 | + function(IAppContainer $c) { |
|
256 | 256 | return new DecryptAll( |
257 | 257 | $c->query('Util'), |
258 | 258 | $c->query('KeyManager'), |
@@ -30,7 +30,7 @@ |
||
30 | 30 | |
31 | 31 | $app = new Application(); |
32 | 32 | if ($encryptionSystemReady) { |
33 | - $app->registerEncryptionModule(); |
|
34 | - $app->registerHooks(); |
|
35 | - $app->setUp(); |
|
33 | + $app->registerEncryptionModule(); |
|
34 | + $app->registerHooks(); |
|
35 | + $app->setUp(); |
|
36 | 36 | } |
@@ -30,158 +30,158 @@ |
||
30 | 30 | |
31 | 31 | class Session { |
32 | 32 | |
33 | - /** @var ISession */ |
|
34 | - protected $session; |
|
35 | - |
|
36 | - const NOT_INITIALIZED = '0'; |
|
37 | - const INIT_EXECUTED = '1'; |
|
38 | - const INIT_SUCCESSFUL = '2'; |
|
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 | - } |
|
33 | + /** @var ISession */ |
|
34 | + protected $session; |
|
35 | + |
|
36 | + const NOT_INITIALIZED = '0'; |
|
37 | + const INIT_EXECUTED = '1'; |
|
38 | + const INIT_SUCCESSFUL = '2'; |
|
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 | 186 | |
187 | 187 | } |
@@ -36,73 +36,73 @@ |
||
36 | 36 | |
37 | 37 | class StatusController extends Controller { |
38 | 38 | |
39 | - /** @var IL10N */ |
|
40 | - private $l; |
|
39 | + /** @var IL10N */ |
|
40 | + private $l; |
|
41 | 41 | |
42 | - /** @var Session */ |
|
43 | - private $session; |
|
42 | + /** @var Session */ |
|
43 | + private $session; |
|
44 | 44 | |
45 | - /** @var IManager */ |
|
46 | - private $encryptionManager; |
|
45 | + /** @var IManager */ |
|
46 | + private $encryptionManager; |
|
47 | 47 | |
48 | - /** |
|
49 | - * @param string $AppName |
|
50 | - * @param IRequest $request |
|
51 | - * @param IL10N $l10n |
|
52 | - * @param Session $session |
|
53 | - * @param IManager $encryptionManager |
|
54 | - */ |
|
55 | - public function __construct($AppName, |
|
56 | - IRequest $request, |
|
57 | - IL10N $l10n, |
|
58 | - Session $session, |
|
59 | - IManager $encryptionManager |
|
60 | - ) { |
|
61 | - parent::__construct($AppName, $request); |
|
62 | - $this->l = $l10n; |
|
63 | - $this->session = $session; |
|
64 | - $this->encryptionManager = $encryptionManager; |
|
65 | - } |
|
48 | + /** |
|
49 | + * @param string $AppName |
|
50 | + * @param IRequest $request |
|
51 | + * @param IL10N $l10n |
|
52 | + * @param Session $session |
|
53 | + * @param IManager $encryptionManager |
|
54 | + */ |
|
55 | + public function __construct($AppName, |
|
56 | + IRequest $request, |
|
57 | + IL10N $l10n, |
|
58 | + Session $session, |
|
59 | + IManager $encryptionManager |
|
60 | + ) { |
|
61 | + parent::__construct($AppName, $request); |
|
62 | + $this->l = $l10n; |
|
63 | + $this->session = $session; |
|
64 | + $this->encryptionManager = $encryptionManager; |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * @NoAdminRequired |
|
69 | - * @return DataResponse |
|
70 | - */ |
|
71 | - public function getStatus() { |
|
67 | + /** |
|
68 | + * @NoAdminRequired |
|
69 | + * @return DataResponse |
|
70 | + */ |
|
71 | + public function getStatus() { |
|
72 | 72 | |
73 | - $status = 'error'; |
|
74 | - $message = 'no valid init status'; |
|
75 | - switch( $this->session->getStatus()) { |
|
76 | - case Session::INIT_EXECUTED: |
|
77 | - $status = 'interactionNeeded'; |
|
78 | - $message = (string)$this->l->t( |
|
79 | - 'Invalid private key for encryption app. Please update your private key password in your personal settings to recover access to your encrypted files.' |
|
80 | - ); |
|
81 | - break; |
|
82 | - case Session::NOT_INITIALIZED: |
|
83 | - $status = 'interactionNeeded'; |
|
84 | - if ($this->encryptionManager->isEnabled()) { |
|
85 | - $message = (string)$this->l->t( |
|
86 | - 'Encryption App is enabled, but your keys are not initialized. Please log-out and log-in again.' |
|
87 | - ); |
|
88 | - } else { |
|
89 | - $message = (string)$this->l->t( |
|
90 | - 'Please enable server side encryption in the admin settings in order to use the encryption module.' |
|
91 | - ); |
|
92 | - } |
|
93 | - break; |
|
94 | - case Session::INIT_SUCCESSFUL: |
|
95 | - $status = 'success'; |
|
96 | - $message = (string)$this->l->t('Encryption app is enabled and ready'); |
|
97 | - } |
|
73 | + $status = 'error'; |
|
74 | + $message = 'no valid init status'; |
|
75 | + switch( $this->session->getStatus()) { |
|
76 | + case Session::INIT_EXECUTED: |
|
77 | + $status = 'interactionNeeded'; |
|
78 | + $message = (string)$this->l->t( |
|
79 | + 'Invalid private key for encryption app. Please update your private key password in your personal settings to recover access to your encrypted files.' |
|
80 | + ); |
|
81 | + break; |
|
82 | + case Session::NOT_INITIALIZED: |
|
83 | + $status = 'interactionNeeded'; |
|
84 | + if ($this->encryptionManager->isEnabled()) { |
|
85 | + $message = (string)$this->l->t( |
|
86 | + 'Encryption App is enabled, but your keys are not initialized. Please log-out and log-in again.' |
|
87 | + ); |
|
88 | + } else { |
|
89 | + $message = (string)$this->l->t( |
|
90 | + 'Please enable server side encryption in the admin settings in order to use the encryption module.' |
|
91 | + ); |
|
92 | + } |
|
93 | + break; |
|
94 | + case Session::INIT_SUCCESSFUL: |
|
95 | + $status = 'success'; |
|
96 | + $message = (string)$this->l->t('Encryption app is enabled and ready'); |
|
97 | + } |
|
98 | 98 | |
99 | - return new DataResponse( |
|
100 | - [ |
|
101 | - 'status' => $status, |
|
102 | - 'data' => [ |
|
103 | - 'message' => $message] |
|
104 | - ] |
|
105 | - ); |
|
106 | - } |
|
99 | + return new DataResponse( |
|
100 | + [ |
|
101 | + 'status' => $status, |
|
102 | + 'data' => [ |
|
103 | + 'message' => $message] |
|
104 | + ] |
|
105 | + ); |
|
106 | + } |
|
107 | 107 | |
108 | 108 | } |
@@ -72,28 +72,28 @@ |
||
72 | 72 | |
73 | 73 | $status = 'error'; |
74 | 74 | $message = 'no valid init status'; |
75 | - switch( $this->session->getStatus()) { |
|
75 | + switch ($this->session->getStatus()) { |
|
76 | 76 | case Session::INIT_EXECUTED: |
77 | 77 | $status = 'interactionNeeded'; |
78 | - $message = (string)$this->l->t( |
|
78 | + $message = (string) $this->l->t( |
|
79 | 79 | 'Invalid private key for encryption app. Please update your private key password in your personal settings to recover access to your encrypted files.' |
80 | 80 | ); |
81 | 81 | break; |
82 | 82 | case Session::NOT_INITIALIZED: |
83 | 83 | $status = 'interactionNeeded'; |
84 | 84 | if ($this->encryptionManager->isEnabled()) { |
85 | - $message = (string)$this->l->t( |
|
85 | + $message = (string) $this->l->t( |
|
86 | 86 | 'Encryption App is enabled, but your keys are not initialized. Please log-out and log-in again.' |
87 | 87 | ); |
88 | 88 | } else { |
89 | - $message = (string)$this->l->t( |
|
89 | + $message = (string) $this->l->t( |
|
90 | 90 | 'Please enable server side encryption in the admin settings in order to use the encryption module.' |
91 | 91 | ); |
92 | 92 | } |
93 | 93 | break; |
94 | 94 | case Session::INIT_SUCCESSFUL: |
95 | 95 | $status = 'success'; |
96 | - $message = (string)$this->l->t('Encryption app is enabled and ready'); |
|
96 | + $message = (string) $this->l->t('Encryption app is enabled and ready'); |
|
97 | 97 | } |
98 | 98 | |
99 | 99 | return new DataResponse( |
@@ -39,242 +39,242 @@ |
||
39 | 39 | |
40 | 40 | class Manager implements IManager { |
41 | 41 | |
42 | - /** @var array */ |
|
43 | - protected $encryptionModules; |
|
44 | - |
|
45 | - /** @var IConfig */ |
|
46 | - protected $config; |
|
47 | - |
|
48 | - /** @var ILogger */ |
|
49 | - protected $logger; |
|
50 | - |
|
51 | - /** @var Il10n */ |
|
52 | - protected $l; |
|
53 | - |
|
54 | - /** @var View */ |
|
55 | - protected $rootView; |
|
56 | - |
|
57 | - /** @var Util */ |
|
58 | - protected $util; |
|
59 | - |
|
60 | - /** @var ArrayCache */ |
|
61 | - protected $arrayCache; |
|
62 | - |
|
63 | - /** |
|
64 | - * @param IConfig $config |
|
65 | - * @param ILogger $logger |
|
66 | - * @param IL10N $l10n |
|
67 | - * @param View $rootView |
|
68 | - * @param Util $util |
|
69 | - * @param ArrayCache $arrayCache |
|
70 | - */ |
|
71 | - public function __construct(IConfig $config, ILogger $logger, IL10N $l10n, View $rootView, Util $util, ArrayCache $arrayCache) { |
|
72 | - $this->encryptionModules = array(); |
|
73 | - $this->config = $config; |
|
74 | - $this->logger = $logger; |
|
75 | - $this->l = $l10n; |
|
76 | - $this->rootView = $rootView; |
|
77 | - $this->util = $util; |
|
78 | - $this->arrayCache = $arrayCache; |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * Check if encryption is enabled |
|
83 | - * |
|
84 | - * @return bool true if enabled, false if not |
|
85 | - */ |
|
86 | - public function isEnabled() { |
|
87 | - |
|
88 | - $installed = $this->config->getSystemValue('installed', false); |
|
89 | - if (!$installed) { |
|
90 | - return false; |
|
91 | - } |
|
92 | - |
|
93 | - $enabled = $this->config->getAppValue('core', 'encryption_enabled', 'no'); |
|
94 | - return $enabled === 'yes'; |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * check if new encryption is ready |
|
99 | - * |
|
100 | - * @return bool |
|
101 | - * @throws ServiceUnavailableException |
|
102 | - */ |
|
103 | - public function isReady() { |
|
104 | - |
|
105 | - if ($this->isKeyStorageReady() === false) { |
|
106 | - throw new ServiceUnavailableException('Key Storage is not ready'); |
|
107 | - } |
|
108 | - |
|
109 | - return true; |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * @param string $user |
|
114 | - */ |
|
115 | - public function isReadyForUser($user) { |
|
116 | - if (!$this->isReady()) { |
|
117 | - return false; |
|
118 | - } |
|
119 | - |
|
120 | - foreach ($this->getEncryptionModules() as $module) { |
|
121 | - /** @var IEncryptionModule $m */ |
|
122 | - $m = call_user_func($module['callback']); |
|
123 | - if (!$m->isReadyForUser($user)) { |
|
124 | - return false; |
|
125 | - } |
|
126 | - } |
|
127 | - |
|
128 | - return true; |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * Registers an callback function which must return an encryption module instance |
|
133 | - * |
|
134 | - * @param string $id |
|
135 | - * @param string $displayName |
|
136 | - * @param callable $callback |
|
137 | - * @throws Exceptions\ModuleAlreadyExistsException |
|
138 | - */ |
|
139 | - public function registerEncryptionModule($id, $displayName, callable $callback) { |
|
140 | - |
|
141 | - if (isset($this->encryptionModules[$id])) { |
|
142 | - throw new Exceptions\ModuleAlreadyExistsException($id, $displayName); |
|
143 | - } |
|
144 | - |
|
145 | - $this->encryptionModules[$id] = [ |
|
146 | - 'id' => $id, |
|
147 | - 'displayName' => $displayName, |
|
148 | - 'callback' => $callback, |
|
149 | - ]; |
|
150 | - |
|
151 | - $defaultEncryptionModuleId = $this->getDefaultEncryptionModuleId(); |
|
152 | - |
|
153 | - if (empty($defaultEncryptionModuleId)) { |
|
154 | - $this->setDefaultEncryptionModule($id); |
|
155 | - } |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * Unregisters an encryption module |
|
160 | - * |
|
161 | - * @param string $moduleId |
|
162 | - */ |
|
163 | - public function unregisterEncryptionModule($moduleId) { |
|
164 | - unset($this->encryptionModules[$moduleId]); |
|
165 | - } |
|
166 | - |
|
167 | - /** |
|
168 | - * get a list of all encryption modules |
|
169 | - * |
|
170 | - * @return array [id => ['id' => $id, 'displayName' => $displayName, 'callback' => callback]] |
|
171 | - */ |
|
172 | - public function getEncryptionModules() { |
|
173 | - return $this->encryptionModules; |
|
174 | - } |
|
175 | - |
|
176 | - /** |
|
177 | - * get a specific encryption module |
|
178 | - * |
|
179 | - * @param string $moduleId |
|
180 | - * @return IEncryptionModule |
|
181 | - * @throws Exceptions\ModuleDoesNotExistsException |
|
182 | - */ |
|
183 | - public function getEncryptionModule($moduleId = '') { |
|
184 | - if (!empty($moduleId)) { |
|
185 | - if (isset($this->encryptionModules[$moduleId])) { |
|
186 | - return call_user_func($this->encryptionModules[$moduleId]['callback']); |
|
187 | - } else { |
|
188 | - $message = "Module with ID: $moduleId does not exist."; |
|
189 | - $hint = $this->l->t('Module with ID: %s does not exist. Please enable it in your apps settings or contact your administrator.', [$moduleId]); |
|
190 | - throw new Exceptions\ModuleDoesNotExistsException($message, $hint); |
|
191 | - } |
|
192 | - } else { |
|
193 | - return $this->getDefaultEncryptionModule(); |
|
194 | - } |
|
195 | - } |
|
196 | - |
|
197 | - /** |
|
198 | - * get default encryption module |
|
199 | - * |
|
200 | - * @return \OCP\Encryption\IEncryptionModule |
|
201 | - * @throws Exceptions\ModuleDoesNotExistsException |
|
202 | - */ |
|
203 | - protected function getDefaultEncryptionModule() { |
|
204 | - $defaultModuleId = $this->getDefaultEncryptionModuleId(); |
|
205 | - if (!empty($defaultModuleId)) { |
|
206 | - if (isset($this->encryptionModules[$defaultModuleId])) { |
|
207 | - return call_user_func($this->encryptionModules[$defaultModuleId]['callback']); |
|
208 | - } else { |
|
209 | - $message = 'Default encryption module not loaded'; |
|
210 | - throw new Exceptions\ModuleDoesNotExistsException($message); |
|
211 | - } |
|
212 | - } else { |
|
213 | - $message = 'No default encryption module defined'; |
|
214 | - throw new Exceptions\ModuleDoesNotExistsException($message); |
|
215 | - } |
|
216 | - |
|
217 | - } |
|
218 | - |
|
219 | - /** |
|
220 | - * set default encryption module Id |
|
221 | - * |
|
222 | - * @param string $moduleId |
|
223 | - * @return bool |
|
224 | - */ |
|
225 | - public function setDefaultEncryptionModule($moduleId) { |
|
226 | - try { |
|
227 | - $this->getEncryptionModule($moduleId); |
|
228 | - } catch (\Exception $e) { |
|
229 | - return false; |
|
230 | - } |
|
231 | - |
|
232 | - $this->config->setAppValue('core', 'default_encryption_module', $moduleId); |
|
233 | - return true; |
|
234 | - } |
|
235 | - |
|
236 | - /** |
|
237 | - * get default encryption module Id |
|
238 | - * |
|
239 | - * @return string |
|
240 | - */ |
|
241 | - public function getDefaultEncryptionModuleId() { |
|
242 | - return $this->config->getAppValue('core', 'default_encryption_module'); |
|
243 | - } |
|
244 | - |
|
245 | - /** |
|
246 | - * Add storage wrapper |
|
247 | - */ |
|
248 | - public function setupStorage() { |
|
249 | - // If encryption is disabled and there are no loaded modules it makes no sense to load the wrapper |
|
250 | - if (!empty($this->encryptionModules) || $this->isEnabled()) { |
|
251 | - $encryptionWrapper = new EncryptionWrapper($this->arrayCache, $this, $this->logger); |
|
252 | - Filesystem::addStorageWrapper('oc_encryption', array($encryptionWrapper, 'wrapStorage'), 2); |
|
253 | - } |
|
254 | - } |
|
255 | - |
|
256 | - |
|
257 | - /** |
|
258 | - * check if key storage is ready |
|
259 | - * |
|
260 | - * @return bool |
|
261 | - */ |
|
262 | - protected function isKeyStorageReady() { |
|
263 | - |
|
264 | - $rootDir = $this->util->getKeyStorageRoot(); |
|
265 | - |
|
266 | - // the default root is always valid |
|
267 | - if ($rootDir === '') { |
|
268 | - return true; |
|
269 | - } |
|
270 | - |
|
271 | - // check if key storage is mounted correctly |
|
272 | - if ($this->rootView->file_exists($rootDir . '/' . Storage::KEY_STORAGE_MARKER)) { |
|
273 | - return true; |
|
274 | - } |
|
275 | - |
|
276 | - return false; |
|
277 | - } |
|
42 | + /** @var array */ |
|
43 | + protected $encryptionModules; |
|
44 | + |
|
45 | + /** @var IConfig */ |
|
46 | + protected $config; |
|
47 | + |
|
48 | + /** @var ILogger */ |
|
49 | + protected $logger; |
|
50 | + |
|
51 | + /** @var Il10n */ |
|
52 | + protected $l; |
|
53 | + |
|
54 | + /** @var View */ |
|
55 | + protected $rootView; |
|
56 | + |
|
57 | + /** @var Util */ |
|
58 | + protected $util; |
|
59 | + |
|
60 | + /** @var ArrayCache */ |
|
61 | + protected $arrayCache; |
|
62 | + |
|
63 | + /** |
|
64 | + * @param IConfig $config |
|
65 | + * @param ILogger $logger |
|
66 | + * @param IL10N $l10n |
|
67 | + * @param View $rootView |
|
68 | + * @param Util $util |
|
69 | + * @param ArrayCache $arrayCache |
|
70 | + */ |
|
71 | + public function __construct(IConfig $config, ILogger $logger, IL10N $l10n, View $rootView, Util $util, ArrayCache $arrayCache) { |
|
72 | + $this->encryptionModules = array(); |
|
73 | + $this->config = $config; |
|
74 | + $this->logger = $logger; |
|
75 | + $this->l = $l10n; |
|
76 | + $this->rootView = $rootView; |
|
77 | + $this->util = $util; |
|
78 | + $this->arrayCache = $arrayCache; |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * Check if encryption is enabled |
|
83 | + * |
|
84 | + * @return bool true if enabled, false if not |
|
85 | + */ |
|
86 | + public function isEnabled() { |
|
87 | + |
|
88 | + $installed = $this->config->getSystemValue('installed', false); |
|
89 | + if (!$installed) { |
|
90 | + return false; |
|
91 | + } |
|
92 | + |
|
93 | + $enabled = $this->config->getAppValue('core', 'encryption_enabled', 'no'); |
|
94 | + return $enabled === 'yes'; |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * check if new encryption is ready |
|
99 | + * |
|
100 | + * @return bool |
|
101 | + * @throws ServiceUnavailableException |
|
102 | + */ |
|
103 | + public function isReady() { |
|
104 | + |
|
105 | + if ($this->isKeyStorageReady() === false) { |
|
106 | + throw new ServiceUnavailableException('Key Storage is not ready'); |
|
107 | + } |
|
108 | + |
|
109 | + return true; |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * @param string $user |
|
114 | + */ |
|
115 | + public function isReadyForUser($user) { |
|
116 | + if (!$this->isReady()) { |
|
117 | + return false; |
|
118 | + } |
|
119 | + |
|
120 | + foreach ($this->getEncryptionModules() as $module) { |
|
121 | + /** @var IEncryptionModule $m */ |
|
122 | + $m = call_user_func($module['callback']); |
|
123 | + if (!$m->isReadyForUser($user)) { |
|
124 | + return false; |
|
125 | + } |
|
126 | + } |
|
127 | + |
|
128 | + return true; |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * Registers an callback function which must return an encryption module instance |
|
133 | + * |
|
134 | + * @param string $id |
|
135 | + * @param string $displayName |
|
136 | + * @param callable $callback |
|
137 | + * @throws Exceptions\ModuleAlreadyExistsException |
|
138 | + */ |
|
139 | + public function registerEncryptionModule($id, $displayName, callable $callback) { |
|
140 | + |
|
141 | + if (isset($this->encryptionModules[$id])) { |
|
142 | + throw new Exceptions\ModuleAlreadyExistsException($id, $displayName); |
|
143 | + } |
|
144 | + |
|
145 | + $this->encryptionModules[$id] = [ |
|
146 | + 'id' => $id, |
|
147 | + 'displayName' => $displayName, |
|
148 | + 'callback' => $callback, |
|
149 | + ]; |
|
150 | + |
|
151 | + $defaultEncryptionModuleId = $this->getDefaultEncryptionModuleId(); |
|
152 | + |
|
153 | + if (empty($defaultEncryptionModuleId)) { |
|
154 | + $this->setDefaultEncryptionModule($id); |
|
155 | + } |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * Unregisters an encryption module |
|
160 | + * |
|
161 | + * @param string $moduleId |
|
162 | + */ |
|
163 | + public function unregisterEncryptionModule($moduleId) { |
|
164 | + unset($this->encryptionModules[$moduleId]); |
|
165 | + } |
|
166 | + |
|
167 | + /** |
|
168 | + * get a list of all encryption modules |
|
169 | + * |
|
170 | + * @return array [id => ['id' => $id, 'displayName' => $displayName, 'callback' => callback]] |
|
171 | + */ |
|
172 | + public function getEncryptionModules() { |
|
173 | + return $this->encryptionModules; |
|
174 | + } |
|
175 | + |
|
176 | + /** |
|
177 | + * get a specific encryption module |
|
178 | + * |
|
179 | + * @param string $moduleId |
|
180 | + * @return IEncryptionModule |
|
181 | + * @throws Exceptions\ModuleDoesNotExistsException |
|
182 | + */ |
|
183 | + public function getEncryptionModule($moduleId = '') { |
|
184 | + if (!empty($moduleId)) { |
|
185 | + if (isset($this->encryptionModules[$moduleId])) { |
|
186 | + return call_user_func($this->encryptionModules[$moduleId]['callback']); |
|
187 | + } else { |
|
188 | + $message = "Module with ID: $moduleId does not exist."; |
|
189 | + $hint = $this->l->t('Module with ID: %s does not exist. Please enable it in your apps settings or contact your administrator.', [$moduleId]); |
|
190 | + throw new Exceptions\ModuleDoesNotExistsException($message, $hint); |
|
191 | + } |
|
192 | + } else { |
|
193 | + return $this->getDefaultEncryptionModule(); |
|
194 | + } |
|
195 | + } |
|
196 | + |
|
197 | + /** |
|
198 | + * get default encryption module |
|
199 | + * |
|
200 | + * @return \OCP\Encryption\IEncryptionModule |
|
201 | + * @throws Exceptions\ModuleDoesNotExistsException |
|
202 | + */ |
|
203 | + protected function getDefaultEncryptionModule() { |
|
204 | + $defaultModuleId = $this->getDefaultEncryptionModuleId(); |
|
205 | + if (!empty($defaultModuleId)) { |
|
206 | + if (isset($this->encryptionModules[$defaultModuleId])) { |
|
207 | + return call_user_func($this->encryptionModules[$defaultModuleId]['callback']); |
|
208 | + } else { |
|
209 | + $message = 'Default encryption module not loaded'; |
|
210 | + throw new Exceptions\ModuleDoesNotExistsException($message); |
|
211 | + } |
|
212 | + } else { |
|
213 | + $message = 'No default encryption module defined'; |
|
214 | + throw new Exceptions\ModuleDoesNotExistsException($message); |
|
215 | + } |
|
216 | + |
|
217 | + } |
|
218 | + |
|
219 | + /** |
|
220 | + * set default encryption module Id |
|
221 | + * |
|
222 | + * @param string $moduleId |
|
223 | + * @return bool |
|
224 | + */ |
|
225 | + public function setDefaultEncryptionModule($moduleId) { |
|
226 | + try { |
|
227 | + $this->getEncryptionModule($moduleId); |
|
228 | + } catch (\Exception $e) { |
|
229 | + return false; |
|
230 | + } |
|
231 | + |
|
232 | + $this->config->setAppValue('core', 'default_encryption_module', $moduleId); |
|
233 | + return true; |
|
234 | + } |
|
235 | + |
|
236 | + /** |
|
237 | + * get default encryption module Id |
|
238 | + * |
|
239 | + * @return string |
|
240 | + */ |
|
241 | + public function getDefaultEncryptionModuleId() { |
|
242 | + return $this->config->getAppValue('core', 'default_encryption_module'); |
|
243 | + } |
|
244 | + |
|
245 | + /** |
|
246 | + * Add storage wrapper |
|
247 | + */ |
|
248 | + public function setupStorage() { |
|
249 | + // If encryption is disabled and there are no loaded modules it makes no sense to load the wrapper |
|
250 | + if (!empty($this->encryptionModules) || $this->isEnabled()) { |
|
251 | + $encryptionWrapper = new EncryptionWrapper($this->arrayCache, $this, $this->logger); |
|
252 | + Filesystem::addStorageWrapper('oc_encryption', array($encryptionWrapper, 'wrapStorage'), 2); |
|
253 | + } |
|
254 | + } |
|
255 | + |
|
256 | + |
|
257 | + /** |
|
258 | + * check if key storage is ready |
|
259 | + * |
|
260 | + * @return bool |
|
261 | + */ |
|
262 | + protected function isKeyStorageReady() { |
|
263 | + |
|
264 | + $rootDir = $this->util->getKeyStorageRoot(); |
|
265 | + |
|
266 | + // the default root is always valid |
|
267 | + if ($rootDir === '') { |
|
268 | + return true; |
|
269 | + } |
|
270 | + |
|
271 | + // check if key storage is mounted correctly |
|
272 | + if ($this->rootView->file_exists($rootDir . '/' . Storage::KEY_STORAGE_MARKER)) { |
|
273 | + return true; |
|
274 | + } |
|
275 | + |
|
276 | + return false; |
|
277 | + } |
|
278 | 278 | |
279 | 279 | |
280 | 280 | } |