Skip to content

Commit fcedda9

Browse files
committed
Pull user info out into a separate struct
1 parent dc3b5d8 commit fcedda9

1 file changed

Lines changed: 25 additions & 17 deletions

File tree

src/lib/lib.rs

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,15 @@ pub enum PostgresConnectTarget {
167167
TargetUnix(Path)
168168
}
169169

170+
/// Authentication information
171+
#[deriving(Clone)]
172+
pub struct PostgresUserInfo {
173+
/// The username
174+
pub user: String,
175+
/// An optional password
176+
pub password: Option<String>,
177+
}
178+
170179
/// Information necessary to open a new connection to a Postgres server.
171180
#[deriving(Clone)]
172181
pub struct PostgresConnectParams {
@@ -180,9 +189,7 @@ pub struct PostgresConnectParams {
180189
///
181190
/// `PostgresConnection::connect` requires a user but `cancel_query` does
182191
/// not.
183-
pub user: Option<String>,
184-
/// An optional password used for authentication
185-
pub password: Option<String>,
192+
pub user: Option<PostgresUserInfo>,
186193
/// The database to connect to. Defaults the value of `user`.
187194
pub database: Option<String>,
188195
/// Runtime parameters to be passed to the Postgres backend.
@@ -235,9 +242,10 @@ impl IntoConnectParams for Url {
235242
TargetTcp(host)
236243
};
237244

238-
let (user, pass) = match user {
239-
Some(UserInfo { user, pass }) => (Some(user), pass),
240-
None => (None, None),
245+
let user = match user {
246+
Some(UserInfo { user, pass }) =>
247+
Some(PostgresUserInfo { user: user, password: pass }),
248+
None => None,
241249
};
242250

243251
let database = if !path.is_empty() {
@@ -252,7 +260,6 @@ impl IntoConnectParams for Url {
252260
target: target,
253261
port: port,
254262
user: user,
255-
password: pass,
256263
database: database,
257264
options: options,
258265
})
@@ -387,7 +394,6 @@ impl InnerPostgresConnection {
387394

388395
let PostgresConnectParams {
389396
user,
390-
password,
391397
database,
392398
mut options,
393399
..
@@ -416,7 +422,7 @@ impl InnerPostgresConnection {
416422
// WITH TIME ZONE values. Timespec converts to GMT internally.
417423
options.push(("TimeZone".to_string(), "GMT".to_string()));
418424
// We have to clone here since we need the user again for auth
419-
options.push(("user".to_string(), user.clone()));
425+
options.push(("user".to_string(), user.user.clone()));
420426
match database {
421427
Some(database) => options.push(("database".to_string(), database)),
422428
None => {}
@@ -427,7 +433,7 @@ impl InnerPostgresConnection {
427433
parameters: options.as_slice()
428434
}]));
429435

430-
try!(conn.handle_auth(user, password));
436+
try!(conn.handle_auth(user));
431437

432438
loop {
433439
match try_pg_conn!(conn.read_message()) {
@@ -473,12 +479,12 @@ impl InnerPostgresConnection {
473479
}
474480
}
475481

476-
fn handle_auth(&mut self, user: String, pass: Option<String>)
482+
fn handle_auth(&mut self, user: PostgresUserInfo)
477483
-> Result<(), PostgresConnectError> {
478484
match try_pg_conn!(self.read_message()) {
479485
AuthenticationOk => return Ok(()),
480486
AuthenticationCleartextPassword => {
481-
let pass = match pass {
487+
let pass = match user.password {
482488
Some(pass) => pass,
483489
None => return Err(MissingPassword)
484490
};
@@ -487,13 +493,13 @@ impl InnerPostgresConnection {
487493
}]));
488494
}
489495
AuthenticationMD5Password { salt } => {
490-
let pass = match pass {
496+
let pass = match user.password {
491497
Some(pass) => pass,
492498
None => return Err(MissingPassword)
493499
};
494500
let hasher = Hasher::new(MD5);
495501
hasher.update(pass.as_bytes());
496-
hasher.update(user.as_bytes());
502+
hasher.update(user.user.as_bytes());
497503
let output = hasher.final().as_slice().to_hex();
498504
let hasher = Hasher::new(MD5);
499505
hasher.update(output.as_bytes());
@@ -708,13 +714,15 @@ impl PostgresConnection {
708714
/// ```
709715
///
710716
/// ```rust,no_run
711-
/// # use postgres::{PostgresConnection, PostgresConnectParams, NoSsl, TargetUnix};
717+
/// # use postgres::{PostgresConnection, PostgresUserInfo, PostgresConnectParams, NoSsl, TargetUnix};
712718
/// # let some_crazy_path = Path::new("");
713719
/// let params = PostgresConnectParams {
714720
/// target: TargetUnix(some_crazy_path),
715721
/// port: None,
716-
/// user: Some("postgres".to_string()),
717-
/// password: None,
722+
/// user: Some(PostgresUserInfo {
723+
/// user: "postgres".to_string(),
724+
/// password: None
725+
/// }),
718726
/// database: None,
719727
/// options: vec![],
720728
/// };

0 commit comments

Comments
 (0)