about summary refs log tree commit diff
path: root/kittybox-rs/src/micropub/util.rs
diff options
context:
space:
mode:
authorVika <vika@fireburn.ru>2022-09-28 03:55:48 +0300
committerVika <vika@fireburn.ru>2022-09-28 03:55:48 +0300
commit6e20a3c51756c2e84290da6ec53b89a5fc58c0fc (patch)
treea360c40dce27b7804001038babd4631476232001 /kittybox-rs/src/micropub/util.rs
parent2f02bf76a40c971b9404aa0913bc8baa7dfde24c (diff)
downloadkittybox-6e20a3c51756c2e84290da6ec53b89a5fc58c0fc.tar.zst
Use tokens from the auth backend to authenticate for Micropub
Diffstat (limited to 'kittybox-rs/src/micropub/util.rs')
-rw-r--r--kittybox-rs/src/micropub/util.rs52
1 files changed, 19 insertions, 33 deletions
diff --git a/kittybox-rs/src/micropub/util.rs b/kittybox-rs/src/micropub/util.rs
index 7c6a0b1..5097878 100644
--- a/kittybox-rs/src/micropub/util.rs
+++ b/kittybox-rs/src/micropub/util.rs
@@ -1,5 +1,5 @@
 use crate::database::Storage;
-use crate::tokenauth::User;
+use kittybox_indieauth::TokenData;
 use chrono::prelude::*;
 use core::iter::Iterator;
 use newbase60::num_to_sxg;
@@ -33,7 +33,7 @@ fn reset_dt(post: &mut serde_json::Value) -> DateTime<FixedOffset> {
     chrono::DateTime::from(curtime)
 }
 
-pub fn normalize_mf2(mut body: serde_json::Value, user: &User) -> (String, serde_json::Value) {
+pub fn normalize_mf2(mut body: serde_json::Value, user: &TokenData) -> (String, serde_json::Value) {
     // Normalize the MF2 object here.
     let me = &user.me;
     let folder = get_folder_from_type(body["type"][0].as_str().unwrap());
@@ -190,7 +190,7 @@ pub(crate) async fn create_feed(
     storage: &impl Storage,
     uid: &str,
     channel: &str,
-    user: &User,
+    user: &TokenData,
 ) -> crate::database::Result<()> {
     let path = url::Url::parse(channel).unwrap().path().to_string();
 
@@ -220,6 +220,16 @@ mod tests {
     use super::*;
     use serde_json::json;
 
+    fn token_data() -> TokenData {
+        TokenData {
+            me: "https://fireburn.ru/".parse().unwrap(),
+            client_id: "https://quill.p3k.io/".parse().unwrap(),
+            scope: kittybox_indieauth::Scopes::new(vec![kittybox_indieauth::Scope::Create]),
+            exp: Some(u64::MAX),
+            iat: Some(0)
+        }
+    }
+
     #[test]
     fn test_form_to_mf2() {
         assert_eq!(
@@ -248,11 +258,7 @@ mod tests {
 
         let (uid, normalized) = normalize_mf2(
             mf2.clone(),
-            &User::new(
-                "https://fireburn.ru/",
-                "https://quill.p3k.io/",
-                "create update media",
-            ),
+            &token_data(),
         );
         assert_eq!(
             normalized["properties"]["uid"][0], mf2["properties"]["uid"][0],
@@ -277,11 +283,7 @@ mod tests {
 
         let (_, normalized) = normalize_mf2(
             mf2.clone(),
-            &User::new(
-                "https://fireburn.ru/",
-                "https://quill.p3k.io/",
-                "create update media",
-            ),
+            &token_data(),
         );
 
         assert_eq!(
@@ -303,11 +305,7 @@ mod tests {
 
         let (_, normalized) = normalize_mf2(
             mf2.clone(),
-            &User::new(
-                "https://fireburn.ru/",
-                "https://quill.p3k.io/",
-                "create update media",
-            ),
+            &token_data(),
         );
 
         assert_eq!(
@@ -327,11 +325,7 @@ mod tests {
 
         let (uid, post) = normalize_mf2(
             mf2,
-            &User::new(
-                "https://fireburn.ru/",
-                "https://quill.p3k.io/",
-                "create update media",
-            ),
+            &token_data(),
         );
         assert_eq!(
             post["properties"]["published"]
@@ -396,11 +390,7 @@ mod tests {
 
         let (_, post) = normalize_mf2(
             mf2,
-            &User::new(
-                "https://fireburn.ru/",
-                "https://quill.p3k.io/",
-                "create update media",
-            ),
+            &token_data(),
         );
         assert!(
             post["properties"]["url"]
@@ -429,11 +419,7 @@ mod tests {
 
         let (uid, post) = normalize_mf2(
             mf2,
-            &User::new(
-                "https://fireburn.ru/",
-                "https://quill.p3k.io/",
-                "create update media",
-            ),
+            &token_data(),
         );
         assert_eq!(
             post["properties"]["uid"][0], uid,