11use crate :: {
22 timeout:: TimeoutExt ,
3+ token_credentials:: cache:: TokenCache ,
34 { AzureCliCredential , ImdsManagedIdentityCredential } ,
45} ;
56use azure_core:: {
@@ -143,26 +144,20 @@ impl TokenCredential for DefaultAzureCredentialEnum {
143144#[ derive( Debug ) ]
144145pub struct DefaultAzureCredential {
145146 sources : Vec < DefaultAzureCredentialEnum > ,
147+ cache : TokenCache ,
146148}
147149
148150impl DefaultAzureCredential {
149151 /// Creates a `DefaultAzureCredential` with specified sources.
150152 ///
151153 /// These sources will be tried in the order provided in the `TokenCredential` authentication flow.
152154 pub fn with_sources ( sources : Vec < DefaultAzureCredentialEnum > ) -> Self {
153- DefaultAzureCredential { sources }
155+ DefaultAzureCredential {
156+ sources,
157+ cache : TokenCache :: new ( ) ,
158+ }
154159 }
155- }
156160
157- impl Default for DefaultAzureCredential {
158- fn default ( ) -> Self {
159- DefaultAzureCredentialBuilder :: new ( ) . build ( )
160- }
161- }
162-
163- #[ cfg_attr( target_arch = "wasm32" , async_trait:: async_trait( ?Send ) ) ]
164- #[ cfg_attr( not( target_arch = "wasm32" ) , async_trait:: async_trait) ]
165- impl TokenCredential for DefaultAzureCredential {
166161 /// Try to fetch a token using each of the credential sources until one succeeds
167162 async fn get_token ( & self , resource : & str ) -> azure_core:: Result < AccessToken > {
168163 let mut errors = Vec :: new ( ) ;
@@ -181,12 +176,32 @@ impl TokenCredential for DefaultAzureCredential {
181176 )
182177 } ) )
183178 }
179+ }
180+
181+ impl Default for DefaultAzureCredential {
182+ fn default ( ) -> Self {
183+ DefaultAzureCredentialBuilder :: new ( ) . build ( )
184+ }
185+ }
186+
187+ #[ cfg_attr( target_arch = "wasm32" , async_trait:: async_trait( ?Send ) ) ]
188+ #[ cfg_attr( not( target_arch = "wasm32" ) , async_trait:: async_trait) ]
189+ impl TokenCredential for DefaultAzureCredential {
190+ async fn get_token ( & self , resource : & str ) -> azure_core:: Result < AccessToken > {
191+ self . cache
192+ . get_token ( resource, self . get_token ( resource) )
193+ . await
194+ }
184195
185196 /// Clear the credential's cache.
186197 async fn clear_cache ( & self ) -> azure_core:: Result < ( ) > {
198+ // clear the internal cache as well as each of the underlying providers
199+ self . cache . clear ( ) . await ?;
200+
187201 for source in & self . sources {
188202 source. clear_cache ( ) . await ?;
189203 }
204+
190205 Ok ( ( ) )
191206 }
192207}
0 commit comments