google_cloud_auth/credentials/
anonymous.rs1use crate::credentials::dynamic::CredentialsProvider;
21use crate::credentials::{CacheableResource, Credentials, EntityTag, Result};
22use http::{Extensions, HeaderMap};
23use std::sync::Arc;
24
25#[derive(Debug)]
26struct AnonymousCredentials {
27 entity_tag: EntityTag,
28}
29
30#[derive(Debug, Default)]
32pub struct Builder {}
33
34impl Builder {
35 pub fn new() -> Self {
37 Self::default()
38 }
39
40 pub fn build(self) -> Credentials {
42 Credentials {
43 inner: Arc::new(AnonymousCredentials {
44 entity_tag: EntityTag::new(),
45 }),
46 }
47 }
48}
49
50#[async_trait::async_trait]
51impl CredentialsProvider for AnonymousCredentials {
52 async fn headers(&self, extensions: Extensions) -> Result<CacheableResource<HeaderMap>> {
53 match extensions.get::<EntityTag>() {
54 Some(tag) if self.entity_tag.eq(tag) => Ok(CacheableResource::NotModified),
55 _ => Ok(CacheableResource::New {
56 data: HeaderMap::new(),
57 entity_tag: self.entity_tag.clone(),
58 }),
59 }
60 }
61}
62
63#[cfg(test)]
64mod tests {
65 use super::*;
66
67 type TestResult = std::result::Result<(), Box<dyn std::error::Error>>;
68
69 #[tokio::test]
70 async fn create_anonymous_credentials() -> TestResult {
71 let creds = Builder::new().build();
72 let mut extensions = Extensions::new();
73 let cached_headers = creds.headers(extensions.clone()).await.unwrap();
74 let (headers, entity_tag) = match cached_headers {
75 CacheableResource::New { entity_tag, data } => (data, entity_tag),
76 CacheableResource::NotModified => unreachable!("expecting new headers"),
77 };
78 assert!(headers.is_empty());
79
80 extensions.insert(entity_tag);
81 let cached_headers = creds.headers(extensions).await.unwrap();
82 match cached_headers {
83 CacheableResource::New { .. } => unreachable!("expecting cached headers"),
84 CacheableResource::NotModified => {}
85 }
86 Ok(())
87 }
88}