openapiv3/
security_scheme.rs

1use indexmap::IndexMap;
2use serde::{Deserialize, Serialize};
3
4/// Defines a security scheme that can be used by the operations.
5/// Supported schemes are HTTP authentication, an API key (either as a
6/// header or as a query parameter), OAuth2's common flows (implicit, password,
7/// application and access code) as defined in RFC6749, and OpenID Connect Discovery.
8#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
9#[serde(tag = "type")]
10pub enum SecurityScheme {
11    #[serde(rename = "apiKey")]
12    APIKey {
13        #[serde(rename = "in")]
14        location: APIKeyLocation,
15        name: String,
16        #[serde(skip_serializing_if = "Option::is_none")]
17        description: Option<String>,
18    },
19    #[serde(rename = "http")]
20    HTTP {
21        // TODO enum. Values recommended (not required) to come from
22        // https://www.iana.org/assignments/http-authschemes/http-authschemes.xhtml
23        scheme: String,
24        #[serde(rename = "bearerFormat")]
25        bearer_format: Option<String>,
26        #[serde(skip_serializing_if = "Option::is_none")]
27        description: Option<String>,
28    },
29    #[serde(rename = "oauth2")]
30    OAuth2 {
31        flows: OAuth2Flows,
32        #[serde(skip_serializing_if = "Option::is_none")]
33        description: Option<String>,
34    },
35    #[serde(rename = "openIdConnect")]
36    OpenIDConnect {
37        #[serde(rename = "openIdConnectUrl")]
38        open_id_connect_url: String,
39        #[serde(skip_serializing_if = "Option::is_none")]
40        description: Option<String>,
41    },
42}
43
44#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
45#[serde(rename_all = "camelCase")]
46pub enum APIKeyLocation {
47    Query,
48    Header,
49    Cookie,
50}
51
52#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
53#[serde(rename_all = "camelCase")]
54pub struct OAuth2Flows {
55    #[serde(default, skip_serializing_if = "Option::is_none")]
56    pub implicit: Option<ImplicitOAuth2Flow>,
57    #[serde(default, skip_serializing_if = "Option::is_none")]
58    pub password: Option<OAuth2Flow>,
59    #[serde(default, skip_serializing_if = "Option::is_none")]
60    pub client_credentials: Option<OAuth2Flow>,
61    #[serde(default, skip_serializing_if = "Option::is_none")]
62    pub authorization_code: Option<AuthCodeOAuth2Flow>,
63}
64
65#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
66#[serde(rename_all = "camelCase")]
67pub struct ImplicitOAuth2Flow {
68    pub authorization_url: String,
69    #[serde(default, skip_serializing_if = "Option::is_none")]
70    pub refresh_url: Option<String>,
71    #[serde(default)]
72    pub scopes: IndexMap<String, String>,
73}
74
75#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
76#[serde(rename_all = "camelCase")]
77pub struct OAuth2Flow {
78    #[serde(default, skip_serializing_if = "Option::is_none")]
79    pub refresh_url: Option<String>,
80    pub token_url: String,
81    #[serde(default)]
82    pub scopes: IndexMap<String, String>,
83}
84
85#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
86#[serde(rename_all = "camelCase")]
87pub struct AuthCodeOAuth2Flow {
88    pub authorization_url: String,
89    pub token_url: String,
90    #[serde(default, skip_serializing_if = "Option::is_none")]
91    pub refresh_url: Option<String>,
92    #[serde(default)]
93    pub scopes: IndexMap<String, String>,
94}