about summary refs log tree commit diff
path: root/kittybox-rs/indieauth/src/lib.rs
diff options
context:
space:
mode:
authorVika <vika@fireburn.ru>2022-07-10 22:27:55 +0300
committerVika <vika@fireburn.ru>2022-07-10 22:29:29 +0300
commitd8556f4eb27412f60fa48b73b18db0e52d388409 (patch)
treeab303d1a1e34897c74d643a844260e08b342aa77 /kittybox-rs/indieauth/src/lib.rs
parent0cae59373c9ab5828804477e8dedc85143ffc224 (diff)
downloadkittybox-d8556f4eb27412f60fa48b73b18db0e52d388409.tar.zst
kittybox-indieauth: improve types and make more of them public
Diffstat (limited to 'kittybox-rs/indieauth/src/lib.rs')
-rw-r--r--kittybox-rs/indieauth/src/lib.rs57
1 files changed, 46 insertions, 11 deletions
diff --git a/kittybox-rs/indieauth/src/lib.rs b/kittybox-rs/indieauth/src/lib.rs
index 23b3923..242317d 100644
--- a/kittybox-rs/indieauth/src/lib.rs
+++ b/kittybox-rs/indieauth/src/lib.rs
@@ -61,7 +61,8 @@ pub struct Metadata {
     pub revocation_endpoint_auth_methods_supported: Option<Vec<RevocationEndpointAuthMethod>>,
     // Note: Scopes isn't used here because this field should be
     // serialized as a list, not as a string
-    pub scopes_supported: Vec<Scope>,
+    #[serde(skip_serializing_if = "Option::is_none")]
+    pub scopes_supported: Option<Vec<Scope>>,
     #[serde(skip_serializing_if = "Option::is_none")]
     pub response_types_supported: Option<Vec<ResponseType>>,
     #[serde(skip_serializing_if = "Option::is_none")]
@@ -112,24 +113,32 @@ pub struct AuthorizationRequest {
 }
 
 #[derive(Debug, Clone, Serialize, Deserialize)]
-struct AuthorizationResponse {
+pub struct AuthorizationResponse {
     pub code: String,
     pub state: State,
-    iss: Url
+    pub iss: Url
 }
 
-#[derive(Debug, Clone, Serialize, Deserialize)]
-struct GrantRequest {
-    grant_type: GrantType,
-    code: String,
-    client_id: Url,
-    redirect_uri: Url,
-    code_verifier: PKCEVerifier
+#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
+#[serde(tag = "grant_type")]
+#[serde(rename_all = "snake_case")]
+pub enum GrantRequest {
+    AuthorizationCode {
+        code: String,
+        client_id: Url,
+        redirect_uri: Url,
+        code_verifier: PKCEVerifier
+    },
+    RefreshToken {
+        refresh_token: String,
+        client_id: url::Url,
+        scope: Option<Scopes>
+    }
 }
 
 #[derive(Debug, Clone, Serialize, Deserialize)]
 #[serde(untagged)]
-enum GrantResponse {
+pub enum GrantResponse {
     AccessToken {
         me: Url,
         #[serde(skip_serializing_if = "Option::is_none")]
@@ -254,4 +263,30 @@ mod tests {
             json!({"error": "insufficient_scope"})
         );
     }
+
+    #[test]
+    fn test_serialize_deserialize_grant_request() {
+        let authorization_code: GrantRequest = GrantRequest::AuthorizationCode {
+            client_id: "https://kittybox.fireburn.ru/".parse().unwrap(),
+            redirect_uri: "https://kittybox.fireburn.ru/.kittybox/login/redirect".parse().unwrap(),
+            code_verifier: PKCEVerifier("helloworld".to_string()),
+            code: "hithere".to_owned()
+        };
+        let serialized = serde_urlencoded::to_string(&[
+            ("grant_type", "authorization_code"),
+            ("code", "hithere"),
+            ("client_id", "https://kittybox.fireburn.ru/"),
+            ("redirect_uri", "https://kittybox.fireburn.ru/.kittybox/login/redirect"),
+            ("code_verifier", "helloworld"),
+        ]).unwrap();
+
+        let deserialized = serde_urlencoded::from_str(&serialized).unwrap();
+
+        assert_eq!(authorization_code, deserialized);
+
+        assert_eq!(
+            serialized,
+            serde_urlencoded::to_string(authorization_code).unwrap()
+        )
+    }
 }