Use secrecy crate.

This commit is contained in:
Luca Palmieri 2021-12-27 13:24:24 +01:00
parent 948b57e52d
commit 0967a6030c
4 changed files with 31 additions and 7 deletions

17
Cargo.lock generated
View file

@ -1809,6 +1809,16 @@ dependencies = [
"untrusted", "untrusted",
] ]
[[package]]
name = "secrecy"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9bd1c54ea06cfd2f6b63219704de0b9b4f72dcc2b8fdef820be6cd799780e91e"
dependencies = [
"serde",
"zeroize",
]
[[package]] [[package]]
name = "security-framework" name = "security-framework"
version = "2.4.2" version = "2.4.2"
@ -2841,6 +2851,7 @@ dependencies = [
"quickcheck", "quickcheck",
"quickcheck_macros", "quickcheck_macros",
"reqwest", "reqwest",
"secrecy",
"serde", "serde",
"serde-aux", "serde-aux",
"serde_json", "serde_json",
@ -2857,6 +2868,12 @@ dependencies = [
"wiremock", "wiremock",
] ]
[[package]]
name = "zeroize"
version = "1.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d68d9dcec5f9b43a30d38c49f91dfedfaac384cb8f085faca366c26207dd1619"
[[package]] [[package]]
name = "zstd" name = "zstd"
version = "0.9.1+zstd.1.5.1" version = "0.9.1+zstd.1.5.1"

View file

@ -30,6 +30,7 @@ serde-aux = "1.0.1"
unicode-segmentation = "1.7.1" unicode-segmentation = "1.7.1"
tracing-actix-web = "0.5.0-beta.6" tracing-actix-web = "0.5.0-beta.6"
validator = "0.12.0" validator = "0.12.0"
secrecy = { version = "0.8", features = ["serde"] }
[dev-dependencies] [dev-dependencies]
once_cell = "1.7.2" once_cell = "1.7.2"

View file

@ -1,4 +1,5 @@
use crate::domain::SubscriberEmail; use crate::domain::SubscriberEmail;
use secrecy::{ExposeSecret, Secret};
use serde_aux::field_attributes::deserialize_number_from_string; use serde_aux::field_attributes::deserialize_number_from_string;
use sqlx::postgres::{PgConnectOptions, PgSslMode}; use sqlx::postgres::{PgConnectOptions, PgSslMode};
use sqlx::ConnectOptions; use sqlx::ConnectOptions;
@ -21,7 +22,7 @@ pub struct ApplicationSettings {
#[derive(serde::Deserialize, Clone)] #[derive(serde::Deserialize, Clone)]
pub struct DatabaseSettings { pub struct DatabaseSettings {
pub username: String, pub username: String,
pub password: String, pub password: Secret<String>,
#[serde(deserialize_with = "deserialize_number_from_string")] #[serde(deserialize_with = "deserialize_number_from_string")]
pub port: u16, pub port: u16,
pub host: String, pub host: String,
@ -39,7 +40,7 @@ impl DatabaseSettings {
PgConnectOptions::new() PgConnectOptions::new()
.host(&self.host) .host(&self.host)
.username(&self.username) .username(&self.username)
.password(&self.password) .password(&self.password.expose_secret())
.port(self.port) .port(self.port)
.ssl_mode(ssl_mode) .ssl_mode(ssl_mode)
} }
@ -55,7 +56,7 @@ impl DatabaseSettings {
pub struct EmailClientSettings { pub struct EmailClientSettings {
pub base_url: String, pub base_url: String,
pub sender_email: String, pub sender_email: String,
pub authorization_token: String, pub authorization_token: Secret<String>,
pub timeout_milliseconds: u64, pub timeout_milliseconds: u64,
} }

View file

@ -1,18 +1,19 @@
use crate::domain::SubscriberEmail; use crate::domain::SubscriberEmail;
use reqwest::Client; use reqwest::Client;
use secrecy::{ExposeSecret, Secret};
pub struct EmailClient { pub struct EmailClient {
http_client: Client, http_client: Client,
base_url: String, base_url: String,
sender: SubscriberEmail, sender: SubscriberEmail,
authorization_token: String, authorization_token: Secret<String>,
} }
impl EmailClient { impl EmailClient {
pub fn new( pub fn new(
base_url: String, base_url: String,
sender: SubscriberEmail, sender: SubscriberEmail,
authorization_token: String, authorization_token: Secret<String>,
timeout: std::time::Duration, timeout: std::time::Duration,
) -> Self { ) -> Self {
let http_client = Client::builder().timeout(timeout).build().unwrap(); let http_client = Client::builder().timeout(timeout).build().unwrap();
@ -41,7 +42,10 @@ impl EmailClient {
}; };
self.http_client self.http_client
.post(&url) .post(&url)
.header("X-Postmark-Server-Token", &self.authorization_token) .header(
"X-Postmark-Server-Token",
self.authorization_token.expose_secret(),
)
.json(&request_body) .json(&request_body)
.send() .send()
.await? .await?
@ -68,6 +72,7 @@ mod tests {
use fake::faker::internet::en::SafeEmail; use fake::faker::internet::en::SafeEmail;
use fake::faker::lorem::en::{Paragraph, Sentence}; use fake::faker::lorem::en::{Paragraph, Sentence};
use fake::{Fake, Faker}; use fake::{Fake, Faker};
use secrecy::Secret;
use wiremock::matchers::{any, header, header_exists, method, path}; use wiremock::matchers::{any, header, header_exists, method, path};
use wiremock::{Mock, MockServer, Request, ResponseTemplate}; use wiremock::{Mock, MockServer, Request, ResponseTemplate};
@ -108,7 +113,7 @@ mod tests {
EmailClient::new( EmailClient::new(
base_url, base_url,
email(), email(),
Faker.fake(), Secret::new(Faker.fake()),
std::time::Duration::from_millis(200), std::time::Duration::from_millis(200),
) )
} }