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> { pub fn secret_raw(&self) -> &Vec<u8> {
// A secret must have been set // A secret must have been set
if !self.has_secret() { if !self.has_secret() {
// TODO: don't panic, return an error instead
panic!("missing secret"); panic!("missing secret");
} }
@ -243,6 +242,11 @@ impl RemoteFile {
b64::encode(self.secret_raw()) 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. /// Check whether a file secret is set.
/// This secret must be set to decrypt a downloaded Send file. /// This secret must be set to decrypt a downloaded Send file.
pub fn has_secret(&self) -> bool { 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 // 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())?; 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 the owner token is set
ensure_owner_token(file.owner_token_mut(), &matcher_main); 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 // 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())?; 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 the owner token is set
ensure_owner_token(file.owner_token_mut(), &matcher_main); 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 // 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())?; 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 the owner token is set
ensure_owner_token(file.owner_token_mut(), &matcher_main); 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 // 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())?; 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 the owner token is set
ensure_owner_token(file.owner_token_mut(), &matcher_main); 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 ok
} }
/// Derive an owner token of a remote file from the current history. /// Derive the file secret and owner token from the history for the given file.
/// The newly derived owner token will be set into the given borrowed remote file. /// The newly derived properties will be set into the given borrowed remote file.
/// This method may be used to automatically derive the owner token for some file actions /// This method may be used to automatically derive the properties for some file actions
/// requiring this token, to prevent the user from having to enter it manually. /// 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. /// nothing will be derived and `false` is returned.
/// If an error occurred while deriving, /// If an error occurred while deriving,
/// the error is printed and `false` is returned. /// the error is printed and `false` is returned.
/// If there was no matching file in the history, /// 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, /// Incognito mode does not have any effect on this method,
/// as it won't ever change the history. /// as it won't ever change the history.
/// ///
/// If an owner token was successfully derived from the history, /// If any property was successfully derived, `true` is returned.
/// `true` is returned. pub fn derive_file_properties(matcher_main: &MainMatcher, file: &mut RemoteFile) -> bool {
/// // Return if all properties are already set
/// Note, to check if an owner token is set in the remote file, if file.has_secret() && file.has_owner_token() {
/// 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() {
return false; return false;
} }
@ -109,7 +105,7 @@ pub fn derive_owner_token(matcher_main: &MainMatcher, file: &mut RemoteFile) ->
Ok(history) => history, Ok(history) => history,
Err(err) => { Err(err) => {
print_error(err.context( print_error(err.context(
"failed to derive file owner token from history, ignoring", "failed to derive file properties from history, ignoring",
)); ));
return false; 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 // Find a matching file, grab and set the owner token if available
match history.get_file(file) { match history.get_file(file) {
Some(f) if f.has_owner_token() => { Some(f) => {
file.set_owner_token(f.owner_token().cloned()); // Set the secret
return true; 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,
} }
} }