|
| 1 | +// Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License. |
| 2 | +// This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 3 | +// Copyright 2019-Present Datadog, Inc. |
| 4 | +use serde::de::{Error, MapAccess, Visitor}; |
| 5 | +use serde::{Deserialize, Deserializer, Serialize}; |
| 6 | +use serde_with::skip_serializing_none; |
| 7 | +use std::fmt::{self, Formatter}; |
| 8 | + |
| 9 | +/// Object to handle JWT authentication when performing the test. |
| 10 | +#[non_exhaustive] |
| 11 | +#[skip_serializing_none] |
| 12 | +#[derive(Clone, Debug, PartialEq, Serialize)] |
| 13 | +pub struct SyntheticsBasicAuthJWT { |
| 14 | + /// Standard JWT claims to automatically inject. |
| 15 | + #[serde(rename = "addClaims")] |
| 16 | + pub add_claims: Option<crate::datadogV1::model::SyntheticsBasicAuthJWTAddClaims>, |
| 17 | + /// Algorithm to use for the JWT authentication. |
| 18 | + #[serde(rename = "algorithm")] |
| 19 | + pub algorithm: crate::datadogV1::model::SyntheticsBasicAuthJWTAlgorithm, |
| 20 | + /// Token time-to-live in seconds. |
| 21 | + #[serde(rename = "expiresIn")] |
| 22 | + pub expires_in: Option<i64>, |
| 23 | + /// Custom JWT header as a JSON string. |
| 24 | + #[serde(rename = "header")] |
| 25 | + pub header: Option<String>, |
| 26 | + /// JWT claims as a JSON string. |
| 27 | + #[serde(rename = "payload")] |
| 28 | + pub payload: String, |
| 29 | + /// Signing key for the JWT authentication. Use the shared secret for `HS256` |
| 30 | + /// or the private key (PEM format) for `RS256` and `ES256`. |
| 31 | + #[serde(rename = "secret")] |
| 32 | + pub secret: String, |
| 33 | + /// Prefix added before the token in the `Authorization` header. Defaults to `Bearer`. |
| 34 | + #[serde(rename = "tokenPrefix")] |
| 35 | + pub token_prefix: Option<String>, |
| 36 | + /// The type of authentication to use when performing the test. |
| 37 | + #[serde(rename = "type")] |
| 38 | + pub type_: crate::datadogV1::model::SyntheticsBasicAuthJWTType, |
| 39 | + #[serde(flatten)] |
| 40 | + pub additional_properties: std::collections::BTreeMap<String, serde_json::Value>, |
| 41 | + #[serde(skip)] |
| 42 | + #[serde(default)] |
| 43 | + pub(crate) _unparsed: bool, |
| 44 | +} |
| 45 | + |
| 46 | +impl SyntheticsBasicAuthJWT { |
| 47 | + pub fn new( |
| 48 | + algorithm: crate::datadogV1::model::SyntheticsBasicAuthJWTAlgorithm, |
| 49 | + payload: String, |
| 50 | + secret: String, |
| 51 | + type_: crate::datadogV1::model::SyntheticsBasicAuthJWTType, |
| 52 | + ) -> SyntheticsBasicAuthJWT { |
| 53 | + SyntheticsBasicAuthJWT { |
| 54 | + add_claims: None, |
| 55 | + algorithm, |
| 56 | + expires_in: None, |
| 57 | + header: None, |
| 58 | + payload, |
| 59 | + secret, |
| 60 | + token_prefix: None, |
| 61 | + type_, |
| 62 | + additional_properties: std::collections::BTreeMap::new(), |
| 63 | + _unparsed: false, |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + pub fn add_claims( |
| 68 | + mut self, |
| 69 | + value: crate::datadogV1::model::SyntheticsBasicAuthJWTAddClaims, |
| 70 | + ) -> Self { |
| 71 | + self.add_claims = Some(value); |
| 72 | + self |
| 73 | + } |
| 74 | + |
| 75 | + pub fn expires_in(mut self, value: i64) -> Self { |
| 76 | + self.expires_in = Some(value); |
| 77 | + self |
| 78 | + } |
| 79 | + |
| 80 | + pub fn header(mut self, value: String) -> Self { |
| 81 | + self.header = Some(value); |
| 82 | + self |
| 83 | + } |
| 84 | + |
| 85 | + pub fn token_prefix(mut self, value: String) -> Self { |
| 86 | + self.token_prefix = Some(value); |
| 87 | + self |
| 88 | + } |
| 89 | + |
| 90 | + pub fn additional_properties( |
| 91 | + mut self, |
| 92 | + value: std::collections::BTreeMap<String, serde_json::Value>, |
| 93 | + ) -> Self { |
| 94 | + self.additional_properties = value; |
| 95 | + self |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +impl<'de> Deserialize<'de> for SyntheticsBasicAuthJWT { |
| 100 | + fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 101 | + where |
| 102 | + D: Deserializer<'de>, |
| 103 | + { |
| 104 | + struct SyntheticsBasicAuthJWTVisitor; |
| 105 | + impl<'a> Visitor<'a> for SyntheticsBasicAuthJWTVisitor { |
| 106 | + type Value = SyntheticsBasicAuthJWT; |
| 107 | + |
| 108 | + fn expecting(&self, f: &mut Formatter<'_>) -> fmt::Result { |
| 109 | + f.write_str("a mapping") |
| 110 | + } |
| 111 | + |
| 112 | + fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error> |
| 113 | + where |
| 114 | + M: MapAccess<'a>, |
| 115 | + { |
| 116 | + let mut add_claims: Option< |
| 117 | + crate::datadogV1::model::SyntheticsBasicAuthJWTAddClaims, |
| 118 | + > = None; |
| 119 | + let mut algorithm: Option< |
| 120 | + crate::datadogV1::model::SyntheticsBasicAuthJWTAlgorithm, |
| 121 | + > = None; |
| 122 | + let mut expires_in: Option<i64> = None; |
| 123 | + let mut header: Option<String> = None; |
| 124 | + let mut payload: Option<String> = None; |
| 125 | + let mut secret: Option<String> = None; |
| 126 | + let mut token_prefix: Option<String> = None; |
| 127 | + let mut type_: Option<crate::datadogV1::model::SyntheticsBasicAuthJWTType> = None; |
| 128 | + let mut additional_properties: std::collections::BTreeMap< |
| 129 | + String, |
| 130 | + serde_json::Value, |
| 131 | + > = std::collections::BTreeMap::new(); |
| 132 | + let mut _unparsed = false; |
| 133 | + |
| 134 | + while let Some((k, v)) = map.next_entry::<String, serde_json::Value>()? { |
| 135 | + match k.as_str() { |
| 136 | + "addClaims" => { |
| 137 | + if v.is_null() { |
| 138 | + continue; |
| 139 | + } |
| 140 | + add_claims = Some(serde_json::from_value(v).map_err(M::Error::custom)?); |
| 141 | + } |
| 142 | + "algorithm" => { |
| 143 | + algorithm = Some(serde_json::from_value(v).map_err(M::Error::custom)?); |
| 144 | + if let Some(ref _algorithm) = algorithm { |
| 145 | + match _algorithm { |
| 146 | + crate::datadogV1::model::SyntheticsBasicAuthJWTAlgorithm::UnparsedObject(_algorithm) => { |
| 147 | + _unparsed = true; |
| 148 | + }, |
| 149 | + _ => {} |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + "expiresIn" => { |
| 154 | + if v.is_null() { |
| 155 | + continue; |
| 156 | + } |
| 157 | + expires_in = Some(serde_json::from_value(v).map_err(M::Error::custom)?); |
| 158 | + } |
| 159 | + "header" => { |
| 160 | + if v.is_null() { |
| 161 | + continue; |
| 162 | + } |
| 163 | + header = Some(serde_json::from_value(v).map_err(M::Error::custom)?); |
| 164 | + } |
| 165 | + "payload" => { |
| 166 | + payload = Some(serde_json::from_value(v).map_err(M::Error::custom)?); |
| 167 | + } |
| 168 | + "secret" => { |
| 169 | + secret = Some(serde_json::from_value(v).map_err(M::Error::custom)?); |
| 170 | + } |
| 171 | + "tokenPrefix" => { |
| 172 | + if v.is_null() { |
| 173 | + continue; |
| 174 | + } |
| 175 | + token_prefix = |
| 176 | + Some(serde_json::from_value(v).map_err(M::Error::custom)?); |
| 177 | + } |
| 178 | + "type" => { |
| 179 | + type_ = Some(serde_json::from_value(v).map_err(M::Error::custom)?); |
| 180 | + if let Some(ref _type_) = type_ { |
| 181 | + match _type_ { |
| 182 | + crate::datadogV1::model::SyntheticsBasicAuthJWTType::UnparsedObject(_type_) => { |
| 183 | + _unparsed = true; |
| 184 | + }, |
| 185 | + _ => {} |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + &_ => { |
| 190 | + if let Ok(value) = serde_json::from_value(v.clone()) { |
| 191 | + additional_properties.insert(k, value); |
| 192 | + } |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | + let algorithm = algorithm.ok_or_else(|| M::Error::missing_field("algorithm"))?; |
| 197 | + let payload = payload.ok_or_else(|| M::Error::missing_field("payload"))?; |
| 198 | + let secret = secret.ok_or_else(|| M::Error::missing_field("secret"))?; |
| 199 | + let type_ = type_.ok_or_else(|| M::Error::missing_field("type_"))?; |
| 200 | + |
| 201 | + let content = SyntheticsBasicAuthJWT { |
| 202 | + add_claims, |
| 203 | + algorithm, |
| 204 | + expires_in, |
| 205 | + header, |
| 206 | + payload, |
| 207 | + secret, |
| 208 | + token_prefix, |
| 209 | + type_, |
| 210 | + additional_properties, |
| 211 | + _unparsed, |
| 212 | + }; |
| 213 | + |
| 214 | + Ok(content) |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + deserializer.deserialize_any(SyntheticsBasicAuthJWTVisitor) |
| 219 | + } |
| 220 | +} |
0 commit comments