Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export declare class AsyncEntry {
/**
* Retrieve the password saved for this entry.
*
* Returns a [NoEntry](Error::NoEntry) error if there isn't one.
* Returns no password if there isn't one.
*
* Can return an [Ambiguous](Error::Ambiguous) error
* if there is more than one platform credential
Expand Down Expand Up @@ -113,7 +113,7 @@ export declare class Entry {
/**
* Retrieve the password saved for this entry.
*
* Returns a [NoEntry](Error::NoEntry) error if there isn't one.
* Returns no password if there isn't one.
*
* Can return an [Ambiguous](Error::Ambiguous) error
* if there is more than one platform credential
Expand Down
5 changes: 3 additions & 2 deletions src/async_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::sync::Arc;
use napi::bindgen_prelude::*;
use napi_derive::napi;

use crate::entry::into_optional_password;
#[cfg(target_os = "linux")]
use crate::linux_credential_builder::LinuxCredentialBuilder;

Expand Down Expand Up @@ -161,7 +162,7 @@ impl AsyncEntry {
#[napi(ts_return_type = "Promise<string | undefined>")]
/// Retrieve the password saved for this entry.
///
/// Returns a [NoEntry](Error::NoEntry) error if there isn't one.
/// Returns no password if there isn't one.
///
/// Can return an [Ambiguous](Error::Ambiguous) error
/// if there is more than one platform credential
Expand Down Expand Up @@ -250,7 +251,7 @@ impl Task for PasswordTask {
type JsValue = Option<String>;

fn compute(&mut self) -> Result<Self::Output> {
Ok(self.inner.get_password().ok())
into_optional_password(self.inner.get_password())
}

fn resolve(&mut self, _env: Env, output: Self::Output) -> Result<Self::JsValue> {
Expand Down
46 changes: 43 additions & 3 deletions src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ use napi_derive::napi;
#[cfg(target_os = "linux")]
use crate::linux_credential_builder::LinuxCredentialBuilder;

pub(crate) fn into_optional_password(
result: keyring_core::Result<String>,
) -> Result<Option<String>> {
match result {
Ok(password) => Ok(Some(password)),
Err(keyring_core::Error::NoEntry) => Ok(None),
Err(error) => Err(anyhow::Error::from(error).into()),
}
}

#[napi]
pub struct Entry {
inner: keyring_core::Entry,
Expand Down Expand Up @@ -146,15 +156,15 @@ impl Entry {
#[napi]
/// Retrieve the password saved for this entry.
///
/// Returns a [NoEntry](Error::NoEntry) error if there isn't one.
/// Returns no password if there isn't one.
///
/// Can return an [Ambiguous](Error::Ambiguous) error
/// if there is more than one platform credential
/// that matches this entry. This can only happen
/// on some platforms, and then only if a third-party
/// application wrote the ambiguous credential.
pub fn get_password(&self) -> Option<String> {
self.inner.get_password().ok()
pub fn get_password(&self) -> Result<Option<String>> {
into_optional_password(self.inner.get_password())
}

#[napi]
Expand Down Expand Up @@ -200,6 +210,36 @@ impl Entry {
}
}

#[cfg(test)]
mod tests {
use super::into_optional_password;

#[test]
fn returns_password_when_found() {
assert_eq!(
into_optional_password(Ok("password".to_string())).unwrap(),
Some("password".to_string())
);
}

#[test]
fn returns_none_when_password_is_missing() {
assert_eq!(
into_optional_password(Err(keyring_core::Error::NoEntry)).unwrap(),
None
);
}

#[test]
fn preserves_non_missing_errors() {
let result = into_optional_password(Err(keyring_core::Error::NoStorageAccess(Box::new(
std::io::Error::other("keychain is locked"),
))));

assert!(result.is_err());
}
}

#[napi(object)]
pub struct Credential {
pub account: String,
Expand Down
Loading