1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//! OAuth2 credential managers for GCP and Firebase Emulator

pub mod emulator;
pub mod error;
pub mod gcp;

use error::CredentialsError;
use error_stack::{Report, ResultExt};
use headers::{authorization::Bearer, Authorization, HeaderMapExt};
use http::header::HeaderMap;
use std::future::Future;

pub trait Credentials: Send + Sync + 'static {
    /// Implementation for generation of OAuth2 access token
    fn get_access_token(
        &self,
        scopes: &[&str],
    ) -> impl Future<Output = Result<String, Report<CredentialsError>>> + Send;

    /// Set credentials for a API request, by default use bearer authorization for passing access token
    fn set_credentials(
        &self,
        headers: &mut HeaderMap,
        scopes: &[&str],
    ) -> impl Future<Output = Result<(), Report<CredentialsError>>> + Send {
        async move {
            let token = self.get_access_token(scopes).await?;

            headers.typed_insert(
                Authorization::<Bearer>::bearer(&token)
                    .change_context(CredentialsError::InvalidAccessToken)?,
            );

            Ok(())
        }
    }
}