| 367 | } |
| 368 | |
| 369 | async fn list_api_keys( |
| 370 | &self, |
| 371 | request: Request<api::ListApiKeysRequest>, |
| 372 | ) -> Result<Response<api::ListApiKeysResponse>, Status> { |
| 373 | let req = request.get_ref(); |
| 374 | |
| 375 | let tenant_id = if req.tenant_id.is_empty() { |
| 376 | None |
| 377 | } else { |
| 378 | Some(Uuid::from_str(&req.tenant_id).map_err(|e| e.status())?) |
| 379 | }; |
| 380 | |
| 381 | if req.is_admin && tenant_id.is_some() { |
| 382 | return Err(Status::invalid_argument( |
| 383 | "tenant_id can not be set with is_admin set to true", |
| 384 | )); |
| 385 | } |
| 386 | |
| 387 | if !req.is_admin && tenant_id.is_none() { |
| 388 | return Err(Status::invalid_argument( |
| 389 | "either is_admin or tenant_id must be set", |
| 390 | )); |
| 391 | } |
| 392 | |
| 393 | self.validator |
| 394 | .validate( |
| 395 | request.extensions(), |
| 396 | validator::ValidateApiKeysAccess::new(validator::Flag::List, tenant_id), |
| 397 | ) |
| 398 | .await?; |
| 399 | |
| 400 | let filters = api_key::Filters { |
| 401 | is_admin: req.is_admin, |
| 402 | tenant_id, |
| 403 | }; |
| 404 | |
| 405 | let count = api_key::get_count(&filters).await.map_err(|e| e.status())?; |
| 406 | let results = api_key::list(req.limit as i64, req.offset as i64, &filters) |
| 407 | .await |
| 408 | .map_err(|e| e.status())?; |
| 409 | |
| 410 | Ok(Response::new(api::ListApiKeysResponse { |
| 411 | total_count: count as u32, |
| 412 | result: results |
| 413 | .iter() |
| 414 | .map(|ak| api::ApiKey { |
| 415 | id: ak.id.to_string(), |
| 416 | name: ak.name.clone(), |
| 417 | is_admin: ak.is_admin, |
| 418 | tenant_id: match ak.tenant_id { |
| 419 | Some(v) => v.to_string(), |
| 420 | None => "".to_string(), |
| 421 | }, |
| 422 | is_read_only: ak.is_read_only, |
| 423 | }) |
| 424 | .collect(), |
| 425 | })) |
| 426 | } |