Skip to content

Commit

Permalink
feat(connect): Wrap password in secrecy crate
Browse files Browse the repository at this point in the history
  • Loading branch information
paulfariello committed Mar 18, 2024
1 parent ed13e0d commit 0dee9e9
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 20 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 6 additions & 3 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!(format!("{}", command.err().unwrap()), "Missing closing quote");
assert_eq!(
format!("{}", command.err().unwrap()),
"Missing closing quote"
);
}

#[test]
Expand Down
23 changes: 6 additions & 17 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use chrono::{DateTime, FixedOffset, Local as LocalTz};
use futures::sink::SinkExt;
use futures::stream::StreamExt;
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 +65,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 +371,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 +398,7 @@ Examples:
aparte.config.accounts.keys().cloned().collect()
})
},
password: Password<String>
password: Password
},
|aparte, _command| {
let account = {
Expand Down Expand Up @@ -1119,7 +1108,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 +1132,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

0 comments on commit 0dee9e9

Please sign in to comment.