Skip to content

Commit

Permalink
style: rename request and response
Browse files Browse the repository at this point in the history
  • Loading branch information
nanderstabel committed Jun 5, 2023
1 parent fb94db5 commit 74a7eb0
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 34 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ async fn main() {
.mount(&mock_server)
.await;

// Create a new `redirect_uri` endpoint on the mock server where the `Provider` will send the `Response`.
// Create a new `redirect_uri` endpoint on the mock server where the `Provider` will send the `AuthorizationResponse`.
Mock::given(method("POST"))
.and(path("/redirect_uri"))
.respond_with(ResponseTemplate::new(200))
Expand Down Expand Up @@ -179,11 +179,11 @@ async fn main() {
// The provider sends it's response to the mock server's `redirect_uri` endpoint.
provider.send_response(response).await.unwrap();

// Assert that the Response was successfully received by the mock server at the expected endpoint.
// Assert that the AuthorizationResponse was successfully received by the mock server at the expected endpoint.
let post_request = mock_server.received_requests().await.unwrap()[1].clone();
assert_eq!(post_request.method, Method::Post);
assert_eq!(post_request.url.path(), "/redirect_uri");
let response: Response = serde_urlencoded::from_bytes(post_request.body.as_slice()).unwrap();
let response: AuthorizationResponse = serde_urlencoded::from_bytes(post_request.body.as_slice()).unwrap();

// The `RelyingParty` then validates the response by decoding the header of the id_token, by fetching the public
// key corresponding to the key identifier and finally decoding the id_token using the public key and by
Expand Down
4 changes: 2 additions & 2 deletions src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ where

let jwt = self.subject.encode(id_token).await?;

let mut builder = Response::builder()
let mut builder = AuthorizationResponse::builder()
.redirect_uri(request.redirect_uri().to_owned())
.id_token(jwt);
if let Some(state) = request.state() {
Expand All @@ -79,7 +79,7 @@ where
builder.build()
}

pub async fn send_response(&self, response: Response) -> Result<()> {
pub async fn send_response(&self, response: AuthorizationResponse) -> Result<()> {
let client = reqwest::Client::new();
let builder = client.post(response.redirect_uri()).form(&response);
builder.send().await?.text().await?;
Expand Down
16 changes: 8 additions & 8 deletions src/relying_party.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ where
RelyingParty { validator }
}

pub async fn encode(&self, request: &Request) -> Result<String> {
pub async fn encode(&self, request: &AuthorizationRequest) -> Result<String> {
self.validator.encode(request).await
}

/// Validates a [`Response`] by decoding the header of the id_token, fetching the public key corresponding to
/// Validates a [`AuthorizationResponse`] by decoding the header of the id_token, fetching the public key corresponding to
/// the key identifier and finally decoding the id_token using the public key and by validating the signature.
// TODO: Validate the claims in the id_token as described here:
// https://openid.net/specs/openid-connect-self-issued-v2-1_0.html#name-self-issued-id-token-valida
pub async fn validate_response(&self, response: &Response) -> Result<IdToken> {
pub async fn validate_response(&self, response: &AuthorizationResponse) -> Result<IdToken> {
let token = response
.id_token()
.to_owned()
Expand Down Expand Up @@ -92,7 +92,7 @@ mod tests {
let relying_party = RelyingParty::new(validator);

// Create a new RequestUrl with response mode `post` for cross-device communication.
let request: Request = RequestUrl::builder()
let request: AuthorizationRequest = RequestUrl::builder()
.response_type(ResponseType::IdToken)
.client_id("did:mock:1".to_string())
.scope(Scope::from(vec![ScopeValue::OpenId, ScopeValue::Phone]))
Expand Down Expand Up @@ -131,14 +131,14 @@ mod tests {
.and_then(TryInto::try_into)
.unwrap();

// Create a new `request_uri` endpoint on the mock server and load it with the JWT encoded `Request`.
// Create a new `request_uri` endpoint on the mock server and load it with the JWT encoded `AuthorizationRequest`.
Mock::given(method("GET"))
.and(path("/request_uri"))
.respond_with(ResponseTemplate::new(200).set_body_string(relying_party.encode(&request).await.unwrap()))
.mount(&mock_server)
.await;

// Create a new `redirect_uri` endpoint on the mock server where the `Provider` will send the `Response`.
// Create a new `redirect_uri` endpoint on the mock server where the `Provider` will send the `AuthorizationResponse`.
Mock::given(method("POST"))
.and(path("/redirect_uri"))
.respond_with(ResponseTemplate::new(200))
Expand Down Expand Up @@ -195,11 +195,11 @@ mod tests {
// The provider sends it's response to the mock server's `redirect_uri` endpoint.
provider.send_response(response).await.unwrap();

// Assert that the Response was successfully received by the mock server at the expected endpoint.
// Assert that the AuthorizationResponse was successfully received by the mock server at the expected endpoint.
let post_request = mock_server.received_requests().await.unwrap()[1].clone();
assert_eq!(post_request.method, Method::Post);
assert_eq!(post_request.url.path(), "/redirect_uri");
let response: Response = serde_urlencoded::from_bytes(post_request.body.as_slice()).unwrap();
let response: AuthorizationResponse = serde_urlencoded::from_bytes(post_request.body.as_slice()).unwrap();

// The `RelyingParty` then validates the response by decoding the header of the id_token, by fetching the public
// key corresponding to the key identifier and finally decoding the id_token using the public key and by
Expand Down
14 changes: 7 additions & 7 deletions src/request/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub mod request_builder;
#[derive(Deserialize, Debug, PartialEq, Clone, Serialize)]
#[serde(untagged, deny_unknown_fields)]
pub enum RequestUrl {
Request(Box<Request>),
Request(Box<AuthorizationRequest>),
// TODO: Add client_id parameter.
RequestUri { request_uri: String },
}
Expand All @@ -63,10 +63,10 @@ impl RequestUrl {
}
}

impl TryInto<Request> for RequestUrl {
impl TryInto<AuthorizationRequest> for RequestUrl {
type Error = anyhow::Error;

fn try_into(self) -> Result<Request, Self::Error> {
fn try_into(self) -> Result<AuthorizationRequest, Self::Error> {
match self {
RequestUrl::Request(request) => Ok(*request),
RequestUrl::RequestUri { .. } => Err(anyhow!("Request is a request URI.")),
Expand Down Expand Up @@ -125,11 +125,11 @@ pub enum ResponseType {
IdToken,
}

/// [`Request`] is a request from a [crate::relying_party::RelyingParty] (RP) to a [crate::provider::Provider] (SIOP).
/// [`AuthorizationRequest`] is a request from a [crate::relying_party::RelyingParty] (RP) to a [crate::provider::Provider] (SIOP).
#[allow(dead_code)]
#[derive(Debug, Getters, PartialEq, Clone, Default, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Request {
pub struct AuthorizationRequest {
pub(crate) response_type: ResponseType,
pub(crate) response_mode: Option<String>,
#[getset(get = "pub")]
Expand All @@ -153,7 +153,7 @@ pub struct Request {
pub(crate) state: Option<String>,
}

impl Request {
impl AuthorizationRequest {
pub fn is_cross_device_request(&self) -> bool {
self.response_mode == Some("post".to_string())
}
Expand Down Expand Up @@ -212,7 +212,7 @@ mod tests {
.unwrap();
assert_eq!(
request_url.clone(),
RequestUrl::Request(Box::new(Request {
RequestUrl::Request(Box::new(AuthorizationRequest {
response_type: ResponseType::IdToken,
response_mode: Some("post".to_string()),
client_id: "did:example:\
Expand Down
6 changes: 3 additions & 3 deletions src/request/request_builder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
claims::ClaimRequests,
request::{Request, RequestUrl, ResponseType},
request::{AuthorizationRequest, RequestUrl, ResponseType},
Registration, Scope,
};
use anyhow::{anyhow, Result};
Expand Down Expand Up @@ -43,7 +43,7 @@ impl RequestUrlBuilder {
let request_uri = self.request_uri.take();
match (request_uri, self.is_empty()) {
(Some(request_uri), true) => Ok(RequestUrl::RequestUri { request_uri }),
(None, _) => Ok(RequestUrl::Request(Box::new(Request {
(None, _) => Ok(RequestUrl::Request(Box::new(AuthorizationRequest {
response_type: self
.response_type
.take()
Expand Down Expand Up @@ -120,7 +120,7 @@ mod tests {

assert_eq!(
request_url,
RequestUrl::Request(Box::new(Request {
RequestUrl::Request(Box::new(AuthorizationRequest {
response_type: ResponseType::IdToken,
response_mode: None,
client_id: "did:example:123".to_string(),
Expand Down
22 changes: 11 additions & 11 deletions src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub enum Openid4vpParams {
/// Submission, or a combination of them.
#[derive(Serialize, Default, Deserialize, Debug, Getters, PartialEq)]
#[skip_serializing_none]
pub struct Response {
pub struct AuthorizationResponse {
#[serde(skip)]
#[getset(get = "pub")]
redirect_uri: String,
Expand All @@ -33,7 +33,7 @@ pub struct Response {
state: Option<String>,
}

impl Response {
impl AuthorizationResponse {
pub fn builder() -> ResponseBuilder {
ResponseBuilder::new()
}
Expand All @@ -54,7 +54,7 @@ impl ResponseBuilder {
ResponseBuilder::default()
}

pub fn build(&mut self) -> Result<Response> {
pub fn build(&mut self) -> Result<AuthorizationResponse> {
let redirect_uri = self
.redirect_uri
.take()
Expand Down Expand Up @@ -82,7 +82,7 @@ impl ResponseBuilder {
)),
}?;

Ok(Response {
Ok(AuthorizationResponse {
redirect_uri,
id_token: self.id_token.take(),
openid4vp_response,
Expand All @@ -104,20 +104,20 @@ mod tests {

#[test]
fn test_valid_response() {
assert!(Response::builder()
assert!(AuthorizationResponse::builder()
.redirect_uri("redirect".to_string())
.id_token("id_token".to_string())
.build()
.is_ok());

assert!(Response::builder()
assert!(AuthorizationResponse::builder()
.redirect_uri("redirect".to_string())
.vp_token("vp_token".to_string())
.presentation_submission("presentation_submission".to_string())
.build()
.is_ok());

assert!(Response::builder()
assert!(AuthorizationResponse::builder()
.redirect_uri("redirect".to_string())
.id_token("id_token".to_string())
.vp_token("vp_token".to_string())
Expand All @@ -129,7 +129,7 @@ mod tests {
#[test]
fn test_invalid_response() {
assert_eq!(
Response::builder()
AuthorizationResponse::builder()
.id_token("id_token".to_string())
.build()
.unwrap_err()
Expand All @@ -138,7 +138,7 @@ mod tests {
);

assert_eq!(
Response::builder()
AuthorizationResponse::builder()
.redirect_uri("redirect".to_string())
.vp_token("vp_token".to_string())
.build()
Expand All @@ -148,7 +148,7 @@ mod tests {
);

assert_eq!(
Response::builder()
AuthorizationResponse::builder()
.redirect_uri("redirect".to_string())
.presentation_submission("presentation_submission".to_string())
.build()
Expand All @@ -158,7 +158,7 @@ mod tests {
);

assert_eq!(
Response::builder()
AuthorizationResponse::builder()
.redirect_uri("redirect".to_string())
.presentation_submission("presentation_submission".to_string())
.openid4vp_response_jwt("response".to_string())
Expand Down

0 comments on commit 74a7eb0

Please sign in to comment.