From d8556f4eb27412f60fa48b73b18db0e52d388409 Mon Sep 17 00:00:00 2001 From: Vika Date: Sun, 10 Jul 2022 22:27:55 +0300 Subject: kittybox-indieauth: improve types and make more of them public --- kittybox-rs/indieauth/src/lib.rs | 57 ++++++++++++++++++++++++++++++------- kittybox-rs/indieauth/src/pkce.rs | 4 +-- kittybox-rs/indieauth/src/scopes.rs | 2 +- 3 files changed, 49 insertions(+), 14 deletions(-) (limited to 'kittybox-rs/indieauth/src') 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>, // Note: Scopes isn't used here because this field should be // serialized as a list, not as a string - pub scopes_supported: Vec, + #[serde(skip_serializing_if = "Option::is_none")] + pub scopes_supported: Option>, #[serde(skip_serializing_if = "Option::is_none")] pub response_types_supported: Option>, #[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 + } } #[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() + ) + } } diff --git a/kittybox-rs/indieauth/src/pkce.rs b/kittybox-rs/indieauth/src/pkce.rs index a0bc291..1d3c58f 100644 --- a/kittybox-rs/indieauth/src/pkce.rs +++ b/kittybox-rs/indieauth/src/pkce.rs @@ -9,8 +9,8 @@ pub enum PKCEMethod { Plain } -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct PKCEVerifier(String); +#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)] +pub struct PKCEVerifier(pub(super) String); impl AsRef for PKCEVerifier { fn as_ref(&self) -> &str { diff --git a/kittybox-rs/indieauth/src/scopes.rs b/kittybox-rs/indieauth/src/scopes.rs index bc57265..e803dca 100644 --- a/kittybox-rs/indieauth/src/scopes.rs +++ b/kittybox-rs/indieauth/src/scopes.rs @@ -80,7 +80,7 @@ impl From<&str> for Scope { } } } -#[derive(Debug, Clone)] +#[derive(PartialEq, Eq, Debug, Clone)] pub struct Scopes(Vec); impl Scopes { pub fn new(scopes: Vec) -> Self { -- cgit 1.4.1