keygen_rs/
config.rs

1use crate::errors::Error;
2use lazy_static::lazy_static;
3use std::sync::RwLock;
4
5#[derive(Clone, Debug)]
6pub struct KeygenConfig {
7    // Common configuration
8    pub api_url: String,
9    pub api_version: String,
10    pub api_prefix: String,
11    pub account: String,
12    pub environment: Option<String>,
13    pub user_agent: Option<String>,
14
15    // License Key Authentication configuration
16    #[cfg(feature = "license-key")]
17    pub product: String,
18    #[cfg(feature = "license-key")]
19    pub package: String,
20    #[cfg(feature = "license-key")]
21    pub license_key: Option<String>,
22    #[cfg(feature = "license-key")]
23    pub public_key: Option<String>,
24    #[cfg(feature = "license-key")]
25    pub platform: Option<String>,
26    #[cfg(feature = "license-key")]
27    pub max_clock_drift: Option<i64>,
28    #[cfg(feature = "license-key")]
29    pub verify_keygen_signature: Option<bool>,
30
31    // Token Authentication configuration
32    #[cfg(feature = "token")]
33    pub token: Option<String>,
34}
35
36impl Default for KeygenConfig {
37    fn default() -> Self {
38        KeygenConfig {
39            // Common defaults
40            api_url: "https://api.keygen.sh".to_string(),
41            api_version: "1.7".to_string(),
42            api_prefix: "v1".to_string(),
43            account: String::new(),
44            environment: None,
45            user_agent: None,
46
47            // License Key Authentication defaults
48            #[cfg(feature = "license-key")]
49            product: String::new(),
50            #[cfg(feature = "license-key")]
51            package: String::new(),
52            #[cfg(feature = "license-key")]
53            license_key: None,
54            #[cfg(feature = "license-key")]
55            public_key: None,
56            #[cfg(feature = "license-key")]
57            platform: None,
58            #[cfg(feature = "license-key")]
59            max_clock_drift: Some(5),
60            #[cfg(feature = "license-key")]
61            verify_keygen_signature: Some(true),
62
63            // Token Authentication defaults
64            #[cfg(feature = "token")]
65            token: None,
66        }
67    }
68}
69
70impl KeygenConfig {
71    /// Create a license key authentication configuration
72    #[cfg(feature = "license-key")]
73    pub fn license_key(
74        account: String,
75        product: String,
76        license_key: String,
77        public_key: String,
78    ) -> Self {
79        KeygenConfig {
80            account,
81            product,
82            license_key: Some(license_key),
83            public_key: Some(public_key),
84            ..Default::default()
85        }
86    }
87
88    /// Create a token authentication configuration
89    #[cfg(feature = "token")]
90    pub fn token(account: String, token: String) -> Self {
91        KeygenConfig {
92            account,
93            token: Some(token),
94            ..Default::default()
95        }
96    }
97}
98
99lazy_static! {
100    static ref KEYGEN_CONFIG: RwLock<KeygenConfig> = RwLock::new(KeygenConfig::default());
101}
102
103pub fn get_config() -> Result<KeygenConfig, Error> {
104    KEYGEN_CONFIG
105        .read()
106        .map_err(|_| Error::UnexpectedError("Config lock poisoned".to_string()))?
107        .clone()
108        .into()
109}
110
111impl From<KeygenConfig> for Result<KeygenConfig, Error> {
112    fn from(config: KeygenConfig) -> Self {
113        Ok(config)
114    }
115}
116
117pub fn set_config(config: KeygenConfig) -> Result<(), Error> {
118    let mut current_config = KEYGEN_CONFIG
119        .write()
120        .map_err(|_| Error::UnexpectedError("Config lock poisoned".to_string()))?;
121    *current_config = config;
122    Ok(())
123}
124
125pub fn set_api_url(api_url: &str) -> Result<(), Error> {
126    let mut current_config = KEYGEN_CONFIG
127        .write()
128        .map_err(|_| Error::UnexpectedError("Config lock poisoned".to_string()))?;
129    current_config.api_url = api_url.to_string();
130    Ok(())
131}
132
133pub fn set_api_version(api_version: &str) -> Result<(), Error> {
134    let mut current_config = KEYGEN_CONFIG
135        .write()
136        .map_err(|_| Error::UnexpectedError("Config lock poisoned".to_string()))?;
137    current_config.api_version = api_version.to_string();
138    Ok(())
139}
140
141pub fn set_api_prefix(api_prefix: &str) -> Result<(), Error> {
142    let mut current_config = KEYGEN_CONFIG
143        .write()
144        .map_err(|_| Error::UnexpectedError("Config lock poisoned".to_string()))?;
145    current_config.api_prefix = api_prefix.to_string();
146    Ok(())
147}
148
149pub fn set_account(account: &str) -> Result<(), Error> {
150    let mut current_config = KEYGEN_CONFIG
151        .write()
152        .map_err(|_| Error::UnexpectedError("Config lock poisoned".to_string()))?;
153    current_config.account = account.to_string();
154    Ok(())
155}
156
157#[cfg(feature = "license-key")]
158pub fn set_product(product: &str) -> Result<(), Error> {
159    let mut current_config = KEYGEN_CONFIG
160        .write()
161        .map_err(|_| Error::UnexpectedError("Config lock poisoned".to_string()))?;
162    current_config.product = product.to_string();
163    Ok(())
164}
165
166#[cfg(feature = "license-key")]
167pub fn set_package(package: &str) -> Result<(), Error> {
168    let mut current_config = KEYGEN_CONFIG
169        .write()
170        .map_err(|_| Error::UnexpectedError("Config lock poisoned".to_string()))?;
171    current_config.package = package.to_string();
172    Ok(())
173}
174
175pub fn set_environment(environment: &str) -> Result<(), Error> {
176    let mut current_config = KEYGEN_CONFIG
177        .write()
178        .map_err(|_| Error::UnexpectedError("Config lock poisoned".to_string()))?;
179    current_config.environment = Some(environment.to_string());
180    Ok(())
181}
182
183#[cfg(feature = "license-key")]
184pub fn set_license_key(license_key: &str) -> Result<(), Error> {
185    let mut current_config = KEYGEN_CONFIG
186        .write()
187        .map_err(|_| Error::UnexpectedError("Config lock poisoned".to_string()))?;
188    current_config.license_key = Some(license_key.to_string());
189    Ok(())
190}
191
192#[cfg(feature = "token")]
193pub fn set_token(token: &str) -> Result<(), Error> {
194    let mut current_config = KEYGEN_CONFIG
195        .write()
196        .map_err(|_| Error::UnexpectedError("Config lock poisoned".to_string()))?;
197    current_config.token = Some(token.to_string());
198    Ok(())
199}
200
201#[cfg(feature = "license-key")]
202pub fn set_public_key(public_key: &str) -> Result<(), Error> {
203    let mut current_config = KEYGEN_CONFIG
204        .write()
205        .map_err(|_| Error::UnexpectedError("Config lock poisoned".to_string()))?;
206    current_config.public_key = Some(public_key.to_string());
207    Ok(())
208}
209
210#[cfg(feature = "license-key")]
211pub fn set_platform(platform: &str) -> Result<(), Error> {
212    let mut current_config = KEYGEN_CONFIG
213        .write()
214        .map_err(|_| Error::UnexpectedError("Config lock poisoned".to_string()))?;
215    current_config.platform = Some(platform.to_string());
216    Ok(())
217}
218
219pub fn set_user_agent(user_agent: &str) -> Result<(), Error> {
220    let mut current_config = KEYGEN_CONFIG
221        .write()
222        .map_err(|_| Error::UnexpectedError("Config lock poisoned".to_string()))?;
223    current_config.user_agent = Some(user_agent.to_string());
224    Ok(())
225}
226
227#[cfg(feature = "license-key")]
228pub fn set_max_clock_drift(max_clock_drift: i64) -> Result<(), Error> {
229    let mut current_config = KEYGEN_CONFIG
230        .write()
231        .map_err(|_| Error::UnexpectedError("Config lock poisoned".to_string()))?;
232    current_config.max_clock_drift = Some(max_clock_drift);
233    Ok(())
234}
235
236pub fn reset_config() -> Result<(), Error> {
237    let mut current_config = KEYGEN_CONFIG
238        .write()
239        .map_err(|_| Error::UnexpectedError("Config lock poisoned".to_string()))?;
240    *current_config = KeygenConfig::default();
241    Ok(())
242}