Skip to content

Commit

Permalink
Merge branch 'feature/secrecy' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
paulfariello committed Mar 18, 2024
2 parents dca098f + 3463625 commit 0c844e0
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 171 deletions.
295 changes: 161 additions & 134 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "aparte"
version = "0.3.0-dev"
version = "0.4.0-dev"
description = "Simple XMPP console client written in Rust and inspired by Profanity."
authors = ["Paul Fariello <[email protected]>"]
edition = "2018"
Expand All @@ -20,8 +20,8 @@ flexi_logger = "^0.27"
backtrace = "^0.3"
futures = "^0.3"
tokio = { version = "^1.10", features = ["full"] }
tokio-xmpp = { path = "../xmpp-rs/tokio-xmpp/" } # "=3.3.0"
xmpp-parsers = { path = "../xmpp-rs/parsers/" } # "^0.19"
tokio-xmpp = { git = "https://gitlab.com/xmpp-rs/xmpp-rs.git" }
xmpp-parsers = { git = "https://gitlab.com/xmpp-rs/xmpp-rs.git" }
rpassword = "^3.0"
uuid = { version = "^1.7", features = ["v4"] }
termion = "1.5"
Expand All @@ -48,6 +48,7 @@ aes-gcm = "0.10.3"
base64 = "0.21.7"
sha1 = "0.10.6"
itertools = "0.12.1"
secrecy = "0.8.0"

[dev-dependencies]
mockall = "^0.9"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Features
- [x] Bookmarks
- [x] Consistent color generation
- [x] MAM
- [ ] Omemo
- [x] Omemo (no MUC support currently)

Install
=======
Expand Down
15 changes: 10 additions & 5 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,13 +275,13 @@ macro_rules! build_subcommand_map(
#[macro_export]
macro_rules! parse_command_args(
($aparte:ident, $command:ident, $index:ident, {}) => ();
($aparte:ident, $command:ident, $index:ident, { $arg:ident: Password<$type:ty> }) => (
($aparte:ident, $command:ident, $index:ident, { $arg:ident: Password }) => (
if $command.args.len() <= $index {
$aparte.schedule(Event::ReadPassword($command.clone()));
return Ok(())
}

let $arg: Password<$type> = Password::from_str(&$command.args[$index])?;
let $arg: Password = Password::from_str(&$command.args[$index])?;

$index += 1;
);
Expand Down Expand Up @@ -669,7 +669,10 @@ mod tests_command_parser {
"/test \"command with arg".to_string(),
);
assert!(command.is_err());
assert_eq!(command.err(), Some("Missing closing quote".to_string()));
assert_eq!(
format!("{}", command.err().unwrap()),
"Missing closing quote"
);
}

#[test]
Expand Down Expand Up @@ -790,12 +793,14 @@ mod tests_command_parser {
#[test]
fn test_command_parse_name() {
let name = Command::parse_name("/me's best client is Aparté");
assert_eq!(Ok("me"), name);
assert!(name.is_ok());
assert_eq!("me", name.unwrap());
}

#[test]
fn test_command_parse_name_without_args() {
let name = Command::parse_name("/close");
assert_eq!(anyhow::Result::Ok("close"), name);
assert!(name.is_ok());
assert_eq!("close", name.unwrap());
}
}
49 changes: 26 additions & 23 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ use std::future::Future;
use std::io::Read;
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering::Relaxed;
use std::sync::{Arc, Mutex};

use anyhow::{Context, Result};
use chrono::{DateTime, FixedOffset, Local as LocalTz};
use futures::sink::SinkExt;
use futures::stream::StreamExt;
use rand::{self, Rng};
use rand::Rng;
use secrecy::{ExposeSecret, Secret};
use termion::event::Key;
use tokio::runtime::Runtime as TokioRuntime;
use tokio::signal::unix;
Expand Down Expand Up @@ -64,7 +67,7 @@ const VERSION: &str = env!("CARGO_PKG_VERSION");
#[derive(Debug, Clone)]
pub enum Event {
Start,
Connect(ConnectionInfo, Password<String>),
Connect(ConnectionInfo, Password),
Connected(Account, Jid),
Disconnected(Account, String),
AuthError(Account, String),
Expand Down Expand Up @@ -370,19 +373,7 @@ impl Display for Mod {
}
}

#[derive(Debug, Clone)]
pub struct Password<T: FromStr>(pub T);

impl<T: FromStr> FromStr for Password<T> {
type Err = T::Err;

fn from_str(s: &str) -> Result<Self, T::Err> {
match T::from_str(s) {
Err(e) => Err(e),
Ok(inner) => Ok(Password(inner)),
}
}
}
pub type Password = Secret<String>;

pub struct Connection {
pub sink: mpsc::UnboundedSender<Element>,
Expand All @@ -409,7 +400,7 @@ Examples:
aparte.config.accounts.keys().cloned().collect()
})
},
password: Password<String>
password: Password
},
|aparte, _command| {
let account = {
Expand Down Expand Up @@ -827,6 +818,7 @@ pub struct Aparte {
send_rx: Option<mpsc::UnboundedReceiver<(Account, Element)>>,
pending_iq: Arc<Mutex<HashMap<Uuid, PendingIqState>>>,
crypto_engines: Arc<Mutex<HashMap<(Account, BareJid), CryptoEngine>>>,
read_password: AtomicBool,
/// Aparté main configuration
pub config: Config,
pub storage: Storage,
Expand Down Expand Up @@ -874,6 +866,7 @@ impl Aparte {
config: config.clone(),
pending_iq: Arc::new(Mutex::new(HashMap::new())),
crypto_engines: Arc::new(Mutex::new(HashMap::new())),
read_password: AtomicBool::new(false),
};

aparte.add_mod(Mod::Completion(mods::completion::CompletionMod::new()));
Expand Down Expand Up @@ -1119,7 +1112,7 @@ impl Aparte {
}
}

pub fn connect(&mut self, connection_info: &ConnectionInfo, password: Password<String>) {
pub fn connect(&mut self, connection_info: &ConnectionInfo, password: Password) {
let account: Account = match Jid::from_str(&connection_info.jid) {
Ok(Jid::Full(jid)) => jid,
Ok(Jid::Bare(jid)) => {
Expand All @@ -1143,7 +1136,7 @@ impl Aparte {
self.log(format!("Connecting as {account}"));
let config = tokio_xmpp::AsyncConfig {
jid: Jid::from(account.clone()),
password: password.0,
password: password.expose_secret().clone(),
server: match (&connection_info.server, &connection_info.port) {
(Some(server), Some(port)) => tokio_xmpp::starttls::ServerConfig::Manual {
host: server.clone(),
Expand Down Expand Up @@ -1233,7 +1226,11 @@ impl Aparte {
}

pub fn handle_event(&mut self, event: Event) -> Result<(), ()> {
log::debug!("Event: {:?}", event);
if self.read_password.load(Relaxed) && matches!(event, Event::Key(..)) {
log::debug!("Event: {:?}", Event::Key(Key::Char('*')));
} else {
log::debug!("Event: {:?}", event);
}
{
let mods = self.mods.clone();
for (_, r#mod) in mods.iter() {
Expand All @@ -1245,10 +1242,13 @@ impl Aparte {
Event::Start => {
self.start();
}
Event::Command(command) => match self.handle_command(command) {
Err(err) => self.log(err),
Ok(()) => {}
},
Event::Command(command) => {
self.read_password.swap(false, Relaxed);
match self.handle_command(command) {
Err(err) => self.log(err),
Ok(()) => {}
}
}
Event::RawCommand(account, context, buf) => {
match self.handle_raw_command(&account, &context, &buf) {
Err(err) => self.log(err),
Expand Down Expand Up @@ -1347,6 +1347,9 @@ impl Aparte {
presence.add_payload(Muc::new());
self.send(&channel.account, presence);
}
Event::ReadPassword(_) => {
self.read_password.swap(true, Relaxed);
}
Event::Quit => {
return Err(());
}
Expand Down
1 change: 0 additions & 1 deletion src/mods/omemo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use std::fmt::{self, Debug};
use std::str::FromStr;

use aes_gcm::{
self,
aead::{Aead, AeadCore, KeyInit, OsRng},
Aes128Gcm,
};
Expand Down
1 change: 0 additions & 1 deletion src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use anyhow::{anyhow, Error, Result};
use async_trait::async_trait;
use diesel::prelude::*;
use diesel::r2d2::{ConnectionManager, Pool};
use diesel::sqlite::SqliteConnection;
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
use xmpp_parsers::BareJid;

Expand Down
3 changes: 0 additions & 3 deletions src/terminus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1257,7 +1257,6 @@ where
T: fmt::Display + Hash + Eq + Ord,
{
fn insert(&mut self, item: T);
fn send_message(&self);
/// PageUp the window, return true if top is reached
fn page_up(&mut self) -> bool;
/// PageDown the window, return true if bottom is reached
Expand Down Expand Up @@ -1467,8 +1466,6 @@ where
true
}
}

fn send_message(&self) {}
}

impl<E, W, I> View<E, W> for BufferedWin<E, W, I>
Expand Down

0 comments on commit 0c844e0

Please sign in to comment.