Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Exercise email verification, Auth codes, and account recovery

Manav Rathiff080fe57b6c
diff --git a/rust/apps/cli/tests/cli/account.rs b/rust/apps/cli/tests/cli/account.rs
index 82b2a8779f..0a8d8e55e1 100644
--- a/rust/apps/cli/tests/cli/account.rs
+++ b/rust/apps/cli/tests/cli/account.rs
@@ -485,20 +485,40 @@ async fn srp_attributes(
#[test]
fn creation_enrollment_and_login_across_processes() -> ente_test_support::TestResult {
use ente_test_support::{Museum, account_fixture::current_totp};
- Museum::run_async(|origin| async move {
+ let inbox = super::mail::MailInbox::new();
+ let port = inbox.port.to_string();
+ let env = [
+ ("ENTE_SMTP_HOST", "127.0.0.1"),
+ ("ENTE_SMTP_PORT", &port),
+ ("ENTE_SMTP_USERNAME", ""),
+ ("ENTE_SMTP_PASSWORD", ""),
+ ("ENTE_SMTP_ENCRYPTION", ""),
+ ];
+ Museum::run_with_env(&env, |museum| {
+ let origin = museum.endpoint();
let home = TestHome::new();
- let email = format!("cli-create-{}@example.org", uuid::Uuid::new_v4());
+ let email = format!("cli-create-{}@example.com", uuid::Uuid::new_v4());
home.json(&[
"account",
"request-otp",
"--email",
&email,
"--host",
- &origin,
+ origin,
"--app",
"auth",
]);
- let input = json!({"password": " secret πŸ™‚ ", "otp": "123456"});
+ let read_code = || {
+ let message = inbox.receive("Subject: Verification code:");
+ assert!(message.contains(&format!("RCPT TO:<{email}>")));
+ message
+ .lines()
+ .find_map(|line| line.strip_prefix("Subject: Verification code: "))
+ .unwrap()
+ .trim()
+ .to_owned()
+ };
+ let input = json!({"password": " secret πŸ™‚ ", "otp": read_code()});
let output = success(home.with_input(
&[
"account",
@@ -506,7 +526,7 @@ fn creation_enrollment_and_login_across_processes() -> ente_test_support::TestRe
"--email",
&email,
"--host",
- &origin,
+ origin,
"--app",
"auth",
"--input",
@@ -548,26 +568,56 @@ fn creation_enrollment_and_login_across_processes() -> ente_test_support::TestRe
json!({"totpEnabled": true})
);
assert!(failure(&home.run(&["account", "totp", "setup"])).contains("already enabled"));
- let second = TestHome::new();
- let login =
- json!({"password": " secret πŸ™‚ ", "otp": "123456", "totp": current_totp(secret)?});
- success(second.with_input(
+ success(home.with_input(
&[
- "account",
- "login",
- "--email",
- &email,
- "--account",
- "personal",
- "--app",
"auth",
- "--host",
- &origin,
- "--input",
+ "api",
+ "/users/email-mfa",
+ "--method",
+ "PUT",
+ "--body",
"-",
],
- &serde_json::to_vec(&login)?,
+ br#"{"isEnabled":true}"#,
));
+ let second = TestHome::new();
+ second.json(&[
+ "account",
+ "request-otp",
+ "--email",
+ &email,
+ "--host",
+ origin,
+ "--app",
+ "auth",
+ "--purpose",
+ "login",
+ ]);
+ let code = read_code();
+ let wrong_code = format!("{:06}", (code.parse::<u32>()? + 1) % 1_000_000);
+ let login = [
+ "account",
+ "login",
+ "--email",
+ &email,
+ "--account",
+ "personal",
+ "--app",
+ "auth",
+ "--host",
+ origin,
+ "--input",
+ "-",
+ ];
+ let mut input =
+ json!({"password": " secret πŸ™‚ ", "otp": wrong_code, "totp": current_totp(secret)?});
+ let error = failure(&second.with_input(&login, &serde_json::to_vec(&input)?));
+ assert!(
+ error.contains("Incorrect email verification code"),
+ "{error}"
+ );
+ input["otp"] = code.into();
+ success(second.with_input(&login, &serde_json::to_vec(&input)?));
assert_eq!(
home.json(&["account", "recovery-key"]),
second.json(&["account", "recovery-key", "--account", "personal"])
diff --git a/rust/apps/cli/tests/cli/admin_museum.rs b/rust/apps/cli/tests/cli/admin_museum.rs
index 296566bca8..6de868af4e 100644
--- a/rust/apps/cli/tests/cli/admin_museum.rs
+++ b/rust/apps/cli/tests/cli/admin_museum.rs
@@ -1,15 +1,3 @@
-use std::{
- io::{BufRead, BufReader},
- net::{TcpListener, TcpStream},
- sync::{
- Arc,
- atomic::{AtomicBool, Ordering},
- mpsc,
- },
- thread,
- time::Duration,
-};
-
use chrono::{Months, Utc};
use ente_accounts::{AccountsClient, AccountsClientConfig, TwoFactorSetup};
use ente_test_support::{Museum, TestResult, account_fixture::current_totp};
@@ -17,6 +5,7 @@ use sha2::{Digest, Sha256};
use uuid::Uuid;
use super::{
+ mail::MailInbox,
museum::{create_account, login},
*,
};
@@ -467,7 +456,7 @@ fn admin_email_reaches_smtp() -> TestResult {
home.json(&args),
json!({"to": "[email protected]", "accepted": true})
);
- let message = inbox.receive_test();
+ let message = inbox.receive("Subject: Test mail from Ente");
let sender = if override_sender {
} else {
@@ -551,87 +540,3 @@ async fn register_passkey(origin: &str, token: &str) {
"attestationObject": b64::encode_url_safe_no_padding(&attestation), "transports": ["internal"]}
}))).await;
}
-
-struct MailInbox {
- port: u16,
- messages: mpsc::Receiver<String>,
- stop: Arc<AtomicBool>,
- worker: Option<thread::JoinHandle<()>>,
-}
-
-impl MailInbox {
- fn new() -> Self {
- let listener = TcpListener::bind("127.0.0.1:0").unwrap();
- let port = listener.local_addr().unwrap().port();
- listener.set_nonblocking(true).unwrap();
- let (send, messages) = mpsc::channel();
- let stop = Arc::new(AtomicBool::new(false));
- let done = stop.clone();
- let worker = thread::spawn(move || {
- while !done.load(Ordering::Relaxed) {
- match listener.accept() {
- Ok((stream, _)) => receive_mail(stream, &send),
- Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => {
- thread::sleep(Duration::from_millis(10))
- }
- Err(error) => panic!("SMTP listener: {error}"),
- }
- }
- });
- Self {
- port,
- messages,
- stop,
- worker: Some(worker),
- }
- }
-
- fn receive_test(&self) -> String {
- loop {
- let message = self.messages.recv_timeout(Duration::from_secs(5)).unwrap();
- if message.contains("Subject: Test mail from Ente") {
- return message;
- }
- }
- }
-}
-
-impl Drop for MailInbox {
- fn drop(&mut self) {
- self.stop.store(true, Ordering::Relaxed);
- let _ = self.worker.take().unwrap().join();
- }
-}
-
-fn receive_mail(mut stream: TcpStream, messages: &mpsc::Sender<String>) {
- stream.set_nonblocking(false).unwrap();
- stream
- .set_read_timeout(Some(Duration::from_secs(5)))
- .unwrap();
- stream.write_all(b"220 localhost\r\n").unwrap();
- let mut reader = BufReader::new(stream.try_clone().unwrap());
- let mut message = String::new();
- loop {
- let mut line = String::new();
- if reader.read_line(&mut line).unwrap() == 0 {
- break;
- }
- message.push_str(&line);
- if line == "DATA\r\n" {
- stream.write_all(b"354 Send data\r\n").unwrap();
- loop {
- line.clear();
- assert_ne!(reader.read_line(&mut line).unwrap(), 0);
- if line == ".\r\n" {
- break;
- }
- message.push_str(&line);
- }
- messages.send(std::mem::take(&mut message)).unwrap();
- } else if line == "QUIT\r\n" {
- stream.write_all(b"221 Bye\r\n").unwrap();
- break;
- }
- stream.write_all(b"250 OK\r\n").unwrap();
- }
-}
diff --git a/rust/apps/cli/tests/cli/auth.rs b/rust/apps/cli/tests/cli/auth.rs
index 871b9c8207..233055fe2f 100644
--- a/rust/apps/cli/tests/cli/auth.rs
+++ b/rust/apps/cli/tests/cli/auth.rs
@@ -1,4 +1,4 @@
-use ente_core::crypto::secretbox;
+use ente_core::{crypto::secretbox, otp};
use mockito::{Matcher, Mock, Server};
use uuid::Uuid;
@@ -57,7 +57,12 @@ fn code_reads_refresh_select_and_render_without_a_backup() {
0,
vec![
entity(1, 10, &uri("GitHub:[email protected]"), &key),
- entity(2, 10, &uri("GitHub:[email protected]"), &key),
+ entity(
+ 2,
+ 10,
+ &json!("otpauth://totp/GitHub:[email protected]?secret=MZXW6YTB"),
+ &key,
+ ),
entity(3, 10, &json!(format!("{URI}#suffix")), &key),
entity(4, 10, &json!("otpauth://totp/Steam:alice?secret=MY"), &key),
entity(
@@ -79,7 +84,8 @@ fn code_reads_refresh_select_and_render_without_a_backup() {
initial.assert();
initial.remove();
let view = |query: &str| {
- home.json(&[
+ let before = chrono::Utc::now().timestamp();
+ let result = home.json(&[
"auth",
"code",
"view",
@@ -87,20 +93,27 @@ fn code_reads_refresh_select_and_render_without_a_backup() {
"--offline",
"--account",
"fixture",
- ])
+ ]);
+ let after = chrono::Utc::now().timestamp();
+ let expiry = chrono::DateTime::parse_from_rfc3339(result["validUntil"].as_str().unwrap())
+ .unwrap()
+ .timestamp();
+ assert_eq!(expiry % 30, 0);
+ assert!(expiry > before && expiry <= after + 30);
+ assert!(result["validUntil"].as_str().unwrap().ends_with('Z'));
+ (result, u64::try_from(expiry - 30).unwrap())
};
for query in [
"BOB gitHUB",
"GitHub ([email protected])",
&Uuid::from_u128(2).to_string(),
] {
- let result = view(query);
+ let (result, time) = view(query);
assert_eq!(result["id"], Uuid::from_u128(2).to_string());
- assert_eq!(result["otp"].as_str().unwrap().len(), 6);
- let expiry =
- chrono::DateTime::parse_from_rfc3339(result["validUntil"].as_str().unwrap()).unwrap();
- assert_eq!(expiry.timestamp() % 30, 0);
- assert!(result["validUntil"].as_str().unwrap().ends_with('Z'));
+ assert_eq!(
+ result["otp"],
+ otp::totp(b"fooba", time, 30, 6, otp::Algorithm::Sha1).unwrap()
+ );
}
let ambiguous = home.run(&["auth", "code", "view", "github", "--offline"]);
let error = failure(&ambiguous);
@@ -117,7 +130,8 @@ fn code_reads_refresh_select_and_render_without_a_backup() {
assert_eq!(all.as_array().unwrap().len(), 4);
assert_eq!(all[2]["type"], "steam");
assert_eq!(all[3]["name"], "Work (a+%2C#tag)");
- assert_eq!(view("steam")["otp"].as_str().unwrap().len(), 5);
+ let (steam, time) = view("steam");
+ assert_eq!(steam["otp"], otp::steam(b"f", time).unwrap());
assert!(
failure(&home.run(&["auth", "code", "view", "Work", "--offline"]))
.contains("HOTP code generation is not supported")
@@ -197,7 +211,7 @@ fn code_reads_refresh_select_and_render_without_a_backup() {
unavailable.assert();
key_request.assert();
drop(server);
- assert_eq!(view("bob")["name"], "Work ([email protected])");
+ assert_eq!(view("bob").0["name"], "Work ([email protected])");
}
#[test]
diff --git a/rust/apps/cli/tests/cli/mail.rs b/rust/apps/cli/tests/cli/mail.rs
new file mode 100644
index 0000000000..9c175a80f3
--- /dev/null
+++ b/rust/apps/cli/tests/cli/mail.rs
@@ -0,0 +1,95 @@
+use std::{
+ io::{BufRead, BufReader, Write},
+ net::{TcpListener, TcpStream},
+ sync::{
+ Arc,
+ atomic::{AtomicBool, Ordering},
+ mpsc,
+ },
+ thread,
+ time::Duration,
+};
+
+pub(super) struct MailInbox {
+ pub(super) port: u16,
+ messages: mpsc::Receiver<String>,
+ stop: Arc<AtomicBool>,
+ worker: Option<thread::JoinHandle<()>>,
+}
+
+impl MailInbox {
+ pub(super) fn new() -> Self {
+ let listener = TcpListener::bind("127.0.0.1:0").unwrap();
+ let port = listener.local_addr().unwrap().port();
+ listener.set_nonblocking(true).unwrap();
+ let (send, messages) = mpsc::channel();
+ let stop = Arc::new(AtomicBool::new(false));
+ let done = stop.clone();
+ let worker = thread::spawn(move || {
+ while !done.load(Ordering::Relaxed) {
+ match listener.accept() {
+ Ok((stream, _)) => receive_mail(stream, &send),
+ Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => {
+ thread::sleep(Duration::from_millis(10))
+ }
+ Err(error) => panic!("SMTP listener: {error}"),
+ }
+ }
+ });
+ Self {
+ port,
+ messages,
+ stop,
+ worker: Some(worker),
+ }
+ }
+
+ pub(super) fn receive(&self, subject: &str) -> String {
+ loop {
+ let message = self.messages.recv_timeout(Duration::from_secs(5)).unwrap();
+ if message.contains(subject) {
+ return message;
+ }
+ }
+ }
+}
+
+impl Drop for MailInbox {
+ fn drop(&mut self) {
+ self.stop.store(true, Ordering::Relaxed);
+ let _ = self.worker.take().unwrap().join();
+ }
+}
+
+fn receive_mail(mut stream: TcpStream, messages: &mpsc::Sender<String>) {
+ stream.set_nonblocking(false).unwrap();
+ stream
+ .set_read_timeout(Some(Duration::from_secs(5)))
+ .unwrap();
+ stream.write_all(b"220 localhost\r\n").unwrap();
+ let mut reader = BufReader::new(stream.try_clone().unwrap());
+ let mut message = String::new();
+ loop {
+ let mut line = String::new();
+ if reader.read_line(&mut line).unwrap() == 0 {
+ break;
+ }
+ message.push_str(&line);
+ if line == "DATA\r\n" {
+ stream.write_all(b"354 Send data\r\n").unwrap();
+ loop {
+ line.clear();
+ assert_ne!(reader.read_line(&mut line).unwrap(), 0);
+ if line == ".\r\n" {
+ break;
+ }
+ message.push_str(&line);
+ }
+ messages.send(std::mem::take(&mut message)).unwrap();
+ } else if line == "QUIT\r\n" {
+ stream.write_all(b"221 Bye\r\n").unwrap();
+ break;
+ }
+ stream.write_all(b"250 OK\r\n").unwrap();
+ }
+}
diff --git a/rust/apps/cli/tests/cli/main.rs b/rust/apps/cli/tests/cli/main.rs
index 018df1aad3..f00050be3e 100644
--- a/rust/apps/cli/tests/cli/main.rs
+++ b/rust/apps/cli/tests/cli/main.rs
@@ -1191,6 +1191,9 @@ mod export_sources;
mod files;
+#[cfg(feature = "museum")]
+mod mail;
+
#[cfg(feature = "museum")]
mod museum;
diff --git a/rust/crates/accounts/tests/accounts.rs b/rust/crates/accounts/tests/accounts.rs
index 3df0856883..28594af249 100644
--- a/rust/crates/accounts/tests/accounts.rs
+++ b/rust/crates/accounts/tests/accounts.rs
@@ -3,9 +3,12 @@
use ente_accounts::{
AccountsClient, AccountsClientConfig, TwoFactorSetup,
+ lifecycle::recover_two_factor,
login::{LoginFlow, LoginStep},
+ models::TwoFactorType,
signup::Signup,
};
+use ente_core::b64;
use ente_test_support::account_fixture::current_totp;
use ente_test_support::{HARDCODED_OTT, HARDCODED_OTT_EMAIL_SUFFIX, Museum, TestResult};
use uuid::Uuid;
@@ -60,6 +63,40 @@ async fn run(endpoint: String) -> TestResult {
assert_eq!(login.secrets.master_key, created.secrets.master_key);
assert_eq!(client.email().await.unwrap(), email);
assert!(client.get_two_factor_status().await.unwrap());
+ let recovery_client = accounts_client(endpoint);
+ recovery_client.send_otp(&email, "login").await?;
+ let pending = recovery_client
+ .verify_email(&email, HARDCODED_OTT, None)
+ .await?;
+ let session = pending.get_two_factor_session_id().unwrap();
+ let envelope = recovery_client
+ .get_two_factor_recovery(session, TwoFactorType::Totp)
+ .await?;
+ let recovered = recover_two_factor(
+ &recovery_client,
+ TwoFactorType::Totp,
+ session,
+ &envelope,
+ created.recovery_key.as_ref().unwrap(),
+ )
+ .await?;
+ assert_eq!(recovered.id, created.user_id);
+ let attributes = &recovered.key_attributes;
+ let kek = ente_accounts::auth::derive_kek(
+ &password,
+ &attributes.kek_salt,
+ attributes.mem_limit,
+ attributes.ops_limit,
+ )?;
+ let secrets =
+ ente_accounts::auth::decrypt_secrets(&kek, attributes, &recovered.encrypted_token)?;
+ recovery_client.set_auth_token(Some(b64::encode_url_safe(&secrets.token)));
+ assert_eq!(recovery_client.email().await?, email);
+ assert!(!recovery_client.get_two_factor_status().await?);
+ assert_eq!(
+ secrets.master_key.as_bytes(),
+ created.secrets.master_key.as_slice()
+ );
Ok(())
}