about summary refs log tree commit diff
path: root/kittybox-rs/indieauth/src/pkce.rs
diff options
context:
space:
mode:
authorVika <vika@fireburn.ru>2022-09-19 17:08:01 +0300
committerVika <vika@fireburn.ru>2022-09-19 17:08:01 +0300
commit696458657b26032e6e2a987c059fd69aaa10508d (patch)
tree57caa3e69a32756f1f5c075782a1abb3e4587d07 /kittybox-rs/indieauth/src/pkce.rs
parent629531bdefad41e8839fa818e68bcf9a083466f8 (diff)
kittybox-indieauth: Allow converting more types to/from strings
Sometimes it is needed, for example, to construct an HTML form
pre-filled with the request data.
Diffstat (limited to 'kittybox-rs/indieauth/src/pkce.rs')
-rw-r--r--kittybox-rs/indieauth/src/pkce.rs21
1 files changed, 21 insertions, 0 deletions
diff --git a/kittybox-rs/indieauth/src/pkce.rs b/kittybox-rs/indieauth/src/pkce.rs
index 249917e..511b9fc 100644
--- a/kittybox-rs/indieauth/src/pkce.rs
+++ b/kittybox-rs/indieauth/src/pkce.rs
@@ -10,12 +10,22 @@ pub enum PKCEMethod {
     //#[default]
     S256,
     /// Plain string by itself. Please don't use this.
+    #[serde(rename = "snake_case")]
     Plain
 }
 // manual impl until Rust 1.62 hits nixos-unstable
 impl Default for PKCEMethod {
     fn default() -> Self { PKCEMethod::S256 }
 }
+impl PKCEMethod {
+    /// Return a string representing a PKCE method as it would be serialized.
+    pub fn as_str(&self) -> &'static str {
+        match self {
+            PKCEMethod::S256 => "S256",
+            PKCEMethod::Plain => "plain"
+        }
+    }
+}
 /// A PKCE verifier string that should be kept in secret until the end
 /// of the authentication ceremony, where it is revealed to prove that
 /// the one who uses the grant is the same entity who it was given to.
@@ -51,6 +61,7 @@ impl PKCEVerifier {
 #[derive(Eq, PartialEq, Debug, Clone, Serialize, Deserialize)]
 pub struct PKCEChallenge {
     code_challenge: String,
+    #[serde(rename = "code_challenge_method")]
     method: PKCEMethod
 }
 
@@ -88,4 +99,14 @@ impl PKCEChallenge {
     pub fn verify(&self, code_verifier: PKCEVerifier) -> bool {
         Self::new(code_verifier, self.method) == *self
     }
+
+    /// Return a reference to the code challenge string.
+    pub fn as_str(&self) -> &str {
+        self.code_challenge.as_str()
+    }
+
+    /// Return the method used to create this challenge.
+    pub fn method(&self) -> PKCEMethod {
+        self.method
+    }
 }