Skip to content

Commit

Permalink
feat: Add support for stored password in config
Browse files Browse the repository at this point in the history
  • Loading branch information
paulfariello committed Mar 18, 2024
1 parent 0c844e0 commit 027a7e4
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 14 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.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ aes-gcm = "0.10.3"
base64 = "0.21.7"
sha1 = "0.10.6"
itertools = "0.12.1"
secrecy = "0.8.0"
secrecy = { version = "0.8.0", features = ["serde"] }

[dev-dependencies]
mockall = "^0.9"
Expand Down
5 changes: 5 additions & 0 deletions src/account.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use secrecy::Secret;
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Expand All @@ -7,6 +8,8 @@ use xmpp_parsers::FullJid;
/// Uniquely identify an account inside Aparté
pub type Account = FullJid;

pub type Password = Secret<String>;

fn false_() -> bool {
false
}
Expand All @@ -19,4 +22,6 @@ pub struct ConnectionInfo {
pub port: Option<u16>,
#[serde(default = "false_")]
pub autoconnect: bool,
#[serde(skip_serializing)]
pub password: Option<Password>,
}
29 changes: 22 additions & 7 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,16 +272,30 @@ macro_rules! build_subcommand_map(
($map:ident, { completion: (|$aparte:ident, $command:ident| $completion:block) $(, $($tail:tt)*)? }) => ();
);

#[macro_export]
macro_rules! parse_lookup_arg(
($aparte:ident, $command:ident, {}) => (None);
($aparte:ident, $command:ident, { lookup: (|$lookup_aparte:ident, $lookup_command:ident| $lookup:block) $(, $($tail:tt)*)? }) => (
(|$lookup_aparte: &mut Aparte, $lookup_command: &mut Command| $lookup)($aparte, &mut $command)
);
);

#[macro_export]
macro_rules! parse_command_args(
($aparte:ident, $command:ident, $index:ident, {}) => ();
($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 = Password::from_str(&$command.args[$index])?;
($aparte:ident, $command:ident, $index:ident, { $arg:ident: Password = $attrs:tt $(,)? }) => (
let $arg: Password = if $command.args.len() <= $index {
let $arg: Option<Password> = parse_lookup_arg!($aparte, $command, $attrs);
match $arg {
None => {
$aparte.schedule(Event::ReadPassword($command.clone()));
return Ok(())
},
Some($arg) => $arg,
}
} else {
Password::from_str(&$command.args[$index])?
};

$index += 1;
);
Expand Down Expand Up @@ -384,6 +398,7 @@ macro_rules! generate_sub_autocompletion(
#[macro_export]
macro_rules! generate_arg_autocompletion(
($autocompletions:ident, $type:ty, {}) => ();
($autocompletions:ident, $type:ty, { lookup: $subs:tt $(, $($tail:tt)*)? }) => ();
($autocompletions:ident, $type:ty, { children: $subs:tt $(, $($tail:tt)*)? }) => (
let mut sub = vec![];
generate_sub_autocompletion!(sub, $subs);
Expand Down
15 changes: 9 additions & 6 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use chrono::{DateTime, FixedOffset, Local as LocalTz};
use futures::sink::SinkExt;
use futures::stream::StreamExt;
use rand::Rng;
use secrecy::{ExposeSecret, Secret};
use secrecy::ExposeSecret;
use termion::event::Key;
use tokio::runtime::Runtime as TokioRuntime;
use tokio::signal::unix;
Expand All @@ -39,7 +39,7 @@ use xmpp_parsers::pubsub::event::PubSubEvent;
use xmpp_parsers::stanza_error::StanzaError;
use xmpp_parsers::{iq, presence, BareJid, Element, FullJid, Jid};

use crate::account::{Account, ConnectionInfo};
use crate::account::{Account, ConnectionInfo, Password};
use crate::async_iq::{IqFuture, PendingIqState};
use crate::color;
use crate::command::{Command, CommandParser};
Expand All @@ -52,7 +52,7 @@ use crate::mods;
use crate::storage::Storage;
use crate::{
command_def, generate_arg_autocompletion, generate_command_autocompletions, generate_help,
parse_command_args,
parse_command_args, parse_lookup_arg,
};
use crate::{contact, conversation};

Expand Down Expand Up @@ -373,8 +373,6 @@ impl Display for Mod {
}
}

pub type Password = Secret<String>;

pub struct Connection {
pub sink: mpsc::UnboundedSender<Element>,
pub account: FullJid,
Expand All @@ -400,7 +398,11 @@ Examples:
aparte.config.accounts.keys().cloned().collect()
})
},
password: Password
password: Password = {
lookup: (|aparte, _command| {
aparte.config.accounts.get(&account_name).map(|account| account.password.clone()).flatten()
})
},
},
|aparte, _command| {
let account = {
Expand All @@ -415,6 +417,7 @@ Examples:
server: None,
port: None,
autoconnect: false,
password: None,
}
} else {
anyhow::bail!("Unknown account or invalid jid {account_name}");
Expand Down

0 comments on commit 027a7e4

Please sign in to comment.