Browse Source

Generalize base64 decode method, allow all formats for single method

timvisee 7 years ago
parent
commit
a7da14ec2c
4 changed files with 15 additions and 18 deletions
  1. 3 3
      api/src/action/download.rs
  2. 10 13
      api/src/crypto/b64.rs
  3. 1 1
      api/src/file/file.rs
  4. 1 1
      api/src/file/metadata.rs

+ 3 - 3
api/src/action/download.rs

@@ -76,7 +76,7 @@ impl<'a> Download<'a> {
 
         // Get the authentication nonce
         // TODO: don't unwrap here, return an error
-        let nonce = b64::decode_standard(
+        let nonce = b64::decode(
             response.headers()
                 .get_raw(HEADER_AUTH_NONCE)
                 .expect("missing authenticate header") 
@@ -115,7 +115,7 @@ impl<'a> Download<'a> {
 
         // Get the metadata nonce
         // TODO: don't unwrap here, return an error
-        let nonce = b64::decode_standard(
+        let nonce = b64::decode(
             response.headers()
                 .get_raw(HEADER_AUTH_NONCE)
                 .expect("missing authenticate header") 
@@ -380,7 +380,7 @@ impl MetadataResponse {
     // TODO: do not unwrap, return a proper error
     pub fn decrypt_metadata(&self, key_set: &KeySet) -> Result<Metadata> {
         // Decode the metadata
-        let raw = b64::decode_url(&self.meta)
+        let raw = b64::decode(&self.meta)
             .expect("failed to decode metadata from server");
 
         // Get the encrypted metadata, and it's tag

+ 10 - 13
api/src/crypto/b64.rs

@@ -13,17 +13,14 @@ pub fn encode(input: &[u8]) -> String {
     base64::encode_config(input, base64::URL_SAFE_NO_PAD)
 }
 
-/// Decode the given string as base64, with the given configuration.
-pub fn decode(input: &str, config: Config) -> Result<Vec<u8>, DecodeError> {
-    base64::decode_config(input, config)
-}
-
-/// Decode the given string as base64, in an URL-safe manner.
-pub fn decode_url(input: &str) -> Result<Vec<u8>, DecodeError> {
-    decode(input, base64::URL_SAFE_NO_PAD)
-}
-
-/// Decode the given string as base64, with the standaard character set.
-pub fn decode_standard(input: &str) -> Result<Vec<u8>, DecodeError> {
-    decode(input, base64::STANDARD)
+/// Decode the given string as base64.
+/// Standard and URL-safe character sets are both supported,
+/// padding is optional.
+pub fn decode(input: &str) -> Result<Vec<u8>, DecodeError> {
+    base64::decode_config(
+        input.replace('+', "-")
+            .replace('/', "_")
+            .trim_right_matches('='),
+        base64::URL_SAFE_NO_PAD,
+    )
 }

+ 1 - 1
api/src/file/file.rs

@@ -181,7 +181,7 @@ impl DownloadFile {
                 .ok_or(FileParseError::InvalidSecret)?
                 .get(1)
             {
-                secret = b64::decode_url(raw.as_str().trim())
+                secret = b64::decode(raw.as_str().trim())
                         .map_err(|_| FileParseError::InvalidSecret)?
             }
         }

+ 1 - 1
api/src/file/metadata.rs

@@ -51,7 +51,7 @@ impl Metadata {
     // TODO: use an input vector length from a constant
     pub fn iv(&self) -> [u8; 12] {
         // Decode the input vector
-        let decoded = b64::decode_url(&self.iv).unwrap();
+        let decoded = b64::decode(&self.iv).unwrap();
 
         // Create a sized array
         *array_ref!(decoded, 0, 12)