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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use reqwest_middleware::RequestBuilder;
use std::fmt::Debug;

/// Allows for cloning of an authentication system
pub trait AuthClone {
    fn clone_box(&self) -> Box<dyn Authenticate>;
}

/// What the authentication does to the request in order to
/// authenticate the client.
pub trait Authenticate: Sync + Send + Debug + AuthClone {
    /// Authenticates the Request with the given authentication credentials
    fn authenticate(&self, request: RequestBuilder) -> RequestBuilder;
}

// TODO: Doc String
#[derive(Clone, PartialEq, Debug)]
pub struct SecretKeyCredential {
    pub auth_id: String,
    pub auth_token: String,
}

impl SecretKeyCredential {
    pub fn new(auth_id: String, auth_token: String) -> Box<Self> {
        Box::new(SecretKeyCredential {
            auth_id,
            auth_token,
        })
    }
}

impl AuthClone for SecretKeyCredential {
    fn clone_box(&self) -> Box<dyn Authenticate> {
        Box::new(self.clone())
    }
}

impl Authenticate for SecretKeyCredential {
    fn authenticate(&self, request: RequestBuilder) -> RequestBuilder {
        request.query(&[
            ("auth-id".to_string(), self.auth_id.clone()),
            ("auth-token".to_string(), self.auth_token.clone()),
        ])
    }
}

// TODO: Doc String
#[derive(Clone, PartialEq, Debug)]
pub struct WebsiteKeyCredential {
    key: String,
    host: String,
}

impl WebsiteKeyCredential {
    pub fn new(key: &str, host: &str) -> Box<Self> {
        Box::new(Self {
            key: key.to_string(),
            host: host.to_string(),
        })
    }
}

impl AuthClone for WebsiteKeyCredential {
    fn clone_box(&self) -> Box<dyn Authenticate> {
        Box::new(self.clone())
    }
}

impl Authenticate for WebsiteKeyCredential {
    fn authenticate(&self, mut request: RequestBuilder) -> RequestBuilder {
        request = request.query(&[("key".to_string(), self.key.clone())]);
        request = request.header(reqwest::header::REFERER, self.host.clone());
        request
    }
}