Skip to main content

fusion_core/configuration/model/
security_setting.rs

1use fusion_common::{
2  string::{deser_str_to_vec_u8, ser_vec_u8_to_str},
3  time::{self, Duration, UtcDateTime},
4};
5use serde::{Deserialize, Serialize};
6use zeroize::ZeroizeOnDrop;
7
8#[derive(Clone, Deserialize, Serialize)]
9pub struct SecuritySetting {
10  pwd: PwdConf,
11  token: TokenConf,
12}
13
14impl SecuritySetting {
15  pub fn pwd(&self) -> &PwdConf {
16    &self.pwd
17  }
18
19  pub fn token(&self) -> &TokenConf {
20    &self.token
21  }
22}
23
24pub trait KeyConf {
25  fn secret_key(&self) -> &[u8];
26  fn expires_at(&self) -> UtcDateTime;
27}
28
29#[derive(Clone, Deserialize, Serialize, ZeroizeOnDrop)]
30pub struct PwdConf {
31  #[serde(deserialize_with = "deser_str_to_vec_u8", serialize_with = "ser_vec_u8_to_str")]
32  secret_key: Vec<u8>,
33
34  /// 密码过期秒数
35  expires_in: i64,
36
37  #[serde(deserialize_with = "deser_str_to_vec_u8", serialize_with = "ser_vec_u8_to_str")]
38  pwd_key: Vec<u8>,
39
40  /// 创建新用户时的默认密码
41  default_pwd: String,
42}
43
44impl PwdConf {
45  pub fn expires_in(&self) -> i64 {
46    self.expires_in
47  }
48
49  pub fn pwd_key(&self) -> &[u8] {
50    &self.pwd_key
51  }
52
53  pub fn default_pwd(&self) -> &str {
54    &self.default_pwd
55  }
56}
57
58impl KeyConf for PwdConf {
59  fn secret_key(&self) -> &[u8] {
60    &self.secret_key
61  }
62
63  fn expires_at(&self) -> UtcDateTime {
64    time::now_utc() + Duration::seconds(self.expires_in())
65  }
66}
67
68#[derive(Clone, Deserialize, Serialize, ZeroizeOnDrop)]
69pub struct TokenConf {
70  #[serde(deserialize_with = "deser_str_to_vec_u8", serialize_with = "ser_vec_u8_to_str")]
71  pub(crate) secret_key: Vec<u8>,
72
73  pub(crate) expires_in: i64,
74
75  #[serde(deserialize_with = "deser_str_to_vec_u8", serialize_with = "ser_vec_u8_to_str")]
76  pub(crate) public_key: Vec<u8>,
77
78  #[serde(deserialize_with = "deser_str_to_vec_u8", serialize_with = "ser_vec_u8_to_str")]
79  pub(crate) private_key: Vec<u8>,
80}
81
82impl TokenConf {
83  pub fn expires_in(&self) -> i64 {
84    self.expires_in
85  }
86
87  pub fn public_key(&self) -> &[u8] {
88    &self.public_key
89  }
90
91  pub fn private_key(&self) -> &[u8] {
92    &self.private_key
93  }
94}
95
96impl KeyConf for TokenConf {
97  fn secret_key(&self) -> &[u8] {
98    &self.secret_key
99  }
100
101  fn expires_at(&self) -> UtcDateTime {
102    time::now_utc() + Duration::seconds(self.expires_in())
103  }
104}