diff --git a/meilisearch-auth/src/key.rs b/meilisearch-auth/src/key.rs index f55993607..baac68637 100644 --- a/meilisearch-auth/src/key.rs +++ b/meilisearch-auth/src/key.rs @@ -29,21 +29,17 @@ pub struct Key { impl Key { pub fn create_from_value(value: Value) -> Result { let name = match value.get("name") { - Some(Value::Null) => None, - Some(des) => Some( - from_value(des.clone()) - .map_err(|_| AuthControllerError::InvalidApiKeyName(des.clone()))?, - ), - None => None, + None | Some(Value::Null) => None, + Some(des) => from_value(des.clone()) + .map(Some) + .map_err(|_| AuthControllerError::InvalidApiKeyName(des.clone()))?, }; let description = match value.get("description") { - Some(Value::Null) => None, - Some(des) => Some( - from_value(des.clone()) - .map_err(|_| AuthControllerError::InvalidApiKeyDescription(des.clone()))?, - ), - None => None, + None | Some(Value::Null) => None, + Some(des) => from_value(des.clone()) + .map(Some) + .map_err(|_| AuthControllerError::InvalidApiKeyDescription(des.clone()))?, }; let uid = value.get("uid").map_or_else( diff --git a/meilisearch-auth/src/lib.rs b/meilisearch-auth/src/lib.rs index 9f9c59c35..578093abf 100644 --- a/meilisearch-auth/src/lib.rs +++ b/meilisearch-auth/src/lib.rs @@ -63,16 +63,16 @@ impl AuthController { .ok_or_else(|| AuthControllerError::ApiKeyNotFound(uid.to_string())) } - pub fn get_uid_from_sha(&self, key: &[u8]) -> Result> { + pub fn get_optional_uid_from_sha(&self, sha: &[u8]) -> Result> { match &self.master_key { - Some(master_key) => self.store.get_uid_from_sha(key, master_key.as_bytes()), + Some(master_key) => self.store.get_uid_from_sha(sha, master_key.as_bytes()), None => Ok(None), } } - pub fn try_get_uid_from_sha(&self, key: &str) -> Result { - self.get_uid_from_sha(key.as_bytes())? - .ok_or_else(|| AuthControllerError::ApiKeyNotFound(key.to_string())) + pub fn get_uid_from_sha(&self, sha: &str) -> Result { + self.get_optional_uid_from_sha(sha.as_bytes())? + .ok_or_else(|| AuthControllerError::ApiKeyNotFound(sha.to_string())) } pub fn get_key_filters( diff --git a/meilisearch-http/src/extractors/authentication/mod.rs b/meilisearch-http/src/extractors/authentication/mod.rs index fdce0f5f0..a6384492c 100644 --- a/meilisearch-http/src/extractors/authentication/mod.rs +++ b/meilisearch-http/src/extractors/authentication/mod.rs @@ -147,7 +147,7 @@ pub mod policies { validation } - /// Extracts the key id used to sign the payload from the payload, without performing any validation. + /// Extracts the key id used to sign the payload, without performing any validation. fn extract_key_id(token: &str) -> Option { let mut validation = tenant_token_validation(); validation.insecure_disable_signature_validation(); @@ -188,7 +188,7 @@ pub mod policies { return Some(filters); } else if let Some(action) = Action::from_repr(A) { // API key - if let Ok(Some(uid)) = auth.get_uid_from_sha(token.as_bytes()) { + if let Ok(Some(uid)) = auth.get_optional_uid_from_sha(token.as_bytes()) { if let Ok(true) = auth.is_key_authorized(uid, action, index) { return auth.get_key_filters(uid, None).ok(); } diff --git a/meilisearch-http/src/routes/api_key.rs b/meilisearch-http/src/routes/api_key.rs index 37ff80ec6..cfe81b301 100644 --- a/meilisearch-http/src/routes/api_key.rs +++ b/meilisearch-http/src/routes/api_key.rs @@ -69,7 +69,7 @@ pub async fn get_api_key( let key = path.into_inner().key; let res = tokio::task::spawn_blocking(move || -> Result<_, AuthControllerError> { - let uid = Uuid::parse_str(&key).or_else(|_| auth_controller.try_get_uid_from_sha(&key))?; + let uid = Uuid::parse_str(&key).or_else(|_| auth_controller.get_uid_from_sha(&key))?; let key = auth_controller.get_key(uid)?; Ok(KeyView::from_key(key, &auth_controller)) @@ -88,7 +88,7 @@ pub async fn patch_api_key( let key = path.into_inner().key; let body = body.into_inner(); let res = tokio::task::spawn_blocking(move || -> Result<_, AuthControllerError> { - let uid = Uuid::parse_str(&key).or_else(|_| auth_controller.try_get_uid_from_sha(&key))?; + let uid = Uuid::parse_str(&key).or_else(|_| auth_controller.get_uid_from_sha(&key))?; let key = auth_controller.update_key(uid, body)?; Ok(KeyView::from_key(key, &auth_controller)) @@ -105,7 +105,7 @@ pub async fn delete_api_key( ) -> Result { let key = path.into_inner().key; tokio::task::spawn_blocking(move || { - let uid = Uuid::parse_str(&key).or_else(|_| auth_controller.try_get_uid_from_sha(&key))?; + let uid = Uuid::parse_str(&key).or_else(|_| auth_controller.get_uid_from_sha(&key))?; auth_controller.delete_key(uid) }) .await