diff --git a/client/src/session/mod.rs b/client/src/session/mod.rs index b6110e7..81f8059 100644 --- a/client/src/session/mod.rs +++ b/client/src/session/mod.rs @@ -74,8 +74,8 @@ impl Session { } /// Tries to authorize the session. If `token` is not accepted by the server, this leads to an - /// [crate::error::Error::InvalidToken] or [crate::error::Error::TokenExpired] error. If the - /// session is successfully authorized, the auth token is saved in the sessions [Storage]. + /// [Error::InvalidToken] or [Error::TokenExpired] error. If the session is successfully + /// authorized, the auth token is saved in the sessions [Storage]. pub async fn auth_with_token(&mut self, token: String) -> Result<(), Error> { let request = self .client @@ -89,11 +89,12 @@ impl Session { .storage .push( storage::keys::KEY_ACCOUNT_AUTH_TOKEN, + true, token.to_owned().into_bytes(), ) .await { - Ok(_) | Err(StorageError::IdCollision(_)) => (), + Ok(_) => (), Err(e) => return Err(Error::Unknown(e.to_string())), } @@ -107,7 +108,7 @@ impl Session { } /// Tries to authorize the session. If the credentials are not accepted by the server, this - /// leads to a [crate::error::Error::AuthenticationFailure] or [crate::error::Error::UserNotFound] error. + /// leads to an [Error::AuthenticationFailure] or [Error::UserNotFound] error. pub async fn auth_with_credentials( &mut self, userid: Uuid, @@ -124,6 +125,19 @@ impl Session { let response = parse_response::(request.await).await?; + match self + .storage + .push( + storage::keys::KEY_ACCOUNT_AUTH_TOKEN, + true, + response.token.to_owned().into_bytes(), + ) + .await + { + Ok(_) => (), + Err(e) => return Err(Error::Unknown(e.to_string())), + } + self.auth = Some(AuthorizedSession { token: response.token, userid, @@ -133,7 +147,7 @@ impl Session { } /// Tries to register a new account on the server. If the server doesn't accept the invite-token, - /// this leads to an [crate::error::Error::InvalidToken] or [crate::error::Error::TokenExpired] error. + /// this leads to an [Error::InvalidToken] or [Error::TokenExpired] error. pub async fn register(&mut self, token: &str, password: &str) -> Result<(), Error> { let request = self .client diff --git a/client/src/storage/mod.rs b/client/src/storage/mod.rs index decf704..d532d38 100644 --- a/client/src/storage/mod.rs +++ b/client/src/storage/mod.rs @@ -18,8 +18,9 @@ pub enum StorageError { #[allow(async_fn_in_trait)] pub trait Storage: Sized { /// Save binary data in the storage. The data is identified by `id` and can be retrieved again - /// using [Storage::pull]. - async fn push(&mut self, id: &str, data: Vec) -> Result<(), StorageError>; + /// using [Storage::pull]. When `force` is set to true, this overrides entries with the same + /// name instead of returning a collision error. + async fn push(&mut self, id: &str, force: bool, data: Vec) -> Result<(), StorageError>; /// Reads binary data identified by `id` from the storage. Returns an error if `id` is unknown. async fn pull(&mut self, id: &str) -> Result<&Vec, StorageError>; diff --git a/client/src/storage/standard.rs b/client/src/storage/standard.rs index c3d40b7..483f743 100644 --- a/client/src/storage/standard.rs +++ b/client/src/storage/standard.rs @@ -1,7 +1,4 @@ -use crate::{ - storage::{Storage, StorageError}, - Session, -}; +use crate::storage::{Storage, StorageError}; use sqlx::SqlitePool; use std::collections::HashMap; @@ -51,8 +48,8 @@ impl StandardStorage { } impl Storage for StandardStorage { - async fn push(&mut self, id: &str, data: Vec) -> Result<(), StorageError> { - if self.entries.contains_key(id) { + async fn push(&mut self, id: &str, force: bool, data: Vec) -> Result<(), StorageError> { + if !force && self.entries.contains_key(id) { return Err(StorageError::IdCollision(id.to_string())); } @@ -101,7 +98,7 @@ mod tests { async fn standard_storage() { let mut storage = StandardStorage::new("sqlite:test.db").await.unwrap(); let data = vec![0xAC, 0xAB]; - storage.push("test", data).await.unwrap(); + storage.push("test", false, data).await.unwrap(); let pulled_data = storage.pull("test").await.unwrap(); assert!(pulled_data[0] == 0xAC && pulled_data[1] == 0xAB);