From 696458657b26032e6e2a987c059fd69aaa10508d Mon Sep 17 00:00:00 2001 From: Vika Date: Mon, 19 Sep 2022 17:08:01 +0300 Subject: 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. --- kittybox-rs/indieauth/src/pkce.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'kittybox-rs/indieauth/src/pkce.rs') 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 + } } -- cgit 1.4.1