set up imap oauth2 config

This commit is contained in:
Clément DOUIN
2023-05-03 16:54:39 +02:00
parent e271ca4293
commit 21f67bc7f5
9 changed files with 1244 additions and 61 deletions
+46 -3
View File
@@ -1,6 +1,7 @@
use pimalaya_email::{
folder::sync::Strategy as SyncFoldersStrategy, EmailHooks, EmailSender, EmailTextPlainFormat,
MaildirConfig, SendmailConfig, SmtpConfig,
ImapAuthConfig, MaildirConfig, OAuth2Config, OAuth2Method, OAuth2Scopes, SendmailConfig,
SmtpConfig,
};
use serde::{Deserialize, Serialize};
use std::{collections::HashSet, path::PathBuf};
@@ -46,8 +47,8 @@ pub struct ImapConfigDef {
pub insecure: Option<bool>,
#[serde(rename = "imap-login")]
pub login: String,
#[serde(rename = "imap-passwd-cmd")]
pub passwd_cmd: String,
#[serde(flatten, with = "ImapAuthConfigDef")]
pub auth: ImapAuthConfig,
#[serde(rename = "imap-notify-cmd")]
pub notify_cmd: Option<String>,
#[serde(rename = "imap-notify-query")]
@@ -56,6 +57,48 @@ pub struct ImapConfigDef {
pub watch_cmds: Option<Vec<String>>,
}
#[derive(Clone, Debug, Default, Eq, PartialEq, Deserialize, Serialize)]
#[serde(remote = "ImapAuthConfig", rename_all = "kebab-case")]
pub enum ImapAuthConfigDef {
#[default]
None,
#[serde(rename = "imap-passwd")]
Passwd(String),
#[serde(rename = "imap-passwd-cmd")]
PasswdCmd(String),
#[serde(rename = "imap-oauth2", with = "OAuth2ConfigDef")]
OAuth2(OAuth2Config),
}
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
#[serde(remote = "OAuth2Config", rename_all = "kebab-case")]
pub struct OAuth2ConfigDef {
#[serde(with = "OAuth2MethodDef")]
pub method: OAuth2Method,
pub client_id: String,
pub client_secret: String,
pub auth_url: String,
pub token_url: String,
#[serde(flatten, with = "OAuth2ScopesDef")]
pub scopes: OAuth2Scopes,
#[serde(default)]
pub pkce: bool,
}
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
#[serde(remote = "OAuth2Method", rename_all = "lowercase")]
pub enum OAuth2MethodDef {
XOAuth2,
OAuthBearer,
}
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
#[serde(remote = "OAuth2Scopes", rename_all = "kebab-case")]
pub enum OAuth2ScopesDef {
Scope(String),
Scopes(Vec<String>),
}
#[derive(Clone, Debug, Default, Eq, PartialEq, Deserialize, Serialize)]
#[serde(remote = "MaildirConfig", rename_all = "kebab-case")]
pub struct MaildirConfigDef {
+5 -4
View File
@@ -46,10 +46,11 @@ pub(crate) fn configure(base: DeserializedBaseAccountConfig) -> Result<Deseriali
.default(base.email.clone())
.interact()?;
backend.passwd_cmd = Input::with_theme(&*THEME)
.with_prompt("What shell command should we run to get your password?")
.default(format!("pass show {}", &base.email))
.interact()?;
// FIXME: add all variants: password, password command and oauth2
// backend.passwd_cmd = Input::with_theme(&*THEME)
// .with_prompt("What shell command should we run to get your password?")
// .default(format!("pass show {}", &base.email))
// .interact()?;
Ok(DeserializedAccountConfig::Imap(
DeserializedImapAccountConfig { base, backend },
+9
View File
@@ -11,6 +11,7 @@ use crate::{folder, ui::table};
const ARG_ACCOUNT: &str = "account";
const ARG_DRY_RUN: &str = "dry-run";
const CMD_ACCOUNTS: &str = "accounts";
const CMD_CONFIGURE: &str = "configure";
const CMD_LIST: &str = "list";
const CMD_SYNC: &str = "sync";
@@ -23,6 +24,8 @@ pub enum Cmd {
List(table::args::MaxTableWidth),
/// Represents the sync account command.
Sync(Option<SyncFoldersStrategy>, DryRun),
/// Configure the current selected account.
Configure,
}
/// Represents the account command matcher.
@@ -51,6 +54,9 @@ pub fn matches(m: &ArgMatches) -> Result<Option<Cmd>> {
info!("list accounts subcommand matched");
let max_table_width = table::args::parse_max_width(m);
Some(Cmd::List(max_table_width))
} else if let Some(_) = m.subcommand_matches(CMD_CONFIGURE) {
info!("configure account subcommand matched");
Some(Cmd::Configure)
} else {
info!("no account subcommand matched, falling back to subcommand list");
Some(Cmd::List(None))
@@ -80,6 +86,9 @@ pub fn subcmd() -> Command {
"Synchronize all folders except the given ones",
))
.arg(dry_run()),
Command::new(CMD_CONFIGURE)
.about("Configure the current selected account")
.aliases(["config", "conf", "cfg"]),
])
}
+10 -2
View File
@@ -6,8 +6,8 @@ use anyhow::Result;
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
use log::{info, trace};
use pimalaya_email::{
folder::sync::Strategy as SyncFoldersStrategy, AccountConfig, Backend, BackendSyncBuilder,
BackendSyncProgressEvent,
folder::sync::Strategy as SyncFoldersStrategy, AccountConfig, Backend, BackendConfig,
BackendSyncBuilder, BackendSyncProgressEvent,
};
use crate::{
@@ -16,6 +16,14 @@ use crate::{
Accounts,
};
/// Configure the current selected account
pub fn configure(account_config: &AccountConfig, backend_config: &BackendConfig) -> Result<()> {
info!("entering the configure account handler");
backend_config.configure(&account_config.name)?;
println!("Account {} configured!", account_config.name);
Ok(())
}
/// Lists all accounts.
pub fn list<'a, P: Printer>(
max_width: Option<usize>,
+3
View File
@@ -137,6 +137,9 @@ fn main() -> Result<()> {
backend.close()?;
return Ok(());
}
Some(account::args::Cmd::Configure) => {
return account::handlers::configure(&account_config, &backend_config);
}
_ => (),
}