rust_keylock 0.18.0

A password manager with goals to be Secure, Simple to use, Portable and Extensible
Documentation
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// Copyright 2017 astonbitecode
// This file is part of rust-keylock password manager.
//
// rust-keylock is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// rust-keylock 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with rust-keylock.  If not, see <http://www.gnu.org/licenses/>.

use std::str::{self, FromStr};

use async_trait::async_trait;
use http::{HeaderMap, HeaderValue};
use http::header::HeaderName;
use log::*;
use reqwest;
use reqwest::{Client as OfficialReqwestClient, Response};

use super::errors::{self, RustKeylockError};

pub mod nextcloud;
pub mod dropbox;

pub(crate) type BoxedRklHttpAsyncClient = Box<dyn RklHttpAsyncClient<ResType=Vec<u8>>>;

/// Defines a task that runs asynchronously in the background.
#[async_trait]
pub trait AsyncTask: Send {
    /// Initializes a task
    async fn init(&mut self) -> errors::Result<()>;
    /// Executes the task
    async fn execute(&self) -> errors::Result<SyncStatus>;
    /// Stops a task
    fn stop(&mut self) -> errors::Result<()>;
}

#[derive(PartialEq, Debug)]
pub(crate) struct ServerVersionData {
    version: String,
    last_modified: String,
}

impl Default for ServerVersionData {
    fn default() -> Self {
        ServerVersionData {
            version: "0".to_string(),
            last_modified: "0".to_string(),
        }
    }
}

#[derive(PartialEq, Debug)]
pub(crate) enum SynchronizerAction {
    Download,
    Upload,
    Ignore,
    DownloadMergeAndUpload,
}

/// Returns the action that should be taken after parsing a Webdav response
///
/// ## Algorithm:
///
/// |           version_local        |     last_sync_version    |          Action
/// | :---------------------------:  | :----------------------: | :------------------------:
/// | bigger than server             | equal to server          | Upload
/// | bigger than server             | smaller than server      | Merge
/// | bigger than server             | bigger than server       | Upload
///
/// | smaller than server            | not equal to local       | Merge
/// | smaller than server            | equal to local           | Download
///
/// | equal to server                | equal to server          | Ignore
///
/// | equal to server                | not equal to server      | Merge
///
/// | non-existing                   | *                        | Download
///
/// | *                              | no information           | Merge (Try to download and then upload)
///
/// | other                          | other                    | Ignore (Error)

pub(crate) fn synchronizer_action(svd: &ServerVersionData,
                                  filename: &str,
                                  saved_at_local: &Option<i64>,
                                  version_local: &Option<i64>,
                                  last_sync_version: &Option<i64>)
                                  -> errors::Result<SynchronizerAction> {
    debug!("The file '{}' on the server was saved at {} with version {}",
           filename,
           svd.last_modified,
           svd.version);
    let version_server = i64::from_str(&svd.version)?;

    debug!("The file '{}' locally was saved at {:?} with version {:?}. Last sync version is {:?}",
           filename,
           saved_at_local,
           version_local,
           last_sync_version);

    match (version_local, version_server, last_sync_version) {
        (&Some(vl), vs, &Some(lsv)) if vl > vs && lsv == vs => {
            debug!("The local version is bigger than the server. The last sync version is equal to the server. \
                        Need to Upload");
            Ok(SynchronizerAction::Upload)
        }
        (&Some(vl), vs, &Some(lsv)) if vl > vs && lsv < vs => {
            debug!("The local version is bigger than the server. The last sync version is smaller than the server. \
                        Need to Merge");
            Ok(SynchronizerAction::DownloadMergeAndUpload)
        }
        (&Some(vl), vs, &Some(lsv)) if vl > vs && lsv > vs => {
            debug!("The local version is bigger than the server. The last sync version is bigger than the server. \
                        Need to Upload");
            Ok(SynchronizerAction::Upload)
        }
        (&Some(vl), vs, &Some(lsv)) if vl < vs && vl != lsv => {
            debug!("The local version is smaller than the server The last sync version is not equal to the local version. \
                        Need to Merge");
            Ok(SynchronizerAction::DownloadMergeAndUpload)
        }
        (&Some(vl), vs, &Some(lsv)) if vl < vs && vl == lsv => {
            debug!("The local version is smaller than the server The last sync version equal to the local version. \
                        Need to Download");
            Ok(SynchronizerAction::Download)
        }
        (&Some(vl), vs, &Some(lsv)) if vl == vs && lsv == vs => {
            debug!("The local version is equal to the server. The last sync version is equal to the server. \
                        Ignoring");
            Ok(SynchronizerAction::Ignore)
        }
        (&Some(vl), vs, &Some(lsv)) if vl == vs && lsv != vs => {
            debug!("The local version is equal to the server. The last sync version is not equal to the server. \
                        Need to merge");
            Ok(SynchronizerAction::DownloadMergeAndUpload)
        }
        (&None, _, _) => {
            debug!("Nothing is saved locally... Need to download");
            Ok(SynchronizerAction::Download)
        }
        (&Some(_), _, &None) => {
            debug!("No information about server... Need to merge");
            Ok(SynchronizerAction::DownloadMergeAndUpload)
        }
        (_, _, _) => {
            error!("The local version, server version and last sync version seem corrupted.");
            Ok(SynchronizerAction::Ignore)
        }
    }
}

/// The status of the synchronize actions
#[derive(PartialEq, Debug)]
pub(crate) enum SyncStatus {
    /// An update is available from the server.
    /// The &'static str is the sync that sends the message, String is the name of the file that is ready to be used if the user selects so.
    NewAvailable(&'static str, String),
    /// The local file was uploaded to the nextcloud server. The &'static str is the sync that sends the message.
    UploadSuccess(&'static str),
    /// An update is available from the nextcloud server but instead of replacing the contents, merging needs to be done.
    /// The &'static str is the sync that sends the message.
    /// The String is the name of the file that is ready to be used if the user selects so.
    NewToMerge(&'static str, String),
    /// None
    None,
}

/// The trait to be implemented by HTTP clients. Used for synchronization with dropbox, nextcloud etc.
#[async_trait]
pub(crate) trait RklHttpAsyncClient: Send {
    type ResType;
    fn header(&mut self, k: &str, v: &str);
    #[allow(dead_code)]
    async fn get(&mut self, uri: &str, additional_headers: &[(&str, &str)]) -> errors::Result<Self::ResType>;
    async fn post(&mut self, uri: &str, additional_headers: &[(&str, &str)], body: Vec<u8>) -> errors::Result<Self::ResType>;
}

#[derive(Debug, Clone)]
pub(crate) struct ReqwestClient {
    headers: HeaderMap,
    client: OfficialReqwestClient,
}

impl Default for ReqwestClient {
    fn default() -> Self {
        ReqwestClient::new()
    }
}

impl ReqwestClient {
    pub(crate) fn new() -> ReqwestClient {
        let client = OfficialReqwestClient::new();
        let headers = HeaderMap::new();
        ReqwestClient { headers, client }
    }

    async fn validate_response(resp: Response) -> errors::Result<Response> {
        if resp.status().is_client_error() || resp.status().is_server_error() {
            let message= format!("Error during HTTP request: {}.", resp.status().to_string());
            let body_bytes = Self::get_body(resp).await?;
            let body_str = str::from_utf8(&body_bytes)?;
            error!("{}: {}", message, body_str);
            Err(RustKeylockError::HttpError(message))
        } else {
            Ok(resp)
        }
    }

    async fn get_body(response: Response) -> errors::Result<Vec<u8>> {
        Ok(response.bytes().await?.to_vec())
    }
}

#[async_trait]
impl RklHttpAsyncClient for ReqwestClient {
    type ResType = Vec<u8>;

    fn header(&mut self, k: &str, v: &str) {
        self.headers.insert(HeaderName::from_str(k).unwrap_or_else(|error| {
            error!("{:?}", error);
            HeaderName::from_static("")
        }), HeaderValue::from_str(v).unwrap_or_else(|error| {
            error!("{:?}", error);
            HeaderValue::from_static("")
        }));
    }

    async fn get(&mut self, uri: &str, additional_headers: &[(&str, &str)]) -> errors::Result<Vec<u8>> {
        let mut builder = self.client
            .get(uri)
            .headers(self.headers.clone());
        for (k, v) in additional_headers {
            builder = builder.header(HeaderName::from_str(k).unwrap_or_else(|error| {
                error!("{:?}", error);
                HeaderName::from_static("")
            }), HeaderValue::from_str(v).unwrap_or_else(|error| {
                error!("{:?}", error);
                HeaderValue::from_static("")
            }));
        }

        let resp = builder.send().await?;
        let resp = Self::validate_response(resp).await?;
        Self::get_body(resp).await
    }

    async fn post(&mut self, uri: &str, additional_headers: &[(&str, &str)], body: Vec<u8>) -> errors::Result<Vec<u8>> {
        let mut builder = self.client
            .post(uri)
            .headers(self.headers.clone())
            .body(body);
        for (k, v) in additional_headers {
            builder = builder.header(HeaderName::from_str(k).unwrap_or_else(|error| {
                error!("{:?}", error);
                HeaderName::from_static("")
            }), HeaderValue::from_str(v).unwrap_or_else(|error| {
                error!("{:?}", error);
                HeaderValue::from_static("")
            }));
        }
        let resp = builder.send().await?;
        let resp = Self::validate_response(resp).await?;
        Self::get_body(resp).await
    }
}

/// The trait to be implemented by HTTP client factories. Provides the needed abstraction that makes the RklHttpAsyncClients testable.
pub(crate) trait RklHttpAsyncFactory: Send + Sync {
    type ClientResType;
    #[allow(dead_code)]
    fn init_factory(&mut self);
    fn create(&self) -> Box<dyn RklHttpAsyncClient<ResType=Self::ClientResType>>;
}

pub(crate) struct ReqwestClientFactory {}

impl ReqwestClientFactory {
    pub(crate) fn new() -> ReqwestClientFactory {
        ReqwestClientFactory {}
    }
}

impl RklHttpAsyncFactory for ReqwestClientFactory {
    type ClientResType = Vec<u8>;

    fn init_factory(&mut self) {
        // Nothing needed yet
    }

    fn create(&self) -> Box<dyn RklHttpAsyncClient<ResType=Self::ClientResType>> {
        Box::new(ReqwestClient::default())
    }
}

#[cfg(test)]
mod async_tests {
    use std::fs::{self, File};
    use std::io::prelude::*;

    use crate::file_handler;

    use super::*;

    // TODO: Test timeout mechanism

    #[test]
    fn parse_web_dav_response() {
        let filename = "parse_web_dav_response";
        create_file_with_contents(filename, "This is a test file");

        // Upload because version_local is bigger than version_server and last_sync_version is equal to version_server
        let wdr1 = ServerVersionData {
            last_modified: "133".to_string(),
            version: "1".to_string(),
        };
        let res1 = synchronizer_action(
            &wdr1,
            filename,
            &Some(133),
            &Some(2),
            &Some(1));
        assert!(res1.is_ok());
        assert!(res1.as_ref().unwrap() == &SynchronizerAction::Upload);

        // Merge because version_local is bigger than version_server and last_sync_version is not equal to version_server
        let wdr2 = ServerVersionData {
            last_modified: "133".to_string(),
            version: "2".to_string(),
        };
        let res2 = synchronizer_action(
            &wdr2,
            filename,
            &Some(133),
            &Some(3),
            &Some(1));
        assert!(res2.is_ok());
        assert!(res2.as_ref().unwrap() == &SynchronizerAction::DownloadMergeAndUpload);

        // Merge because version_local is smaller than version_server and last_sync_version is not equal to version_local
        let wdr3 = ServerVersionData {
            last_modified: "133".to_string(),
            version: "3".to_string(),
        };
        let res3 = synchronizer_action(
            &wdr3,
            filename,
            &Some(133),
            &Some(2),
            &Some(1));
        assert!(res3.is_ok());
        assert!(res3.as_ref().unwrap() == &SynchronizerAction::DownloadMergeAndUpload);

        // Download because version_local is smaller than version_server and last_sync_version equal to version_local
        let wdr4 = ServerVersionData {
            last_modified: "133".to_string(),
            version: "3".to_string(),
        };
        let res4 = synchronizer_action(
            &wdr4,
            filename,
            &Some(133),
            &Some(2),
            &Some(2));
        assert!(res4.is_ok());
        assert!(res4.as_ref().unwrap() == &SynchronizerAction::Download);

        // Ignore because version_local is equal to version_server and last_sync_version equal to version_server
        let wdr5 = ServerVersionData {
            last_modified: "133".to_string(),
            version: "3".to_string(),
        };
        let res5 = synchronizer_action(
            &wdr5,
            filename,
            &Some(133),
            &Some(3),
            &Some(3));
        assert!(res5.is_ok());
        assert!(res5.as_ref().unwrap() == &SynchronizerAction::Ignore);

        // Merge because version_local is equal to version_server and last_sync_version is not equal to version_server
        let wdr6 = ServerVersionData {
            last_modified: "133".to_string(),
            version: "3".to_string(),
        };
        let res6 = synchronizer_action(
            &wdr6,
            filename,
            &Some(133),
            &Some(3),
            &Some(2));
        assert!(res6.is_ok());
        assert!(res6.as_ref().unwrap() == &SynchronizerAction::DownloadMergeAndUpload);

        let _ = file_handler::delete_file(filename);
    }

    fn create_file_with_contents(filename: &str, contents: &str) {
        let default_rustkeylock_dir_path_buf = file_handler::default_rustkeylock_location();
        let default_rustkeylock_dir = default_rustkeylock_dir_path_buf.to_str().unwrap();
        let creation_result = fs::create_dir_all(default_rustkeylock_dir).map(|_| {
            let path_buf = file_handler::default_toml_path(filename);
            let path = path_buf.to_str().unwrap();
            let mut file = File::create(path).unwrap();
            assert!(file.write_all(contents.as_bytes()).is_ok());
        });
        assert!(creation_result.is_ok());
    }
}