2021-11-09 01:31:27 +08:00
|
|
|
use std::str;
|
|
|
|
|
|
|
|
use actix_web::{web, HttpRequest, HttpResponse};
|
2022-01-04 21:10:30 +08:00
|
|
|
|
2022-03-03 01:22:34 +08:00
|
|
|
use meilisearch_auth::{error::AuthControllerError, Action, AuthController, Key};
|
2021-11-09 01:31:27 +08:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use serde_json::Value;
|
2022-02-14 22:32:41 +08:00
|
|
|
use time::OffsetDateTime;
|
2021-11-09 01:31:27 +08:00
|
|
|
|
2022-03-05 03:12:44 +08:00
|
|
|
use crate::extractors::{
|
|
|
|
authentication::{policies::*, GuardedData},
|
|
|
|
sequential_extractor::SeqHandler,
|
|
|
|
};
|
2022-03-03 01:22:34 +08:00
|
|
|
use meilisearch_error::{Code, ResponseError};
|
2021-11-09 01:31:27 +08:00
|
|
|
|
|
|
|
pub fn configure(cfg: &mut web::ServiceConfig) {
|
|
|
|
cfg.service(
|
|
|
|
web::resource("")
|
2022-03-05 03:12:44 +08:00
|
|
|
.route(web::post().to(SeqHandler(create_api_key)))
|
|
|
|
.route(web::get().to(SeqHandler(list_api_keys))),
|
2021-11-09 01:31:27 +08:00
|
|
|
)
|
|
|
|
.service(
|
|
|
|
web::resource("/{api_key}")
|
2022-03-05 03:12:44 +08:00
|
|
|
.route(web::get().to(SeqHandler(get_api_key)))
|
|
|
|
.route(web::patch().to(SeqHandler(patch_api_key)))
|
|
|
|
.route(web::delete().to(SeqHandler(delete_api_key))),
|
2021-11-09 01:31:27 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn create_api_key(
|
|
|
|
auth_controller: GuardedData<MasterPolicy, AuthController>,
|
|
|
|
body: web::Json<Value>,
|
|
|
|
_req: HttpRequest,
|
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2022-03-03 01:22:34 +08:00
|
|
|
let v = body.into_inner();
|
|
|
|
let res = tokio::task::spawn_blocking(move || -> Result<_, AuthControllerError> {
|
|
|
|
let key = auth_controller.create_key(v)?;
|
|
|
|
Ok(KeyView::from_key(key, &auth_controller))
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.map_err(|e| ResponseError::from_msg(e.to_string(), Code::Internal))??;
|
2021-11-09 01:31:27 +08:00
|
|
|
|
|
|
|
Ok(HttpResponse::Created().json(res))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn list_api_keys(
|
|
|
|
auth_controller: GuardedData<MasterPolicy, AuthController>,
|
|
|
|
_req: HttpRequest,
|
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2022-03-03 01:22:34 +08:00
|
|
|
let res = tokio::task::spawn_blocking(move || -> Result<_, AuthControllerError> {
|
|
|
|
let keys = auth_controller.list_keys()?;
|
|
|
|
let res: Vec<_> = keys
|
|
|
|
.into_iter()
|
|
|
|
.map(|k| KeyView::from_key(k, &auth_controller))
|
|
|
|
.collect();
|
|
|
|
Ok(res)
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.map_err(|e| ResponseError::from_msg(e.to_string(), Code::Internal))??;
|
2021-11-09 01:31:27 +08:00
|
|
|
|
2022-01-04 21:10:30 +08:00
|
|
|
Ok(HttpResponse::Ok().json(KeyListView::from(res)))
|
2021-11-09 01:31:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_api_key(
|
|
|
|
auth_controller: GuardedData<MasterPolicy, AuthController>,
|
|
|
|
path: web::Path<AuthParam>,
|
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2022-03-03 01:22:34 +08:00
|
|
|
let api_key = path.into_inner().api_key;
|
|
|
|
let res = tokio::task::spawn_blocking(move || -> Result<_, AuthControllerError> {
|
|
|
|
let key = auth_controller.get_key(&api_key)?;
|
|
|
|
Ok(KeyView::from_key(key, &auth_controller))
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.map_err(|e| ResponseError::from_msg(e.to_string(), Code::Internal))??;
|
2021-11-09 01:31:27 +08:00
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().json(res))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn patch_api_key(
|
|
|
|
auth_controller: GuardedData<MasterPolicy, AuthController>,
|
|
|
|
body: web::Json<Value>,
|
|
|
|
path: web::Path<AuthParam>,
|
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2022-03-03 01:22:34 +08:00
|
|
|
let api_key = path.into_inner().api_key;
|
|
|
|
let body = body.into_inner();
|
|
|
|
let res = tokio::task::spawn_blocking(move || -> Result<_, AuthControllerError> {
|
|
|
|
let key = auth_controller.update_key(&api_key, body)?;
|
|
|
|
Ok(KeyView::from_key(key, &auth_controller))
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
.map_err(|e| ResponseError::from_msg(e.to_string(), Code::Internal))??;
|
2021-11-09 01:31:27 +08:00
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().json(res))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn delete_api_key(
|
|
|
|
auth_controller: GuardedData<MasterPolicy, AuthController>,
|
|
|
|
path: web::Path<AuthParam>,
|
|
|
|
) -> Result<HttpResponse, ResponseError> {
|
2022-03-03 01:22:34 +08:00
|
|
|
let api_key = path.into_inner().api_key;
|
|
|
|
tokio::task::spawn_blocking(move || auth_controller.delete_key(&api_key))
|
|
|
|
.await
|
|
|
|
.map_err(|e| ResponseError::from_msg(e.to_string(), Code::Internal))??;
|
2021-11-09 01:31:27 +08:00
|
|
|
|
2021-12-06 22:45:41 +08:00
|
|
|
Ok(HttpResponse::NoContent().finish())
|
2021-11-09 01:31:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct AuthParam {
|
|
|
|
api_key: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct KeyView {
|
|
|
|
description: Option<String>,
|
|
|
|
key: String,
|
|
|
|
actions: Vec<Action>,
|
|
|
|
indexes: Vec<String>,
|
2022-02-14 22:32:41 +08:00
|
|
|
#[serde(serialize_with = "time::serde::rfc3339::option::serialize")]
|
|
|
|
expires_at: Option<OffsetDateTime>,
|
|
|
|
#[serde(serialize_with = "time::serde::rfc3339::serialize")]
|
|
|
|
created_at: OffsetDateTime,
|
|
|
|
#[serde(serialize_with = "time::serde::rfc3339::serialize")]
|
|
|
|
updated_at: OffsetDateTime,
|
2021-11-09 01:31:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl KeyView {
|
2022-01-12 22:35:33 +08:00
|
|
|
fn from_key(key: Key, auth: &AuthController) -> Self {
|
2021-11-09 01:31:27 +08:00
|
|
|
let key_id = str::from_utf8(&key.id).unwrap();
|
2022-01-12 22:35:33 +08:00
|
|
|
let generated_key = auth.generate_key(key_id).unwrap_or_default();
|
2021-11-09 01:31:27 +08:00
|
|
|
|
|
|
|
KeyView {
|
|
|
|
description: key.description,
|
|
|
|
key: generated_key,
|
|
|
|
actions: key.actions,
|
|
|
|
indexes: key.indexes,
|
2022-02-14 22:32:41 +08:00
|
|
|
expires_at: key.expires_at,
|
|
|
|
created_at: key.created_at,
|
|
|
|
updated_at: key.updated_at,
|
2021-11-09 01:31:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-04 21:10:30 +08:00
|
|
|
|
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
struct KeyListView {
|
|
|
|
results: Vec<KeyView>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Vec<KeyView>> for KeyListView {
|
|
|
|
fn from(results: Vec<KeyView>) -> Self {
|
|
|
|
Self { results }
|
|
|
|
}
|
|
|
|
}
|