Also derive file secrets from history

This commit is contained in:
timvisee 2018-05-10 19:03:33 +02:00
parent ca0400d2e6
commit 7aa9ecbcdf
No known key found for this signature in database
GPG key ID: 109CBA0BF74036C2
6 changed files with 34 additions and 24 deletions

View file

@ -231,7 +231,6 @@ impl RemoteFile {
pub fn secret_raw(&self) -> &Vec<u8> {
// A secret must have been set
if !self.has_secret() {
// TODO: don't panic, return an error instead
panic!("missing secret");
}
@ -243,6 +242,11 @@ impl RemoteFile {
b64::encode(self.secret_raw())
}
/// Set the secret for this file.
pub fn set_secret(&mut self, secret: Vec<u8>) {
self.secret = secret;
}
/// Check whether a file secret is set.
/// This secret must be set to decrypt a downloaded Send file.
pub fn has_secret(&self) -> bool {

View file

@ -46,7 +46,7 @@ impl<'a> Delete<'a> {
// Parse the remote file based on the share URL, derive the owner token from history
let mut file = RemoteFile::parse_url(url, matcher_delete.owner())?;
history_tool::derive_owner_token(&matcher_main, &mut file);
history_tool::derive_file_properties(&matcher_main, &mut file);
// Ensure the owner token is set
ensure_owner_token(file.owner_token_mut(), &matcher_main);

View file

@ -58,7 +58,7 @@ impl<'a> Info<'a> {
// Parse the remote file based on the share URL, derive the owner token from history
let mut file = RemoteFile::parse_url(url, matcher_info.owner())?;
history_tool::derive_owner_token(&matcher_main, &mut file);
history_tool::derive_file_properties(&matcher_main, &mut file);
// Ensure the owner token is set
ensure_owner_token(file.owner_token_mut(), &matcher_main);

View file

@ -44,7 +44,7 @@ impl<'a> Params<'a> {
// Parse the remote file based on the share URL, derive the owner token from history
let mut file = RemoteFile::parse_url(url, matcher_params.owner())?;
history_tool::derive_owner_token(&matcher_main, &mut file);
history_tool::derive_file_properties(&matcher_main, &mut file);
// Ensure the owner token is set
ensure_owner_token(file.owner_token_mut(), &matcher_main);

View file

@ -43,7 +43,7 @@ impl<'a> Password<'a> {
// Parse the remote file based on the share URL, derive the owner token from history
let mut file = RemoteFile::parse_url(url, matcher_password.owner())?;
history_tool::derive_owner_token(&matcher_main, &mut file);
history_tool::derive_file_properties(&matcher_main, &mut file);
// Ensure the owner token is set
ensure_owner_token(file.owner_token_mut(), &matcher_main);

View file

@ -79,28 +79,24 @@ pub fn remove(matcher_main: &MainMatcher, file: &RemoteFile) -> bool {
ok
}
/// Derive an owner token of a remote file from the current history.
/// The newly derived owner token will be set into the given borrowed remote file.
/// This method may be used to automatically derive the owner token for some file actions
/// requiring this token, to prevent the user from having to enter it manually.
/// Derive the file secret and owner token from the history for the given file.
/// The newly derived properties will be set into the given borrowed remote file.
/// This method may be used to automatically derive the properties for some file actions
/// requiring an owner token or secret, to prevent the user from having to enter it manually.
///
/// If the file already has an owner token set,
/// If the file already has all properties set,
/// nothing will be derived and `false` is returned.
/// If an error occurred while deriving,
/// the error is printed and `false` is returned.
/// If there was no matching file in the history,
/// and no owner token could be derived, `false` is returned.
/// and no properties could be derived, `false` is returned.
/// Incognito mode does not have any effect on this method,
/// as it won't ever change the history.
///
/// If an owner token was successfully derived from the history,
/// `true` is returned.
///
/// Note, to check if an owner token is set in the remote file,
/// use the `file.has_owner_token()` method instead of the returned boolean from this method.
pub fn derive_owner_token(matcher_main: &MainMatcher, file: &mut RemoteFile) -> bool {
// Return if the remote file already has an owner token set, or if we're incognito
if file.has_owner_token() {
/// If any property was successfully derived, `true` is returned.
pub fn derive_file_properties(matcher_main: &MainMatcher, file: &mut RemoteFile) -> bool {
// Return if all properties are already set
if file.has_secret() && file.has_owner_token() {
return false;
}
@ -109,7 +105,7 @@ pub fn derive_owner_token(matcher_main: &MainMatcher, file: &mut RemoteFile) ->
Ok(history) => history,
Err(err) => {
print_error(err.context(
"failed to derive file owner token from history, ignoring",
"failed to derive file properties from history, ignoring",
));
return false;
}
@ -117,10 +113,20 @@ pub fn derive_owner_token(matcher_main: &MainMatcher, file: &mut RemoteFile) ->
// Find a matching file, grab and set the owner token if available
match history.get_file(file) {
Some(f) if f.has_owner_token() => {
file.set_owner_token(f.owner_token().cloned());
return true;
Some(f) => {
// Set the secret
if f.has_secret() {
file.set_secret(f.secret_raw().clone());
}
// Set the owner token
if f.has_owner_token() {
file.set_owner_token(f.owner_token().cloned());
}
// Return whether any property was derived
return f.has_secret() || f.has_owner_token();
},
_ => return false,
None => return false,
}
}