Skip to content

Commit d097111

Browse files
committed
Add get_password method
1 parent c01f513 commit d097111

3 files changed

Lines changed: 18 additions & 3 deletions

File tree

sqlx-postgres/src/connection/establish.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl PgConnection {
7777

7878
stream
7979
.send(Password::Cleartext(
80-
options.password.as_deref().unwrap_or_default(),
80+
&options.get_password().unwrap_or_default(),
8181
))
8282
.await?;
8383
}
@@ -91,7 +91,7 @@ impl PgConnection {
9191
stream
9292
.send(Password::Md5 {
9393
username: &options.username,
94-
password: options.password.as_deref().unwrap_or_default(),
94+
password: &options.get_password().unwrap_or_default(),
9595
salt: body.salt,
9696
})
9797
.await?;

sqlx-postgres/src/connection/sasl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ pub(crate) async fn authenticate(
8787

8888
// SaltedPassword := Hi(Normalize(password), salt, i)
8989
let salted_password = hi(
90-
options.password.as_deref().unwrap_or_default(),
90+
&options.get_password().unwrap_or_default(),
9191
&cont.salt,
9292
cont.iterations,
9393
)?;

sqlx-postgres/src/options/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,21 @@ impl PgConnectOptions {
519519
&self.username
520520
}
521521

522+
/// Get the password.
523+
///
524+
/// ```rust
525+
/// # use sqlx_postgres::PgConnectOptions;
526+
/// let options = PgConnectOptions::new()
527+
/// .password("53C237");
528+
/// assert_eq!(options.get_password().as_deref(), Some("53C237"));
529+
/// ```
530+
pub fn get_password(&self) -> Option<Cow<'_, str>> {
531+
if self.password.is_some() {
532+
return self.password.as_deref().map(Cow::Borrowed);
533+
}
534+
None
535+
}
536+
522537
/// Get the current database name.
523538
///
524539
/// # Example

0 commit comments

Comments
 (0)