mirror of
https://github.com/ArcaneChat/core.git
synced 2026-07-03 14:05:33 +02:00
Add IP and parameter support to dcaccount URI scheme
Co-authored-by: adbenitez <24558636+adbenitez@users.noreply.github.com>
This commit is contained in:
@@ -656,6 +656,8 @@ async fn decode_ideltachat(context: &Context, prefix: &str, qr: &str) -> Result<
|
||||
/// scheme: `DCACCOUNT:example.org`
|
||||
/// or `DCACCOUNT:https://example.org/new`
|
||||
/// or `DCACCOUNT:https://example.org/new_email?t=1w_7wDjgjelxeX884x96v3`
|
||||
/// or `DCACCOUNT:example.org?p=password&v=1&ih=imap.example.org&ip=993&...`
|
||||
/// or `DCACCOUNT:192.168.1.1?p=password&v=1&ih=192.168.1.1&ip=993&...`
|
||||
fn decode_account(qr: &str) -> Result<Qr> {
|
||||
let payload = qr
|
||||
.get(DCACCOUNT_SCHEME.len()..)
|
||||
@@ -673,9 +675,17 @@ fn decode_account(qr: &str) -> Result<Qr> {
|
||||
bail!("Bad scheme for account URL: {:?}.", url.scheme());
|
||||
}
|
||||
} else {
|
||||
Ok(Qr::Account {
|
||||
domain: payload.to_string(),
|
||||
})
|
||||
// Check if the payload contains query parameters
|
||||
// Format: domain?p=password&v=1&...
|
||||
if let Some((domain_or_ip, query)) = payload.split_once('?') {
|
||||
// Parse query parameters using dclogin scheme parsing logic
|
||||
dclogin_scheme::decode_account_with_params(domain_or_ip, query)
|
||||
} else {
|
||||
// Simple domain or IP without parameters
|
||||
Ok(Qr::Account {
|
||||
domain: payload.to_string(),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,8 @@ use std::collections::HashMap;
|
||||
use anyhow::{Context as _, Result, bail};
|
||||
|
||||
use deltachat_contact_tools::may_be_valid_addr;
|
||||
use rand::TryRngCore as _;
|
||||
use rand::distr::{Alphanumeric, SampleString};
|
||||
|
||||
use super::{DCLOGIN_SCHEME, Qr};
|
||||
use crate::login_param::{EnteredCertificateChecks, EnteredLoginParam, EnteredServerLoginParam};
|
||||
@@ -200,6 +202,62 @@ pub(crate) fn login_param_from_login_qr(
|
||||
}
|
||||
}
|
||||
|
||||
/// Decodes a DCACCOUNT QR code with query parameters.
|
||||
/// scheme: `DCACCOUNT:example.org?p=password&v=1[&options]`
|
||||
/// or `DCACCOUNT:192.168.1.1?p=password&v=1[&options]`
|
||||
///
|
||||
/// This generates a random email address using the provided domain/IP
|
||||
/// and parses the query parameters similarly to dclogin scheme.
|
||||
pub(super) fn decode_account_with_params(domain_or_ip: &str, query: &str) -> Result<Qr> {
|
||||
// Create a fake URL to parse query parameters using the url crate
|
||||
let fake_url = format!("dcaccount://{}?{}", domain_or_ip, query);
|
||||
let url = url::Url::parse(&fake_url)
|
||||
.context("Failed to parse DCACCOUNT with parameters")?;
|
||||
|
||||
// Parse query parameters
|
||||
let parameter_map: HashMap<String, String> = url.query_pairs()
|
||||
.map(|(key, value)| (key.into_owned(), value.into_owned()))
|
||||
.collect();
|
||||
|
||||
// Parse options similar to dclogin
|
||||
let options: LoginOptions = match parameter_map.get("v").map(|i| i.parse::<u32>()) {
|
||||
Some(Ok(1)) => LoginOptions::V1 {
|
||||
mail_pw: parameter_map
|
||||
.get("p")
|
||||
.map(|s| s.to_owned())
|
||||
.context("password missing")?,
|
||||
imap_host: parameter_map.get("ih").map(|s| s.to_owned()),
|
||||
imap_port: parse_port(parameter_map.get("ip"))
|
||||
.context("could not parse imap port")?,
|
||||
imap_username: parameter_map.get("iu").map(|s| s.to_owned()),
|
||||
imap_password: parameter_map.get("ipw").map(|s| s.to_owned()),
|
||||
imap_security: parse_socket_security(parameter_map.get("is"))?,
|
||||
smtp_host: parameter_map.get("sh").map(|s| s.to_owned()),
|
||||
smtp_port: parse_port(parameter_map.get("sp"))
|
||||
.context("could not parse smtp port")?,
|
||||
smtp_username: parameter_map.get("su").map(|s| s.to_owned()),
|
||||
smtp_password: parameter_map.get("spw").map(|s| s.to_owned()),
|
||||
smtp_security: parse_socket_security(parameter_map.get("ss"))?,
|
||||
certificate_checks: parse_certificate_checks(parameter_map.get("ic"))?,
|
||||
},
|
||||
Some(Ok(v)) => LoginOptions::UnsuportedVersion(v),
|
||||
Some(Err(_)) => bail!("version could not be parsed as number"),
|
||||
None => bail!("invalid DCACCOUNT payload: version missing"),
|
||||
};
|
||||
|
||||
// Generate a random email address using the domain/IP
|
||||
// Use the provided username if available, otherwise generate a random one
|
||||
let address = if let Some(username) = parameter_map.get("u") {
|
||||
format!("{}@{}", username, domain_or_ip)
|
||||
} else {
|
||||
let rng = &mut rand::rngs::OsRng.unwrap_err();
|
||||
let username = Alphanumeric.sample_string(rng, 9);
|
||||
format!("{}@{}", username, domain_or_ip)
|
||||
};
|
||||
|
||||
Ok(Qr::Login { address, options })
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use anyhow::bail;
|
||||
|
||||
@@ -711,6 +711,135 @@ async fn test_decode_account() -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn test_decode_account_with_ip() -> Result<()> {
|
||||
let ctx = TestContext::new().await;
|
||||
|
||||
// Test IP address without parameters
|
||||
let qr = check_qr(&ctx.ctx, "dcaccount:192.168.1.1").await?;
|
||||
assert_eq!(
|
||||
qr,
|
||||
Qr::Account {
|
||||
domain: "192.168.1.1".to_string()
|
||||
}
|
||||
);
|
||||
|
||||
// Test another IP format
|
||||
let qr = check_qr(&ctx.ctx, "DCACCOUNT:10.0.0.1").await?;
|
||||
assert_eq!(
|
||||
qr,
|
||||
Qr::Account {
|
||||
domain: "10.0.0.1".to_string()
|
||||
}
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn test_decode_account_with_params() -> Result<()> {
|
||||
// Test dcaccount with query parameters (domain)
|
||||
let ctx = TestContext::new().await;
|
||||
let qr = check_qr(
|
||||
&ctx.ctx,
|
||||
"dcaccount:example.org?p=secret&v=1&ih=imap.example.org&ip=993&is=ssl",
|
||||
)
|
||||
.await?;
|
||||
|
||||
if let Qr::Login { address, options } = qr {
|
||||
// Email address should have been generated with the domain
|
||||
assert!(address.ends_with("@example.org"));
|
||||
|
||||
if let dclogin_scheme::LoginOptions::V1 {
|
||||
mail_pw,
|
||||
imap_host,
|
||||
imap_port,
|
||||
imap_security,
|
||||
..
|
||||
} = options
|
||||
{
|
||||
assert_eq!(mail_pw, "secret");
|
||||
assert_eq!(imap_host, Some("imap.example.org".to_string()));
|
||||
assert_eq!(imap_port, Some(993));
|
||||
assert_eq!(imap_security, Some(Socket::Ssl));
|
||||
} else {
|
||||
bail!("Expected LoginOptions::V1");
|
||||
}
|
||||
} else {
|
||||
bail!("Expected Qr::Login, got {:?}", qr);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn test_decode_account_with_params_ip() -> Result<()> {
|
||||
// Test dcaccount with query parameters (IP address)
|
||||
let ctx = TestContext::new().await;
|
||||
let qr = check_qr(
|
||||
&ctx.ctx,
|
||||
"DCACCOUNT:192.168.1.1?p=mypassword&v=1&ih=192.168.1.1&ip=143&is=starttls&sh=192.168.1.1&sp=587&ss=starttls",
|
||||
)
|
||||
.await?;
|
||||
|
||||
if let Qr::Login { address, options } = qr {
|
||||
// Email address should have been generated with the IP
|
||||
assert!(address.ends_with("@192.168.1.1"));
|
||||
|
||||
if let dclogin_scheme::LoginOptions::V1 {
|
||||
mail_pw,
|
||||
imap_host,
|
||||
imap_port,
|
||||
imap_security,
|
||||
smtp_host,
|
||||
smtp_port,
|
||||
smtp_security,
|
||||
..
|
||||
} = options
|
||||
{
|
||||
assert_eq!(mail_pw, "mypassword");
|
||||
assert_eq!(imap_host, Some("192.168.1.1".to_string()));
|
||||
assert_eq!(imap_port, Some(143));
|
||||
assert_eq!(imap_security, Some(Socket::Starttls));
|
||||
assert_eq!(smtp_host, Some("192.168.1.1".to_string()));
|
||||
assert_eq!(smtp_port, Some(587));
|
||||
assert_eq!(smtp_security, Some(Socket::Starttls));
|
||||
} else {
|
||||
bail!("Expected LoginOptions::V1");
|
||||
}
|
||||
} else {
|
||||
bail!("Expected Qr::Login, got {:?}", qr);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn test_decode_account_with_custom_username() -> Result<()> {
|
||||
// Test dcaccount with custom username parameter
|
||||
let ctx = TestContext::new().await;
|
||||
let qr = check_qr(
|
||||
&ctx.ctx,
|
||||
"dcaccount:example.org?p=secret&v=1&u=alice",
|
||||
)
|
||||
.await?;
|
||||
|
||||
if let Qr::Login { address, options } = qr {
|
||||
// Email address should use the custom username
|
||||
assert_eq!(address, "alice@example.org");
|
||||
|
||||
if let dclogin_scheme::LoginOptions::V1 { mail_pw, .. } = options {
|
||||
assert_eq!(mail_pw, "secret");
|
||||
} else {
|
||||
bail!("Expected LoginOptions::V1");
|
||||
}
|
||||
} else {
|
||||
bail!("Expected Qr::Login, got {:?}", qr);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn test_decode_tg_socks_proxy() -> Result<()> {
|
||||
let t = TestContext::new().await;
|
||||
|
||||
Reference in New Issue
Block a user