diff --git a/api/src/file/remote_file.rs b/api/src/file/remote_file.rs index 7b4f9c6..f89a5fd 100644 --- a/api/src/file/remote_file.rs +++ b/api/src/file/remote_file.rs @@ -231,7 +231,6 @@ impl RemoteFile { pub fn secret_raw(&self) -> &Vec { // 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) { + 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 { diff --git a/cli/src/action/delete.rs b/cli/src/action/delete.rs index 538869d..b5294c9 100644 --- a/cli/src/action/delete.rs +++ b/cli/src/action/delete.rs @@ -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); diff --git a/cli/src/action/info.rs b/cli/src/action/info.rs index 29d6258..582ea25 100644 --- a/cli/src/action/info.rs +++ b/cli/src/action/info.rs @@ -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); diff --git a/cli/src/action/params.rs b/cli/src/action/params.rs index 97a44a8..7dd33e6 100644 --- a/cli/src/action/params.rs +++ b/cli/src/action/params.rs @@ -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); diff --git a/cli/src/action/password.rs b/cli/src/action/password.rs index 87a4275..b7bbb4a 100644 --- a/cli/src/action/password.rs +++ b/cli/src/action/password.rs @@ -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); diff --git a/cli/src/history_tool.rs b/cli/src/history_tool.rs index b72a191..4828c72 100644 --- a/cli/src/history_tool.rs +++ b/cli/src/history_tool.rs @@ -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, } }