2021-11-09 01:31:27 +08:00
|
|
|
use crate::action::Action;
|
|
|
|
use crate::error::{AuthControllerError, Result};
|
2022-05-25 16:32:47 +08:00
|
|
|
use crate::store::KeyId;
|
|
|
|
|
2021-11-09 01:31:27 +08:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use serde_json::{from_value, Value};
|
2022-02-14 22:32:41 +08:00
|
|
|
use time::format_description::well_known::Rfc3339;
|
|
|
|
use time::macros::{format_description, time};
|
|
|
|
use time::{Date, OffsetDateTime, PrimitiveDateTime};
|
2022-05-25 16:32:47 +08:00
|
|
|
use uuid::Uuid;
|
2021-11-09 01:31:27 +08:00
|
|
|
|
|
|
|
#[derive(Debug, Deserialize, Serialize)]
|
|
|
|
pub struct Key {
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub description: Option<String>,
|
2022-05-25 16:32:47 +08:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub name: Option<String>,
|
|
|
|
pub uid: KeyId,
|
2021-11-09 01:31:27 +08:00
|
|
|
pub actions: Vec<Action>,
|
|
|
|
pub indexes: Vec<String>,
|
2022-02-14 22:32:41 +08:00
|
|
|
#[serde(with = "time::serde::rfc3339::option")]
|
|
|
|
pub expires_at: Option<OffsetDateTime>,
|
|
|
|
#[serde(with = "time::serde::rfc3339")]
|
|
|
|
pub created_at: OffsetDateTime,
|
|
|
|
#[serde(with = "time::serde::rfc3339")]
|
|
|
|
pub updated_at: OffsetDateTime,
|
2021-11-09 01:31:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Key {
|
|
|
|
pub fn create_from_value(value: Value) -> Result<Self> {
|
2022-05-25 16:32:47 +08:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2022-02-03 01:18:17 +08:00
|
|
|
let description = match value.get("description") {
|
|
|
|
Some(Value::Null) => None,
|
|
|
|
Some(des) => Some(
|
2021-11-09 01:31:27 +08:00
|
|
|
from_value(des.clone())
|
2022-02-03 01:18:17 +08:00
|
|
|
.map_err(|_| AuthControllerError::InvalidApiKeyDescription(des.clone()))?,
|
|
|
|
),
|
|
|
|
None => None,
|
|
|
|
};
|
2021-11-09 01:31:27 +08:00
|
|
|
|
2022-05-25 16:32:47 +08:00
|
|
|
let uid = value.get("uid").map_or_else(
|
|
|
|
|| Ok(Uuid::new_v4()),
|
|
|
|
|uid| {
|
|
|
|
from_value(uid.clone())
|
|
|
|
.map_err(|_| AuthControllerError::InvalidApiKeyUid(uid.clone()))
|
|
|
|
},
|
|
|
|
)?;
|
2021-11-09 01:31:27 +08:00
|
|
|
|
|
|
|
let actions = value
|
|
|
|
.get("actions")
|
|
|
|
.map(|act| {
|
|
|
|
from_value(act.clone())
|
|
|
|
.map_err(|_| AuthControllerError::InvalidApiKeyActions(act.clone()))
|
|
|
|
})
|
|
|
|
.ok_or(AuthControllerError::MissingParameter("actions"))??;
|
|
|
|
|
|
|
|
let indexes = value
|
|
|
|
.get("indexes")
|
|
|
|
.map(|ind| {
|
|
|
|
from_value(ind.clone())
|
|
|
|
.map_err(|_| AuthControllerError::InvalidApiKeyIndexes(ind.clone()))
|
|
|
|
})
|
|
|
|
.ok_or(AuthControllerError::MissingParameter("indexes"))??;
|
|
|
|
|
|
|
|
let expires_at = value
|
|
|
|
.get("expiresAt")
|
2021-12-06 22:45:41 +08:00
|
|
|
.map(parse_expiration_date)
|
|
|
|
.ok_or(AuthControllerError::MissingParameter("expiresAt"))??;
|
2021-11-09 01:31:27 +08:00
|
|
|
|
2022-02-14 22:32:41 +08:00
|
|
|
let created_at = OffsetDateTime::now_utc();
|
|
|
|
let updated_at = created_at;
|
2021-11-09 01:31:27 +08:00
|
|
|
|
|
|
|
Ok(Self {
|
2022-05-25 16:32:47 +08:00
|
|
|
name,
|
2021-11-09 01:31:27 +08:00
|
|
|
description,
|
2022-05-25 16:32:47 +08:00
|
|
|
uid,
|
2021-11-09 01:31:27 +08:00
|
|
|
actions,
|
|
|
|
indexes,
|
|
|
|
expires_at,
|
|
|
|
created_at,
|
|
|
|
updated_at,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn update_from_value(&mut self, value: Value) -> Result<()> {
|
|
|
|
if let Some(des) = value.get("description") {
|
|
|
|
let des = from_value(des.clone())
|
|
|
|
.map_err(|_| AuthControllerError::InvalidApiKeyDescription(des.clone()));
|
|
|
|
self.description = des?;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(act) = value.get("actions") {
|
|
|
|
let act = from_value(act.clone())
|
|
|
|
.map_err(|_| AuthControllerError::InvalidApiKeyActions(act.clone()));
|
|
|
|
self.actions = act?;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(ind) = value.get("indexes") {
|
|
|
|
let ind = from_value(ind.clone())
|
|
|
|
.map_err(|_| AuthControllerError::InvalidApiKeyIndexes(ind.clone()));
|
|
|
|
self.indexes = ind?;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(exp) = value.get("expiresAt") {
|
2021-12-06 22:45:41 +08:00
|
|
|
self.expires_at = parse_expiration_date(exp)?;
|
2021-11-09 01:31:27 +08:00
|
|
|
}
|
|
|
|
|
2022-02-14 22:32:41 +08:00
|
|
|
self.updated_at = OffsetDateTime::now_utc();
|
2021-11-09 01:31:27 +08:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn default_admin() -> Self {
|
2022-02-14 22:32:41 +08:00
|
|
|
let now = OffsetDateTime::now_utc();
|
2022-05-25 16:32:47 +08:00
|
|
|
let uid = Uuid::new_v4();
|
2021-11-09 01:31:27 +08:00
|
|
|
Self {
|
2022-05-25 16:32:47 +08:00
|
|
|
name: Some("admin".to_string()),
|
2021-11-09 01:31:27 +08:00
|
|
|
description: Some("Default Admin API Key (Use it for all other operations. Caution! Do not use it on a public frontend)".to_string()),
|
2022-05-25 16:32:47 +08:00
|
|
|
uid,
|
2021-11-09 01:31:27 +08:00
|
|
|
actions: vec![Action::All],
|
|
|
|
indexes: vec!["*".to_string()],
|
|
|
|
expires_at: None,
|
2022-02-14 22:32:41 +08:00
|
|
|
created_at: now,
|
|
|
|
updated_at: now,
|
2021-11-09 01:31:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn default_search() -> Self {
|
2022-02-14 22:32:41 +08:00
|
|
|
let now = OffsetDateTime::now_utc();
|
2022-05-25 16:32:47 +08:00
|
|
|
let uid = Uuid::new_v4();
|
2021-11-09 01:31:27 +08:00
|
|
|
Self {
|
2022-05-25 16:32:47 +08:00
|
|
|
name: Some("search".to_string()),
|
2021-11-09 01:31:27 +08:00
|
|
|
description: Some(
|
|
|
|
"Default Search API Key (Use it to search from the frontend)".to_string(),
|
|
|
|
),
|
2022-05-25 16:32:47 +08:00
|
|
|
uid,
|
2021-11-09 01:31:27 +08:00
|
|
|
actions: vec![Action::Search],
|
|
|
|
indexes: vec!["*".to_string()],
|
|
|
|
expires_at: None,
|
2022-02-14 22:32:41 +08:00
|
|
|
created_at: now,
|
|
|
|
updated_at: now,
|
2021-11-09 01:31:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-14 22:32:41 +08:00
|
|
|
fn parse_expiration_date(value: &Value) -> Result<Option<OffsetDateTime>> {
|
2021-12-06 22:45:41 +08:00
|
|
|
match value {
|
2022-02-14 22:32:41 +08:00
|
|
|
Value::String(string) => OffsetDateTime::parse(string, &Rfc3339)
|
2021-12-06 22:45:41 +08:00
|
|
|
.or_else(|_| {
|
2022-02-14 22:32:41 +08:00
|
|
|
PrimitiveDateTime::parse(
|
|
|
|
string,
|
|
|
|
format_description!(
|
|
|
|
"[year repr:full base:calendar]-[month repr:numerical]-[day]T[hour]:[minute]:[second]"
|
|
|
|
),
|
|
|
|
).map(|datetime| datetime.assume_utc())
|
2021-12-06 22:45:41 +08:00
|
|
|
})
|
|
|
|
.or_else(|_| {
|
2022-02-14 22:32:41 +08:00
|
|
|
PrimitiveDateTime::parse(
|
|
|
|
string,
|
|
|
|
format_description!(
|
|
|
|
"[year repr:full base:calendar]-[month repr:numerical]-[day] [hour]:[minute]:[second]"
|
|
|
|
),
|
|
|
|
).map(|datetime| datetime.assume_utc())
|
|
|
|
})
|
|
|
|
.or_else(|_| {
|
|
|
|
Date::parse(string, format_description!(
|
|
|
|
"[year repr:full base:calendar]-[month repr:numerical]-[day]"
|
|
|
|
)).map(|date| PrimitiveDateTime::new(date, time!(00:00)).assume_utc())
|
2021-12-06 22:45:41 +08:00
|
|
|
})
|
|
|
|
.map_err(|_| AuthControllerError::InvalidApiKeyExpiresAt(value.clone()))
|
|
|
|
// check if the key is already expired.
|
|
|
|
.and_then(|d| {
|
2022-02-14 22:32:41 +08:00
|
|
|
if d > OffsetDateTime::now_utc() {
|
2021-12-06 22:45:41 +08:00
|
|
|
Ok(d)
|
|
|
|
} else {
|
|
|
|
Err(AuthControllerError::InvalidApiKeyExpiresAt(value.clone()))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.map(Option::Some),
|
|
|
|
Value::Null => Ok(None),
|
|
|
|
_otherwise => Err(AuthControllerError::InvalidApiKeyExpiresAt(value.clone())),
|
|
|
|
}
|
|
|
|
}
|