paperclip_core/v3/models/
security_scheme.rs1use super::v2;
2
3macro_rules! to_indexmap {
4 ($v2:expr) => {
5 $v2.scopes.iter().fold(Default::default(), |mut i, (k, v)| {
6 i.insert(k.to_string(), v.to_string());
7 i
8 })
9 };
10}
11
12impl From<v2::SecurityScheme> for openapiv3::SecurityScheme {
13 fn from(v2: v2::SecurityScheme) -> Self {
14 match v2.type_.as_str() {
15 "basic" => openapiv3::SecurityScheme::HTTP {
16 scheme: "basic".to_string(),
17 bearer_format: None,
18 description: v2.description,
19 extensions: Default::default(),
20 },
21 "apiKey" => openapiv3::SecurityScheme::APIKey {
22 location: match v2.in_.unwrap_or_default().as_str() {
23 "query" => openapiv3::APIKeyLocation::Query,
24 "header" => openapiv3::APIKeyLocation::Header,
25 _ => openapiv3::APIKeyLocation::Query,
26 },
27 name: v2.name.unwrap_or_default(),
28 description: v2.description,
29 extensions: Default::default(),
30 },
31 "oauth2" => {
32 let flow = v2.flow.unwrap_or_default();
33 openapiv3::SecurityScheme::OAuth2 {
34 flows: openapiv3::OAuth2Flows {
35 implicit: match flow.as_str() {
36 "implicit" => Some(openapiv3::ImplicitOAuth2Flow {
37 authorization_url: v2.auth_url.clone().unwrap_or_default(),
38 refresh_url: None,
39 scopes: to_indexmap!(v2),
40 extensions: Default::default(),
41 }),
42 _ => None,
43 },
44 password: match flow.as_str() {
45 "password" => Some(openapiv3::PasswordOAuth2Flow {
46 refresh_url: None,
47 token_url: v2.token_url.clone().unwrap_or_default(),
48 scopes: to_indexmap!(v2),
49 extensions: Default::default(),
50 }),
51 _ => None,
52 },
53 client_credentials: match flow.as_str() {
54 "application" => Some(openapiv3::ClientCredentialsOAuth2Flow {
55 refresh_url: None,
56 token_url: v2.token_url.clone().unwrap_or_default(),
57 scopes: to_indexmap!(v2),
58 extensions: Default::default(),
59 }),
60 _ => None,
61 },
62 authorization_code: match flow.as_str() {
63 "accessCode" => Some(openapiv3::AuthorizationCodeOAuth2Flow {
64 authorization_url: v2.auth_url.clone().unwrap_or_default(),
65 token_url: v2.token_url.clone().unwrap_or_default(),
66 refresh_url: None,
67 scopes: to_indexmap!(v2),
68 extensions: Default::default(),
69 }),
70 _ => None,
71 },
72 extensions: Default::default(),
73 },
74 description: v2.description,
75 extensions: Default::default(),
76 }
77 }
78 type_ => {
79 debug_assert!(false, "Invalid Security Type: {}", type_);
80 openapiv3::SecurityScheme::HTTP {
81 scheme: "invalid".to_string(),
82 bearer_format: None,
83 description: v2.description,
84 extensions: Default::default(),
85 }
86 }
87 }
88 }
89}