Skip to content

Commit

Permalink
refactor imap oauth2 and password config using sub crates from lib
Browse files Browse the repository at this point in the history
  • Loading branch information
soywod committed May 6, 2023
1 parent 9dfdebb commit b478c54
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 32 deletions.
35 changes: 32 additions & 3 deletions Cargo.lock

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

8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,19 @@ clap = "4.0"
clap_complete = "4.0"
clap_mangen = "0.2"
console = "0.15.2"
dirs = "4.0.0"
dialoguer = "0.10.2"
dirs = "4.0.0"
email_address = "0.2.4"
env_logger = "0.8"
erased-serde = "0.3"
pimalaya-email = { git = "https://git.sr.ht/~soywod/pimalaya" }
indicatif = "0.17"
log = "0.4"
once_cell = "1.16.0"
# pimalaya-email = { git = "https://git.sr.ht/~soywod/pimalaya" }
pimalaya-email = { path = "/home/soywod/sourcehut/pimalaya/email" }
pimalaya-keyring = { path = "/home/soywod/sourcehut/pimalaya/keyring" }
pimalaya-process = { path = "/home/soywod/sourcehut/pimalaya/process" }
pimalaya-secret = { path = "/home/soywod/sourcehut/pimalaya/secret" }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
shellexpand = "2.1"
Expand Down
88 changes: 73 additions & 15 deletions src/config/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use pimalaya_email::{
folder::sync::Strategy as SyncFoldersStrategy, EmailHooks, EmailSender, EmailTextPlainFormat,
ImapAuthConfig, MaildirConfig, OAuth2ClientSecret, OAuth2Config, OAuth2Method, OAuth2Scopes,
SendmailConfig, SmtpConfig,
ImapAuthConfig, MaildirConfig, OAuth2Config, OAuth2Method, OAuth2Scopes, SendmailConfig,
SmtpConfig,
};
use pimalaya_keyring::Entry;
use pimalaya_process::Cmd;
use pimalaya_secret::Secret;
use serde::{Deserialize, Serialize};
use std::{collections::HashSet, path::PathBuf};

Expand All @@ -12,6 +15,14 @@ use pimalaya_email::ImapConfig;
#[cfg(feature = "notmuch-backend")]
use pimalaya_email::NotmuchConfig;

#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
#[serde(remote = "Cmd", from = "String")]
pub struct CmdDef;

#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
#[serde(remote = "Entry", from = "String")]
pub struct EntryDef;

#[derive(Clone, Debug, Default, Eq, PartialEq, Deserialize, Serialize)]
#[serde(remote = "SmtpConfig")]
struct SmtpConfigDef {
Expand Down Expand Up @@ -47,7 +58,7 @@ pub struct ImapConfigDef {
pub insecure: Option<bool>,
#[serde(rename = "imap-login")]
pub login: String,
#[serde(rename = "imap-auth", with = "ImapAuthConfigDef")]
#[serde(flatten, with = "ImapAuthConfigDef")]
pub auth: ImapAuthConfig,
#[serde(rename = "imap-notify-cmd")]
pub notify_cmd: Option<String>,
Expand All @@ -58,51 +69,98 @@ pub struct ImapConfigDef {
}

#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
#[serde(remote = "ImapAuthConfig", rename_all = "kebab-case")]
#[serde(remote = "ImapAuthConfig", tag = "imap-auth", rename_all = "lowercase")]
pub enum ImapAuthConfigDef {
#[serde(skip)]
None,
RawPasswd(String),
PasswdCmd(String),
#[serde(with = "OAuth2ConfigDef", rename = "oauth2")]
#[serde(with = "PasswdDef")]
Passwd(Secret),
#[serde(with = "OAuth2ConfigDef")]
OAuth2(OAuth2Config),
}

#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
#[serde(remote = "Secret", rename_all = "kebab-case")]
pub enum PasswdDef {
#[serde(rename = "imap-passwd")]
Raw(String),
#[serde(rename = "imap-passwd-cmd", with = "CmdDef")]
Cmd(Cmd),
#[serde(rename = "imap-passwd-keyring", with = "EntryDef")]
Keyring(Entry),
}

#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
#[serde(remote = "OAuth2Config", rename_all = "kebab-case")]
pub struct OAuth2ConfigDef {
#[serde(with = "OAuth2MethodDef")]
#[serde(rename = "imap-oauth2-method", with = "OAuth2MethodDef")]
pub method: OAuth2Method,
#[serde(rename = "imap-oauth2-client-id")]
pub client_id: String,
#[serde(with = "OAuth2ClientSecretDef")]
pub client_secret: OAuth2ClientSecret,
#[serde(flatten, with = "ClientSecretDef")]
pub client_secret: Secret,
#[serde(rename = "imap-oauth2-auth-url")]
pub auth_url: String,
#[serde(rename = "imap-oauth2-token-url")]
pub token_url: String,
#[serde(flatten, with = "AccessTokenDef")]
pub access_token: Secret,
#[serde(flatten, with = "RefreshTokenDef")]
pub refresh_token: Secret,
#[serde(flatten, with = "OAuth2ScopesDef")]
pub scopes: OAuth2Scopes,
#[serde(default)]
pub pkce: bool,
}

#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
#[serde(remote = "OAuth2ClientSecret", rename_all = "kebab-case")]
pub enum OAuth2ClientSecretDef {
#[serde(remote = "Secret", rename_all = "kebab-case")]
pub enum ClientSecretDef {
#[serde(rename = "imap-oauth2-client-secret")]
Raw(String),
#[serde(rename = "imap-oauth2-client-secret-cmd", with = "CmdDef")]
Cmd(Cmd),
#[serde(rename = "imap-oauth2-client-secret-keyring", with = "EntryDef")]
Keyring(Entry),
}

#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
#[serde(remote = "Secret", rename_all = "kebab-case")]
pub enum AccessTokenDef {
#[serde(rename = "imap-oauth2-access-token")]
Raw(String),
#[serde(rename = "imap-oauth2-access-token-cmd", with = "CmdDef")]
Cmd(Cmd),
#[serde(rename = "imap-oauth2-access-token-keyring", with = "EntryDef")]
Keyring(Entry),
}

#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
#[serde(remote = "Secret", rename_all = "kebab-case")]
pub enum RefreshTokenDef {
#[serde(rename = "imap-oauth2-refresh-token")]
Raw(String),
Cmd(String),
Keyring,
#[serde(rename = "imap-oauth2-refresh-token-cmd", with = "CmdDef")]
Cmd(Cmd),
#[serde(rename = "imap-oauth2-refresh-token-keyring", with = "EntryDef")]
Keyring(Entry),
}

#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
#[serde(remote = "OAuth2Method", rename_all = "lowercase")]
#[serde(remote = "OAuth2Method")]
pub enum OAuth2MethodDef {
#[serde(rename = "xoauth2", alias = "XOAUTH2")]
XOAuth2,
#[serde(rename = "oauthbearer", alias = "OAUTHBEARER")]
OAuthBearer,
}

#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
#[serde(remote = "OAuth2Scopes", rename_all = "kebab-case")]
pub enum OAuth2ScopesDef {
#[serde(rename = "imap-oauth2-scope")]
Scope(String),
#[serde(rename = "imap-oauth2-scopes")]
Scopes(Vec<String>),
}

Expand Down
16 changes: 5 additions & 11 deletions src/domain/account/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,21 @@ use crate::{
};

/// Configure the current selected account
pub fn configure(
account_config: &AccountConfig,
backend_config: &BackendConfig,
reset: bool,
) -> Result<()> {
pub fn configure(backend_config: &BackendConfig, reset: bool) -> Result<()> {
info!("entering the configure account handler");
match backend_config {
BackendConfig::None => (),
BackendConfig::Maildir(_) => (),
#[cfg(feature = "imap-backend")]
BackendConfig::Imap(imap_config) => {
imap_config.auth.configure(
&account_config.name,
reset,
configure_oauth2_client_secret,
)?;
imap_config
.auth
.configure(reset, configure_oauth2_client_secret)?;
}
#[cfg(feature = "notmuch-backend")]
BackendConfig::Notmuch(config) => (),
};
println!("Account {} configured!", account_config.name);
println!("Account successfully configured!");
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ fn main() -> Result<()> {
return Ok(());
}
Some(account::args::Cmd::Configure(reset)) => {
return account::handlers::configure(&account_config, &backend_config, reset);
return account::handlers::configure(&backend_config, reset);
}
_ => (),
}
Expand Down

0 comments on commit b478c54

Please sign in to comment.