Skip to content

Commit

Permalink
return application json encoded authorization response
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Tate <[email protected]>
  • Loading branch information
Ryanmtate committed Oct 31, 2024
1 parent ab9a643 commit 4d73ec2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
18 changes: 18 additions & 0 deletions src/core/response/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ impl AuthorizationResponse {

Ok(Self::Unencoded(UntypedObject(map).try_into()?))
}

/// Decode the Authorization Response from 'application/json'.
pub fn from_json(bytes: &[u8]) -> Result<Self> {
let value = serde_json::from_slice::<Value>(bytes)?;
let map = value
.as_object()
.context("failed to parse JSON object")?
.clone();
Ok(Self::Unencoded(UntypedObject(map).try_into()?))
}
}

#[derive(Debug, Clone)]
Expand All @@ -57,6 +67,14 @@ impl UnencodedAuthorizationResponse {
.context("failed to encode response as 'application/x-www-form-urlencoded'")
}

/// Encode the authorization response as `application/json`.
pub fn into_json(self) -> Result<String> {
let mut inner = self.0;
inner.insert(self.1);
inner.insert(self.2);
serde_json::to_string(&inner).context("failed to encode response as 'application/json'")
}

/// Return the Verifiable Presentation Token.
pub fn vp_token(&self) -> &VpToken {
&self.1
Expand Down
4 changes: 2 additions & 2 deletions src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ pub trait Wallet: RequestVerifier + Sync {
let http_request_body = match request.response_mode() {
ResponseMode::DirectPost => {
http_request_builder = http_request_builder
.header(CONTENT_TYPE, "application/x-www-form-urlencoded")
.header(CONTENT_TYPE, "application/json")
.method("POST");

let AuthorizationResponse::Unencoded(unencoded) = response else {
bail!("unexpected AuthorizationResponse format")
};

unencoded.into_x_www_form_urlencoded()?.into_bytes()
unencoded.into_json()?.into_bytes()
}
ResponseMode::DirectPostJwt => {
http_request_builder = http_request_builder
Expand Down

0 comments on commit 4d73ec2

Please sign in to comment.