rusty_box/
auth.rs

1//! Box API authentication
2pub mod access_token;
3pub mod auth_ccg;
4pub mod auth_client;
5pub mod auth_developer;
6pub mod auth_errors;
7pub mod auth_jwt;
8pub mod auth_oauth;
9
10use async_trait::async_trait;
11
12use crate::AuthError;
13
14use self::auth_client::Headers;
15
16/// Trait for authentication methods
17#[async_trait]
18pub trait Auth<'a> {
19    async fn access_token(&mut self) -> Result<String, AuthError>;
20    async fn to_json(&mut self) -> Result<String, AuthError>;
21    fn base_api_url(&self) -> String;
22    fn user_agent(&self) -> String;
23}
24
25impl dyn Auth<'_> {
26    pub async fn auth_header(&mut self) -> Result<Headers, AuthError> {
27        let mut header = Headers::new();
28
29        let header_name = "Authorization".to_string();
30        let header_value = format!("Bearer {}", self.access_token().await?);
31
32        header.insert(header_name, header_value);
33
34        Ok(header)
35    }
36}
37
38// implement debug
39impl std::fmt::Debug for dyn Auth<'_> {
40    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41        f.debug_struct("Auth").finish()
42    }
43}