keyring-manager 0.10.0

Cross-platform library for managing passwords
Documentation
#[cfg(target_os = "linux")]
use dbus_secret_service::Error as SsError;
#[cfg(target_os = "macos")]
use security_framework::base::Error as SfError;
use std::fmt;
use std::string::FromUtf8Error;

#[cfg(target_os = "ios")]
pub type OSStatus = i32;
pub type Result<T> = ::std::result::Result<T, KeyringError>;

/// An error from a keyring backend operation.
#[derive(Debug)]
pub enum KeyringError {
    /// Filesystem error from the insecure file backend.
    IoError(std::io::Error),
    /// macOS keychain error.
    #[cfg(target_os = "macos")]
    MacOsKeychainError(SfError),
    /// iOS keychain error (OSStatus code).
    #[cfg(target_os = "ios")]
    IOSKeychainError(OSStatus),
    /// Linux Secret Service error.
    #[cfg(target_os = "linux")]
    SecretServiceError(SsError),
    /// Windows credential vault error, carrying the `GetLastError` code.
    #[cfg(target_os = "windows")]
    WindowsVaultError(u32),
    /// Android keystore error.
    #[cfg(target_os = "android")]
    AndroidKeystoreError(String),
    /// No keyring backend is available on this platform.
    NoBackendFound,
    /// No value is stored for the requested key.
    NoPasswordFound,
    /// The store is locked, or absence could not be proven because the store is locked. Any stored
    /// value is intact; retry once the device is unlocked. Never treat this as absence: a caller
    /// that reprovisions on absence would overwrite a secret it could not read.
    Locked,
    /// The active backend does not implement this operation (e.g. key enumeration on an OS keychain).
    Unsupported,
    /// A stored value was not valid UTF-8.
    Parse(FromUtf8Error),
    /// Any other error, carrying a message.
    Generic(String),
}

impl fmt::Display for KeyringError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            KeyringError::IoError(ref err) => write!(f, "Keyring IO error: {}", err),
            #[cfg(target_os = "macos")]
            KeyringError::MacOsKeychainError(ref err) => {
                write!(f, "MacOS keychain Error: {}", err)
            }
            #[cfg(target_os = "ios")]
            KeyringError::IOSKeychainError(ref err) => {
                write!(f, "iOS keychain Error: {}", err)
            }
            #[cfg(target_os = "linux")]
            KeyringError::SecretServiceError(ref err) => write!(f, "Secret service error: {}", err),
            #[cfg(target_os = "windows")]
            KeyringError::WindowsVaultError(code) => {
                write!(f, "Windows vault error: {}", code)
            }
            #[cfg(target_os = "android")]
            KeyringError::AndroidKeystoreError(s) => {
                write!(f, "Android keystore error: {}", s)
            }
            KeyringError::NoBackendFound => write!(f, "Keyring error: No Backend Found"),
            KeyringError::NoPasswordFound => write!(f, "Keyring error: No Password Found"),
            KeyringError::Locked => write!(f, "Keyring error: Store Locked"),
            KeyringError::Unsupported => {
                write!(f, "Keyring error: Operation Unsupported by backend")
            }
            KeyringError::Parse(ref err) => write!(f, "Keyring parse error: {}", err),
            KeyringError::Generic(ref err) => write!(f, "Keyring generic error: {}", err),
        }
    }
}

impl std::error::Error for KeyringError {
    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
        match *self {
            KeyringError::IoError(ref err) => Some(err),
            #[cfg(target_os = "linux")]
            KeyringError::SecretServiceError(ref err) => Some(err),
            #[cfg(target_os = "macos")]
            KeyringError::MacOsKeychainError(ref err) => Some(err),
            _ => None,
        }
    }
}

impl From<std::io::Error> for KeyringError {
    fn from(err: std::io::Error) -> KeyringError {
        KeyringError::IoError(err)
    }
}

#[cfg(target_os = "linux")]
impl From<SsError> for KeyringError {
    fn from(err: SsError) -> KeyringError {
        KeyringError::SecretServiceError(err)
    }
}

#[cfg(target_os = "macos")]
impl From<SfError> for KeyringError {
    fn from(err: SfError) -> KeyringError {
        KeyringError::MacOsKeychainError(err)
    }
}

#[cfg(target_os = "ios")]
impl From<OSStatus> for KeyringError {
    fn from(err: OSStatus) -> KeyringError {
        KeyringError::IOSKeychainError(err)
    }
}

impl From<FromUtf8Error> for KeyringError {
    fn from(err: FromUtf8Error) -> KeyringError {
        KeyringError::Parse(err)
    }
}

impl From<String> for KeyringError {
    fn from(err: String) -> KeyringError {
        KeyringError::Generic(err)
    }
}