lib_client_gitlab/
auth.rs

1use async_trait::async_trait;
2use reqwest::header::HeaderMap;
3
4use crate::error::Result;
5
6#[async_trait]
7pub trait AuthStrategy: Send + Sync {
8    async fn apply(&self, headers: &mut HeaderMap) -> Result<()>;
9}
10
11/// Private token authentication.
12#[derive(Debug, Clone)]
13pub struct PrivateTokenAuth {
14    token: String,
15}
16
17impl PrivateTokenAuth {
18    pub fn new(token: impl Into<String>) -> Self {
19        Self {
20            token: token.into(),
21        }
22    }
23}
24
25#[async_trait]
26impl AuthStrategy for PrivateTokenAuth {
27    async fn apply(&self, headers: &mut HeaderMap) -> Result<()> {
28        headers.insert("PRIVATE-TOKEN", self.token.parse().unwrap());
29        Ok(())
30    }
31}
32
33/// OAuth2 bearer token authentication.
34#[derive(Debug, Clone)]
35pub struct OAuthAuth {
36    token: String,
37}
38
39impl OAuthAuth {
40    pub fn new(token: impl Into<String>) -> Self {
41        Self {
42            token: token.into(),
43        }
44    }
45}
46
47#[async_trait]
48impl AuthStrategy for OAuthAuth {
49    async fn apply(&self, headers: &mut HeaderMap) -> Result<()> {
50        headers.insert(
51            "Authorization",
52            format!("Bearer {}", self.token).parse().unwrap(),
53        );
54        Ok(())
55    }
56}
57
58/// Job token authentication (for CI/CD).
59#[derive(Debug, Clone)]
60pub struct JobTokenAuth {
61    token: String,
62}
63
64impl JobTokenAuth {
65    pub fn new(token: impl Into<String>) -> Self {
66        Self {
67            token: token.into(),
68        }
69    }
70}
71
72#[async_trait]
73impl AuthStrategy for JobTokenAuth {
74    async fn apply(&self, headers: &mut HeaderMap) -> Result<()> {
75        headers.insert("JOB-TOKEN", self.token.parse().unwrap());
76        Ok(())
77    }
78}