swagger/
lib.rs

1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3//use serde_yaml::Value;
4use std::collections::HashMap;
5mod refs;
6use refs::*;
7mod path;
8use path::*;
9mod param;
10use param::*;
11mod schema;
12use schema::*;
13mod ep;
14use ep::*;
15pub mod scan;
16pub mod tables;
17pub use tables::*;
18//use mapper::digest::{Method, ParamDescriptor, PayloadDescriptor, QuePay, ValueDescriptor};
19//use mapper::path::Path as DPath;
20pub use scan::*;
21mod utils;
22pub use utils::*;
23
24//Info Object
25#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
26pub struct License {
27    pub name: String,
28    pub url: Option<String>,
29}
30#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
31pub struct Contact {
32    pub name: Option<String>,
33    pub url: Option<String>,
34    pub email: Option<String>,
35}
36#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
37pub struct Info {
38    pub title: String,
39    pub description: Option<String>,
40    #[serde(rename = "termsOfService")]
41    pub tos: Option<String>,
42    pub contact: Option<Contact>,
43    pub license: Option<License>,
44    pub version: String,
45}
46//End Info Object
47//Server Object
48#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
49pub struct ServerVariable {
50    #[serde(rename = "enum")]
51    pub var_enum: Option<Vec<String>>,
52    pub default: String,
53    pub description: Option<String>,
54}
55#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
56pub struct Server {
57    #[serde(rename(deserialize = "url"))]
58    pub base_url: String,
59    pub description: Option<String>,
60    pub variables: Option<HashMap<String, ServerVariable>>,
61}
62
63//End Server Object
64//Path Object
65type Security = HashMap<String, Vec<String>>;
66type Callback = HashMap<String, HashMap<String, PathItem>>;
67type Content = HashMap<String, MediaType>;
68type Examples = HashMap<String, Example>;
69type EncodingMap = HashMap<String, Encoding>;
70//Practicaly Any
71//type Schema = Value;
72type HeaderMap = HashMap<String, HeaderRef>;
73type Responses = HashMap<String, ResponseRef>;
74type Links = HashMap<String, LinkRef>;
75//Any
76type LinkParameters = HashMap<String, Value>;
77#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
78pub struct Link {
79    #[serde(rename = "operationRef")]
80    pub operation_ref: Option<String>,
81    #[serde(rename = "operationId")]
82    pub operation_id: Option<String>,
83    pub parameters: Option<LinkParameters>,
84    //Any
85    #[serde(rename = "requestBody")]
86    pub request_body: Option<Value>,
87    pub description: Option<String>,
88    pub server: Option<Server>,
89}
90#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
91pub struct Response {
92    pub description: Option<String>,
93    pub headers: Option<HeaderMap>,
94    pub content: Option<Content>,
95    pub links: Option<Links>,
96}
97#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
98pub struct Header {
99    pub description: Option<String>,
100    pub required: Option<bool>,
101    pub deprecated: Option<bool>,
102    #[serde(rename = "allowEmptyValue")]
103    pub allow_empty_value: Option<bool>,
104    //Any
105    pub example: Option<Value>,
106    pub examples: Option<Examples>,
107    pub style: Option<String>,
108    pub explode: Option<bool>,
109    #[serde(rename = "allowReserved")]
110    pub allow_reserved: Option<bool>,
111    pub schema: Option<SchemaRef>,
112}
113#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
114pub struct Encoding {
115    #[serde(rename = "contentType")]
116    pub conent_type: Option<String>,
117    pub headers: Option<HeaderMap>,
118    pub style: Option<String>,
119    pub explode: Option<bool>,
120    #[serde(rename = "allowReserved")]
121    pub allow_reserved: Option<bool>,
122}
123#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
124pub struct MediaType {
125    pub schema: Option<SchemaRef>,
126    //Any
127    pub example: Option<Value>,
128    pub examples: Option<Examples>,
129    pub encoding: Option<EncodingMap>,
130}
131#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
132pub struct ExternalDocs {
133    pub url: String,
134    pub description: Option<String>,
135}
136#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
137pub struct ReqBody {
138    pub description: Option<String>,
139    pub content: Content,
140    pub required: Option<bool>,
141}
142#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
143pub struct Example {
144    pub summary: Option<String>,
145    pub description: Option<String>,
146    //Any
147    pub value: Value,
148    #[serde(rename = "externalValue")]
149    pub external_value: Option<String>,
150}
151
152//End Path Object
153
154//Components Object
155type Schemas = HashMap<String, SchemaRef>;
156type Params = HashMap<String, ParamRef>;
157type ReqBodies = HashMap<String, ReqRef>;
158type SecSchemes = HashMap<String, SecSchemeRef>;
159type CallbackComp = HashMap<String, PathItem>;
160type Callbacks = HashMap<String, CallbackRef>;
161#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
162pub struct OAuth {
163    #[serde(rename = "authorizationUrl")]
164    pub authorization_url: Option<String>,
165    #[serde(rename = "tokenUrl")]
166    pub token_url: Option<String>,
167    #[serde(rename = "refreshUrl")]
168    pub refresh_url: Option<String>,
169    pub scopes: HashMap<String, String>,
170}
171#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
172pub struct OAuthFlows {
173    pub implicit: Option<OAuth>,
174    pub password: Option<OAuth>,
175    #[serde(rename = "clientCredentials")]
176    pub client_credentials: Option<OAuth>,
177    #[serde(rename = "authorizationCode")]
178    pub authorization_code: Option<OAuth>,
179}
180#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
181pub struct SecScheme {
182    #[serde(rename = "type")]
183    pub tp: String,
184    pub description: Option<String>,
185    pub name: Option<String>,
186    #[serde(rename = "in")]
187    pub scheme_in: Option<String>,
188    pub scheme: Option<String>,
189    #[serde(rename = "bearerFormat")]
190    pub bearer_format: Option<String>,
191    pub flows: Option<OAuthFlows>,
192    pub openid_connect_url: Option<String>,
193}
194#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
195pub struct Components {
196    pub schemas: Option<Schemas>,
197    pub responses: Option<Responses>,
198    pub parameters: Option<Params>,
199    pub examples: Option<Examples>,
200    #[serde(rename = "requestBodies")]
201    pub request_bodies: Option<ReqBodies>,
202    pub headers: Option<HeaderMap>,
203    #[serde(rename = "securitySchemes")]
204    pub security_schemes: Option<SecSchemes>,
205    pub links: Option<Links>,
206    pub callbacks: Option<Callbacks>,
207}
208//End Components Object
209
210//Tag Object
211#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
212pub struct Tag {
213    pub name: String,
214    pub description: Option<String>,
215    #[serde(rename = "externalDocs")]
216    pub external_docs: Option<ExternalDocs>,
217}
218//End Tag Object
219
220pub trait OAS {
221    fn get_paths(&self) -> Paths;
222    fn version(&self) -> String;
223    fn info(&self) -> Info;
224    fn servers(&self) -> Option<Vec<Server>>;
225    fn components(&self) -> Option<Components>;
226    fn security(&self) -> Option<Vec<Security>>;
227    fn tags(&self) -> Option<Vec<Tag>>;
228    fn ext_docs(&self) -> Option<ExternalDocs>;
229}
230#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
231pub struct Swagger {
232    pub openapi: String,
233    pub info: Info,
234    pub servers: Option<Vec<Server>>,
235    pub paths: Paths,
236    pub components: Option<Components>,
237    pub security: Option<Vec<Security>>,
238    pub tags: Option<Vec<Tag>>,
239    #[serde(rename = "externalDocs")]
240    pub external_docs: Option<ExternalDocs>,
241}
242#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
243pub struct OAS3_1 {
244    pub openapi: String,
245    pub info: Info,
246    pub servers: Option<Vec<Server>>,
247    pub webhooks: Option<Paths>,
248    pub paths: Option<Paths>,
249    pub components: Option<Components>,
250    pub security: Option<Vec<Security>>,
251    pub tags: Option<Vec<Tag>>,
252    #[serde(rename = "externalDocs")]
253    pub external_docs: Option<ExternalDocs>,
254}
255impl OAS for Swagger {
256    fn get_paths(&self) -> Paths {
257        self.paths.clone()
258    }
259    fn version(&self) -> String {
260        self.openapi.clone()
261    }
262    fn info(&self) -> Info {
263        self.info.clone()
264    }
265    fn servers(&self) -> Option<Vec<Server>> {
266        self.servers.clone()
267    }
268    fn components(&self) -> Option<Components> {
269        self.components.clone()
270    }
271    fn security(&self) -> Option<Vec<Security>> {
272        self.security.clone()
273    }
274    fn tags(&self) -> Option<Vec<Tag>> {
275        self.tags.clone()
276    }
277    fn ext_docs(&self) -> Option<ExternalDocs> {
278        self.external_docs.clone()
279    }
280}
281impl OAS for OAS3_1 {
282    fn get_paths(&self) -> Paths {
283        let mut paths = HashMap::new();
284        if let Some(p) = self.paths.clone() {
285            paths.extend(p);
286        }
287        if let Some(p) = self.webhooks.clone() {
288            paths.extend(p);
289        }
290        paths
291    }
292    fn version(&self) -> String {
293        self.openapi.clone()
294    }
295    fn info(&self) -> Info {
296        self.info.clone()
297    }
298    fn servers(&self) -> Option<Vec<Server>> {
299        self.servers.clone()
300    }
301    fn components(&self) -> Option<Components> {
302        self.components.clone()
303    }
304    fn security(&self) -> Option<Vec<Security>> {
305        self.security.clone()
306    }
307    fn tags(&self) -> Option<Vec<Tag>> {
308        self.tags.clone()
309    }
310    fn ext_docs(&self) -> Option<ExternalDocs> {
311        self.external_docs.clone()
312    }
313}
314/*
315impl Swagger{
316    pub fn convert_to_map(&self,swagger_value:Value)->Digest{
317        let a = PassiveSwaggerScan::new(swagger_value.clone()).run(ScanType::Full);
318        print_checks_table(&a);
319        print_alerts_table(&a);
320        let mut eps:Vec<Endpoint> = vec![];
321       // let _ = self.fingerprint(&swagger_value);
322        let _paths:HashMap<String,PathItem> = self.paths.iter().enumerate().map(|(_i,(path,item))|{
323            let methods:HashMap<Method,u32> = item.get_ops().iter().map(|o| (o.0.clone(),1u32)).collect();
324            let dm = item.params().iter().map(|p| {
325                (p.inner(&swagger_value).from(),1u32)
326            }).collect();
327            let mut queries = vec![];
328            for param in item.params(){
329                let inner = param.inner(&swagger_value);
330                if let QuePay::Query = inner.from(){
331                    //needs payload assesment
332                    queries.push(ParamPayload{param:inner.name(),payload:String::new()});
333                }
334            }
335            eps.push(Endpoint{
336                path:item.into_digest_path(path.to_string(),&swagger_value),
337                methods:Split::from_hashmap(&methods),
338                payload_delivery_methods:Split::from_hashmap(&dm),
339                common_req_headers:OtherHeaderMap::default(),
340                common_res_headers:OtherHeaderMap::default(),
341                req_res_payloads:RRPayload::default(),
342            });
343            (path.clone(),item.clone())
344        }).collect();
345        Digest::default()
346    }
347}*/