From 5f3bc4a21582ba8e8bdfc50430792ae934be340f Mon Sep 17 00:00:00 2001 From: Ryan Tate Date: Thu, 31 Oct 2024 11:59:31 -0700 Subject: [PATCH] use serde deserialize serialize derive for authorization response Signed-off-by: Ryan Tate --- src/core/object/mod.rs | 17 --------------- src/core/response/mod.rs | 45 ++++++++++++---------------------------- 2 files changed, 13 insertions(+), 49 deletions(-) diff --git a/src/core/object/mod.rs b/src/core/object/mod.rs index 02447c5..02ad16f 100644 --- a/src/core/object/mod.rs +++ b/src/core/object/mod.rs @@ -1,5 +1,3 @@ -use std::collections::BTreeMap; - use anyhow::{Context, Error, Result}; use serde::{Deserialize, Serialize}; use serde_json::{Map, Value as Json}; @@ -61,21 +59,6 @@ impl UntypedObject { ), } } - - /// Flatten the structure for posting as a form. - pub(crate) fn flatten_for_form(self) -> Result> { - self.0 - .into_iter() - .map(|(k, v)| { - if let Json::String(s) = v { - return Ok((k, s)); - } - serde_json::to_string(&v) - .map(|v| (k, v)) - .map_err(Error::from) - }) - .collect() - } } impl From for Json { diff --git a/src/core/response/mod.rs b/src/core/response/mod.rs index a6e1b1b..12781a7 100644 --- a/src/core/response/mod.rs +++ b/src/core/response/mod.rs @@ -1,13 +1,7 @@ -use super::{ - object::{ParsingErrorContext, UntypedObject}, - presentation_submission::PresentationSubmission, -}; - -use std::collections::BTreeMap; +use super::{object::UntypedObject, presentation_submission::PresentationSubmission}; use anyhow::{Context, Error, Result}; use serde::{Deserialize, Serialize}; -use serde_json::Value; use url::Url; use self::parameters::VpToken; @@ -26,45 +20,34 @@ impl AuthorizationResponse { return Ok(Self::Jwt(jwt)); } - let flattened = serde_urlencoded::from_bytes::>(bytes) + let unencoded = serde_urlencoded::from_bytes::(bytes) .context("failed to construct flat map")?; - let map = flattened - .into_iter() - .map(|(k, v)| { - let v = serde_json::from_str::(&v).unwrap_or(Value::String(v)); - (k, v) - }) - .collect(); - - Ok(Self::Unencoded(UntypedObject(map).try_into()?)) + + Ok(Self::Unencoded(unencoded)) } } -#[derive(Debug, Clone)] -pub struct UnencodedAuthorizationResponse( - pub UntypedObject, - pub VpToken, - pub PresentationSubmission, -); +#[derive(Debug, Clone, Deserialize, Serialize)] +pub struct UnencodedAuthorizationResponse { + pub vp_token: VpToken, + pub presentation_submission: PresentationSubmission, +} impl UnencodedAuthorizationResponse { /// Encode the Authorization Response as 'application/x-www-form-urlencoded'. pub fn into_x_www_form_urlencoded(self) -> Result { - let mut inner = self.0; - inner.insert(self.1); - inner.insert(self.2); - serde_urlencoded::to_string(inner.flatten_for_form()?) + serde_urlencoded::to_string(&self) .context("failed to encode response as 'application/x-www-form-urlencoded'") } /// Return the Verifiable Presentation Token. pub fn vp_token(&self) -> &VpToken { - &self.1 + &self.vp_token } /// Return the Presentation Submission. pub fn presentation_submission(&self) -> &PresentationSubmission { - &self.2 + &self.presentation_submission } } @@ -91,9 +74,7 @@ impl TryFrom for UnencodedAuthorizationResponse { type Error = Error; fn try_from(value: UntypedObject) -> Result { - let vp_token = value.get().parsing_error()?; - let presentation_submission = value.get().parsing_error()?; - Ok(Self(value, vp_token, presentation_submission)) + Ok(serde_json::from_value(serde_json::Value::Object(value.0))?) } }