From 5f45525ad06c87d28c845204b75c0e6b70ba0320 Mon Sep 17 00:00:00 2001 From: Vika Date: Tue, 3 Dec 2024 08:46:31 +0300 Subject: Small tweaks for compatibility with Kittybox's inconsistent implementation I need to fix that, but that'll take a small refactor of Kittybox. I want things to work and be liberal in what I accept, so I put in some shortcuts. --- src/lib.rs | 4 ++-- src/micropub.rs | 16 ++++++++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index 6048e10..fc50b52 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -260,6 +260,7 @@ impl App { // Skip the token if we can't access ?q=config if let Err(micropub::Error::Micropub(err)) = micropub.config().await { + tracing::warn!("Micropub token seems to be invalid. Let's try refreshing."); if err.error == kittybox_util::micropub::ErrorKind::NotAuthorized { // Token may have expired. See if we have a refresh token and renew. let _ = libsecret::password_clear_future(Some(schema), attrs_ref).await; @@ -473,12 +474,11 @@ impl AsyncComponent for App { ) { match message { Input::SignOut => { - if self.micropub.take().is_some() { + if let Some(micropub) = self.micropub.take() { let _ = libsecret::password_clear_future( Some(&self.secret_schema), Default::default(), ).await; - self.micropub = None; } }, Input::Authorize(data) => { diff --git a/src/micropub.rs b/src/micropub.rs index dbd8f9a..04fa893 100644 --- a/src/micropub.rs +++ b/src/micropub.rs @@ -4,7 +4,7 @@ pub use kittybox_util::micropub::{Error as MicropubError, Config, QueryType}; #[derive(Debug)] pub struct Client { micropub: String, - access_token: String, + pub access_token: String, http: soup::Session, } @@ -43,6 +43,9 @@ impl Client { headers.append("Authorization", &format!("Bearer {}", self.access_token)); let body = self.http.send_and_read_future(&exch, glib::Priority::DEFAULT).await?; + if exch.status() == soup::Status::Unauthorized { + return Err(MicropubError::from(kittybox_util::micropub::ErrorKind::NotAuthorized).into()) + } Ok(serde_json::from_slice(&body)?) } @@ -69,8 +72,17 @@ impl Client { soup::Status::InternalServerError | soup::Status::BadGateway | soup::Status::ServiceUnavailable => { todo!("micropub server is down") }, + soup::Status::Unauthorized => { + Err(MicropubError::from(kittybox_util::micropub::ErrorKind::NotAuthorized).into()) + } _ => { - let error = serde_json::from_slice::(&body)?; + let error = match serde_json::from_slice::(&body) { + Ok(error) => error, + Err(err) => { + tracing::debug!("Error serializing body: {}", String::from_utf8_lossy(&body)); + Err(err)? + } + }; Err(error.into()) } -- cgit 1.4.1