Skip to content

Native SASL support #243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 128 additions & 5 deletions Cargo.lock

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

11 changes: 11 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ bufstream = "0.1.3"
imap-proto = "0.16.1"
nom = { version = "7.1.0", default-features = false }
base64 = "0.21"
rsasl = { version = "2.0.0", default-features = false, features = ["std", "provider_base64"], optional = true }
chrono = { version = "0.4", default-features = false, features = ["std"]}
lazy_static = "1.4"
ouroboros = "0.15.0"
Expand All @@ -42,6 +43,8 @@ failure = "0.1.8"
mime = "0.3.4"
openssl = "0.10.35"

rsasl = { version = "2.0.0", default-features = false, features = ["config_builder"] }

[[example]]
name = "basic"
required-features = ["default"]
Expand Down Expand Up @@ -69,3 +72,11 @@ required-features = ["default"]
[[test]]
name = "imap_integration"
required-features = ["default"]

[[example]]
name = "rustls_sasl"
required-features = ["rustls-tls", "rsasl", "rsasl/registry_static"]

[[example]]
name = "rustls_sasl_gss"
required-features = ["rustls-tls", "rsasl", "rsasl/registry_static", "rsasl/gssapi"]
85 changes: 85 additions & 0 deletions examples/rustls_sasl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
extern crate imap;

use std::{env, error::Error};
use rsasl::callback::{Context, Request, SessionData};
use rsasl::config::SASLConfig;
use rsasl::prelude::{Mechname, SessionError};
use rsasl::property::{AuthId, AuthzId, Hostname, Password};

fn main() -> Result<(), Box<dyn Error>> {
// Read config from environment or .env file
let host = env::var("HOST").expect("missing envvar host");
let user = env::var("MAILUSER").ok();
let password = env::var("PASSWORD").ok();
let port = 993;

if let Some(email) = fetch_inbox_top(host, user, password, port)? {
println!("{}", &email);
}

Ok(())
}

struct MyCb {
authid: Option<String>,
authzid: Option<String>,
passwd: Option<String>,
host: String,
}
impl rsasl::callback::SessionCallback for MyCb {
fn callback(&self, _session_data: &SessionData, _context: &Context, request: &mut Request) -> Result<(), SessionError> {
if let Some(authid) = self.authid.as_deref() { request.satisfy::<AuthId>(authid)?; }
if let Some(authzid) = self.authzid.as_deref() { request.satisfy::<AuthzId>(authzid)?; }
if let Some(passwd) = self.passwd.as_deref() { request.satisfy::<Password>(passwd.as_bytes())?; }
if let Some(authid) = self.authid.as_ref() { request.satisfy::<AuthId>(authid)?; }
request.satisfy::<Hostname>(&self.host)?;
Ok(())
}
}

fn fetch_inbox_top(
host: String,
user: Option<String>,
password: Option<String>,
port: u16,
) -> Result<Option<String>, Box<dyn Error>> {
let client = imap::ClientBuilder::new(&host, port).rustls()?;

let cb = MyCb {
authid: user,
authzid: None,
passwd: password,
host,
};
let saslconfig = SASLConfig::builder().with_defaults().with_callback(cb)?;

println!("SASL configuration options — enable features like 'rsasl/plain' or 'rsasl/sha2' to add available mechanisms:");
println!("{saslconfig:?}");

// the client we have here is unauthenticated.
// to do anything useful with the e-mails, we need to log in
let mut imap_session = client.sasl_auth(saslconfig).map_err(|e| e.0)?;

// we want to fetch the first email in the INBOX mailbox
imap_session.select("INBOX")?;

// fetch message number 1 in this mailbox, along with its RFC822 field.
// RFC 822 dictates the format of the body of e-mails
let messages = imap_session.fetch("1", "RFC822")?;
let message = if let Some(m) = messages.iter().next() {
m
} else {
return Ok(None);
};

// extract the message's body
let body = message.body().expect("message did not have a body!");
let body = std::str::from_utf8(body)
.expect("message was not valid utf-8")
.to_string();

// be nice to the server and log out
imap_session.logout()?;

Ok(Some(body))
}
Loading