apisix_admin_client/models/plugins/
key_auth.rs

1use serde::{Deserialize, Serialize};
2use crate::{Result};
3use crate::models::Plugin;
4
5/// Builder pattern to create a KeyAuth
6#[serde_with::skip_serializing_none]
7#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
8pub struct KeyAuthBuilder {
9    pub hide_credentials: Option<bool>,
10    pub query: Option<String>,
11    pub header: Option<String>,
12}
13
14impl KeyAuthBuilder {
15    pub fn new() -> Self {
16        KeyAuth::default().into()
17    }
18
19    /// Apache APISIX will pass the request header or query string that contains the
20    /// authentication information to the Upstream if hide_credentials is false
21    /// Otherwise the authentication information will be removed before proxying
22    pub fn with_hide_credentials(mut self, hide_credentials: bool) -> Self {
23        self.hide_credentials = Some(hide_credentials);
24        self
25    }
26    /// The query string to get the key from
27    /// Lower priority than header
28    pub fn with_query(mut self, query: impl Into<String>) -> Self {
29        self.query = Some(query.into());
30        self
31    }
32
33    /// The header to get the key from
34    pub fn with_header(mut self, header: impl Into<String>) -> Self {
35        self.header = Some(header.into());
36        self
37    }
38
39    pub fn build(self) -> Result<KeyAuth> {
40        Ok(KeyAuth {
41            hide_credentials: self.hide_credentials,
42            query: self.query,
43            header: self.header,
44        })
45    }
46}
47
48/// The key-auth Plugin is used to add an authentication key (API key) to a Route or a Service.
49/// This works well with a Consumer. Consumers of the API can then add their key to
50/// the query string or the header to authenticate their requests.
51/// [Documentation](https://apisix.apache.org/docs/apisix/plugins/key-auth/)
52#[serde_with::skip_serializing_none]
53#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
54pub struct KeyAuth {
55    pub hide_credentials: Option<bool>,
56    pub query: Option<String>,
57    pub header: Option<String>,
58}
59
60impl From<KeyAuth> for KeyAuthBuilder {
61    fn from(item: KeyAuth) -> Self {
62        KeyAuthBuilder {
63            hide_credentials: item.hide_credentials,
64            query: item.query,
65            header: item.header,
66        }
67    }
68}
69impl Plugin for KeyAuth {}
70
71
72// region: tests
73#[cfg(test)]
74mod tests {
75    use serde_json::{to_string, to_string_pretty};
76    use super::*;
77    use tracing::{error, info};
78    use tracing_test::traced_test;
79    use crate::models::admin_upstream_requests::UpstreamType;
80    use crate::models::common::TypedItem;
81
82    #[traced_test]
83    #[tokio::test]
84    async fn test_parse_key_auth_empty_response() {
85        let nodes = r#"{}"#;
86        let nodes: KeyAuth = serde_json::from_str(nodes).unwrap();
87        assert_eq!(nodes.query, None);
88        assert_eq!(nodes.header, None);
89        assert_eq!(nodes.hide_credentials, None);
90    }
91
92    #[traced_test]
93    #[tokio::test]
94    async fn test_parse_key_auth_response() {
95        let nodes = r#"
96        {
97            "hide_credentials": false,
98            "query": "apikey",
99            "header": "apikey"
100        }"#;
101        let nodes: KeyAuth = serde_json::from_str(nodes).unwrap();
102        assert_eq!(nodes.hide_credentials.unwrap(), false);
103        assert_eq!(nodes.query.unwrap(), "apikey");
104        assert_eq!(nodes.header.unwrap(), "apikey");
105    }
106}
107// endregion: tests