alibi_api/plugins/api_key/
config.rs1use super::*;
2#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
8pub enum ApiKeyReferences {
9 #[default]
11 User,
12 Organization,
15}
16
17#[derive(Clone)]
19#[expect(
20 clippy::struct_excessive_bools,
21 reason = "Independent configuration switches model distinct upstream behavior, rather than mutually exclusive states"
22)]
23pub struct ApiKeyConfig {
24 pub config_id: String,
27 pub references: ApiKeyReferences,
30
31 pub storage: ApiKeyStorageMode,
33 pub fallback_to_database: bool,
35 pub secondary_storage: Option<Arc<dyn alibi_core::store::CacheAdapter>>,
37 pub custom_storage: Option<Arc<dyn ApiKeyStorage>>,
39
40 pub key_length: f64,
45 pub prefix: Option<String>,
46 pub default_permissions: Option<ApiKeyPermissions>,
48 pub custom_key_generator: Option<Arc<dyn ApiKeyGenerator>>,
50 pub default_permissions_callback: Option<Arc<dyn ApiKeyDefaultPermissions>>,
52
53 pub api_key_headers: Vec<String>,
55 pub custom_api_key_getter: Option<Arc<dyn ApiKeyGetter>>,
57 pub custom_api_key_validator: Option<Arc<dyn ApiKeyValidator>>,
59
60 pub disable_key_hashing: bool,
62
63 pub starting_characters_length: f64,
68 pub store_starting_characters: bool,
69
70 pub max_prefix_length: f64,
73 pub min_prefix_length: f64,
75
76 pub max_name_length: f64,
79 pub min_name_length: f64,
81 pub require_name: bool,
82
83 pub enable_metadata: bool,
85
86 pub key_expiration: KeyExpirationConfig,
88
89 pub rate_limit: RateLimitDefaults,
91
92 pub enable_session_for_api_keys: bool,
94 pub defer_updates: bool,
99}
100
101impl ApiKeyConfig {
102 pub(in crate::plugins::api_key) const fn normalized(mut self) -> Self {
103 if self.key_length == 0.0 || self.key_length.is_nan() {
105 self.key_length = 64.0;
106 }
107 self
108 }
109}
110
111impl std::fmt::Debug for ApiKeyConfig {
112 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
113 f.debug_struct("ApiKeyConfig")
114 .field("config_id", &self.config_id)
115 .field("references", &self.references)
116 .field("key_length", &self.key_length)
117 .field("prefix", &self.prefix)
118 .field("custom_key_generator", &self.custom_key_generator.is_some())
119 .field(
120 "default_permissions_callback",
121 &self.default_permissions_callback.is_some(),
122 )
123 .field("api_key_headers", &self.api_key_headers)
124 .field(
125 "custom_api_key_getter",
126 &self.custom_api_key_getter.is_some(),
127 )
128 .field(
129 "custom_api_key_validator",
130 &self.custom_api_key_validator.is_some(),
131 )
132 .field(
133 "enable_session_for_api_keys",
134 &self.enable_session_for_api_keys,
135 )
136 .field("storage", &self.storage)
137 .field("fallback_to_database", &self.fallback_to_database)
138 .field("secondary_storage", &self.secondary_storage.is_some())
139 .field("custom_storage", &self.custom_storage.is_some())
140 .field("defer_updates", &self.defer_updates)
141 .finish_non_exhaustive()
142 }
143}
144
145#[derive(Debug, Clone)]
147pub struct KeyExpirationConfig {
148 pub default_expires_in: Option<f64>,
152 pub disable_custom_expires_time: bool,
154 pub max_expires_in: f64,
156 pub min_expires_in: f64,
158}
159
160impl Default for KeyExpirationConfig {
161 fn default() -> Self {
162 Self {
163 default_expires_in: None,
164 disable_custom_expires_time: false,
165 max_expires_in: 365.0,
166 min_expires_in: 1.0,
167 }
168 }
169}
170
171#[derive(Debug, Clone)]
173pub struct RateLimitDefaults {
174 pub enabled: bool,
175 pub time_window: f64,
177 pub max_requests: f64,
179}
180
181impl Default for RateLimitDefaults {
182 fn default() -> Self {
183 Self {
184 enabled: true,
185 time_window: 86_400_000.0, max_requests: 10.0,
187 }
188 }
189}
190
191impl Default for ApiKeyConfig {
192 fn default() -> Self {
193 Self {
194 storage: ApiKeyStorageMode::Database,
195 fallback_to_database: false,
196 custom_storage: None,
197 secondary_storage: None,
198 config_id: "default".to_owned(),
199 references: ApiKeyReferences::default(),
200 key_length: 64.0,
201 prefix: None,
202 default_permissions: None,
203 custom_key_generator: None,
204 default_permissions_callback: None,
205 api_key_headers: vec!["x-api-key".to_owned()],
206 custom_api_key_getter: None,
207 custom_api_key_validator: None,
208 disable_key_hashing: false,
209 starting_characters_length: 6.0,
210 store_starting_characters: true,
211 max_prefix_length: 32.0,
212 min_prefix_length: 1.0,
213 max_name_length: 32.0,
214 min_name_length: 1.0,
215 require_name: false,
216 enable_metadata: false,
217 key_expiration: KeyExpirationConfig::default(),
218 rate_limit: RateLimitDefaults::default(),
219 enable_session_for_api_keys: false,
220 defer_updates: false,
221 }
222 }
223}