Skip to content

Commit 08bcb84

Browse files
client: Add a as_str helper
1 parent 0cc9eb1 commit 08bcb84

5 files changed

Lines changed: 28 additions & 20 deletions

File tree

cargo-credential/src/main.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ impl SecretServiceCredential {
2727
return Err(Error::NotFound);
2828
}
2929

30+
let secret = items[0]
31+
.secret()
32+
.await
33+
.map_err(|err| Error::Other(Box::new(err)))?;
3034
let token = Secret::from(
31-
std::str::from_utf8(
32-
&items[0]
33-
.secret()
34-
.await
35-
.map_err(|err| Error::Other(Box::new(err)))?,
36-
)
37-
.unwrap()
38-
.to_owned(),
35+
secret
36+
.as_str()
37+
.ok_or_else(|| Error::Other("secret is not valid UTF-8".into()))?
38+
.to_owned(),
3939
);
4040

4141
Ok(CredentialResponse::Get {

cli/src/main.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,12 @@ impl ItemOutput {
147147
.unwrap();
148148

149149
let secret_str = secret.map(|s| {
150-
let bytes = s.as_bytes();
151150
if as_hex {
152-
hex::encode(bytes)
151+
hex::encode(s.as_bytes())
153152
} else {
154-
match std::str::from_utf8(bytes) {
155-
Ok(s) => s.to_string(),
156-
Err(_) => hex::encode(bytes),
153+
match s.as_str() {
154+
Some(s) => s.to_string(),
155+
None => hex::encode(s.as_bytes()),
157156
}
158157
}
159158
});

client/src/secret.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@ impl Secret {
107107
}
108108
}
109109

110+
/// Returns the secret as a string slice, or `None` if it is not valid
111+
/// UTF-8.
112+
pub fn as_str(&self) -> Option<&str> {
113+
match self {
114+
Self::Text(text) => Some(text.as_str()),
115+
Self::Blob(bytes) => std::str::from_utf8(bytes).ok(),
116+
}
117+
}
118+
110119
pub fn as_bytes(&self) -> &[u8] {
111120
match self {
112121
Self::Text(text) => text.as_bytes(),

git-credential/src/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ impl Credential {
161161
}
162162
}
163163

164-
fn parse_secret(secret: &[u8]) -> (String, Option<String>, Option<String>) {
165-
let text = std::str::from_utf8(secret).unwrap_or_default();
164+
fn parse_secret(secret: &oo7::Secret) -> (String, Option<String>, Option<String>) {
165+
let text = secret.as_str().unwrap_or_default();
166166
let mut lines = text.split('\n');
167167
let password = lines.next().unwrap_or_default().to_owned();
168168
let mut password_expiry_utc = None;
@@ -186,7 +186,7 @@ async fn run(action: &str, credential: &Credential, collection: &Collection) ->
186186
if let Some(item) = items.first() {
187187
let attrs = item.attributes_as::<GitSchema>().await?;
188188
let secret = item.secret().await?;
189-
let (password, expiry, oauth) = parse_secret(secret.as_bytes());
189+
let (password, expiry, oauth) = parse_secret(&secret);
190190

191191
if let Some(user) = &attrs.user {
192192
println!("username={user}");
@@ -222,7 +222,7 @@ async fn run(action: &str, credential: &Credential, collection: &Collection) ->
222222
&& let Some(item) = items.first()
223223
{
224224
let secret = item.secret().await?;
225-
let (stored_password, ..) = parse_secret(secret.as_bytes());
225+
let (stored_password, ..) = parse_secret(&secret);
226226
if stored_password != *password {
227227
return Ok(());
228228
}

server/src/tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -535,15 +535,15 @@ impl MockPrompterService {
535535
let pwd = queue.remove(0);
536536
tracing::debug!(
537537
"MockPrompter: using password from queue (length: {}, queue remaining: {})",
538-
std::str::from_utf8(pwd.as_bytes()).unwrap_or("<binary>"),
538+
pwd.as_str().unwrap_or("<binary>"),
539539
queue.len()
540540
);
541541
pwd
542542
} else {
543543
let pwd = unlock_password.lock().await.clone().unwrap();
544544
tracing::debug!(
545545
"MockPrompter: using default password (length: {})",
546-
std::str::from_utf8(pwd.as_bytes()).unwrap_or("<binary>")
546+
pwd.as_str().unwrap_or("<binary>")
547547
);
548548
pwd
549549
};
@@ -796,7 +796,7 @@ impl MockPrompterServicePlasma {
796796
let pwd = self.unlock_password.lock().await.clone().unwrap();
797797
tracing::debug!(
798798
"MockPrompterServicePlasma: using default password (length: {})",
799-
std::str::from_utf8(pwd.as_bytes()).unwrap_or("<binary>")
799+
pwd.as_str().unwrap_or("<binary>")
800800
);
801801
MockPrompterServicePlasma::send_secret(&connection, &callback_path, &pwd).await?;
802802
};

0 commit comments

Comments
 (0)