Skip to main content

openapi_nexus/spec/oas30/spec/
security_scheme.rs

1use serde::{Deserialize, Serialize};
2
3use super::{ErrorRef, Flows, FromRef, OpenApiV30Spec, Ref, RefType};
4
5/// Defines a security scheme that can be used by the operations (OAS 3.0: no mutualTLS).
6#[allow(clippy::large_enum_variant)]
7#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
8#[serde(tag = "type")]
9pub enum SecurityScheme {
10    #[serde(rename = "apiKey")]
11    ApiKey {
12        #[serde(skip_serializing_if = "Option::is_none")]
13        description: Option<String>,
14
15        name: String,
16
17        #[serde(rename = "in")]
18        location: String,
19    },
20
21    #[serde(rename = "http")]
22    Http {
23        #[serde(skip_serializing_if = "Option::is_none")]
24        description: Option<String>,
25
26        scheme: String,
27
28        #[serde(rename = "bearerFormat")]
29        bearer_format: Option<String>,
30    },
31
32    #[serde(rename = "oauth2")]
33    OAuth2 {
34        #[serde(skip_serializing_if = "Option::is_none")]
35        description: Option<String>,
36
37        flows: Flows,
38    },
39
40    #[serde(rename = "openIdConnect")]
41    OpenIdConnect {
42        #[serde(skip_serializing_if = "Option::is_none")]
43        description: Option<String>,
44
45        #[serde(rename = "openIdConnectUrl")]
46        open_id_connect_url: String,
47    },
48}
49
50impl FromRef for SecurityScheme {
51    fn from_ref(spec: &OpenApiV30Spec, path: &str) -> Result<Self, ErrorRef> {
52        let refpath = path.parse::<Ref>()?;
53
54        match refpath.kind {
55            RefType::SecurityScheme => spec
56                .components
57                .as_ref()
58                .and_then(|cs| cs.security_schemes.get(&refpath.name))
59                .ok_or_else(|| ErrorRef::Unresolvable {
60                    path: path.to_owned(),
61                })
62                .and_then(|oor| oor.resolve(spec)),
63            typ => Err(ErrorRef::MismatchedType {
64                expected: typ,
65                actual: RefType::SecurityScheme,
66            }),
67        }
68    }
69}