Skip to content

Commit

Permalink
Merge pull request #10 from 8451/0.1.1-auth-bugfix-update-dep
Browse files Browse the repository at this point in the history
Updated Dependencies & Bugfix
  • Loading branch information
aidankovacic-8451 authored Nov 9, 2022
2 parents a23b2b7 + c0e683b commit d2b5032
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 28 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@
/target

.env

# For IntelliJ IDEA users
.idea
16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,25 @@

[package]
name = "flight-cli"
version = "0.1.0"
version = "0.1.1"
license = "MIT License"
description = "A Command Line Interface for Apache Arrow Flight"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
arrow = "20.0.0"
arrow-flight = "20.0.0"
azure_core = "0.4.0"
azure_identity = "0.5.0"
arrow = "26.0.0"
arrow-flight = "26.0.0"
azure_core = "0.6.0"
azure_identity = "0.7.0"
chrono = "0.4.20"
clap = { version = "3.2.13", features = ["derive"] }
confy = "0.4.0"
clap = { version = "4.0.18", features = ["derive"] }
confy = "0.5.1"
futures = "0.3.21"
http = "0.2.8"
oauth2 = "4.2.3"
parquet = { version = "20.0.0", features = ["async"] }
parquet = { version = "26.0.0", features = ["async"] }
serde = { version = "1.0.140", features = ["derive"] }
tokio = { version = "1.20.0", features = ["tokio-macros", "net"] }
tonic = { version = "0.8.0", features = ["tls", "tls-roots"] }
Expand Down
39 changes: 28 additions & 11 deletions src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,18 @@ impl FromStr for IdentityProvider {
pub async fn authorize(
identity_provider: &IdentityProvider,
scope: &String,
) -> Result<AccessToken, Box<dyn Error>> {
) -> Result<Option<AccessToken>, Box<dyn Error>> {
let stored_token = check_stored_token(identity_provider)?;
if let Some(access_token) = stored_token.0 {
Ok(access_token)
Ok(Some(access_token))
} else {
let refresh_token = stored_token.1;
match identity_provider {
IdentityProvider::Azure => {
Ok(azure::azure_authorize(scope.to_string(), refresh_token).await?)
Ok(Some(azure::azure_authorize(scope.to_string(), refresh_token).await?))
},
IdentityProvider::None => {
Ok(None)
}
_ => Err(Box::new(CliError::new(
"Identity Provider not currently implemented!".to_string(),
Expand All @@ -87,7 +90,7 @@ pub async fn authorize(
fn check_stored_token(
provider: &IdentityProvider,
) -> Result<(Option<AccessToken>, Option<RefreshToken>), Box<dyn Error>> {
let store: AuthStore = confy::load("flight-cli-auth")?;
let store: AuthStore = confy::load("flight-cli", "auth-store")?;
if !store.token.secret().to_string().eq("") && store.identity_provider == *provider {
let current_time = Utc::now();
if store.expiry_time > current_time {
Expand Down Expand Up @@ -128,25 +131,39 @@ fn store_access_token(
}
};

confy::store("flight-cli-auth", &new_store)?;
confy::store("flight-cli", "auth-store",&new_store)?;
Ok(())
}

pub struct OAuth2Interceptor {
pub(crate) access_token: String,
pub(crate) access_token: Option<String>,
}

impl OAuth2Interceptor {
pub fn new(token: Option<AccessToken>) -> Self {
if let Some(access_token) = token {
Self { access_token: Some(access_token.secret().clone()) }
} else {
Self { access_token: None }
}
}
}

impl Interceptor for OAuth2Interceptor {
fn call(&mut self, mut request: Request<()>) -> Result<Request<()>, Status> {
if !self.access_token.eq("") {
let new_token: MetadataValue<_> =
format!("Bearer {}", self.access_token).parse().unwrap();
request.metadata_mut().insert("authorization", new_token);
}
if let Some(access_token) = &self.access_token {
if access_token == "" {
let new_token: MetadataValue<_> =
format!("Bearer {}", access_token).parse().unwrap();
request.metadata_mut().insert("authorization", new_token);
}
};

Ok(request)
}
}


#[derive(Serialize, Deserialize, Debug)]
pub struct AuthStore {
token: AccessToken,
Expand Down
2 changes: 1 addition & 1 deletion src/auth/azure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ async fn azure_browser_authorization(
let code = development::naive_redirect_server(&c, 47471)?;

// Exchange the token with one that can be used for authorization
let tr = c.exchange(code).await?;
let tr = c.exchange(azure_core::new_http_client(), code).await?;
let access_token = AccessToken::new(tr.access_token().secret().to_string());
store_access_token(
&access_token,
Expand Down
17 changes: 9 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,13 @@ impl Default for ConnectionConfig {
}
}

static APP_NAME: &str = env!("CARGO_PKG_NAME");
static CONFIG_NAME: &str = "server-cfg";

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let cli = Cli::parse();
let cfg: ConnectionConfig = confy::load("flight-cli")?;
let cfg: ConnectionConfig = confy::load(APP_NAME, CONFIG_NAME)?;
let tls_flag: &bool = &cli.tls;

let cfg = merge_cfg_and_options(cfg, &cli);
Expand All @@ -230,7 +233,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
let client = connect_to_flight_server(&new_cfg).await;
match client {
Ok(_) => {
confy::store("flight-cli", &new_cfg)?;
confy::store(APP_NAME, CONFIG_NAME, &new_cfg)?;
println!("Changed flight server to {}", new_cfg.address);
Ok(())
}
Expand All @@ -245,7 +248,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
tls: cfg.tls,
tls_certs: None,
};
confy::store("flight-cli", &new_cfg)?;
confy::store(APP_NAME, CONFIG_NAME, &new_cfg)?;
println!(
"Set OAuth Provider to {:?} with scope \"{}\"",
provider, scope
Expand All @@ -269,7 +272,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
tls: true,
tls_certs: Some(tls_paths),
};
confy::store("flight-cli", &new_cfg)?;
confy::store(APP_NAME, CONFIG_NAME, &new_cfg)?;
Ok(())
}
Commands::ClearTlsCerts { disable_tls } => {
Expand All @@ -280,7 +283,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
tls: !*disable_tls,
tls_certs: None,
};
confy::store("flight-cli", &new_cfg)?;
confy::store(APP_NAME, CONFIG_NAME, &new_cfg)?;
Ok(())
}
Commands::CurrentServer => {
Expand Down Expand Up @@ -459,9 +462,7 @@ async fn connect_to_flight_server(
};
Ok(FlightServiceClient::with_interceptor(
channel,
OAuth2Interceptor {
access_token: token.secret().clone(),
},
OAuth2Interceptor::new(token),
))
}

Expand Down

0 comments on commit d2b5032

Please sign in to comment.