Skip to content

Commit c7c9225

Browse files
committed
refactor: [#599] extract types for torrust_index::config::v1::auth::Auth
1 parent f83a5f7 commit c7c9225

File tree

5 files changed

+49
-9
lines changed

5 files changed

+49
-9
lines changed

src/config/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ fn parse_url(url_str: &str) -> Result<Url, url::ParseError> {
338338
#[cfg(test)]
339339
mod tests {
340340

341+
use crate::config::v1::auth::SecretKey;
341342
use crate::config::{Configuration, ConfigurationPublic, Info, Settings};
342343

343344
#[cfg(test)]
@@ -521,7 +522,7 @@ mod tests {
521522

522523
assert_eq!(
523524
configuration.get_all().await.auth.secret_key,
524-
"OVERRIDDEN AUTH SECRET KEY".to_string()
525+
SecretKey::new("OVERRIDDEN AUTH SECRET KEY")
525526
);
526527
}
527528

@@ -542,7 +543,7 @@ mod tests {
542543

543544
let settings = Configuration::load_settings(&info).expect("Could not load configuration from file");
544545

545-
assert_eq!(settings.auth.secret_key, "OVERRIDDEN AUTH SECRET KEY".to_string());
546+
assert_eq!(settings.auth.secret_key, SecretKey::new("OVERRIDDEN AUTH SECRET KEY"));
546547

547548
Ok(())
548549
});

src/config/v1/auth.rs

+42-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::fmt;
2+
13
use serde::{Deserialize, Serialize};
24

35
/// Authentication options.
@@ -10,7 +12,7 @@ pub struct Auth {
1012
/// The maximum password length.
1113
pub max_password_length: usize,
1214
/// The secret key used to sign JWT tokens.
13-
pub secret_key: String,
15+
pub secret_key: SecretKey,
1416
}
1517

1618
impl Default for Auth {
@@ -19,14 +21,14 @@ impl Default for Auth {
1921
email_on_signup: EmailOnSignup::default(),
2022
min_password_length: 6,
2123
max_password_length: 64,
22-
secret_key: "MaxVerstappenWC2021".to_string(),
24+
secret_key: SecretKey::new("MaxVerstappenWC2021"),
2325
}
2426
}
2527
}
2628

2729
impl Auth {
2830
pub fn override_secret_key(&mut self, secret_key: &str) {
29-
self.secret_key = secret_key.to_string();
31+
self.secret_key = SecretKey::new(secret_key);
3032
}
3133
}
3234

@@ -46,3 +48,40 @@ impl Default for EmailOnSignup {
4648
Self::Optional
4749
}
4850
}
51+
52+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
53+
pub struct SecretKey(String);
54+
55+
impl SecretKey {
56+
/// # Panics
57+
///
58+
/// Will panic if the key if empty.
59+
#[must_use]
60+
pub fn new(key: &str) -> Self {
61+
assert!(!key.is_empty(), "secret key cannot be empty");
62+
63+
Self(key.to_owned())
64+
}
65+
66+
#[must_use]
67+
pub fn as_bytes(&self) -> &[u8] {
68+
self.0.as_bytes()
69+
}
70+
}
71+
72+
impl fmt::Display for SecretKey {
73+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
74+
write!(f, "{}", self.0)
75+
}
76+
}
77+
78+
#[cfg(test)]
79+
mod tests {
80+
use super::SecretKey;
81+
82+
#[test]
83+
#[should_panic(expected = "secret key cannot be empty")]
84+
fn secret_key_can_not_be_empty() {
85+
drop(SecretKey::new(""));
86+
}
87+
}

src/config/v1/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub mod website;
1111
use serde::{Deserialize, Serialize};
1212

1313
use self::api::Api;
14-
use self::auth::Auth;
14+
use self::auth::{Auth, SecretKey};
1515
use self::database::Database;
1616
use self::image_cache::ImageCache;
1717
use self::mail::Mail;
@@ -60,7 +60,7 @@ impl Settings {
6060
"***".clone_into(&mut self.tracker.token);
6161
"***".clone_into(&mut self.database.connect_url);
6262
"***".clone_into(&mut self.mail.password);
63-
"***".clone_into(&mut self.auth.secret_key);
63+
self.auth.secret_key = SecretKey::new("***");
6464
}
6565
}
6666

src/web/api/client/v1/contexts/settings/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ impl From<DomainAuth> for Auth {
135135
email_on_signup: format!("{:?}", auth.email_on_signup),
136136
min_password_length: auth.min_password_length,
137137
max_password_length: auth.max_password_length,
138-
secret_key: auth.secret_key,
138+
secret_key: auth.secret_key.to_string(),
139139
}
140140
}
141141
}

tests/common/contexts/settings/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ impl From<DomainAuth> for Auth {
134134
email_on_signup: format!("{:?}", auth.email_on_signup),
135135
min_password_length: auth.min_password_length,
136136
max_password_length: auth.max_password_length,
137-
secret_key: auth.secret_key,
137+
secret_key: auth.secret_key.to_string(),
138138
}
139139
}
140140
}

0 commit comments

Comments
 (0)