π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Keep authenticated client construction in accounts
Neeraj Gupta042208e22a7f
diff --git a/rust/crates/accounts/src/client.rs b/rust/crates/accounts/src/client.rsindex 26997f356c..1de64cc55c 100644--- a/rust/crates/accounts/src/client.rs+++ b/rust/crates/accounts/src/client.rs@@ -30,25 +30,26 @@ fn require_srp_m2(auth_response: &AuthResponse) -> Result<&str> {pub struct AccountsClient {pub(crate) api: Api,- client_package: String,+ config: AccountsClientConfig,}impl AccountsClient {pub fn new(config: AccountsClientConfig) -> Result<Self> {+ Ok(Self::with_http(Http::new()?, config))+ }++ fn with_http(http: Http, mut config: AccountsClientConfig) -> Self {let api = Api::new(- Http::new()?,+ http,ApiConfig {- origin: config.origin,+ origin: config.origin.clone(),client_package: Some(config.client_package.clone()),- client_version: config.client_version,- user_agent: config.user_agent,- auth: config.auth_token.map(Auth::User),+ client_version: config.client_version.clone(),+ user_agent: config.user_agent.clone(),+ auth: config.auth_token.take().map(Auth::User),},);- Ok(Self {- api,- client_package: config.client_package,- })+ Self { api, config }}pub fn set_auth_token(&self, auth_token: Option<String>) {@@ -56,14 +57,14 @@ impl AccountsClient {}pub fn with_auth_token(&self, auth_token: String) -> Self {- Self {- api: self.api.with_auth(Auth::User(auth_token)),- client_package: self.client_package.clone(),- }+ Self::with_http(+ self.api.http().clone(),+ self.config.clone().with_auth_token(auth_token),+ )}pub fn client_package(&self) -> &str {- &self.client_package+ &self.config.client_package}pub async fn email(&self) -> Result<String> {@@ -613,6 +614,41 @@ mod tests {.unwrap()}+ #[tokio::test]+ async fn with_auth_token_preserves_client_settings_and_scopes_auth() {+ let mut server = Server::new_async().await;+ let public = AccountsClient::new(+ AccountsClientConfig::new("io.ente.photos")+ .with_origin(server.url())+ .with_client_version("1.0.0")+ .with_user_agent("ente-accounts-test"),+ )+ .unwrap();+ let authorized = public.with_auth_token("tok".into());++ for (client, auth) in [+ (&authorized, mockito::Matcher::Exact("tok".into())),+ (&public, mockito::Matcher::Missing),+ ] {+ let request = server+ .mock("GET", "/users/details/v2")+ .match_query(mockito::Matcher::UrlEncoded(+ "memoryCount".into(),+ "false".into(),+ ))+ .match_header("x-auth-token", auth)+ .match_header("x-client-package", "io.ente.photos")+ .match_header("x-client-version", "1.0.0")+ .match_header("user-agent", "ente-accounts-test")+ .with_body(r#"{"email":"[email protected]"}"#)+ .create_async()+ .await;+ assert_eq!(client.client_package(), "io.ente.photos");+ assert_eq!(client.email().await.unwrap(), "[email protected]");+ request.assert_async().await;+ }+ }+#[tokio::test]async fn send_otp_classifies_account_errors() {let cases = [diff --git a/rust/crates/core/src/http.rs b/rust/crates/core/src/http.rsindex 4a2e09c457..d0a7ea69db 100644--- a/rust/crates/core/src/http.rs+++ b/rust/crates/core/src/http.rs@@ -251,18 +251,6 @@ impl Api {*self.auth.write().unwrap_or_else(PoisonError::into_inner) = auth;}- pub fn with_auth(&self, auth: Auth) -> Self {- Self {- http: self.http.clone(),- origin: self.origin.clone(),- client_package: self.client_package.clone(),- client_version: self.client_version.clone(),- #[cfg(not(target_arch = "wasm32"))]- user_agent: self.user_agent.clone(),- auth: RwLock::new(Some(auth)),- }- }-pub fn get(&self, path: &str) -> RequestBuilder {self.request(Method::GET, path)}@@ -544,7 +532,7 @@ mod tests {}#[tokio::test]- async fn api_sends_client_headers_and_scopes_auth() {+ async fn api_sends_client_and_auth_headers() {let mut server = Server::new_async().await;let mock = server.mock("GET", "/ping")@@ -555,22 +543,12 @@ mod tests {.create_async().await;- let public_mock = server- .mock("GET", "/ping")- .match_header("x-client-package", "io.ente.test")- .match_header("x-auth-token", mockito::Matcher::Missing)- .with_body(r#"{"message":"public","id":"abc"}"#)- .create_async()- .await;- let public_api = api(&server, None);- let authorized = public_api.with_auth(Auth::User("tok".into()));- let response = authorized.ping().await.unwrap();+ let api = api(&server, Some(Auth::User("tok".into())));+ let response = api.ping().await.unwrap();mock.assert_async().await;assert_eq!(response.message, "pong");assert_eq!(response.id, "abc");- assert_eq!(public_api.ping().await.unwrap().message, "public");- public_mock.assert_async().await;}#[tokio::test]