chiark / gitweb /
Make everything in AuthConfig optional.
authorSimon Tatham <anakin@pobox.com>
Wed, 17 Jan 2024 12:42:52 +0000 (12:42 +0000)
committerSimon Tatham <anakin@pobox.com>
Wed, 17 Jan 2024 12:42:52 +0000 (12:42 +0000)
src/auth.rs
src/client.rs
src/login.rs

index 4a9215f40b5bec4f6dce976494d66ada6ae3b9ca..9388f64246d3a01af6776f24e81d1d4c4a9f4287 100644 (file)
@@ -28,13 +28,13 @@ impl std::fmt::Display for AuthError {
 
 #[derive(Serialize, Deserialize, Debug)]
 pub struct AuthConfig {
-    pub account_id: String,
-    pub username: String,
-    pub instance_url: String,
-    pub instance_domain: String,
-    pub client_id: String,
-    pub client_secret: String,
-    pub user_token: String,
+    pub account_id: Option<String>,
+    pub username: Option<String>,
+    pub instance_url: Option<String>,
+    pub instance_domain: Option<String>,
+    pub client_id: Option<String>,
+    pub client_secret: Option<String>,
+    pub user_token: Option<String>,
 }
 
 impl AuthConfig {
index 00e382e4fa0a7bbed8930b408d716df2305c43aa..248a14cffdc91201e350516890e6e3e25ef7ca03 100644 (file)
@@ -487,16 +487,32 @@ impl Client {
     pub fn fq(&self, acct: &str) -> String {
         match acct.contains('@') {
             true => acct.to_owned(),
-            false => acct.to_owned() + "@" + &self.auth.instance_domain,
+            false => {
+                acct.to_owned()
+                    + "@"
+                    + self
+                        .auth
+                        .instance_domain
+                        .as_deref()
+                        .expect("Should never call fq in pre-login UI states")
+            }
         }
     }
 
     pub fn our_account_id(&self) -> String {
-        self.auth.account_id.clone()
+        self.auth
+            .account_id
+            .as_ref()
+            .expect("Should never call our_account_id in pre-login UI states")
+            .clone()
     }
 
     pub fn our_account_fq(&self) -> String {
-        self.fq(&self.auth.username)
+        self.fq(self
+            .auth
+            .username
+            .as_deref()
+            .expect("Should never call our_account_fq in pre-login UI states"))
     }
 
     fn consume_transaction_log(&mut self, log: TransactionLogEntry) {
@@ -514,8 +530,14 @@ impl Client {
             ));
         }
 
-        let base_url = self.auth.instance_url.to_owned() + "/api/";
-        req.build(&base_url, client, Some(&self.auth.user_token))
+        let base_url = self
+            .auth
+            .instance_url
+            .as_ref()
+            .expect("Should have set up an instance URL before calling")
+            .clone()
+            + "/api/";
+        req.build(&base_url, client, self.auth.user_token.as_deref())
     }
 
     fn api_request(
@@ -593,12 +615,14 @@ impl Client {
         // information.
 
         if let Some(ac) = self.accounts.get(id) {
-            if ac.id != self.auth.account_id || ac.source.is_some() {
+            if Some(&ac.id) != self.auth.account_id.as_ref()
+                || ac.source.is_some()
+            {
                 return Ok(ac.clone());
             }
         }
 
-        let req = if id == self.auth.account_id {
+        let req = if Some(id) == self.auth.account_id.as_deref() {
             Req::get(&format!("v1/accounts/verify_credentials"))
         } else {
             Req::get(&format!("v1/accounts/{id}"))
@@ -1116,9 +1140,10 @@ impl Client {
                             format!("redirection to suspicious URL {}", sval),
                         ));
                     }
-                    req = client
-                        .request(method, newurl)
-                        .bearer_auth(&self.auth.user_token);
+                    req = client.request(method, newurl);
+                    if let Some(ref token) = self.auth.user_token {
+                        req = req.bearer_auth(token);
+                    }
                     let (newrsp, log) =
                         execute_and_log_request(&self.client, req.build()?)?;
                     self.consume_transaction_log(log);
index b3017fc54c3a746dd8ba7aa29378fc32131692ff..487c6a365bc3e3aa0827bb3ffd6df1c651b0c76f 100644 (file)
@@ -163,7 +163,7 @@ pub fn login(
         Err(AuthError::Nonexistent(..)) => Ok(()),
         Ok(auth) => Err(TopLevelError::new("", &format!(
             "you are already logged in as {0}@{1}! Use --config to specify a separate configuration directory for another login, or delete {2} to remove the existing login details",
-            auth.username, auth.instance_domain, cfgloc.get_path("auth").display()))),
+            auth.username.as_deref().unwrap_or("<None>"), auth.instance_domain.as_deref().unwrap_or("<None>"), cfgloc.get_path("auth").display()))),
         Err(e) => Err(TopLevelError::from(e)),
     }?;
 
@@ -228,13 +228,13 @@ pub fn login(
 
     // Save everything!
     let auth = AuthConfig {
-        account_id: account.id,
-        username: account.username,
-        instance_url: instance_url.to_owned(),
-        instance_domain: instance.domain,
-        client_id: app.client_id.unwrap(),
-        client_secret: app.client_secret.unwrap(),
-        user_token: user_token.access_token,
+        account_id: Some(account.id),
+        username: Some(account.username),
+        instance_url: Some(instance_url.to_owned()),
+        instance_domain: Some(instance.domain),
+        client_id: Some(app.client_id.unwrap()),
+        client_secret: Some(app.client_secret.unwrap()),
+        user_token: Some(user_token.access_token),
     };
 
     let mut json = serde_json::to_string_pretty(&auth).unwrap();