fix(daemon): do not try to sync if logged out (#2037)

* fix(daemon): do not try to sync if logged out

I've also added Settings::logged_in, as there are a few places where we
switch on login state.

* make session_token a function
This commit is contained in:
Ellie Huxtable 2024-05-21 10:43:08 +07:00 committed by GitHub
parent b636be8b02
commit 8dc8448de0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 36 additions and 33 deletions

View File

@ -55,7 +55,10 @@ pub async fn diff(
) -> Result<(Vec<Diff>, RecordStatus), SyncError> {
let client = Client::new(
&settings.sync_address,
&settings.session_token,
settings
.session_token()
.map_err(|e| SyncError::RemoteRequestError { msg: e.to_string() })?
.as_str(),
settings.network_connect_timeout,
settings.network_timeout,
)
@ -270,7 +273,10 @@ pub async fn sync_remote(
) -> Result<(i64, Vec<RecordId>), SyncError> {
let client = Client::new(
&settings.sync_address,
&settings.session_token,
settings
.session_token()
.map_err(|e| SyncError::RemoteRequestError { msg: e.to_string() })?
.as_str(),
settings.network_connect_timeout,
settings.network_timeout,
)

View File

@ -1,10 +1,5 @@
use std::{
collections::HashMap,
convert::TryFrom,
fmt,
io::prelude::*,
path::{Path, PathBuf},
str::FromStr,
collections::HashMap, convert::TryFrom, fmt, io::prelude::*, path::PathBuf, str::FromStr,
};
use atuin_common::record::HostId;
@ -458,11 +453,6 @@ pub struct Settings {
#[serde(default)]
pub daemon: Daemon,
// This is automatically loaded when settings is created. Do not set in
// config! Keep secrets and settings apart.
#[serde(skip)]
pub session_token: String,
}
impl Settings {
@ -568,6 +558,21 @@ impl Settings {
}
}
pub fn logged_in(&self) -> bool {
let session_path = self.session_path.as_str();
PathBuf::from(session_path).exists()
}
pub fn session_token(&self) -> Result<String> {
if !self.logged_in() {
return Err(eyre!("Tried to load session; not logged in"));
}
let session_path = self.session_path.as_str();
Ok(fs_err::read_to_string(session_path)?)
}
#[cfg(feature = "check-update")]
fn needs_update_check(&self) -> Result<bool> {
let last_check = Settings::last_version_check()?;
@ -688,7 +693,6 @@ impl Settings {
)?
.set_default("scroll_context_lines", 1)?
.set_default("shell_up_key_binding", false)?
.set_default("session_token", "")?
.set_default("workspaces", false)?
.set_default("ctrl_n_shortcuts", false)?
.set_default("secrets_filter", true)?
@ -778,14 +782,6 @@ impl Settings {
let session_path = shellexpand::full(&session_path)?;
settings.session_path = session_path.to_string();
// Finally, set the auth token
if Path::new(session_path.to_string().as_str()).exists() {
let token = fs_err::read_to_string(session_path.to_string())?;
settings.session_token = token.trim().to_string();
} else {
settings.session_token = String::from("not logged in");
}
Ok(settings)
}

View File

@ -191,7 +191,7 @@ async fn sync_upload(
pub async fn sync(settings: &Settings, force: bool, db: &impl Database) -> Result<()> {
let client = api_client::Client::new(
&settings.sync_address,
&settings.session_token,
settings.session_token()?.as_str(),
settings.network_connect_timeout,
settings.network_timeout,
)?;

View File

@ -38,6 +38,11 @@ pub async fn worker(
ticker.tick().await;
tracing::info!("sync worker tick");
if !settings.logged_in() {
tracing::debug!("not logged in, skipping sync tick");
continue;
}
let res = sync::sync(&settings, &store).await;
if let Err(e) = res {

View File

@ -26,7 +26,7 @@ pub async fn run(
) -> Result<()> {
let client = api_client::Client::new(
&settings.sync_address,
&settings.session_token,
settings.session_token()?.as_str(),
settings.network_connect_timeout,
settings.network_timeout,
)?;

View File

@ -12,7 +12,7 @@ pub async fn run(settings: &Settings) -> Result<()> {
let client = api_client::Client::new(
&settings.sync_address,
&settings.session_token,
settings.session_token()?.as_str(),
settings.network_connect_timeout,
settings.network_timeout,
)?;

View File

@ -35,9 +35,7 @@ fn get_input() -> Result<String> {
impl Cmd {
pub async fn run(&self, settings: &Settings, store: &SqliteStore) -> Result<()> {
let session_path = settings.session_path.as_str();
if PathBuf::from(session_path).exists() {
if settings.logged_in() {
println!(
"You are already logged in! Please run 'atuin logout' if you wish to login again"
);

View File

@ -1,5 +1,3 @@
use std::path::PathBuf;
use eyre::{Context, Result};
use fs_err::remove_file;
@ -8,7 +6,7 @@ use atuin_client::settings::Settings;
pub fn run(settings: &Settings) -> Result<()> {
let session_path = settings.session_path.as_str();
if PathBuf::from(session_path).exists() {
if settings.logged_in() {
remove_file(session_path).context("Failed to remove session file")?;
println!("You have logged out!");
} else {

View File

@ -37,7 +37,7 @@ impl Push {
let client = Client::new(
&settings.sync_address,
&settings.session_token,
settings.session_token()?.as_str(),
settings.network_connect_timeout,
settings.network_timeout * 10, // we may be deleting a lot of data... so up the
// timeout

View File

@ -16,7 +16,7 @@ pub async fn run(settings: &Settings, db: &impl Database) -> Result<()> {
let client = api_client::Client::new(
&settings.sync_address,
&settings.session_token,
settings.session_token()?.as_str(),
settings.network_connect_timeout,
settings.network_timeout,
)?;