assemblyline_models/datastore/
apikey.rs

1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3use struct_metadata::Described;
4
5use crate::{ElasticMeta, Readable};
6use crate::datastore::user::{UserRole, AclCatagory};
7
8const APIKEY_ID_DELIMETER: &str = "+";
9pub const FORBIDDEN_APIKEY_CHARACTERS: &str = r"[+@!#$%^&*()<>?/\|}{~:]";
10
11
12/// Model of Apikey
13#[derive(Serialize, Deserialize, Described)]
14#[metadata_type(ElasticMeta)]
15#[metadata(index=true, store=true)]
16pub struct Apikey {
17    /// Access Control List for the API key
18    pub acl: Vec<AclCatagory>,
19    /// BCrypt hash of the password for the apikey
20    pub password: String,
21    /// List of roles tied to the API key
22    #[serde(default)]
23    pub roles: Vec<UserRole>,
24    /// Username
25    #[metadata(copyto="__text__")]
26    pub uname: String,
27    /// Name of the key
28    #[metadata(copyto="__text__")]
29    pub key_name: String,
30    /// The date this API key is created.
31    #[serde(default="chrono::Utc::now")]
32    pub creation_date: DateTime<Utc>,
33    /// Expiry timestamp.
34    #[serde(default)]
35    pub expiry_ts: Option<DateTime<Utc>>,
36    /// The last time this API key was used.
37    #[serde(default)]
38    pub last_used: Option<DateTime<Utc>>,
39}
40
41impl Readable for Apikey {
42    fn set_from_archive(&mut self, _from_archive: bool) { }
43}
44
45pub fn get_apikey_id(keyname: &str, uname: &str) -> String {
46    format!("{keyname}{APIKEY_ID_DELIMETER}{uname}")
47}
48
49pub fn split_apikey_id(key_id: &str) -> Option<(&str, &str)> {
50    key_id.split_once(APIKEY_ID_DELIMETER)
51}