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
38
39
40
41
//! Box API authentication
pub mod access_token;
pub mod auth_ccg;
pub mod auth_client;
pub mod auth_developer;
pub mod auth_errors;

use async_trait::async_trait;

use crate::AuthError;

use self::auth_client::Headers;

/// Trait for authentication methods
#[async_trait]
pub trait Auth<'a> {
    async fn access_token(&mut self) -> Result<String, AuthError>;
    async fn to_json(&mut self) -> Result<String, AuthError>;
    fn base_api_url(&self) -> String;
    fn user_agent(&self) -> String;
}

impl dyn Auth<'_> {
    pub async fn auth_header(&mut self) -> Result<Headers, AuthError> {
        let mut header = Headers::new();

        let header_name = "Authorization".to_string();
        let header_value = format!("Bearer {}", self.access_token().await?);

        header.insert(header_name, header_value);

        Ok(header)
    }
}

// implement debug
impl std::fmt::Debug for dyn Auth<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Auth").finish()
    }
}