Skip to content

Commit cb609be

Browse files
committed
Defer username default
1 parent 75cc986 commit cb609be

File tree

3 files changed

+23
-22
lines changed

3 files changed

+23
-22
lines changed

postgres/src/config.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,8 @@ impl Config {
158158
self
159159
}
160160

161-
/// Gets the user to authenticate with.
162-
///
163-
/// If no user has been configured with the [`user`](Config::user) method,
164-
/// then this defaults to the user executing this process. It always
165-
/// returns `Some`.
166-
// FIXME remove option
161+
/// Gets the user to authenticate with, if one has been configured with
162+
/// the `user` method.
167163
pub fn get_user(&self) -> Option<&str> {
168164
self.config.get_user()
169165
}

tokio-postgres/src/config.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ pub enum Host {
190190
/// ```
191191
#[derive(Clone, PartialEq, Eq)]
192192
pub struct Config {
193-
pub(crate) user: String,
193+
pub(crate) user: Option<String>,
194194
pub(crate) password: Option<Vec<u8>>,
195195
pub(crate) dbname: Option<String>,
196196
pub(crate) options: Option<String>,
@@ -219,7 +219,7 @@ impl Config {
219219
/// Creates a new configuration.
220220
pub fn new() -> Config {
221221
Config {
222-
user: whoami::username(),
222+
user: None,
223223
password: None,
224224
dbname: None,
225225
options: None,
@@ -247,18 +247,14 @@ impl Config {
247247
///
248248
/// Defaults to the user executing this process.
249249
pub fn user(&mut self, user: &str) -> &mut Config {
250-
self.user = user.to_string();
250+
self.user = Some(user.to_string());
251251
self
252252
}
253253

254-
/// Gets the user to authenticate with.
255-
///
256-
/// If no user has been configured with the [`user`](Config::user) method,
257-
/// then this defaults to the user executing this process. It always
258-
/// returns `Some`.
259-
// FIXME remove option
254+
/// Gets the user to authenticate with, if one has been configured with
255+
/// the `user` method.
260256
pub fn get_user(&self) -> Option<&str> {
261-
Some(&self.user)
257+
self.user.as_deref()
262258
}
263259

264260
/// Sets the password to authenticate with.

tokio-postgres/src/connect_raw.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,10 @@ where
9696
delayed: VecDeque::new(),
9797
};
9898

99-
startup(&mut stream, config).await?;
100-
authenticate(&mut stream, config).await?;
99+
let user = config.user.clone().unwrap_or_else(whoami::username);
100+
101+
startup(&mut stream, config, &user).await?;
102+
authenticate(&mut stream, config, &user).await?;
101103
let (process_id, secret_key, parameters) = read_info(&mut stream).await?;
102104

103105
let (sender, receiver) = mpsc::unbounded();
@@ -107,13 +109,17 @@ where
107109
Ok((client, connection))
108110
}
109111

110-
async fn startup<S, T>(stream: &mut StartupStream<S, T>, config: &Config) -> Result<(), Error>
112+
async fn startup<S, T>(
113+
stream: &mut StartupStream<S, T>,
114+
config: &Config,
115+
user: &str,
116+
) -> Result<(), Error>
111117
where
112118
S: AsyncRead + AsyncWrite + Unpin,
113119
T: AsyncRead + AsyncWrite + Unpin,
114120
{
115121
let mut params = vec![("client_encoding", "UTF8")];
116-
params.push(("user", &config.user));
122+
params.push(("user", user));
117123
if let Some(dbname) = &config.dbname {
118124
params.push(("database", &**dbname));
119125
}
@@ -133,7 +139,11 @@ where
133139
.map_err(Error::io)
134140
}
135141

136-
async fn authenticate<S, T>(stream: &mut StartupStream<S, T>, config: &Config) -> Result<(), Error>
142+
async fn authenticate<S, T>(
143+
stream: &mut StartupStream<S, T>,
144+
config: &Config,
145+
user: &str,
146+
) -> Result<(), Error>
137147
where
138148
S: AsyncRead + AsyncWrite + Unpin,
139149
T: TlsStream + Unpin,
@@ -156,7 +166,6 @@ where
156166
Some(Message::AuthenticationMd5Password(body)) => {
157167
can_skip_channel_binding(config)?;
158168

159-
let user = &config.user;
160169
let pass = config
161170
.password
162171
.as_ref()

0 commit comments

Comments
 (0)