Skip to content

Commit

Permalink
Fix connection string
Browse files Browse the repository at this point in the history
  • Loading branch information
AsafMah committed Jan 17, 2024
1 parent 09ee9e8 commit b244624
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[workspace]
members = ["azure-kusto-data"]
resolver = "2"
4 changes: 3 additions & 1 deletion azure-kusto-data/src/authorization_policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ impl Policy for AuthorizationPolicy {
}
};

let token = cred.get_token(&resource).await?;
let scope = format!("{}/.default", resource);

let token = cred.get_token(&[&scope]).await?;

request.insert_header(AUTHORIZATION, &format!("Bearer {}", token.token.secret()));

Expand Down
2 changes: 1 addition & 1 deletion azure-kusto-data/src/connection_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::error::ConnectionStringError;
/// Function that handles the device code flow.
pub type DeviceCodeFunction = Arc<dyn Fn(&str) -> String + Send + Sync>;
/// Function that returns a token.
pub type TokenCallbackFunction = Arc<dyn Fn(&str) -> String + Send + Sync>;
pub type TokenCallbackFunction = Arc<dyn Fn(&[&str]) -> String + Send + Sync>;

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
enum ConnectionStringKey {
Expand Down
32 changes: 25 additions & 7 deletions azure-kusto-data/src/credentials.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! Custom credentials for Azure Data Explorer.
use crate::connection_string::TokenCallbackFunction;
use azure_core::auth::{AccessToken, TokenCredential, TokenResponse};
use azure_core::auth::{AccessToken, TokenCredential};
use std::fmt::{Debug, Formatter};
use std::time::Duration;
use time::OffsetDateTime;

Expand All @@ -14,12 +15,16 @@ pub struct ConstTokenCredential {
}
#[async_trait::async_trait]
impl TokenCredential for ConstTokenCredential {
async fn get_token(&self, _resource: &str) -> azure_core::Result<TokenResponse> {
Ok(TokenResponse {
token: AccessToken::new(self.token.clone()),
async fn get_token(&self, _: &[&str]) -> azure_core::Result<AccessToken> {
Ok(AccessToken {
token: self.token.clone().into(),
expires_on: OffsetDateTime::now_utc() + Duration::from_secs(SECONDS_IN_50_YEARS),
})
}

async fn clear_cache(&self) -> azure_core::Result<()> {
Ok(())
}
}

/// Uses a user provided callback that accepts the resource and returns a token in order to authenticate.
Expand All @@ -28,16 +33,29 @@ pub struct CallbackTokenCredential {
pub(crate) time_to_live: Option<Duration>,
}

impl Debug for CallbackTokenCredential {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CallbackTokenCredential")
.field("token_callback", &"<REDACTED>")
.field("time_to_live", &self.time_to_live)
.finish()
}
}

#[async_trait::async_trait]
impl TokenCredential for CallbackTokenCredential {
async fn get_token(&self, resource: &str) -> azure_core::Result<TokenResponse> {
async fn get_token(&self, scopes: &[&str]) -> azure_core::Result<AccessToken> {
let callback = &self.token_callback;
Ok(TokenResponse {
token: AccessToken::new(callback(resource)),
Ok(AccessToken {
token: callback(scopes).into(),
expires_on: OffsetDateTime::now_utc()
+ self
.time_to_live
.unwrap_or(Duration::from_secs(SECONDS_IN_50_YEARS)),
})
}

async fn clear_cache(&self) -> azure_core::Result<()> {
Ok(())
}
}

0 comments on commit b244624

Please sign in to comment.