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

This commit is contained in:
timvisee 2018-04-07 11:09:44 +02:00
parent 718238b35e
commit b47d2941d0
No known key found for this signature in database
GPG key ID: 109CBA0BF74036C2
7 changed files with 22 additions and 34 deletions

View file

@ -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))

View file

@ -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))

View file

@ -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

View file

@ -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))

View file

@ -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))

View file

@ -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() {

View file

@ -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)