diff options
| author | Ellie Huxtable <ellie@elliehuxtable.com> | 2021-05-09 20:11:17 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-05-09 20:11:17 +0100 |
| commit | e43e5ce74a85d87a625295b9b089a1b5b8e26fab (patch) | |
| tree | afbc97503090e320250440160a8ea9ee775fc170 /src/command | |
| parent | Fix resh importer crashing on end of file (#92) (diff) | |
| download | atuin-e43e5ce74a85d87a625295b9b089a1b5b8e26fab.zip | |
Add logout, only login if not already logged in (#91)
Also:
- Ensures that a key is generated as soon as a user registers!
- Ensures that "atuin key" will generate a key if needed, and doesn't
double base64 encode data
And a few other little fixes :)
Resolves #85
Resolves #86
Diffstat (limited to 'src/command')
| -rw-r--r-- | src/command/login.rs | 12 | ||||
| -rw-r--r-- | src/command/logout.rs | 12 | ||||
| -rw-r--r-- | src/command/mod.rs | 12 | ||||
| -rw-r--r-- | src/command/register.rs | 3 |
4 files changed, 36 insertions, 3 deletions
diff --git a/src/command/login.rs b/src/command/login.rs index eacb2105..9a144580 100644 --- a/src/command/login.rs +++ b/src/command/login.rs @@ -22,6 +22,16 @@ pub struct Cmd { impl Cmd { pub fn run(&self, settings: &Settings) -> Result<()> { + let session_path = atuin_common::utils::data_dir().join("session"); + + if session_path.exists() { + println!( + "You are already logged in! Please run 'atuin logout' if you wish to login again" + ); + + return Ok(()); + } + let session = api_client::login( settings.sync_address.as_str(), self.username.as_str(), @@ -34,7 +44,7 @@ impl Cmd { let key_path = settings.key_path.as_str(); let mut file = File::create(key_path)?; - file.write_all(&base64::decode(self.key.clone())?)?; + file.write_all(self.key.as_bytes())?; println!("Logged in!"); diff --git a/src/command/logout.rs b/src/command/logout.rs new file mode 100644 index 00000000..ec7d8dcb --- /dev/null +++ b/src/command/logout.rs @@ -0,0 +1,12 @@ +use std::fs::remove_file; + +pub fn run() { + let session_path = atuin_common::utils::data_dir().join("session"); + + if session_path.exists() { + remove_file(session_path.as_path()).expect("Failed to remove session file"); + println!("You have logged out!"); + } else { + println!("You are not logged in"); + } +} diff --git a/src/command/mod.rs b/src/command/mod.rs index 161c99d9..6050c4eb 100644 --- a/src/command/mod.rs +++ b/src/command/mod.rs @@ -13,6 +13,7 @@ mod history; mod import; mod init; mod login; +mod logout; mod register; mod search; mod server; @@ -83,6 +84,9 @@ pub enum AtuinCmd { #[structopt(about = "login to the configured server")] Login(login::Cmd), + #[structopt(about = "log out")] + Logout, + #[structopt(about = "register with the configured server")] Register(register::Cmd), @@ -136,6 +140,10 @@ impl AtuinCmd { Self::Sync { force } => sync::run(&client_settings, force, &mut db).await, Self::Login(l) => l.run(&client_settings), + Self::Logout => { + logout::run(); + Ok(()) + } Self::Register(r) => register::run( &client_settings, r.username.as_str(), @@ -143,8 +151,8 @@ impl AtuinCmd { r.password.as_str(), ), Self::Key => { - let key = std::fs::read(client_settings.key_path.as_str())?; - println!("{}", base64::encode(key)); + let key = atuin_client::encryption::load_key(&client_settings)?; + println!("{}", atuin_client::encryption::encode_key(key)?); Ok(()) } diff --git a/src/command/register.rs b/src/command/register.rs index acf9b1a3..7c38f483 100644 --- a/src/command/register.rs +++ b/src/command/register.rs @@ -27,5 +27,8 @@ pub fn run(settings: &Settings, username: &str, email: &str, password: &str) -> let mut file = File::create(path)?; file.write_all(session.session.as_bytes())?; + // Create a new key, and save it to disk + let _key = atuin_client::encryption::new_key(settings)?; + Ok(()) } |
