|
| 1 | +//! Module containing everything related to Keys API. |
| 2 | +//! |
| 3 | +//! More info [here](https://typesense.org/docs/0.20.0/api/api-keys.html). |
| 4 | +
|
| 5 | +use hmac::{Hmac, Mac, NewMac}; |
| 6 | +use serde::{Deserialize, Serialize}; |
| 7 | +use sha2::Sha256; |
| 8 | + |
| 9 | +use super::Client; |
| 10 | +use crate::transport::HttpLowLevel; |
| 11 | + |
| 12 | +/// To interact with the Keys API. |
| 13 | +pub struct ClientKeys<T> { |
| 14 | + pub(super) client: Client<T>, |
| 15 | +} |
| 16 | + |
| 17 | +impl<T> ClientKeys<T> |
| 18 | +where |
| 19 | + T: HttpLowLevel, |
| 20 | +{ |
| 21 | + /// Create an API Key. |
| 22 | + /// |
| 23 | + /// More info [here](https://typesense.org/docs/0.20.0/api/api-keys.html#create-an-api-key). |
| 24 | + pub async fn create( |
| 25 | + &self, |
| 26 | + actions: Vec<Actions>, |
| 27 | + collections: Vec<String>, |
| 28 | + description: impl Into<Option<String>>, |
| 29 | + expires_at: impl Into<Option<usize>>, |
| 30 | + ) -> crate::Result<ClientKeyCreate> { |
| 31 | + let create = Create { |
| 32 | + actions, |
| 33 | + collections, |
| 34 | + description: description.into(), |
| 35 | + expires_at: expires_at.into(), |
| 36 | + }; |
| 37 | + |
| 38 | + let response = self |
| 39 | + .client |
| 40 | + .post("/keys", serde_json::to_vec(&create)?) |
| 41 | + .await?; |
| 42 | + |
| 43 | + let body = response.into_body(); |
| 44 | + Ok(serde_json::from_slice(&body)?) |
| 45 | + } |
| 46 | + |
| 47 | + /// Retrieve (metadata about) a key. |
| 48 | + /// |
| 49 | + /// More info [here](https://typesense.org/docs/0.20.0/api/api-keys.html#retrieve-an-api-key). |
| 50 | + pub async fn retrieve(&self, n: usize) -> crate::Result<ClientKeyRetrieve> { |
| 51 | + let response = self.client.get(format!("/keys/{}", n).as_str()).await?; |
| 52 | + |
| 53 | + let body = response.into_body(); |
| 54 | + Ok(serde_json::from_slice(&body)?) |
| 55 | + } |
| 56 | + |
| 57 | + /// Retrieve (metadata about) all keys. |
| 58 | + /// |
| 59 | + /// More info [here](https://typesense.org/docs/0.20.0/api/api-keys.html#list-all-keys). |
| 60 | + pub async fn retrieve_all(&self) -> crate::Result<ClientKeyRetrieveAll> { |
| 61 | + let response = self.client.get("/keys").await?; |
| 62 | + |
| 63 | + let body = response.into_body(); |
| 64 | + Ok(serde_json::from_slice(&body)?) |
| 65 | + } |
| 66 | + |
| 67 | + /// Delete an API key given its ID. |
| 68 | + /// |
| 69 | + /// More info [here](https://typesense.org/docs/0.20.0/api/api-keys.html#delete-api-key). |
| 70 | + pub async fn delete(&self, n: usize) -> crate::Result<ClientKeyDelete> { |
| 71 | + let response = self.client.delete(format!("/keys/{}", n).as_str()).await?; |
| 72 | + |
| 73 | + let body = response.into_body(); |
| 74 | + Ok(serde_json::from_slice(&body)?) |
| 75 | + } |
| 76 | + |
| 77 | + /// Generate a scoped search API key that can have embedded search parameters in them. |
| 78 | + /// |
| 79 | + /// More info [here](https://typesense.org/docs/0.20.0/api/api-keys.html#generate-scoped-search-key). |
| 80 | + pub async fn generate_scoped_search_key( |
| 81 | + key: impl AsRef<str>, |
| 82 | + filter_by: impl AsRef<str>, |
| 83 | + expires_at: usize, |
| 84 | + ) -> crate::Result<String> { |
| 85 | + let generate_scoped_search_key = GenerateScopedSearchKey { |
| 86 | + filter_by: filter_by.as_ref().to_string(), |
| 87 | + expires_at, |
| 88 | + }; |
| 89 | + let params = serde_json::to_string(&generate_scoped_search_key)?; |
| 90 | + |
| 91 | + let mut mac = Hmac::<Sha256>::new_from_slice(key.as_ref().as_bytes()).unwrap(); |
| 92 | + mac.update(params.as_bytes()); |
| 93 | + let result = mac.finalize(); |
| 94 | + let digest = base64::encode(result.into_bytes()); |
| 95 | + |
| 96 | + let key_prefix = &key.as_ref()[0..4]; |
| 97 | + let raw_scoped_key = format!("{}{}{}", digest, key_prefix, params); |
| 98 | + |
| 99 | + Ok(base64::encode(raw_scoped_key.as_bytes())) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +/// Enum over the possible list of Actions. |
| 104 | +/// |
| 105 | +/// More info [here](https://typesense.org/docs/0.19.0/api/api-keys.html#sample-actions). |
| 106 | +#[derive(Serialize, Deserialize)] |
| 107 | +pub enum Actions { |
| 108 | + /// Allows only search requests. |
| 109 | + #[serde(rename = "documents:search")] |
| 110 | + DocumentsSearch, |
| 111 | + |
| 112 | + /// Allows fetching a single document. |
| 113 | + #[serde(rename = "documents:get")] |
| 114 | + DocumentsGet, |
| 115 | + |
| 116 | + /// Allow all kinds of collection related operations. |
| 117 | + #[serde(rename = "documents:*")] |
| 118 | + DocumentsAll, |
| 119 | + |
| 120 | + /// Allows a collection to be deleted. |
| 121 | + #[serde(rename = "collections:delete")] |
| 122 | + CollectionsDelete, |
| 123 | + |
| 124 | + /// Allows a collection to be created. |
| 125 | + #[serde(rename = "collections:create")] |
| 126 | + CollectionsCreate, |
| 127 | + |
| 128 | + /// Allow all kinds of collection related operations. |
| 129 | + #[serde(rename = "collections:*")] |
| 130 | + CollectionsAll, |
| 131 | + |
| 132 | + /// Allows all operations. |
| 133 | + #[serde(rename = "*")] |
| 134 | + All, |
| 135 | +} |
| 136 | + |
| 137 | +/// Structure returned by [`ClientKeys::create`] function. |
| 138 | +#[derive(Serialize, Deserialize)] |
| 139 | +pub struct ClientKeyCreate { |
| 140 | + /// Key ID |
| 141 | + pub id: usize, |
| 142 | + |
| 143 | + /// Key Actions |
| 144 | + pub actions: Vec<Actions>, |
| 145 | + |
| 146 | + /// Key Collections |
| 147 | + pub collections: Vec<String>, |
| 148 | + |
| 149 | + /// Key Value |
| 150 | + pub value: String, |
| 151 | + |
| 152 | + /// Key Description |
| 153 | + pub description: String, |
| 154 | +} |
| 155 | + |
| 156 | +/// Structure returned by [`ClientKeys::retrieve`] function. |
| 157 | +#[derive(Serialize, Deserialize)] |
| 158 | +pub struct ClientKeyRetrieve { |
| 159 | + /// Key Actions |
| 160 | + pub actions: Vec<Actions>, |
| 161 | + |
| 162 | + /// Key Collections |
| 163 | + pub collections: Vec<String>, |
| 164 | + |
| 165 | + /// Key Description |
| 166 | + pub description: String, |
| 167 | + |
| 168 | + /// Key ID |
| 169 | + pub id: usize, |
| 170 | + |
| 171 | + /// Key Value Prefix |
| 172 | + pub value_prefix: String, |
| 173 | +} |
| 174 | + |
| 175 | +/// Structure returned by [`ClientKeys::retrieve_all`] function. |
| 176 | +#[derive(Serialize, Deserialize)] |
| 177 | +pub struct ClientKeyRetrieveAll { |
| 178 | + /// Vector of all the Keys |
| 179 | + pub keys: Vec<ClientKeyRetrieve>, |
| 180 | +} |
| 181 | + |
| 182 | +/// Structure returned by [`ClientKeys::delete`] function. |
| 183 | +#[derive(Serialize, Deserialize)] |
| 184 | +pub struct ClientKeyDelete { |
| 185 | + /// ID of the deleted Key |
| 186 | + pub id: usize, |
| 187 | +} |
| 188 | + |
| 189 | +#[derive(Serialize)] |
| 190 | +struct Create { |
| 191 | + actions: Vec<Actions>, |
| 192 | + collections: Vec<String>, |
| 193 | + description: Option<String>, |
| 194 | + expires_at: Option<usize>, |
| 195 | +} |
| 196 | + |
| 197 | +#[derive(Serialize)] |
| 198 | +struct GenerateScopedSearchKey { |
| 199 | + filter_by: String, |
| 200 | + expires_at: usize, |
| 201 | +} |
0 commit comments