Skip to content

Commit

Permalink
Refuse logging in if key is empty
Browse files Browse the repository at this point in the history
Before the previous commit, an empty key file would be created if key
wasn't specified, and after the previous commit, the key file would not
be created if the key wasn't specified and stay empty if it was empty.

Now the log command checks the key file if a key is not specified and
exits with an error message if either the key file couldn't be opened or
is empty. If a key is specified, the key file is just created with it as
before.
  • Loading branch information
trygveaa committed Feb 5, 2023
1 parent ab2eacd commit 678163b
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/command/client/sync/login.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use std::io;

use clap::Parser;
use eyre::Result;
use tokio::{fs::File, io::AsyncWriteExt};
use eyre::{bail, Result};
use tokio::{
fs::File,
io::{AsyncReadExt, AsyncWriteExt},
};

use atuin_client::{api_client, settings::Settings};
use atuin_common::api::LoginRequest;
Expand Down Expand Up @@ -42,6 +45,23 @@ impl Cmd {
let username = or_user_input(&self.username, "username");
let key = or_user_input(&self.key, "encryption key [blank to use existing key file]");
let password = self.password.clone().unwrap_or_else(read_user_password);

let key_path = settings.key_path.as_str();
if key.is_empty() {
let mut file = match File::open(key_path).await {
Err(_e) => bail!("key file couldn't be opened, key must be specified"),
Ok(f) => f,
};
let mut bytes = Vec::new();
file.read_to_end(&mut bytes).await?;
if bytes.is_empty() {
bail!("key file empty, key must be specified");
}
} else {
let mut file = File::create(key_path).await?;
file.write_all(key.as_bytes()).await?;
}

let session = api_client::login(
settings.sync_address.as_str(),
LoginRequest { username, password },
Expand All @@ -52,12 +72,6 @@ impl Cmd {
let mut file = File::create(session_path).await?;
file.write_all(session.session.as_bytes()).await?;

if !key.is_empty() {
let key_path = settings.key_path.as_str();
let mut file = File::create(key_path).await?;
file.write_all(key.as_bytes()).await?;
}

println!("Logged in!");

Ok(())
Expand Down

0 comments on commit 678163b

Please sign in to comment.