Skip to content

Commit

Permalink
client: Rename portal module to file
Browse files Browse the repository at this point in the history
As it is not really portal specific but rather a gnome-keyring file backend implementation.
By renaming it to using file module, we can possibly split the gnome format into a separate sub-module and introduce
a kwallet one later on.
  • Loading branch information
bilelmoussaoui committed Dec 4, 2024
1 parent 3bb4c3e commit 207b6c6
Show file tree
Hide file tree
Showing 20 changed files with 50 additions and 50 deletions.
6 changes: 3 additions & 3 deletions client/src/crypto/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use sha2::Sha256;
use subtle::ConstantTimeEq;
use zeroize::{Zeroize, Zeroizing};

use crate::{portal, Key};
use crate::{file, Key};

type EncAlg = cbc::Encryptor<aes::Aes128>;
type DecAlg = cbc::Decryptor<aes::Aes128>;
Expand Down Expand Up @@ -138,7 +138,7 @@ pub(crate) fn verify_checksum_md5(digest: impl AsRef<[u8]>, content: impl AsRef<

pub(crate) fn derive_key(
secret: impl AsRef<[u8]>,
key_strength: Result<(), portal::WeakKeyError>,
key_strength: Result<(), file::WeakKeyError>,
salt: impl AsRef<[u8]>,
iteration_count: usize,
) -> Key {
Expand All @@ -157,7 +157,7 @@ pub(crate) fn derive_key(

pub(crate) fn legacy_derive_key_and_iv(
secret: impl AsRef<[u8]>,
key_strength: Result<(), portal::WeakKeyError>,
key_strength: Result<(), file::WeakKeyError>,
salt: impl AsRef<[u8]>,
iteration_count: usize,
) -> (Key, Vec<u8>) {
Expand Down
10 changes: 5 additions & 5 deletions client/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
pub enum Error {
/// File backend error.
Portal(crate::portal::Error),
File(crate::file::Error),
/// Secret Service error.
DBus(crate::dbus::Error),
}

impl From<crate::portal::Error> for Error {
fn from(e: crate::portal::Error) -> Self {
Self::Portal(e)
impl From<crate::file::Error> for Error {
fn from(e: crate::file::Error) -> Self {
Self::File(e)
}
}

Expand All @@ -29,7 +29,7 @@ impl std::error::Error for Error {}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Portal(e) => write!(f, "Portal error {e}"),
Self::File(e) => write!(f, "File backend error {e}"),
Self::DBus(e) => write!(f, "DBus error {e}"),
}
}
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use endi::{Endian, ReadBytes};
use super::{Item, Secret};
use crate::{
crypto,
portal::{AttributeValue, Error, WeakKeyError},
file::{AttributeValue, Error, WeakKeyError},
AsAttributes,
};

Expand Down
4 changes: 2 additions & 2 deletions client/src/portal/api/mod.rs → client/src/file/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub(super) use legacy_keyring::{Keyring as LegacyKeyring, MAJOR_VERSION as LEGAC
use super::{Item, Secret};
use crate::{
crypto,
portal::{Error, WeakKeyError},
file::{Error, WeakKeyError},
AsAttributes, Key,
};

Expand Down Expand Up @@ -79,7 +79,7 @@ pub struct Keyring {
iteration_count: u32,
modified_time: u64,
usage_count: u32,
pub(in crate::portal) items: Vec<EncryptedItem>,
pub(in crate::file) items: Vec<EncryptedItem>,
}

impl Keyring {
Expand Down
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions client/src/portal/mod.rs → client/src/file/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! File backend implementation backed by the [Secret portal](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Secret.html).
//! File backend implementation that can be backed by the [Secret portal](https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.Secret.html).
//!
//! ```no_run
//! use std::collections::HashMap;
//!
//! use oo7::portal::Keyring;
//! use oo7::file::Keyring;
//!
//! # async fn run() -> oo7::Result<()> {
//! let keyring = Keyring::load_default().await?;
Expand Down
File renamed without changes.
10 changes: 5 additions & 5 deletions client/src/key.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use zeroize::{Zeroize, ZeroizeOnDrop};
use zvariant::Type;

use crate::{crypto, portal};
use crate::{crypto, file};

/// A key.
#[derive(Debug, Zeroize, ZeroizeOnDrop)]
pub struct Key {
key: Vec<u8>,
#[zeroize(skip)]
strength: Result<(), portal::WeakKeyError>,
strength: Result<(), file::WeakKeyError>,
}

impl AsRef<[u8]> for Key {
Expand All @@ -25,16 +25,16 @@ impl AsMut<[u8]> for Key {

impl Key {
pub fn new(key: Vec<u8>) -> Self {
Self::new_with_strength(key, Err(portal::WeakKeyError::StrengthUnknown))
Self::new_with_strength(key, Err(file::WeakKeyError::StrengthUnknown))
}

pub(crate) fn check_strength(&self) -> Result<(), portal::WeakKeyError> {
pub(crate) fn check_strength(&self) -> Result<(), file::WeakKeyError> {
self.strength
}

pub(crate) fn new_with_strength(
key: Vec<u8>,
strength: Result<(), portal::WeakKeyError>,
strength: Result<(), file::WeakKeyError>,
) -> Self {
Self { key, strength }
}
Expand Down
22 changes: 11 additions & 11 deletions client/src/keyring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use async_lock::RwLock;
use tokio::sync::RwLock;
use zeroize::Zeroizing;

use crate::{dbus, portal, AsAttributes, Result};
use crate::{dbus, file, AsAttributes, Result};

/// A [Secret Service](crate::dbus) or [file](crate::portal) backed keyring
/// A [Secret Service](crate::dbus) or [file](crate::file) backed keyring
/// implementation.
///
/// It will automatically use the file backend if the application is sandboxed
Expand All @@ -20,7 +20,7 @@ use crate::{dbus, portal, AsAttributes, Result};
#[derive(Debug)]
pub enum Keyring {
#[doc(hidden)]
File(Arc<portal::Keyring>),
File(Arc<file::Keyring>),
#[doc(hidden)]
DBus(dbus::Collection<'static>),
}
Expand All @@ -33,16 +33,16 @@ impl Keyring {
#[cfg(feature = "tracing")]
tracing::debug!("Application is sandboxed, using the file backend");

match portal::Keyring::load_default().await {
Ok(portal) => return Ok(Self::File(Arc::new(portal))),
match file::Keyring::load_default().await {
Ok(file) => return Ok(Self::File(Arc::new(file))),
// Do nothing in this case, we are supposed to fallback to the host keyring
Err(super::portal::Error::Portal(ashpd::Error::PortalNotFound(_))) => {
Err(super::file::Error::Portal(ashpd::Error::PortalNotFound(_))) => {
#[cfg(feature = "tracing")]
tracing::debug!(
"org.freedesktop.portal.Secrets is not available, falling back to the Secret Service backend"
);
}
Err(e) => return Err(crate::Error::Portal(e)),
Err(e) => return Err(crate::Error::File(e)),
};
} else {
#[cfg(feature = "tracing")]
Expand Down Expand Up @@ -159,13 +159,13 @@ impl Keyring {
#[derive(Debug)]
pub enum Item {
#[doc(hidden)]
File(RwLock<portal::Item>, Arc<portal::Keyring>),
File(RwLock<file::Item>, Arc<file::Keyring>),
#[doc(hidden)]
DBus(dbus::Item<'static>),
}

impl Item {
fn for_file(item: portal::Item, backend: Arc<portal::Keyring>) -> Self {
fn for_file(item: file::Item, backend: Arc<file::Keyring>) -> Self {
Self::File(RwLock::new(item), backend)
}

Expand Down Expand Up @@ -353,8 +353,8 @@ mod tests {
let path = dir.join("default.keyring");

let password = b"test";
let secret = portal::Secret::from(password.to_vec());
let keyring = Keyring::File(portal::Keyring::load(&path, secret).await?.into());
let secret = file::Secret::from(password.to_vec());
let keyring = Keyring::File(file::Keyring::load(&path, secret).await?.into());

let items = keyring.items().await?;
assert_eq!(items.len(), 0);
Expand Down
4 changes: 2 additions & 2 deletions client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ mod crypto;
#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
pub mod crypto;
pub mod dbus;
pub mod portal;
pub mod file;

mod keyring;

Expand All @@ -55,7 +55,7 @@ pub trait AsAttributes {
fn hash<'a>(&'a self, key: &Key) -> Vec<(&'a str, zeroize::Zeroizing<Vec<u8>>)> {
self.as_attributes()
.into_iter()
.map(|(k, v)| (k, crate::portal::AttributeValue::from(v).mac(key)))
.map(|(k, v)| (k, crate::file::AttributeValue::from(v).mac(key)))
.collect()
}
}
Expand Down
6 changes: 3 additions & 3 deletions client/src/migration.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{dbus::Service, portal::Keyring, AsAttributes, Result};
use crate::{dbus::Service, file::Keyring, AsAttributes, Result};

/// Helper to migrate your secrets from the host Secret Service
/// to the sandboxed file backend.
Expand All @@ -8,8 +8,8 @@ use crate::{dbus::Service, portal::Keyring, AsAttributes, Result};
pub async fn migrate(attributes: Vec<impl AsAttributes>, replace: bool) -> Result<()> {
let service = Service::new().await?;
let file_backend = match Keyring::load_default().await {
Ok(portal) => Ok(portal),
Err(super::portal::Error::Portal(ashpd::Error::PortalNotFound(_))) => {
Ok(file) => Ok(file),
Err(super::file::Error::Portal(ashpd::Error::PortalNotFound(_))) => {
#[cfg(feature = "tracing")]
tracing::debug!("Portal not available, no migration to do");
return Ok(());
Expand Down
8 changes: 4 additions & 4 deletions portal/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ pub enum Error {
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::Rand(e) => f.write_fmt(format_args!("Rand error {e}")),
Error::Oo7(e) => f.write_fmt(format_args!("DBus error: {e}")),
Error::Io(e) => f.write_fmt(format_args!("IO error: {e}")),
Error::Portal(e) => f.write_fmt(format_args!("Portal error: {e}")),
Self::Rand(e) => f.write_fmt(format_args!("Rand error {e}")),
Self::Oo7(e) => f.write_fmt(format_args!("DBus error: {e}")),
Self::Io(e) => f.write_fmt(format_args!("IO error: {e}")),
Self::Portal(e) => f.write_fmt(format_args!("Portal error: {e}")),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion server/src/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use oo7::{
api::{Properties, SecretInner},
ServiceError,
},
portal::Keyring,
file::Keyring,
};
use tokio::sync::{Mutex, RwLock};
use zbus::{interface, object_server::SignalEmitter, proxy::Defaults, zvariant};
Expand Down
12 changes: 6 additions & 6 deletions server/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ use std::fmt;
#[derive(Debug)]
pub enum Error {
// File backend error
Portal(oo7::portal::Error),
File(oo7::file::Error),
// Zbus error
Zbus(zbus::Error),
// IO error
IO(std::io::Error),
// Empty password error
EmptyPassword,
// Invalid item error
InvalidItem(oo7::portal::InvalidItemError),
InvalidItem(oo7::file::InvalidItemError),
}

impl From<zbus::Error> for Error {
Expand All @@ -20,9 +20,9 @@ impl From<zbus::Error> for Error {
}
}

impl From<oo7::portal::Error> for Error {
fn from(err: oo7::portal::Error) -> Self {
Self::Portal(err)
impl From<oo7::file::Error> for Error {
fn from(err: oo7::file::Error) -> Self {
Self::File(err)
}
}

Expand All @@ -35,7 +35,7 @@ impl From<std::io::Error> for Error {
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Portal(err) => write!(f, "Portal error {err}"),
Self::File(err) => write!(f, "Portal error {err}"),
Self::Zbus(err) => write!(f, "Zbus error {err}"),
Self::IO(err) => write!(f, "IO error {err}"),
Self::EmptyPassword => write!(f, "Login password can't be empty"),
Expand Down
6 changes: 3 additions & 3 deletions server/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{

use oo7::{
dbus::{api::SecretInner, ServiceError},
portal,
file,
};
use tokio::sync::Mutex;
use zbus::zvariant::OwnedObjectPath;
Expand All @@ -18,7 +18,7 @@ use crate::{collection::Collection, Service};
pub struct Item {
// Properties
locked: Arc<AtomicBool>,
inner: Arc<Mutex<oo7::portal::Item>>,
inner: Arc<Mutex<oo7::file::Item>>,
// Other attributes
service: Service,
collection_path: OwnedObjectPath,
Expand Down Expand Up @@ -171,7 +171,7 @@ impl Item {

impl Item {
pub fn new(
item: portal::Item,
item: file::Item,
locked: bool,
service: Service,
collection_path: &OwnedObjectPath,
Expand Down
2 changes: 1 addition & 1 deletion server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod service;
mod session;

use clap::Parser;
use oo7::portal::Secret;
use oo7::file::Secret;
use service::Service;

use crate::error::Error;
Expand Down
2 changes: 1 addition & 1 deletion server/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use oo7::{
api::{Properties, SecretInner},
Algorithm, ServiceError,
},
portal::{Keyring, Secret},
file::{Keyring, Secret},
Key,
};
use tokio::sync::{Mutex, RwLock};
Expand Down

0 comments on commit 207b6c6

Please sign in to comment.