swagger/scan/active/http_client/
auth.rs

1use super::*;
2use base64::encode;
3//use mapper::digest::Header as RequestParameter;
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, Hash)]
7pub struct Custom {
8    dm: QuePay,
9    name: String,
10    value: String,
11}
12impl Custom {
13    pub fn from_3_vals(vals: Vec<&str>) -> Self {
14        if vals.len() != 3 {
15            panic!("Authentication doesn't match Cherrybomb's scheme!");
16        }
17        let dm = match vals[0].trim().to_lowercase().as_str() {
18            "headers" => QuePay::Headers,
19            "path" => QuePay::Path,
20            "query" => QuePay::Query,
21            "payload" => QuePay::Payload,
22            _ => QuePay::None,
23        };
24        Custom {
25            dm,
26            name: vals[1].to_string(),
27            value: vals[2].to_string(),
28        }
29    }
30}
31#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, Hash)]
32pub enum Authorization {
33    Authorization(Auth),
34    JWT(String),
35    APIKey(String),
36    Cookie(String),
37    Custom(Custom),
38    None,
39}
40impl Default for Authorization {
41    fn default() -> Self {
42        Self::None
43    }
44}
45impl Authorization {
46    pub fn from_parts(tp: &str, value: String) -> Self {
47        match tp {
48            "0" => {
49                let vals: Vec<&str> = value.split(':').collect();
50                Self::Authorization(Auth::Basic(vals[0].to_string(), vals[1].to_string()))
51            }
52            "1" => Self::Authorization(Auth::Bearer(value)),
53            "2" => Self::JWT(value),
54            "3" => Self::APIKey(value),
55            "4" => Self::APIKey(value),
56            "5" => {
57                let vals: Vec<&str> = value.split(',').collect();
58                Self::Custom(Custom::from_3_vals(vals))
59            }
60            _ => Self::None,
61        }
62    }
63    pub fn get_auth(&self) -> Option<RequestParameter> {
64        match self {
65            Self::Authorization(Auth::Basic(username, password)) => Some(RequestParameter {
66                dm: QuePay::Headers,
67                name: String::from("Authorization"),
68                value: format!("Basic {}", encode(format!("{}:{}", username, password))),
69            }),
70            Self::Authorization(Auth::Bearer(token)) => Some(RequestParameter {
71                dm: QuePay::Headers,
72                name: String::from("Authorization"),
73                value: format!("Bearer {}", token),
74            }),
75            Self::JWT(token) => Some(RequestParameter {
76                dm: QuePay::Headers,
77                name: String::from("jwt"),
78                value: token.to_string(),
79            }),
80            Self::APIKey(key) => Some(RequestParameter {
81                dm: QuePay::Headers,
82                name: String::from("X-API-Key"),
83                value: key.to_string(),
84            }),
85            Self::Cookie(cookie) => Some(RequestParameter {
86                dm: QuePay::Headers,
87                name: String::from("Cookie"),
88                value: cookie.to_string(),
89            }),
90            Self::Custom(custom) => Some(RequestParameter {
91                dm: custom.dm,
92                name: custom.name.clone(),
93                value: custom.value.clone(),
94            }),
95            _ => None,
96        }
97    }
98}
99#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, Hash)]
100pub enum Auth {
101    Basic(String, String),
102    Bearer(String),
103    Other,
104}
105impl Default for Auth {
106    fn default() -> Self {
107        Self::Other
108    }
109}