Made AuthToken be type alias for u8;32 + Used correct type alliases for the clients ledger

This commit is contained in:
Jedrzej Stuczynski
2019-12-18 10:03:12 +00:00
parent 06c645cf56
commit 507f37fcec
3 changed files with 27 additions and 28 deletions
+10 -11
View File
@@ -40,18 +40,15 @@ pub enum ProviderRequestError {
}
pub trait ProviderRequest
where
Self: Sized,
where
Self: Sized,
{
fn get_prefix() -> [u8; 2];
fn to_bytes(&self) -> Vec<u8>;
fn from_bytes(bytes: &[u8]) -> Result<Self, ProviderRequestError>;
}
#[derive(Debug)]
pub struct AuthToken {
pub value: [u8; 32]
}
pub type AuthToken = [u8; 32];
#[derive(Debug)]
pub struct PullRequest {
@@ -61,7 +58,10 @@ pub struct PullRequest {
}
impl PullRequest {
pub fn new(destination_address: sphinx::route::DestinationAddressBytes, auth_token: AuthToken) -> Self {
pub fn new(
destination_address: sphinx::route::DestinationAddressBytes,
auth_token: AuthToken,
) -> Self {
PullRequest {
auth_token,
destination_address,
@@ -96,11 +96,11 @@ impl ProviderRequest for PullRequest {
let mut destination_address = [0u8; 32];
destination_address.copy_from_slice(&bytes[2..]);
let mut auth_token= [0u8; 32];
let mut auth_token = [0u8; 32];
auth_token.copy_from_slice(&bytes[34..]);
Ok(PullRequest {
auth_token: AuthToken { value: auth_token },
auth_token,
destination_address,
})
}
@@ -139,9 +139,8 @@ impl ProviderRequest for RegisterRequest {
destination_address.copy_from_slice(&bytes[2..]);
Ok(RegisterRequest {
destination_address
destination_address,
})
}
}
+15 -16
View File
@@ -47,13 +47,13 @@ impl From<StoreError> for ClientProcessingError {
#[derive(Debug, Clone)]
pub(crate) struct ClientProcessingData {
store_dir: PathBuf,
registered_clients_ledger: HashMap<DestinationAddressBytes, Vec<u8>>,
registered_clients_ledger: HashMap<AuthToken, DestinationAddressBytes>,
}
impl ClientProcessingData {
pub(crate) fn new(
store_dir: PathBuf,
registered_clients_ledger: HashMap<DestinationAddressBytes, Vec<u8>>,
registered_clients_ledger: HashMap<AuthToken, DestinationAddressBytes>,
) -> Self {
ClientProcessingData {
store_dir,
@@ -106,10 +106,10 @@ impl ClientRequestProcessor {
fn process_pull_messages_request(
req: PullRequest,
store_dir: &Path,
registered_client_ledger: &mut HashMap<DestinationAddressBytes, Vec<u8>>,
registered_client_ledger: &mut HashMap<AuthToken, DestinationAddressBytes>,
) -> Result<PullResponse, ClientProcessingError> {
println!("Processing pull!");
if registered_client_ledger.contains_key(&req.auth_token.value) {
if registered_client_ledger.contains_key(&req.auth_token) {
let retrieved_messages =
ClientStorage::retrieve_client_files(req.destination_address, store_dir)?;
Ok(PullResponse::new(retrieved_messages))
@@ -121,7 +121,7 @@ impl ClientRequestProcessor {
fn register_new_client(
req: RegisterRequest,
store_dir: &Path,
registered_client_ledger: &mut HashMap<DestinationAddressBytes, Vec<u8>>,
registered_client_ledger: &mut HashMap<AuthToken, DestinationAddressBytes>,
provider_secret_key: Scalar,
) -> Result<RegisterResponse, ClientProcessingError> {
println!("Processing register new client request!");
@@ -129,12 +129,11 @@ impl ClientRequestProcessor {
req.destination_address.to_vec(),
provider_secret_key,
);
if !registered_client_ledger.contains_key(&auth_token.value) {
registered_client_ledger
.insert(auth_token.value.clone(), req.destination_address.to_vec());
if !registered_client_ledger.contains_key(&auth_token) {
registered_client_ledger.insert(auth_token, req.destination_address);
ClientRequestProcessor::create_storage_dir(req.destination_address, store_dir);
}
Ok(RegisterResponse::new(auth_token.value.to_vec()))
Ok(RegisterResponse::new(auth_token.to_vec()))
}
fn create_storage_dir(
@@ -149,12 +148,12 @@ impl ClientRequestProcessor {
}
fn generate_new_auth_token(data: Vec<u8>, key: Scalar) -> AuthToken {
let mut auth_token =
let mut auth_token_raw =
HmacSha256::new_varkey(&key.to_bytes()).expect("HMAC can take key of any size");
auth_token.input(&data);
let mut result = [0u8; 32];
result.copy_from_slice(&auth_token.result().code().to_vec());
AuthToken { value: result }
auth_token_raw.input(&data);
let mut auth_token = [0u8; 32];
auth_token.copy_from_slice(&auth_token_raw.result().code().to_vec());
auth_token
}
}
@@ -167,7 +166,7 @@ mod register_new_client {
let req1 = RegisterRequest {
destination_address: [1u8; 32],
};
let mut registered_client_ledger: HashMap<DestinationAddressBytes, Vec<u8>> =
let mut registered_client_ledger: HashMap<AuthToken, DestinationAddressBytes> =
HashMap::new();
let store_dir = Path::new("./foo/");
let key = Scalar::from_bytes_mod_order([1u8; 32]);
@@ -197,7 +196,7 @@ mod register_new_client {
let req1 = RegisterRequest {
destination_address: [1u8; 32],
};
let mut registered_client_ledger: HashMap<DestinationAddressBytes, Vec<u8>> =
let mut registered_client_ledger: HashMap<AuthToken, DestinationAddressBytes> =
HashMap::new();
let store_dir = Path::new("./foo/");
let key = Scalar::from_bytes_mod_order([1u8; 32]);
+2 -1
View File
@@ -12,6 +12,7 @@ use crate::provider::mix_handling::{MixPacketProcessor, MixProcessingData};
use crate::provider::storage::ClientStorage;
use std::collections::HashMap;
use sphinx::route::DestinationAddressBytes;
use sfw_provider_requests::requests::AuthToken;
mod client_handling;
mod mix_handling;
@@ -27,7 +28,7 @@ pub struct ServiceProvider {
client_network_address: SocketAddr,
secret_key: Scalar,
store_dir: PathBuf,
registered_clients_ledger: HashMap<DestinationAddressBytes, Vec<u8>>,
registered_clients_ledger: HashMap<AuthToken, DestinationAddressBytes>,
}
impl ServiceProvider {