summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs4
-rw-r--r--src/micropub.rs16
2 files changed, 16 insertions, 4 deletions
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())
             }