Skip to content

Commit

Permalink
refactor: [#599] extract types for torrust_index::config::v1::auth::Auth
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed May 23, 2024
1 parent f83a5f7 commit c7c9225
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 9 deletions.
5 changes: 3 additions & 2 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ fn parse_url(url_str: &str) -> Result<Url, url::ParseError> {
#[cfg(test)]
mod tests {

use crate::config::v1::auth::SecretKey;
use crate::config::{Configuration, ConfigurationPublic, Info, Settings};

#[cfg(test)]
Expand Down Expand Up @@ -521,7 +522,7 @@ mod tests {

assert_eq!(
configuration.get_all().await.auth.secret_key,
"OVERRIDDEN AUTH SECRET KEY".to_string()
SecretKey::new("OVERRIDDEN AUTH SECRET KEY")
);
}

Expand All @@ -542,7 +543,7 @@ mod tests {

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

assert_eq!(settings.auth.secret_key, "OVERRIDDEN AUTH SECRET KEY".to_string());
assert_eq!(settings.auth.secret_key, SecretKey::new("OVERRIDDEN AUTH SECRET KEY"));

Ok(())
});
Expand Down
45 changes: 42 additions & 3 deletions src/config/v1/auth.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt;

use serde::{Deserialize, Serialize};

/// Authentication options.
Expand All @@ -10,7 +12,7 @@ pub struct Auth {
/// The maximum password length.
pub max_password_length: usize,
/// The secret key used to sign JWT tokens.
pub secret_key: String,
pub secret_key: SecretKey,
}

impl Default for Auth {
Expand All @@ -19,14 +21,14 @@ impl Default for Auth {
email_on_signup: EmailOnSignup::default(),
min_password_length: 6,
max_password_length: 64,
secret_key: "MaxVerstappenWC2021".to_string(),
secret_key: SecretKey::new("MaxVerstappenWC2021"),
}
}
}

impl Auth {
pub fn override_secret_key(&mut self, secret_key: &str) {
self.secret_key = secret_key.to_string();
self.secret_key = SecretKey::new(secret_key);
}
}

Expand All @@ -46,3 +48,40 @@ impl Default for EmailOnSignup {
Self::Optional
}
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct SecretKey(String);

impl SecretKey {
/// # Panics
///
/// Will panic if the key if empty.
#[must_use]
pub fn new(key: &str) -> Self {
assert!(!key.is_empty(), "secret key cannot be empty");

Self(key.to_owned())
}

#[must_use]
pub fn as_bytes(&self) -> &[u8] {
self.0.as_bytes()
}
}

impl fmt::Display for SecretKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}

#[cfg(test)]
mod tests {
use super::SecretKey;

#[test]
#[should_panic(expected = "secret key cannot be empty")]
fn secret_key_can_not_be_empty() {
drop(SecretKey::new(""));
}
}
4 changes: 2 additions & 2 deletions src/config/v1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub mod website;
use serde::{Deserialize, Serialize};

use self::api::Api;
use self::auth::Auth;
use self::auth::{Auth, SecretKey};
use self::database::Database;
use self::image_cache::ImageCache;
use self::mail::Mail;
Expand Down Expand Up @@ -60,7 +60,7 @@ impl Settings {
"***".clone_into(&mut self.tracker.token);
"***".clone_into(&mut self.database.connect_url);
"***".clone_into(&mut self.mail.password);
"***".clone_into(&mut self.auth.secret_key);
self.auth.secret_key = SecretKey::new("***");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/web/api/client/v1/contexts/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ impl From<DomainAuth> for Auth {
email_on_signup: format!("{:?}", auth.email_on_signup),
min_password_length: auth.min_password_length,
max_password_length: auth.max_password_length,
secret_key: auth.secret_key,
secret_key: auth.secret_key.to_string(),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/common/contexts/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ impl From<DomainAuth> for Auth {
email_on_signup: format!("{:?}", auth.email_on_signup),
min_password_length: auth.min_password_length,
max_password_length: auth.max_password_length,
secret_key: auth.secret_key,
secret_key: auth.secret_key.to_string(),
}
}
}
Expand Down

0 comments on commit c7c9225

Please sign in to comment.