openzeppelin_relayer/models/api_key/
repository.rs

1use crate::{
2    models::{ApiError, ApiKeyRequest, SecretString},
3    utils::{deserialize_secret_string, serialize_secret_string},
4};
5use chrono::Utc;
6use serde::{Deserialize, Serialize};
7use uuid::Uuid;
8
9#[derive(Serialize, Deserialize, Debug, Clone)]
10pub struct ApiKeyRepoModel {
11    pub id: String,
12    #[serde(
13        serialize_with = "serialize_secret_string",
14        deserialize_with = "deserialize_secret_string"
15    )]
16    pub value: SecretString,
17    pub name: String,
18    pub allowed_origins: Vec<String>,
19    pub created_at: String,
20    pub permissions: Vec<String>,
21}
22
23impl ApiKeyRepoModel {
24    pub fn new(
25        name: String,
26        value: SecretString,
27        permissions: Vec<String>,
28        allowed_origins: Vec<String>,
29    ) -> Self {
30        Self {
31            id: Uuid::new_v4().to_string(),
32            value,
33            name,
34            permissions,
35            allowed_origins,
36            created_at: Utc::now().to_string(),
37        }
38    }
39}
40
41impl TryFrom<ApiKeyRequest> for ApiKeyRepoModel {
42    type Error = ApiError;
43
44    fn try_from(request: ApiKeyRequest) -> Result<Self, Self::Error> {
45        let allowed_origins = request.allowed_origins.unwrap_or(vec!["*".to_string()]);
46
47        Ok(Self {
48            id: Uuid::new_v4().to_string(),
49            value: SecretString::new(&Uuid::new_v4().to_string()),
50            name: request.name,
51            permissions: request.permissions,
52            allowed_origins,
53            created_at: Utc::now().to_string(),
54        })
55    }
56}
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    #[test]
63    fn test_api_key_repo_model_new() {
64        let api_key_repo_model = ApiKeyRepoModel::new(
65            "test-name".to_string(),
66            SecretString::new("test-value"),
67            vec!["relayer:all:execute".to_string()],
68            vec!["*".to_string()],
69        );
70        assert_eq!(api_key_repo_model.name, "test-name");
71        assert_eq!(api_key_repo_model.value, SecretString::new("test-value"));
72        assert_eq!(
73            api_key_repo_model.permissions,
74            vec!["relayer:all:execute".to_string()]
75        );
76        assert_eq!(api_key_repo_model.allowed_origins, vec!["*".to_string()]);
77    }
78
79    #[test]
80    fn test_api_key_repo_model_try_from() {
81        let api_key_request = ApiKeyRequest {
82            name: "test-name".to_string(),
83            permissions: vec!["relayer:all:execute".to_string()],
84            allowed_origins: Some(vec!["*".to_string()]),
85        };
86        let api_key_repo_model = ApiKeyRepoModel::try_from(api_key_request).unwrap();
87        assert_eq!(api_key_repo_model.name, "test-name");
88        assert_eq!(
89            api_key_repo_model.permissions,
90            vec!["relayer:all:execute".to_string()]
91        );
92        assert_eq!(api_key_repo_model.allowed_origins, vec!["*".to_string()]);
93    }
94}