Skip to content

Commit

Permalink
Add UserInfoRequest::set_response_type (#146)
Browse files Browse the repository at this point in the history
Resolves #145.
  • Loading branch information
FabianLars committed Jan 30, 2024
1 parent 502ad93 commit 53a82e6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,8 @@ pub use types::{
};

pub use user_info::{
UserInfoClaims, UserInfoError, UserInfoJsonWebToken, UserInfoRequest, UserInfoUrl,
UserInfoClaims, UserInfoError, UserInfoJsonWebToken, UserInfoRequest, UserInfoResponseType,
UserInfoUrl,
};
use verification::{AudiencesClaim, IssuerClaim};
pub use verification::{
Expand Down Expand Up @@ -1183,6 +1184,7 @@ where
.ok_or(ConfigurationError::MissingUrl("userinfo"))?,
access_token,
require_signed_response: false,
response_type: UserInfoResponseType::Json,
signed_response_verifier: UserInfoVerifier::new(
self.client_id.clone(),
self.issuer.clone(),
Expand Down
33 changes: 32 additions & 1 deletion src/user_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ where
pub(super) access_token: AccessToken,
pub(super) require_signed_response: bool,
pub(super) signed_response_verifier: UserInfoVerifier<'static, JE, JS, JT, JU, K>,
pub(super) response_type: UserInfoResponseType,
}
impl<'a, JE, JS, JT, JU, K> UserInfoRequest<'a, JE, JS, JT, JU, K>
where
Expand Down Expand Up @@ -94,11 +95,15 @@ where

fn prepare_request(&self) -> HttpRequest {
let (auth_header, auth_value) = auth_bearer(&self.access_token);
let accept_value = match self.response_type {
UserInfoResponseType::Jwt => MIME_TYPE_JWT,
_ => MIME_TYPE_JSON,
};
HttpRequest {
url: self.url.url().clone(),
method: Method::GET,
headers: vec![
(ACCEPT, HeaderValue::from_static(MIME_TYPE_JSON)),
(ACCEPT, HeaderValue::from_static(accept_value)),
(auth_header, auth_value),
]
.into_iter()
Expand Down Expand Up @@ -193,6 +198,14 @@ where
.require_audience_match(aud_required);
self
}

///
/// Specifies the expected response type by setting the `Accept` header. Note that the server can ignore this header.
///
pub fn set_response_type(mut self, response_type: UserInfoResponseType) -> Self {
self.response_type = response_type;
self
}
}

///
Expand Down Expand Up @@ -444,6 +457,24 @@ new_url_type![
UserInfoUrl
];

///
/// Indicates via the `Accept` header the body response type the server should use to return the user info. Note that the server can ignore this header.
///
/// Defaults to Json.
///
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum UserInfoResponseType {
///
/// Sets the `Accept` header to `application/json`.
///
Json,
///
/// Sets the `Accept` header to `application/jwt`.
///
Jwt,
}

///
/// Error retrieving user info.
///
Expand Down

0 comments on commit 53a82e6

Please sign in to comment.