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/lib.rs | 14 ++++++++++++++ kittybox-rs/indieauth/src/pkce.rs | 21 +++++++++++++++++++++ kittybox-rs/indieauth/src/scopes.rs | 6 ++++++ 3 files changed, 41 insertions(+) diff --git a/kittybox-rs/indieauth/src/lib.rs b/kittybox-rs/indieauth/src/lib.rs index 745ee1e..2cce1b9 100644 --- a/kittybox-rs/indieauth/src/lib.rs +++ b/kittybox-rs/indieauth/src/lib.rs @@ -70,6 +70,15 @@ pub enum ResponseType { /// requested. Code } +// TODO serde_variant +impl ResponseType { + /// Return the response type as it would appear in serialized form. + pub fn as_str(&self) -> &'static str { + match self { + ResponseType::Code => "code", + } + } +} /// Grant types that are described in the IndieAuth spec. /// @@ -252,6 +261,11 @@ impl State { Self(String::from_utf8(bytes).unwrap()) } } +impl AsRef for State { + fn as_ref(&self) -> &str { + self.0.as_str() + } +} /// The authorization request that should be affixed to the URL of an /// authorization endpoint to start the IndieAuth ceremony. 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 + } } diff --git a/kittybox-rs/indieauth/src/scopes.rs b/kittybox-rs/indieauth/src/scopes.rs index ae039a6..d74878e 100644 --- a/kittybox-rs/indieauth/src/scopes.rs +++ b/kittybox-rs/indieauth/src/scopes.rs @@ -83,6 +83,12 @@ impl From<&str> for Scope { } } } +impl FromStr for Scope { + type Err = std::convert::Infallible; + fn from_str(s: &str) -> Result { + Ok(s.into()) + } +} /// A list of scopes that serializes to a space-separated string instead of a list. /// -- cgit 1.4.1