Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enable user to disable check for token exchange response body #281

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/devicecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ where
C: SyncHttpClient,
EF: ExtraDeviceAuthorizationFields,
{
endpoint_response(http_client.call(self.prepare_request()?)?)
endpoint_response(http_client.call(self.prepare_request()?)?, true)
}

/// Asynchronously sends the request to the authorization server and returns a Future.
Expand All @@ -196,7 +196,7 @@ where
C: AsyncHttpClient<'c>,
EF: ExtraDeviceAuthorizationFields,
{
Box::pin(async move { endpoint_response(http_client.call(self.prepare_request()?).await?) })
Box::pin(async move { endpoint_response(http_client.call(self.prepare_request()?).await?, true) })
}
}

Expand Down Expand Up @@ -413,7 +413,7 @@ where
};

// Explicitly process the response with a DeviceCodeErrorResponse
let res = endpoint_response::<RE, DeviceCodeErrorResponse, TR>(http_response);
let res = endpoint_response::<RE, DeviceCodeErrorResponse, TR>(http_response, true);
match res {
// On a ServerResponse error, the error needs inspecting as a DeviceCodeErrorResponse
// to work out whether a retry needs to happen.
Expand Down
5 changes: 4 additions & 1 deletion src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ pub(crate) fn endpoint_request<'a>(

pub(crate) fn endpoint_response<RE, TE, DO>(
http_response: HttpResponse,
check_body: bool
) -> Result<DO, RequestTokenError<RE, TE>>
where
RE: Error,
Expand All @@ -164,7 +165,9 @@ where
{
check_response_status(&http_response)?;

check_response_body(&http_response)?;
if check_body {
check_response_body(&http_response)?;
}

let response_body = http_response.body().as_slice();
serde_path_to_error::deserialize(&mut serde_json::Deserializer::from_slice(response_body))
Expand Down
4 changes: 2 additions & 2 deletions src/introspection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ where
where
C: SyncHttpClient,
{
endpoint_response(http_client.call(self.prepare_request()?)?)
endpoint_response(http_client.call(self.prepare_request()?)?, true)
}

/// Asynchronously sends the request to the authorization server and returns a Future.
Expand All @@ -180,7 +180,7 @@ where
Self: 'c,
C: AsyncHttpClient<'c>,
{
Box::pin(async move { endpoint_response(http_client.call(self.prepare_request()?).await?) })
Box::pin(async move { endpoint_response(http_client.call(self.prepare_request()?).await?, true) })
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/revocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ where
Self: 'c,
C: AsyncHttpClient<'c>,
{
Box::pin(async move { endpoint_response(http_client.call(self.prepare_request()?).await?) })
Box::pin(async move { endpoint_response(http_client.call(self.prepare_request()?).await?, true) })
}
}

Expand Down
36 changes: 28 additions & 8 deletions src/token/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ where
pkce_verifier: None,
token_url,
redirect_url: self.redirect_url.as_ref().map(Cow::Borrowed),
check_response_body: None,
_phantom: PhantomData,
}
}
Expand Down Expand Up @@ -142,6 +143,7 @@ where
pub(crate) pkce_verifier: Option<PkceCodeVerifier>,
pub(crate) token_url: &'a TokenUrl,
pub(crate) redirect_url: Option<Cow<'a, RedirectUrl>>,
pub(crate) check_response_body: Option<bool>,
pub(crate) _phantom: PhantomData<(TE, TR)>,
}
impl<'a, TE, TR> CodeTokenRequest<'a, TE, TR>
Expand Down Expand Up @@ -187,6 +189,15 @@ where
self
}

///
/// Disables body checks for exchange response.
/// Useful when APIs use a different Content-Type for instance.
///
pub fn disable_check_response_body(mut self) -> Self {
self.check_response_body = Some(false);
self
}

fn prepare_request<RE>(self) -> Result<HttpRequest, RequestTokenError<RE, TE>>
where
RE: Error + 'static,
Expand All @@ -212,6 +223,13 @@ where
.map_err(|err| RequestTokenError::Other(format!("failed to prepare request: {err}")))
}

fn should_check_response_body(&self) -> bool {
match self.check_response_body {
None => true,
Some(c) => c
}
}

/// Synchronously sends the request to the authorization server and awaits a response.
pub fn request<C>(
self,
Expand All @@ -220,7 +238,8 @@ where
where
C: SyncHttpClient,
{
endpoint_response(http_client.call(self.prepare_request()?)?)
let check_body = self.should_check_response_body();
endpoint_response(http_client.call(self.prepare_request()?)?, check_body)
}

/// Asynchronously sends the request to the authorization server and returns a Future.
Expand All @@ -232,7 +251,8 @@ where
Self: 'c,
C: AsyncHttpClient<'c>,
{
Box::pin(async move { endpoint_response(http_client.call(self.prepare_request()?).await?) })
let check_body = self.should_check_response_body();
Box::pin(async move { endpoint_response(http_client.call(self.prepare_request()?).await?, check_body) })
}
}

Expand Down Expand Up @@ -304,7 +324,7 @@ where
where
C: SyncHttpClient,
{
endpoint_response(http_client.call(self.prepare_request()?)?)
endpoint_response(http_client.call(self.prepare_request()?)?, true)
}
/// Asynchronously sends the request to the authorization server and awaits a response.
pub fn request_async<'c, C>(
Expand All @@ -315,7 +335,7 @@ where
Self: 'c,
C: AsyncHttpClient<'c>,
{
Box::pin(async move { endpoint_response(http_client.call(self.prepare_request()?).await?) })
Box::pin(async move { endpoint_response(http_client.call(self.prepare_request()?).await?, true) })
}

fn prepare_request<RE>(&self) -> Result<HttpRequest, RequestTokenError<RE, TE>>
Expand Down Expand Up @@ -408,7 +428,7 @@ where
where
C: SyncHttpClient,
{
endpoint_response(http_client.call(self.prepare_request()?)?)
endpoint_response(http_client.call(self.prepare_request()?)?, true)
}

/// Asynchronously sends the request to the authorization server and awaits a response.
Expand All @@ -420,7 +440,7 @@ where
Self: 'c,
C: AsyncHttpClient<'c>,
{
Box::pin(async move { endpoint_response(http_client.call(self.prepare_request()?).await?) })
Box::pin(async move { endpoint_response(http_client.call(self.prepare_request()?).await?, true) })
}

fn prepare_request<RE>(&self) -> Result<HttpRequest, RequestTokenError<RE, TE>>
Expand Down Expand Up @@ -512,7 +532,7 @@ where
where
C: SyncHttpClient,
{
endpoint_response(http_client.call(self.prepare_request()?)?)
endpoint_response(http_client.call(self.prepare_request()?)?, true)
}

/// Asynchronously sends the request to the authorization server and awaits a response.
Expand All @@ -524,7 +544,7 @@ where
Self: 'c,
C: AsyncHttpClient<'c>,
{
Box::pin(async move { endpoint_response(http_client.call(self.prepare_request()?).await?) })
Box::pin(async move { endpoint_response(http_client.call(self.prepare_request()?).await?, true) })
}

fn prepare_request<RE>(&self) -> Result<HttpRequest, RequestTokenError<RE, TE>>
Expand Down