Skip to content

Commit

Permalink
refactor: destructure to ensure we consumed all the fields
Browse files Browse the repository at this point in the history
  • Loading branch information
ctron committed Jan 26, 2024
1 parent e9a8abb commit 6238ee4
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 29 deletions.
14 changes: 10 additions & 4 deletions src/agent/client/oauth2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,25 @@ impl OAuth2Client {

#[async_trait(?Send)]
impl Client for OAuth2Client {
type TokenResponse = ::oauth2::basic::BasicTokenResponse;
type TokenResponse = BasicTokenResponse;
type Configuration = oauth2::Config;
type LoginState = LoginState;
type SessionState = ();

async fn from_config(config: Self::Configuration) -> Result<Self, OAuth2Error> {
let oauth2::Config {
client_id,
auth_url,
token_url,
} = config;

let client = BasicClient::new(
ClientId::new(config.client_id),
ClientId::new(client_id),
None,
AuthUrl::new(config.auth_url)
AuthUrl::new(auth_url)
.map_err(|err| OAuth2Error::Configuration(format!("invalid auth URL: {err}")))?,
Some(
TokenUrl::new(config.token_url).map_err(|err| {
TokenUrl::new(token_url).map_err(|err| {
OAuth2Error::Configuration(format!("invalid token URL: {err}"))
})?,
),
Expand Down
29 changes: 15 additions & 14 deletions src/agent/client/openid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,16 @@ impl Client for OpenIdClient {
);

async fn from_config(config: Self::Configuration) -> Result<Self, OAuth2Error> {
let issuer = IssuerUrl::new(config.issuer_url)
let openid::Config {
client_id,
issuer_url,
end_session_url,
after_logout_url,
post_logout_redirect_name,
additional_trusted_audiences,
} = config;

let issuer = IssuerUrl::new(issuer_url)
.map_err(|err| OAuth2Error::Configuration(format!("invalid issuer URL: {err}")))?;

let metadata = ExtendedProviderMetadata::discover_async(issuer, async_http_client)
Expand All @@ -94,30 +103,22 @@ impl Client for OpenIdClient {
OAuth2Error::Configuration(format!("Failed to discover client: {err}"))
})?;

let end_session_url = config
.additional
.end_session_url
let end_session_url = end_session_url
.map(|url| Url::parse(&url))
.transpose()
.map_err(|err| {
OAuth2Error::Configuration(format!("Unable to parse end_session_url: {err}"))
})?
.or_else(|| metadata.additional_metadata().end_session_endpoint.clone());

let after_logout_url = config.additional.after_logout_url;

let client = CoreClient::from_provider_metadata(
metadata,
ClientId::new(config.client_id.clone()),
None,
);
let client = CoreClient::from_provider_metadata(metadata, ClientId::new(client_id), None);

Ok(Self {
client,
end_session_url,
after_logout_url,
post_logout_redirect_name: config.additional.post_logout_redirect_name,
additional_trusted_audiences: config.additional.additional_trusted_audiences,
post_logout_redirect_name,
additional_trusted_audiences,
})
}

Expand Down Expand Up @@ -271,7 +272,7 @@ impl OpenIdClient {
fn after_logout_url(&self) -> Option<String> {
if let Some(after) = &self.after_logout_url {
if Url::parse(after).is_ok() {
// test if the is an absolute URL
// test if this is an absolute URL
return Some(after.to_string());
}

Expand Down
1 change: 0 additions & 1 deletion src/agent/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ pub struct AgentConfiguration<C: Client> {
pub grace_period: Duration,
pub audience: Option<String>,
pub options: Option<LoginOptions>,
pub valid_audiences: Option<Vec<String>>,
}

impl<C: Client> PartialEq for AgentConfiguration<C> {
Expand Down
20 changes: 14 additions & 6 deletions src/agent/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,13 +326,21 @@ where
}

async fn make_client(config: AgentConfiguration<C>) -> Result<(C, InnerConfig), OAuth2Error> {
let client = C::from_config(config.config).await?;
let AgentConfiguration {
config,
scopes,
grace_period,
audience,
options,
} = config;

let client = C::from_config(config).await?;

let inner = InnerConfig {
scopes: config.scopes,
grace_period: config.grace_period,
audience: config.audience,
options: config.options,
scopes,
grace_period,
audience,
options,
};

Ok((client, inner))
Expand All @@ -341,7 +349,7 @@ where
/// When initializing, try to detect the state from the URL and session state.
///
/// Returns `false` if there is no authentication state found and the result is final.
/// Otherwise it returns `true` and spawns a request for e.g. a code exchange.
/// Otherwise, it returns `true` and spawns a request for e.g. a code exchange.
async fn detect_state(&mut self) -> Result<bool, OAuth2Error> {
let client = self.client.as_ref().ok_or(OAuth2Error::NotInitialized)?;

Expand Down
4 changes: 0 additions & 4 deletions src/components/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ pub struct OAuth2Properties<C: Client> {
#[prop_or_default]
pub audience: Option<String>,

#[prop_or_default]
pub valid_audiences: Option<Vec<String>>,

/// Children which will have access to the [`OAuth2Context`].
#[prop_or_default]
pub children: Children,
Expand Down Expand Up @@ -135,7 +132,6 @@ impl<C: Client> OAuth2<C> {
config: props.config.clone(),
scopes: props.scopes.clone(),
grace_period: props.grace_period,
valid_audiences: props.valid_audiences.clone(),
options: props.options.clone(),
audience: props.audience.clone(),
}
Expand Down

0 comments on commit 6238ee4

Please sign in to comment.