From: Simon Tatham Date: Wed, 17 Jan 2024 12:42:52 +0000 (+0000) Subject: Make everything in AuthConfig optional. X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=fb31e7ab7bebcbd67f71f65f8f81d818ac99a061;p=mastodonochrome.git Make everything in AuthConfig optional. --- diff --git a/src/auth.rs b/src/auth.rs index 4a9215f..9388f64 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -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, + pub username: Option, + pub instance_url: Option, + pub instance_domain: Option, + pub client_id: Option, + pub client_secret: Option, + pub user_token: Option, } impl AuthConfig { diff --git a/src/client.rs b/src/client.rs index 00e382e..248a14c 100644 --- a/src/client.rs +++ b/src/client.rs @@ -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); diff --git a/src/login.rs b/src/login.rs index b3017fc..487c6a3 100644 --- a/src/login.rs +++ b/src/login.rs @@ -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(""), auth.instance_domain.as_deref().unwrap_or(""), 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();