fix(client >> storage): a registration now overrides the stored auth token with the new one
This commit is contained in:
parent
3b240d6ca9
commit
9bac0c2a78
|
@ -74,8 +74,8 @@ impl<S: Storage> Session<S> {
|
|||
}
|
||||
|
||||
/// 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<S: Storage> Session<S> {
|
|||
.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<S: Storage> Session<S> {
|
|||
}
|
||||
|
||||
/// 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<S: Storage> Session<S> {
|
|||
|
||||
let response = parse_response::<data::api::account::auth::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<S: Storage> Session<S> {
|
|||
}
|
||||
|
||||
/// 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
|
||||
|
|
|
@ -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<u8>) -> 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<u8>) -> 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<u8>, StorageError>;
|
||||
|
|
|
@ -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<u8>) -> Result<(), StorageError> {
|
||||
if self.entries.contains_key(id) {
|
||||
async fn push(&mut self, id: &str, force: bool, data: Vec<u8>) -> 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);
|
||||
|
|
Loading…
Reference in New Issue