lib_client_jira/
auth.rs

1use async_trait::async_trait;
2use base64::{engine::general_purpose::STANDARD, Engine};
3use reqwest::header::HeaderMap;
4
5use crate::error::Result;
6
7#[async_trait]
8pub trait AuthStrategy: Send + Sync {
9    async fn apply(&self, headers: &mut HeaderMap) -> Result<()>;
10}
11
12/// Basic authentication (email:api_token).
13#[derive(Debug, Clone)]
14pub struct BasicAuth {
15    email: String,
16    api_token: String,
17}
18
19impl BasicAuth {
20    pub fn new(email: impl Into<String>, api_token: impl Into<String>) -> Self {
21        Self {
22            email: email.into(),
23            api_token: api_token.into(),
24        }
25    }
26}
27
28#[async_trait]
29impl AuthStrategy for BasicAuth {
30    async fn apply(&self, headers: &mut HeaderMap) -> Result<()> {
31        let credentials = format!("{}:{}", self.email, self.api_token);
32        let encoded = STANDARD.encode(credentials);
33        headers.insert(
34            "Authorization",
35            format!("Basic {}", encoded).parse().unwrap(),
36        );
37        Ok(())
38    }
39}
40
41/// Bearer token authentication (OAuth2).
42#[derive(Debug, Clone)]
43pub struct BearerAuth {
44    token: String,
45}
46
47impl BearerAuth {
48    pub fn new(token: impl Into<String>) -> Self {
49        Self {
50            token: token.into(),
51        }
52    }
53}
54
55#[async_trait]
56impl AuthStrategy for BearerAuth {
57    async fn apply(&self, headers: &mut HeaderMap) -> Result<()> {
58        headers.insert(
59            "Authorization",
60            format!("Bearer {}", self.token).parse().unwrap(),
61        );
62        Ok(())
63    }
64}