summary refs log tree commit diff
diff options
context:
space:
mode:
authorVika <vika@fireburn.ru>2024-12-03 08:46:31 +0300
committerVika <vika@fireburn.ru>2024-12-03 08:46:31 +0300
commit5f45525ad06c87d28c845204b75c0e6b70ba0320 (patch)
treefc9390f7217c5695140d8cf6b6efcc6a36d397d7
parentef6b0f6ee3b769356f7bc852eb240e26f2d895cf (diff)
downloadbowl-5f45525ad06c87d28c845204b75c0e6b70ba0320.tar.zst
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.
-rw-r--r--Cargo.lock6
-rw-r--r--Cargo.toml2
-rw-r--r--src/lib.rs4
-rw-r--r--src/micropub.rs16
4 files changed, 20 insertions, 8 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 2070369..64150bf 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -834,7 +834,7 @@ dependencies = [
 [[package]]
 name = "kittybox-indieauth"
 version = "0.2.0"
-source = "git+https://git.vikanezrimaya.xyz/kittybox#99e23922e6e1cdd2cce34e28a565d479e74bcfbe"
+source = "git+https://git.vikanezrimaya.xyz/kittybox#ca76b67e985583ebc4276d6dce9dc74fde3af3bc"
 dependencies = [
  "data-encoding",
  "rand",
@@ -845,8 +845,8 @@ dependencies = [
 
 [[package]]
 name = "kittybox-util"
-version = "0.2.0"
-source = "git+https://git.vikanezrimaya.xyz/kittybox#99e23922e6e1cdd2cce34e28a565d479e74bcfbe"
+version = "0.3.0"
+source = "git+https://git.vikanezrimaya.xyz/kittybox#ca76b67e985583ebc4276d6dce9dc74fde3af3bc"
 dependencies = [
  "futures-util",
  "serde",
diff --git a/Cargo.toml b/Cargo.toml
index 610b530..e0ada2c 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -17,7 +17,7 @@ gio = { version = "0.20.1", features = ["v2_80"] }
 glib = { version = "0.20.1", features = ["log"] }
 gtk = { version = "0.9.0", package = "gtk4", features = ["gnome_46", "v4_14"] }
 kittybox-indieauth = { git = "https://git.vikanezrimaya.xyz/kittybox", version = "0.2.0" }
-kittybox-util = { git = "https://git.vikanezrimaya.xyz/kittybox", version = "0.2.0" }
+kittybox-util = { git = "https://git.vikanezrimaya.xyz/kittybox", version = "0.3.0" }
 libsecret = { version = "0.6.0", features = ["v0_21_2"] }
 log = { version = "0.4.22", features = ["std"] }
 microformats = "0.9.1"
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::<MicropubError>(&body)?;
+                let error = match serde_json::from_slice::<MicropubError>(&body) {
+                    Ok(error) => error,
+                    Err(err) => {
+                        tracing::debug!("Error serializing body: {}", String::from_utf8_lossy(&body));
+                        Err(err)?
+                    }
+                };
 
                 Err(error.into())
             }