#[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 {
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) {
));
}
- 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(
// 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}"))
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);
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)),
}?;
// 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();