smarty_rust_sdk/sdk/
authentication.rs

1use reqwest_middleware::RequestBuilder;
2use std::fmt::Debug;
3
4/// Allows for cloning of an authentication system
5pub trait AuthClone {
6    fn clone_box(&self) -> Box<dyn Authenticate>;
7}
8
9/// What the authentication does to the request in order to
10/// authenticate the client.
11pub trait Authenticate: Sync + Send + Debug + AuthClone {
12    /// Authenticates the Request with the given authentication credentials
13    fn authenticate(&self, request: RequestBuilder) -> RequestBuilder;
14}
15
16#[derive(Clone, PartialEq, Debug)]
17pub struct SecretKeyCredential {
18    pub auth_id: String,
19    pub auth_token: String,
20}
21
22impl SecretKeyCredential {
23    pub fn new(auth_id: String, auth_token: String) -> Box<Self> {
24        Box::new(SecretKeyCredential {
25            auth_id,
26            auth_token,
27        })
28    }
29}
30
31impl AuthClone for SecretKeyCredential {
32    fn clone_box(&self) -> Box<dyn Authenticate> {
33        Box::new(self.clone())
34    }
35}
36
37impl Authenticate for SecretKeyCredential {
38    fn authenticate(&self, request: RequestBuilder) -> RequestBuilder {
39        request.query(&[
40            ("auth-id".to_string(), self.auth_id.clone()),
41            ("auth-token".to_string(), self.auth_token.clone()),
42        ])
43    }
44}
45
46#[derive(Clone, PartialEq, Debug)]
47pub struct WebsiteKeyCredential {
48    key: String,
49    host: String,
50}
51
52impl WebsiteKeyCredential {
53    pub fn new(key: &str, host: &str) -> Box<Self> {
54        Box::new(Self {
55            key: key.to_string(),
56            host: host.to_string(),
57        })
58    }
59}
60
61impl AuthClone for WebsiteKeyCredential {
62    fn clone_box(&self) -> Box<dyn Authenticate> {
63        Box::new(self.clone())
64    }
65}
66
67impl Authenticate for WebsiteKeyCredential {
68    fn authenticate(&self, mut request: RequestBuilder) -> RequestBuilder {
69        request = request.query(&[("key".to_string(), self.key.clone())]);
70        request = request.header(reqwest::header::REFERER, self.host.clone());
71        request
72    }
73}