Skip to content

Commit

Permalink
don't save empty guard tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
icewind1991 committed Aug 24, 2024
1 parent 8c54ba2 commit 5161363
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
12 changes: 8 additions & 4 deletions src/auth/guarddata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,18 @@ impl GuardDataStore for FileGuardDataStore {
type Err = FileStoreError;

async fn store(&mut self, account: &str, machine_token: String) -> Result<(), Self::Err> {
let mut tokens = self.all_tokens()?;
tokens.insert(account.into(), machine_token);
self.save(tokens)
if !machine_token.is_empty() {
let mut tokens = self.all_tokens()?;
tokens.insert(account.into(), machine_token);
self.save(tokens)
} else {
Ok(())
}
}

async fn load(&mut self, account: &str) -> Result<Option<String>, Self::Err> {
let mut tokens = self.all_tokens()?;
Ok(tokens.remove(account))
Ok(tokens.remove(account).filter(|token| !token.is_empty()))
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/auth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ impl PendingAuth {
return Ok(Tokens {
access_token: Token(response.take_access_token()),
refresh_token: Token(response.take_refresh_token()),
new_guard_data: response.take_new_guard_data(),
new_guard_data: response.new_guard_data,
});
}
}
Expand All @@ -203,8 +203,7 @@ pub(crate) struct Tokens {
#[allow(dead_code)]
pub access_token: Token,
pub refresh_token: Token,
#[allow(dead_code)]
pub new_guard_data: String,
pub new_guard_data: Option<String>,
}

async fn poll_until_info(
Expand Down
7 changes: 4 additions & 3 deletions src/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,10 @@ impl Connection {
Either::Right((tokens, _)) => tokens?,
};

debug!(account, "saving guard data");
if let Err(e) = guard_data_store.store(account, tokens.new_guard_data).await {
error!(error = ?e, "failed to store guard data");
if let Some(guard_data) = tokens.new_guard_data {
if let Err(e) = guard_data_store.store(account, guard_data).await {
error!(error = ?e, "failed to store guard data");
}
}

connection.session = login(
Expand Down

0 comments on commit 5161363

Please sign in to comment.