mirror of
https://github.com/meilisearch/MeiliSearch
synced 2025-07-04 12:27:13 +02:00
Implements the get and delete tasks route
This commit is contained in:
parent
f00a285a6d
commit
742d0ee531
25 changed files with 1787 additions and 85 deletions
|
@ -13,6 +13,7 @@ use meilisearch_types::error::{Code, ResponseError};
|
|||
use meilisearch_types::keys::{CreateApiKey, Key, PatchApiKey};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use time::OffsetDateTime;
|
||||
use utoipa::{IntoParams, OpenApi, ToSchema};
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::PAGINATION_DEFAULT_LIMIT;
|
||||
|
@ -21,6 +22,20 @@ use crate::extractors::authentication::GuardedData;
|
|||
use crate::extractors::sequential_extractor::SeqHandler;
|
||||
use crate::routes::Pagination;
|
||||
|
||||
#[derive(OpenApi)]
|
||||
#[openapi(
|
||||
paths(create_api_key, list_api_keys, get_api_key, patch_api_key, delete_api_key),
|
||||
tags((
|
||||
name = "Keys",
|
||||
description = "Manage API `keys` for a Meilisearch instance. Each key has a given set of permissions.
|
||||
You must have the master key or the default admin key to access the keys route. More information about the keys and their rights.
|
||||
Accessing any route under `/keys` without having set a master key will result in an error.",
|
||||
external_docs(url = "https://www.meilisearch.com/docs/reference/api/keys"),
|
||||
|
||||
)),
|
||||
)]
|
||||
pub struct ApiKeyApi;
|
||||
|
||||
pub fn configure(cfg: &mut web::ServiceConfig) {
|
||||
cfg.service(
|
||||
web::resource("")
|
||||
|
@ -35,6 +50,52 @@ pub fn configure(cfg: &mut web::ServiceConfig) {
|
|||
);
|
||||
}
|
||||
|
||||
|
||||
/// Create an API Key
|
||||
///
|
||||
/// Create an API Key.
|
||||
#[utoipa::path(
|
||||
post,
|
||||
path = "/",
|
||||
tag = "Keys",
|
||||
security(("Bearer" = ["keys.create", "keys.*", "*"])),
|
||||
request_body = CreateApiKey,
|
||||
responses(
|
||||
(status = 202, description = "Key has been created", body = KeyView, content_type = "application/json", example = json!(
|
||||
{
|
||||
"uid": "01b4bc42-eb33-4041-b481-254d00cce834",
|
||||
"key": "d0552b41536279a0ad88bd595327b96f01176a60c2243e906c52ac02375f9bc4",
|
||||
"name": "Indexing Products API key",
|
||||
"description": null,
|
||||
"actions": [
|
||||
"documents.add"
|
||||
],
|
||||
"indexes": [
|
||||
"products"
|
||||
],
|
||||
"expiresAt": "2021-11-13T00:00:00Z",
|
||||
"createdAt": "2021-11-12T10:00:00Z",
|
||||
"updatedAt": "2021-11-12T10:00:00Z"
|
||||
}
|
||||
)),
|
||||
(status = 401, description = "The route has been hit on an unprotected instance", body = ResponseError, content_type = "application/json", example = json!(
|
||||
{
|
||||
"message": "Meilisearch is running without a master key. To access this API endpoint, you must have set a master key at launch.",
|
||||
"code": "missing_master_key",
|
||||
"type": "auth",
|
||||
"link": "https://docs.meilisearch.com/errors#missing_master_key"
|
||||
}
|
||||
)),
|
||||
(status = 401, description = "The authorization header is missing", body = ResponseError, content_type = "application/json", example = json!(
|
||||
{
|
||||
"message": "The Authorization header is missing. It must use the bearer authorization method.",
|
||||
"code": "missing_authorization_header",
|
||||
"type": "auth",
|
||||
"link": "https://docs.meilisearch.com/errors#missing_authorization_header"
|
||||
}
|
||||
)),
|
||||
)
|
||||
)]
|
||||
pub async fn create_api_key(
|
||||
auth_controller: GuardedData<ActionPolicy<{ actions::KEYS_CREATE }>, Data<AuthController>>,
|
||||
body: AwebJson<CreateApiKey, DeserrJsonError>,
|
||||
|
@ -51,11 +112,14 @@ pub async fn create_api_key(
|
|||
Ok(HttpResponse::Created().json(res))
|
||||
}
|
||||
|
||||
#[derive(Deserr, Debug, Clone, Copy)]
|
||||
#[derive(Deserr, Debug, Clone, Copy, IntoParams)]
|
||||
#[deserr(error = DeserrQueryParamError, rename_all = camelCase, deny_unknown_fields)]
|
||||
#[into_params(rename_all = "camelCase", parameter_in = Query)]
|
||||
pub struct ListApiKeys {
|
||||
#[into_params(value_type = usize, default = 0)]
|
||||
#[deserr(default, error = DeserrQueryParamError<InvalidApiKeyOffset>)]
|
||||
pub offset: Param<usize>,
|
||||
#[into_params(value_type = usize, default = PAGINATION_DEFAULT_LIMIT)]
|
||||
#[deserr(default = Param(PAGINATION_DEFAULT_LIMIT), error = DeserrQueryParamError<InvalidApiKeyLimit>)]
|
||||
pub limit: Param<usize>,
|
||||
}
|
||||
|
@ -66,6 +130,60 @@ impl ListApiKeys {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/// Get API Keys
|
||||
///
|
||||
/// List all API Keys
|
||||
/// TODO: Tamo fix the return type
|
||||
#[utoipa::path(
|
||||
get,
|
||||
path = "/",
|
||||
tag = "Keys",
|
||||
security(("Bearer" = ["keys.get", "keys.*", "*"])),
|
||||
params(ListApiKeys),
|
||||
responses(
|
||||
(status = 202, description = "List of keys", body = serde_json::Value, content_type = "application/json", example = json!(
|
||||
{
|
||||
"results": [
|
||||
{
|
||||
"uid": "01b4bc42-eb33-4041-b481-254d00cce834",
|
||||
"key": "d0552b41536279a0ad88bd595327b96f01176a60c2243e906c52ac02375f9bc4",
|
||||
"name": "An API Key",
|
||||
"description": null,
|
||||
"actions": [
|
||||
"documents.add"
|
||||
],
|
||||
"indexes": [
|
||||
"movies"
|
||||
],
|
||||
"expiresAt": "2022-11-12T10:00:00Z",
|
||||
"createdAt": "2021-11-12T10:00:00Z",
|
||||
"updatedAt": "2021-11-12T10:00:00Z"
|
||||
}
|
||||
],
|
||||
"limit": 20,
|
||||
"offset": 0,
|
||||
"total": 1
|
||||
}
|
||||
)),
|
||||
(status = 401, description = "The route has been hit on an unprotected instance", body = ResponseError, content_type = "application/json", example = json!(
|
||||
{
|
||||
"message": "Meilisearch is running without a master key. To access this API endpoint, you must have set a master key at launch.",
|
||||
"code": "missing_master_key",
|
||||
"type": "auth",
|
||||
"link": "https://docs.meilisearch.com/errors#missing_master_key"
|
||||
}
|
||||
)),
|
||||
(status = 401, description = "The authorization header is missing", body = ResponseError, content_type = "application/json", example = json!(
|
||||
{
|
||||
"message": "The Authorization header is missing. It must use the bearer authorization method.",
|
||||
"code": "missing_authorization_header",
|
||||
"type": "auth",
|
||||
"link": "https://docs.meilisearch.com/errors#missing_authorization_header"
|
||||
}
|
||||
)),
|
||||
)
|
||||
)]
|
||||
pub async fn list_api_keys(
|
||||
auth_controller: GuardedData<ActionPolicy<{ actions::KEYS_GET }>, Data<AuthController>>,
|
||||
list_api_keys: AwebQueryParameter<ListApiKeys, DeserrQueryParamError>,
|
||||
|
@ -84,6 +202,52 @@ pub async fn list_api_keys(
|
|||
Ok(HttpResponse::Ok().json(page_view))
|
||||
}
|
||||
|
||||
|
||||
/// Get an API Key
|
||||
///
|
||||
/// Get an API key from its `uid` or its `key` field.
|
||||
#[utoipa::path(
|
||||
get,
|
||||
path = "/{key}",
|
||||
tag = "Keys",
|
||||
security(("Bearer" = ["keys.get", "keys.*", "*"])),
|
||||
params(("uidOrKey" = String, Path, format = Password, example = "7b198a7f-52a0-4188-8762-9ad93cd608b2", description = "The `uid` or `key` field of an existing API key", nullable = false)),
|
||||
responses(
|
||||
(status = 200, description = "The key is returned", body = KeyView, content_type = "application/json", example = json!(
|
||||
{
|
||||
"uid": "01b4bc42-eb33-4041-b481-254d00cce834",
|
||||
"key": "d0552b41536279a0ad88bd595327b96f01176a60c2243e906c52ac02375f9bc4",
|
||||
"name": "An API Key",
|
||||
"description": null,
|
||||
"actions": [
|
||||
"documents.add"
|
||||
],
|
||||
"indexes": [
|
||||
"movies"
|
||||
],
|
||||
"expiresAt": "2022-11-12T10:00:00Z",
|
||||
"createdAt": "2021-11-12T10:00:00Z",
|
||||
"updatedAt": "2021-11-12T10:00:00Z"
|
||||
}
|
||||
)),
|
||||
(status = 401, description = "The route has been hit on an unprotected instance", body = ResponseError, content_type = "application/json", example = json!(
|
||||
{
|
||||
"message": "Meilisearch is running without a master key. To access this API endpoint, you must have set a master key at launch.",
|
||||
"code": "missing_master_key",
|
||||
"type": "auth",
|
||||
"link": "https://docs.meilisearch.com/errors#missing_master_key"
|
||||
}
|
||||
)),
|
||||
(status = 401, description = "The authorization header is missing", body = ResponseError, content_type = "application/json", example = json!(
|
||||
{
|
||||
"message": "The Authorization header is missing. It must use the bearer authorization method.",
|
||||
"code": "missing_authorization_header",
|
||||
"type": "auth",
|
||||
"link": "https://docs.meilisearch.com/errors#missing_authorization_header"
|
||||
}
|
||||
)),
|
||||
)
|
||||
)]
|
||||
pub async fn get_api_key(
|
||||
auth_controller: GuardedData<ActionPolicy<{ actions::KEYS_GET }>, Data<AuthController>>,
|
||||
path: web::Path<AuthParam>,
|
||||
|
@ -103,6 +267,55 @@ pub async fn get_api_key(
|
|||
Ok(HttpResponse::Ok().json(res))
|
||||
}
|
||||
|
||||
|
||||
/// Update an API Key
|
||||
///
|
||||
/// Update an API key from its `uid` or its `key` field.
|
||||
/// Only the `name` and `description` of the api key can be updated.
|
||||
/// If there is an issue with the `key` or `uid` of a key, then you must recreate one from scratch.
|
||||
#[utoipa::path(
|
||||
patch,
|
||||
path = "/{key}",
|
||||
tag = "Keys",
|
||||
security(("Bearer" = ["keys.update", "keys.*", "*"])),
|
||||
params(("uidOrKey" = String, Path, format = Password, example = "7b198a7f-52a0-4188-8762-9ad93cd608b2", description = "The `uid` or `key` field of an existing API key", nullable = false)),
|
||||
request_body = PatchApiKey,
|
||||
responses(
|
||||
(status = 200, description = "The key have been updated", body = KeyView, content_type = "application/json", example = json!(
|
||||
{
|
||||
"uid": "01b4bc42-eb33-4041-b481-254d00cce834",
|
||||
"key": "d0552b41536279a0ad88bd595327b96f01176a60c2243e906c52ac02375f9bc4",
|
||||
"name": "An API Key",
|
||||
"description": null,
|
||||
"actions": [
|
||||
"documents.add"
|
||||
],
|
||||
"indexes": [
|
||||
"movies"
|
||||
],
|
||||
"expiresAt": "2022-11-12T10:00:00Z",
|
||||
"createdAt": "2021-11-12T10:00:00Z",
|
||||
"updatedAt": "2021-11-12T10:00:00Z"
|
||||
}
|
||||
)),
|
||||
(status = 401, description = "The route has been hit on an unprotected instance", body = ResponseError, content_type = "application/json", example = json!(
|
||||
{
|
||||
"message": "Meilisearch is running without a master key. To access this API endpoint, you must have set a master key at launch.",
|
||||
"code": "missing_master_key",
|
||||
"type": "auth",
|
||||
"link": "https://docs.meilisearch.com/errors#missing_master_key"
|
||||
}
|
||||
)),
|
||||
(status = 401, description = "The authorization header is missing", body = ResponseError, content_type = "application/json", example = json!(
|
||||
{
|
||||
"message": "The Authorization header is missing. It must use the bearer authorization method.",
|
||||
"code": "missing_authorization_header",
|
||||
"type": "auth",
|
||||
"link": "https://docs.meilisearch.com/errors#missing_authorization_header"
|
||||
}
|
||||
)),
|
||||
)
|
||||
)]
|
||||
pub async fn patch_api_key(
|
||||
auth_controller: GuardedData<ActionPolicy<{ actions::KEYS_UPDATE }>, Data<AuthController>>,
|
||||
body: AwebJson<PatchApiKey, DeserrJsonError>,
|
||||
|
@ -123,6 +336,39 @@ pub async fn patch_api_key(
|
|||
Ok(HttpResponse::Ok().json(res))
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// Update an API Key
|
||||
///
|
||||
/// Update an API key from its `uid` or its `key` field.
|
||||
/// Only the `name` and `description` of the api key can be updated.
|
||||
/// If there is an issue with the `key` or `uid` of a key, then you must recreate one from scratch.
|
||||
#[utoipa::path(
|
||||
delete,
|
||||
path = "/{key}",
|
||||
tag = "Keys",
|
||||
security(("Bearer" = ["keys.delete", "keys.*", "*"])),
|
||||
params(("uidOrKey" = String, Path, format = Password, example = "7b198a7f-52a0-4188-8762-9ad93cd608b2", description = "The `uid` or `key` field of an existing API key", nullable = false)),
|
||||
responses(
|
||||
(status = NO_CONTENT, description = "The key have been removed"),
|
||||
(status = 401, description = "The route has been hit on an unprotected instance", body = ResponseError, content_type = "application/json", example = json!(
|
||||
{
|
||||
"message": "Meilisearch is running without a master key. To access this API endpoint, you must have set a master key at launch.",
|
||||
"code": "missing_master_key",
|
||||
"type": "auth",
|
||||
"link": "https://docs.meilisearch.com/errors#missing_master_key"
|
||||
}
|
||||
)),
|
||||
(status = 401, description = "The authorization header is missing", body = ResponseError, content_type = "application/json", example = json!(
|
||||
{
|
||||
"message": "The Authorization header is missing. It must use the bearer authorization method.",
|
||||
"code": "missing_authorization_header",
|
||||
"type": "auth",
|
||||
"link": "https://docs.meilisearch.com/errors#missing_authorization_header"
|
||||
}
|
||||
)),
|
||||
)
|
||||
)]
|
||||
pub async fn delete_api_key(
|
||||
auth_controller: GuardedData<ActionPolicy<{ actions::KEYS_DELETE }>, Data<AuthController>>,
|
||||
path: web::Path<AuthParam>,
|
||||
|
@ -144,19 +390,30 @@ pub struct AuthParam {
|
|||
key: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
#[derive(Debug, Serialize, ToSchema)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct KeyView {
|
||||
pub(super) struct KeyView {
|
||||
/// The name of the API Key if any
|
||||
name: Option<String>,
|
||||
/// The description of the API Key if any
|
||||
description: Option<String>,
|
||||
/// The actual API Key you can send to Meilisearch
|
||||
key: String,
|
||||
/// The `Uuid` specified while creating the key or autogenerated by Meilisearch.
|
||||
uid: Uuid,
|
||||
/// The actions accessible with this key.
|
||||
actions: Vec<Action>,
|
||||
/// The indexes accessible with this key.
|
||||
indexes: Vec<String>,
|
||||
/// The expiration date of the key. Once this timestamp is exceeded the key is not deleted but cannot be used anymore.
|
||||
#[serde(serialize_with = "time::serde::rfc3339::option::serialize")]
|
||||
expires_at: Option<OffsetDateTime>,
|
||||
/// The date of creation of this API Key.
|
||||
#[schema(read_only)]
|
||||
#[serde(serialize_with = "time::serde::rfc3339::serialize")]
|
||||
created_at: OffsetDateTime,
|
||||
/// The date of the last update made on this key.
|
||||
#[schema(read_only)]
|
||||
#[serde(serialize_with = "time::serde::rfc3339::serialize")]
|
||||
updated_at: OffsetDateTime,
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue