diff --git a/Cargo.toml b/Cargo.toml index 739a289..303c71e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,2 +1,3 @@ [workspace] members = ["azure-kusto-data"] +resolver = "2" diff --git a/azure-kusto-data/src/authorization_policy.rs b/azure-kusto-data/src/authorization_policy.rs index d5132fd..472c8fe 100644 --- a/azure-kusto-data/src/authorization_policy.rs +++ b/azure-kusto-data/src/authorization_policy.rs @@ -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())); diff --git a/azure-kusto-data/src/connection_string.rs b/azure-kusto-data/src/connection_string.rs index 2495eb9..61511cc 100644 --- a/azure-kusto-data/src/connection_string.rs +++ b/azure-kusto-data/src/connection_string.rs @@ -22,7 +22,7 @@ use crate::error::ConnectionStringError; /// Function that handles the device code flow. pub type DeviceCodeFunction = Arc String + Send + Sync>; /// Function that returns a token. -pub type TokenCallbackFunction = Arc String + Send + Sync>; +pub type TokenCallbackFunction = Arc String + Send + Sync>; #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] enum ConnectionStringKey { diff --git a/azure-kusto-data/src/credentials.rs b/azure-kusto-data/src/credentials.rs index c4b3642..b4b4771 100644 --- a/azure-kusto-data/src/credentials.rs +++ b/azure-kusto-data/src/credentials.rs @@ -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; @@ -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 { - Ok(TokenResponse { - token: AccessToken::new(self.token.clone()), + async fn get_token(&self, _: &[&str]) -> azure_core::Result { + 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. @@ -28,16 +33,29 @@ pub struct CallbackTokenCredential { pub(crate) time_to_live: Option, } +impl Debug for CallbackTokenCredential { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + f.debug_struct("CallbackTokenCredential") + .field("token_callback", &"") + .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 { + async fn get_token(&self, scopes: &[&str]) -> azure_core::Result { 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(()) + } }