Skip to content

Commit

Permalink
feat: password struct
Browse files Browse the repository at this point in the history
  • Loading branch information
zaaarf committed Feb 15, 2025
1 parent a4c2583 commit 3ca0f7c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 24 deletions.
52 changes: 31 additions & 21 deletions src/api/config.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
//! # Config
//! Data structure defining clients configuration
use std::fmt::{Debug, Display};

use serde::Serialize;

/// Configuration struct for the `codemp` client.
///
/// `username` and `password` are required fields, everything else is optional.
///
/// `host`, `port` and `tls` affect all connections to all gRPC services; the
/// resulting endpoint is composed like this:
/// http{tls?'s':''}://{host}:{port}
#[derive(Clone, Default)]
#[derive(Clone, Debug, Default)]
#[cfg_attr(feature = "js", napi_derive::napi(object))]
#[cfg_attr(feature = "py", pyo3::pyclass(get_all, set_all))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct Config {
/// User identifier used to register, possibly your email.
pub username: String,
/// User password chosen upon registration.
pub password: String, // must not leak this!
pub password: Password, // must not leak this!
/// Address of server to connect to, default api.code.mp.
pub host: Option<String>,
/// Port to connect to, default 50053.
Expand All @@ -30,7 +34,7 @@ impl Config {
pub fn new(username: impl ToString, password: impl ToString) -> Self {
Self {
username: username.to_string(),
password: password.to_string(),
password: password.to_string().into(),
host: None,
port: None,
tls: None,
Expand Down Expand Up @@ -62,24 +66,30 @@ impl Config {
}
}

// manual impl: we want to obfuscate the password field!!
// TODO: can we just tag password to be obfuscated in debug print?
// reimplementing the whole Debug thing is pretty lame
impl std::fmt::Debug for Config {
#[derive(Clone, Default)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize), serde(transparent))]
pub struct Password(String);

impl From<String> for Password {
fn from(value: String) -> Self {
Password(value)
}
}

impl From<Password> for String {
fn from(value: Password) -> Self {
value.0
}
}

impl Display for Password {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if f.alternate() {
write!(f,
r#"""Config {{
username: {},
password: ********,
host: {:#?},
port: {:#?},
tls: {:#?}
}}"""#,
self.username, self.host, self.port, self.tls
)
} else {
write!(f, "Config {{ username: {}, password: ********, host: {:?}, port: {:?}, tls: {:?} }}", self.username, self.host, self.port, self.tls)
}
write!(f, "********")
}
}

impl Debug for Password {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "********")
}
}
2 changes: 1 addition & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl Client {
let resp = auth
.login(LoginRequest {
username: config.username.clone(),
password: config.password.clone(),
password: config.password.to_string().clone(),
})
.await?
.into_inner();
Expand Down
2 changes: 1 addition & 1 deletion src/ffi/python/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ impl Config {

Ok(Self {
username,
password,
password: password.into(),
host,
port,
tls,
Expand Down
2 changes: 1 addition & 1 deletion src/tests/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl ScopedFixture<crate::Client> for ClientFixture {
});
let client = crate::Client::connect(crate::api::Config {
username,
password,
password: password.into(),
tls: Some(false),
..Default::default()
})
Expand Down

0 comments on commit 3ca0f7c

Please sign in to comment.