aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorQuentin Dufour <quentin@deuxfleurs.fr>2023-12-27 18:33:06 +0100
committerQuentin Dufour <quentin@deuxfleurs.fr>2023-12-27 18:33:06 +0100
commitccc9b6abb66ebda0b91b4e21f8ec2fb2e87390f7 (patch)
tree985f9d136a981ec7cc43984c976c60e9295340d5 /src
parent7744625c18aff5990a792bb13a44b60d8c4d4fc5 (diff)
downloadaerogramme-ccc9b6abb66ebda0b91b4e21f8ec2fb2e87390f7.tar.gz
aerogramme-ccc9b6abb66ebda0b91b4e21f8ec2fb2e87390f7.zip
add a --dev mode
Diffstat (limited to 'src')
-rw-r--r--src/config.rs1
-rw-r--r--src/login/demo_provider.rs48
-rw-r--r--src/login/mod.rs1
-rw-r--r--src/main.rs26
-rw-r--r--src/server.rs3
5 files changed, 75 insertions, 4 deletions
diff --git a/src/config.rs b/src/config.rs
index 1438910..b9c1f09 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -26,6 +26,7 @@ pub struct ProviderConfig {
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(tag = "user_driver")]
pub enum UserManagement {
+ Demo,
Static(LoginStaticConfig),
Ldap(LoginLdapConfig),
}
diff --git a/src/login/demo_provider.rs b/src/login/demo_provider.rs
new file mode 100644
index 0000000..0efb37c
--- /dev/null
+++ b/src/login/demo_provider.rs
@@ -0,0 +1,48 @@
+use crate::login::*;
+use crate::storage::*;
+
+pub struct DemoLoginProvider{
+ keys: CryptoKeys,
+ in_memory_store: in_memory::MemDb,
+}
+
+impl DemoLoginProvider {
+ pub fn new() -> Self {
+ Self {
+ keys: CryptoKeys::init(),
+ in_memory_store: in_memory::MemDb::new(),
+ }
+ }
+}
+
+#[async_trait]
+impl LoginProvider for DemoLoginProvider {
+ async fn login(&self, username: &str, password: &str) -> Result<Credentials> {
+ tracing::debug!(user=%username, "login");
+
+ if username != "alice" {
+ bail!("user does not exist");
+ }
+
+ if password != "hunter2" {
+ bail!("wrong password");
+ }
+
+ let storage = self.in_memory_store.builder("alice").await;
+ let keys = self.keys.clone();
+
+ Ok(Credentials { storage, keys })
+ }
+
+ async fn public_login(&self, email: &str) -> Result<PublicCredentials> {
+ tracing::debug!(user=%email, "public_login");
+ if email != "alice@example.tld" {
+ bail!("invalid email address");
+ }
+
+ let storage = self.in_memory_store.builder("alice").await;
+ let public_key = self.keys.public.clone();
+
+ Ok(PublicCredentials { storage, public_key })
+ }
+}
diff --git a/src/login/mod.rs b/src/login/mod.rs
index 2926738..6f2ca31 100644
--- a/src/login/mod.rs
+++ b/src/login/mod.rs
@@ -1,5 +1,6 @@
pub mod ldap_provider;
pub mod static_provider;
+pub mod demo_provider;
use base64::Engine;
use std::sync::Arc;
diff --git a/src/main.rs b/src/main.rs
index 3221c2e..3baa8e2 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -29,7 +29,12 @@ struct Args {
#[clap(subcommand)]
command: Command,
+ /// A special mode dedicated to developers, NOT INTENDED FOR PRODUCTION
+ #[clap(long)]
+ dev: bool,
+
#[clap(short, long, env = "CONFIG_FILE", default_value = "aerogramme.toml")]
+ /// Path to the main Aerogramme configuration file
config_file: PathBuf,
}
@@ -158,7 +163,22 @@ async fn main() -> Result<()> {
tracing_subscriber::fmt::init();
let args = Args::parse();
- let any_config = read_config(args.config_file)?;
+ let any_config = if args.dev {
+ use std::net::*;
+ AnyConfig::Provider(ProviderConfig {
+ pid: None,
+ imap: ImapConfig {
+ bind_addr: SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)), 1143),
+ },
+ lmtp: LmtpConfig {
+ bind_addr: SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)), 1025),
+ hostname: "example.tld".to_string(),
+ },
+ users: UserManagement::Demo,
+ })
+ } else {
+ read_config(args.config_file)?
+ };
match (&args.command, any_config) {
(Command::Companion(subcommand), AnyConfig::Companion(config)) => match subcommand {
@@ -184,8 +204,8 @@ async fn main() -> Result<()> {
ProviderCommand::Account(cmd) => {
let user_file = match config.users {
UserManagement::Static(conf) => conf.user_list,
- UserManagement::Ldap(_) => {
- panic!("LDAP account management is not supported from Aerogramme.")
+ _ => {
+ panic!("Only static account management is supported from Aerogramme.")
}
};
account_management(&args.command, cmd, user_file)?;
diff --git a/src/server.rs b/src/server.rs
index 28e0b27..1b8677b 100644
--- a/src/server.rs
+++ b/src/server.rs
@@ -11,7 +11,7 @@ use crate::config::*;
use crate::imap;
use crate::lmtp::*;
use crate::login::ArcLoginProvider;
-use crate::login::{ldap_provider::*, static_provider::*};
+use crate::login::{ldap_provider::*, static_provider::*, demo_provider::*};
pub struct Server {
lmtp_server: Option<Arc<LmtpServer>>,
@@ -36,6 +36,7 @@ impl Server {
pub async fn from_provider_config(config: ProviderConfig) -> Result<Self> {
tracing::info!("Init as provider");
let login: ArcLoginProvider = match config.users {
+ UserManagement::Demo => Arc::new(DemoLoginProvider::new()),
UserManagement::Static(x) => Arc::new(StaticLoginProvider::new(x).await?),
UserManagement::Ldap(x) => Arc::new(LdapLoginProvider::new(x)?),
};