From ea8beb985247aac4345ecefc8ec551f52f5f1a24 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Tue, 26 Nov 2019 04:20:43 -0500 Subject: allow multiple oauth configurations using the same auth type this should allow us to configure a separate oauth application for tt web than normal (since the redirect_url needs to be different) --- teleterm/src/config.rs | 99 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 64 insertions(+), 35 deletions(-) (limited to 'teleterm/src/config.rs') diff --git a/teleterm/src/config.rs b/teleterm/src/config.rs index 7454a2a..4000cb7 100644 --- a/teleterm/src/config.rs +++ b/teleterm/src/config.rs @@ -872,51 +872,80 @@ pub fn oauth_configs<'a, D>( ) -> std::result::Result< std::collections::HashMap< crate::protocol::AuthType, - crate::oauth::Config, + std::collections::HashMap< + crate::protocol::AuthClient, + crate::oauth::Config, + >, >, D::Error, > where D: serde::de::Deserializer<'a>, { - let configs = - >::deserialize( - deserializer, - )?; - let mut ret = std::collections::HashMap::new(); - for (key, config) in configs { + let configs = , + >>::deserialize(deserializer)?; + let mut all_configs = std::collections::HashMap::new(); + for (key, client_configs) in configs { let auth_type = crate::protocol::AuthType::try_from(key.as_str()) .map_err(serde::de::Error::custom)?; - let real_config = match auth_type { - crate::protocol::AuthType::RecurseCenter => { - let client_id = config - .client_id - .context(crate::error::OauthMissingConfiguration { - field: "client_id", - auth_type, - }) + let mut auth_type_configs = std::collections::HashMap::new(); + for (key, config) in client_configs { + let auth_client = + crate::protocol::AuthClient::try_from(key.as_str()) .map_err(serde::de::Error::custom)?; - let client_secret = config - .client_secret - .context(crate::error::OauthMissingConfiguration { - field: "client_secret", - auth_type, - }) - .map_err(serde::de::Error::custom)?; - crate::oauth::RecurseCenter::config( - &client_id, - &client_secret, - ) - } - ty if !ty.is_oauth() => { - return Err(Error::AuthTypeNotOauth { ty: auth_type }) - .map_err(serde::de::Error::custom); - } - _ => unreachable!(), - }; - ret.insert(auth_type, real_config); + let real_config = match auth_type { + crate::protocol::AuthType::RecurseCenter => { + let client_id = config + .client_id + .context(crate::error::OauthMissingConfiguration { + field: "client_id", + auth_type, + auth_client, + }) + .map_err(serde::de::Error::custom)?; + let client_secret = config + .client_secret + .context(crate::error::OauthMissingConfiguration { + field: "client_secret", + auth_type, + auth_client, + }) + .map_err(serde::de::Error::custom)?; + let redirect_url = + if auth_client == crate::protocol::AuthClient::Cli { + url::Url::parse(crate::oauth::CLI_REDIRECT_URL) + .unwrap() + } else { + config + .redirect_url + .context( + crate::error::OauthMissingConfiguration { + field: "redirect_url", + auth_type, + auth_client, + }, + ) + .map_err(serde::de::Error::custom)? + }; + crate::oauth::RecurseCenter::config( + &client_id, + &client_secret, + &redirect_url, + ) + } + ty if !ty.is_oauth() => { + return Err(Error::AuthTypeNotOauth { ty: auth_type }) + .map_err(serde::de::Error::custom); + } + _ => unreachable!(), + }; + auth_type_configs.insert(auth_client, real_config); + } + all_configs.insert(auth_type, auth_type_configs); } - Ok(ret) + Ok(all_configs) } #[derive(serde::Deserialize, Debug)] -- cgit v1.2.3-54-g00ecf