KeyBoxen 0.1.0

Standalone secret-service daemon for window managers
// Copyright (C) 2022 KeyBoxen Authors
// SPDX-License-Identifier: GPL-3.0-or-later

//! DBus transaction data
//!
//! This module implements the data structures that are exchanged over
//! the DBus with the clients.

use serde::{Deserialize, Serialize};
use zbus::zvariant::{OwnedObjectPath, Type};
use zbus::DBusError;

/// DBus secret object
///
/// Item [secrets][ch02] that are exchanged over the DBus in response to
/// GetSecrets RPC call. The `value` may be encrypted, based on
/// algorithms and keys agreed upon session initiation and contained
/// in the session object. The data description is available in the
/// [API spec][ch14.sec].
///
/// [ch02]: https://specifications.freedesktop.org/secret-service/latest/ch02.html
/// [ch14.sec]: https://specifications.freedesktop.org/secret-service/latest/ch14.html#type-Secret
#[derive(Serialize, Deserialize, Type)]
pub struct DBusSecret {
    session: OwnedObjectPath,
    parameters: Vec<u8>,
    value: Vec<u8>,
    content_type: String,
}

/// Dbus Errors
///
/// Errors that are raised by the Secret Service over DBus, often as
/// responses to failed RPC invocations. Some errors are standard
/// [DBus Errors]. Others are specific [Secrets Errors][ch15].
///
/// [DBus Errors]: https://gitlab.freedesktop.org/dbus/dbus/-/blob/b30c2c293d6c599adafca5b82cb7df6351ca2fa1/dbus/dbus-protocol.h#L352
/// [ch15]: https://specifications.freedesktop.org/secret-service/latest/ch15.html
#[derive(DBusError, Debug)]
#[dbus_error(prefix = "org.freedesktop")]
pub enum DBusErrorKind {
    /// Unknown or fall-through ZBus error.
    #[dbus_error(zbus_error)]
    ZBus(zbus::Error),

    /// Algorithm is not supported
    #[dbus_error(name = "DBus.Error.NotSupported")]
    NotSupported(String),

    /// The object must be unlocked before this action can be carried out
    #[dbus_error(name = "Secret.Error.IsLocked")]
    IsLocked(String),

    /// The session does not exist
    #[dbus_error(name = "Secret.Error.NoSession")]
    NoSession(String),

    /// No such item or collection exists
    #[dbus_error(name = "Secret.Error.NoSuchObject")]
    NoSuchObject(String),
}