소스 검색

Fix the nonce header, as it's always the same, simplifying the logic

timvisee 7 년 전
부모
커밋
b47d2941d0
7개의 변경된 파일22개의 추가작업 그리고 34개의 파일을 삭제
  1. 2 2
      api/src/action/delete.rs
  2. 2 2
      api/src/action/info.rs
  3. 3 8
      api/src/action/metadata.rs
  4. 2 2
      api/src/action/params.rs
  5. 2 2
      api/src/action/password.rs
  6. 2 2
      api/src/action/upload.rs
  7. 9 16
      api/src/api/nonce.rs

+ 2 - 2
api/src/action/delete.rs

@@ -4,7 +4,7 @@ use api::data::{
     Error as DataError,
     OwnedData,
 };
-use api::nonce::{NonceError, request_auth_nonce};
+use api::nonce::{NonceError, request_nonce};
 use ext::status_code::StatusCodeExt;
 use file::remote_file::RemoteFile;
 
@@ -46,7 +46,7 @@ impl<'a> Delete<'a> {
     fn fetch_auth_nonce(&self, client: &Client)
         -> Result<Vec<u8>, PrepareError>
     {
-        request_auth_nonce(
+        request_nonce(
             client,
             self.file.download_url(false),
         ).map_err(|err| PrepareError::Auth(err))

+ 2 - 2
api/src/action/info.rs

@@ -10,7 +10,7 @@ use api::data::{
     Error as DataError,
     OwnedData,
 };
-use api::nonce::{NonceError, request_auth_nonce};
+use api::nonce::{NonceError, request_nonce};
 use ext::status_code::StatusCodeExt;
 use file::remote_file::RemoteFile;
 
@@ -52,7 +52,7 @@ impl<'a> Info<'a> {
     fn fetch_auth_nonce(&self, client: &Client)
         -> Result<Vec<u8>, PrepareError>
     {
-        request_auth_nonce(
+        request_nonce(
             client,
             self.file.download_url(false),
         ).map_err(|err| PrepareError::Auth(err))

+ 3 - 8
api/src/action/metadata.rs

@@ -4,12 +4,7 @@ use reqwest::{Client, StatusCode};
 use reqwest::header::Authorization;
 use serde_json;
 
-use api::nonce::{
-    HEADER_NONCE,
-    header_nonce,
-    NonceError,
-    request_auth_nonce,
-};
+use api::nonce::{header_nonce, NonceError, request_nonce};
 use crypto::b64;
 use crypto::key_set::KeySet;
 use crypto::sig::signature_encoded;
@@ -55,7 +50,7 @@ impl<'a> Metadata<'a> {
     fn fetch_auth_nonce(&self, client: &Client)
         -> Result<Vec<u8>, RequestError>
     {
-        request_auth_nonce(
+        request_nonce(
             client,
             self.file.download_url(false),
         ).map_err(|err| RequestError::Auth(err))
@@ -92,7 +87,7 @@ impl<'a> Metadata<'a> {
         }
 
         // Get the metadata nonce
-        let nonce = header_nonce(HEADER_NONCE, &response)
+        let nonce = header_nonce(&response)
             .map_err(|err| MetaError::Nonce(err))?;
 
         // Parse the metadata response, and decrypt it

+ 2 - 2
api/src/action/params.rs

@@ -4,7 +4,7 @@ use api::data::{
     Error as DataError,
     OwnedData,
 };
-use api::nonce::{NonceError, request_auth_nonce};
+use api::nonce::{NonceError, request_nonce};
 use ext::status_code::StatusCodeExt;
 use file::remote_file::RemoteFile;
 
@@ -68,7 +68,7 @@ impl<'a> Params<'a> {
     fn fetch_auth_nonce(&self, client: &Client)
         -> Result<Vec<u8>, PrepareError>
     {
-        request_auth_nonce(
+        request_nonce(
             client,
             self.file.download_url(false),
         ).map_err(|err| PrepareError::Auth(err))

+ 2 - 2
api/src/action/password.rs

@@ -4,7 +4,7 @@ use api::data::{
     Error as DataError,
     OwnedData,
 };
-use api::nonce::{NonceError, request_auth_nonce};
+use api::nonce::{NonceError, request_nonce};
 use crypto::key_set::KeySet;
 use ext::status_code::StatusCodeExt;
 use file::remote_file::RemoteFile;
@@ -62,7 +62,7 @@ impl<'a> Password<'a> {
     fn fetch_auth_nonce(&self, client: &Client)
         -> Result<Vec<u8>, PrepareError>
     {
-        request_auth_nonce(
+        request_nonce(
             client,
             self.file.download_url(false),
         ).map_err(|err| PrepareError::Auth(err))

+ 2 - 2
api/src/action/upload.rs

@@ -22,7 +22,7 @@ use url::{
     Url,
 };
 
-use api::nonce::{HEADER_NONCE, header_nonce};
+use api::nonce::header_nonce;
 use crypto::key_set::KeySet;
 use ext::status_code::StatusCodeExt;
 use file::remote_file::RemoteFile;
@@ -259,7 +259,7 @@ impl Upload {
         }
 
         // Try to get the nonce, don't error on failure
-        let nonce = header_nonce(HEADER_NONCE, &response).ok();
+        let nonce = header_nonce(&response).ok();
 
         // Decode the response
         let response: UploadResponse = match response.json() {

+ 9 - 16
api/src/api/nonce.rs

@@ -5,10 +5,11 @@ use crypto::b64;
 use ext::status_code::StatusCodeExt;
 
 /// The name of the header the nonce is delivered in.
-pub const HEADER_NONCE: &'static str = "WWW-Authenticate";
+const HEADER_NONCE: &'static str = "WWW-Authenticate";
 
-/// Do a new request, and fetch the nonce from the header with the given name.
-pub fn request_nonce(client: &Client, url: Url, header: &str)
+/// Do a new request, and extract the nonce from a header in the given
+/// response.
+pub fn request_nonce(client: &Client, url: Url)
     -> Result<Vec<u8>, NonceError>
 {
     // Make the request
@@ -28,26 +29,18 @@ pub fn request_nonce(client: &Client, url: Url, header: &str)
         // }
     }
 
-    // Fetch the nonce
-    header_nonce(header, &response)
+    // Extract the nonce
+    header_nonce(&response)
 }
 
-/// Do a new request, and fetch the authentication nonce from the header with
-/// the given name.
-pub fn request_auth_nonce(client: &Client, url: Url)
-    -> Result<Vec<u8>, NonceError>
-{
-    request_nonce(client, url, HEADER_NONCE)
-}
-
-/// Get a nonce from a header in the given response.
-pub fn header_nonce(header: &str, response: &Response)
+/// Extract the nonce from a header in the given response.
+pub fn header_nonce(response: &Response)
     -> Result<Vec<u8>, NonceError>
 {
     // Get the authentication nonce
     b64::decode(
         response.headers()
-            .get_raw(header)
+            .get_raw(HEADER_NONCE)
             .ok_or(NonceError::NoNonceHeader)?
             .one()
             .ok_or(NonceError::MalformedNonce)