Skip to content

Commit

Permalink
fix: the default options did not get applied
Browse files Browse the repository at this point in the history
  • Loading branch information
ctron committed Jan 26, 2024
1 parent 6238ee4 commit 35bdd19
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 24 deletions.
7 changes: 5 additions & 2 deletions src/agent/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::LoginOptions;
use super::{LoginOptions, LogoutOptions};
use crate::agent::Client;
use std::time::Duration;

Expand All @@ -9,14 +9,17 @@ pub struct AgentConfiguration<C: Client> {
pub scopes: Vec<String>,
pub grace_period: Duration,
pub audience: Option<String>,
pub options: Option<LoginOptions>,

pub default_login_options: Option<LoginOptions>,
pub default_logout_options: Option<LogoutOptions>,
}

impl<C: Client> PartialEq for AgentConfiguration<C> {
fn eq(&self, other: &Self) -> bool {
self.config == other.config
&& self.scopes == other.scopes
&& self.grace_period == other.grace_period
&& self.audience == other.audience
}
}

Expand Down
49 changes: 37 additions & 12 deletions src/agent/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ where
C: Client,
{
Configure(AgentConfiguration<C>),
StartLogin(LoginOptions),
Logout(LogoutOptions),
StartLogin(Option<LoginOptions>),
Logout(Option<LogoutOptions>),
Refresh,
}

Expand Down Expand Up @@ -188,7 +188,8 @@ pub struct InnerConfig {
scopes: Vec<String>,
grace_period: Duration,
audience: Option<String>,
options: Option<LoginOptions>,
default_login_options: Option<LoginOptions>,
default_logout_options: Option<LogoutOptions>,
}

impl<C> InnerAgent<C>
Expand Down Expand Up @@ -331,7 +332,8 @@ where
scopes,
grace_period,
audience,
options,
default_login_options,
default_logout_options,
} = config;

let client = C::from_config(config).await?;
Expand All @@ -340,7 +342,8 @@ where
scopes,
grace_period,
audience,
options,
default_login_options,
default_logout_options,
};

Ok((client, inner))
Expand Down Expand Up @@ -419,7 +422,7 @@ where
fn post_login_redirect(&self) -> Result<(), OAuth2Error> {
let config = self.config.as_ref().ok_or(OAuth2Error::NotInitialized)?;
let Some(redirect_callback) = config
.options
.default_login_options
.as_ref()
.and_then(|opts| opts.post_login_redirect_callback.clone())
else {
Expand Down Expand Up @@ -529,18 +532,21 @@ where
self.configured(Self::make_client(config).await).await;
}

fn start_login(&mut self, options: LoginOptions) -> Result<(), OAuth2Error> {
fn start_login(&mut self, options: Option<LoginOptions>) -> Result<(), OAuth2Error> {
let client = self.client.as_ref().ok_or(OAuth2Error::NotInitialized)?;
let config = self.config.as_ref().ok_or(OAuth2Error::NotInitialized)?;

let options =
options.unwrap_or_else(|| config.default_login_options.clone().unwrap_or_default());

let current_url = Self::current_url().map_err(OAuth2Error::StartLogin)?;

// take the parameter value first, then the agent configured value, then fall back to the default
let redirect_url = options
.redirect_url
.or_else(|| {
config
.options
.default_login_options
.as_ref()
.and_then(|opts| opts.redirect_url.clone())
})
Expand All @@ -565,7 +571,7 @@ where
let mut login_url = login_context.url;

login_url.query_pairs_mut().extend_pairs(options.query);
if let Some(options) = &config.options {
if let Some(options) = &config.default_login_options {
login_url
.query_pairs_mut()
.extend_pairs(options.query.clone());
Expand All @@ -586,12 +592,19 @@ where
Ok(())
}

fn logout_opts(&mut self, options: LogoutOptions) {
fn logout_opts(&mut self, options: Option<LogoutOptions>) {
if let Some(client) = &self.client {
if let Some(session_state) = self.session_state.clone() {
// let the client know that log out, clients may navigate to a different
// page
log::debug!("Notify client of logout");
let options = options
.or_else(|| {
self.config
.as_ref()
.and_then(|config| config.default_logout_options.clone())
})
.unwrap_or_default();
client.logout(session_state, options);
}
}
Expand All @@ -618,15 +631,27 @@ where
.map_err(|_| Error::NoAgent)
}

fn start_login(&self) -> Result<(), Error> {
self.tx
.try_send(Msg::StartLogin(None))
.map_err(|_| Error::NoAgent)
}

fn start_login_opts(&self, options: LoginOptions) -> Result<(), Error> {
self.tx
.try_send(Msg::StartLogin(options))
.try_send(Msg::StartLogin(Some(options)))
.map_err(|_| Error::NoAgent)
}

fn logout(&self) -> Result<(), Error> {
self.tx
.try_send(Msg::Logout(None))
.map_err(|_| Error::NoAgent)
}

fn logout_opts(&self, options: LogoutOptions) -> Result<(), Error> {
self.tx
.try_send(Msg::Logout(options))
.try_send(Msg::Logout(Some(options)))
.map_err(|_| Error::NoAgent)
}
}
8 changes: 2 additions & 6 deletions src/agent/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,13 @@ pub trait OAuth2Operations<C: Client> {
fn configure(&self, config: AgentConfiguration<C>) -> Result<(), Error>;

/// Start a login flow with default options.
fn start_login(&self) -> Result<(), Error> {
self.start_login_opts(Default::default())
}
fn start_login(&self) -> Result<(), Error>;

/// Start a login flow.
fn start_login_opts(&self, options: LoginOptions) -> Result<(), Error>;

/// Trigger the logout with default options.
fn logout(&self) -> Result<(), Error> {
self.logout_opts(Default::default())
}
fn logout(&self) -> Result<(), Error>;

/// Trigger the logout.
fn logout_opts(&self, options: LogoutOptions) -> Result<(), Error>;
Expand Down
13 changes: 9 additions & 4 deletions src/components/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod agent;
pub use agent::*;

use crate::{
agent::{AgentConfiguration, Client, LoginOptions, OAuth2Operations},
agent::{AgentConfiguration, Client, LoginOptions, LogoutOptions, OAuth2Operations},
context::{LatestAccessToken, OAuth2Context},
};
use agent::Agent as AgentContext;
Expand Down Expand Up @@ -36,9 +36,13 @@ pub struct OAuth2Properties<C: Client> {
#[prop_or_default]
pub children: Children,

/// Default [`LoginOptions`] that will be used for every request
/// Default [`LoginOptions`] that will be used unless more specific options have been requested.
#[prop_or_default]
pub options: Option<LoginOptions>,
pub login_options: Option<LoginOptions>,

/// Default [`LogoutOptions`] that will be used unless more specific options have been requested.
#[prop_or_default]
pub logout_options: Option<LogoutOptions>,
}

impl<C: Client> PartialEq for OAuth2Properties<C> {
Expand Down Expand Up @@ -132,8 +136,9 @@ impl<C: Client> OAuth2<C> {
config: props.config.clone(),
scopes: props.scopes.clone(),
grace_period: props.grace_period,
options: props.options.clone(),
audience: props.audience.clone(),
default_login_options: props.login_options.clone(),
default_logout_options: props.logout_options.clone(),
}
}
}
Expand Down

0 comments on commit 35bdd19

Please sign in to comment.