1
#![cfg_attr(docsrs, feature(doc_cfg))]
2
#![deny(rustdoc::broken_intra_doc_links)]
3
#![doc = include_str!("../README.md")]
4
#![deny(unsafe_code)]
5
#[cfg(all(all(feature = "tokio", feature = "async-std"), not(doc)))]
6
compile_error!("You can't enable both async-std & tokio features at once");
7
#[cfg(all(not(feature = "tokio"), not(feature = "async-std"), not(doc)))]
8
compile_error!("You have to enable either tokio or async-std feature");
9
#[cfg(all(all(feature = "native_crypto", feature = "openssl_crypto"), not(doc)))]
10
compile_error!("You can't enable both openssl_crypto & native_crypto features at once");
11
#[cfg(all(
12
    not(feature = "native_crypto"),
13
    not(feature = "openssl_crypto"),
14
    not(doc)
15
))]
16
compile_error!("You have to enable either openssl_crypto or native_crypto feature");
17

            
18
use std::collections::HashMap;
19

            
20
mod error;
21
mod key;
22
mod mac;
23
mod migration;
24

            
25
pub use key::Key;
26
pub use mac::Mac;
27

            
28
#[cfg(not(feature = "unstable"))]
29
mod crypto;
30
#[cfg(feature = "unstable")]
31
#[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
32
pub mod crypto;
33
pub mod dbus;
34
pub mod file;
35

            
36
mod keyring;
37
mod secret;
38

            
39
pub use ashpd;
40
#[cfg(feature = "schema")]
41
#[cfg_attr(docsrs, doc(cfg(feature = "schema")))]
42
pub use error::SchemaError;
43
pub use error::{Error, Result};
44
pub use keyring::{Item, Keyring};
45
pub use migration::migrate;
46
#[cfg(feature = "schema")]
47
#[cfg_attr(docsrs, doc(cfg(feature = "schema")))]
48
pub use oo7_macros::SecretSchema;
49
pub use secret::{ContentType, Secret};
50
pub use zbus;
51

            
52
/// A schema attribute.
53
///
54
/// Currently the key, is not really used but would allow
55
/// to map a Rust struct of simple types to an item attributes with type check.
56
pub const XDG_SCHEMA_ATTRIBUTE: &str = "xdg:schema";
57

            
58
/// The default schema for items without a specific schema.
59
pub const GENERIC_SCHEMA: &str = "org.freedesktop.Secret.Generic";
60

            
61
/// A content type attribute.
62
///
63
/// Defines the type of the secret stored in the item.
64
pub const CONTENT_TYPE_ATTRIBUTE: &str = "xdg:content-type";
65

            
66
/// An item/collection attributes.
67
pub trait AsAttributes {
68
    fn as_attributes(&self) -> HashMap<String, String>;
69

            
70
12
    fn as_search_attributes(&self) -> HashMap<String, String> {
71
12
        let mut attributes = self.as_attributes();
72
24
        attributes.remove(CONTENT_TYPE_ATTRIBUTE);
73
12
        attributes
74
    }
75

            
76
68
    fn search_attributes(&self) -> HashMap<String, String> {
77
68
        self.as_attributes()
78
    }
79

            
80
37
    fn hash(&self, key: &Key) -> Vec<(String, std::result::Result<Mac, crate::crypto::Error>)> {
81
37
        self.search_attributes()
82
            .into_iter()
83
112
            .map(|(k, v)| (k, crypto::compute_mac(v.as_bytes(), key)))
84
            .collect()
85
    }
86
}
87

            
88
macro_rules! impl_as_attributes {
89
    ($rust_type:ty) => {
90
        impl<K, V> AsAttributes for $rust_type
91
        where
92
            K: AsRef<str>,
93
            V: AsRef<str>,
94
        {
95
37
            fn as_attributes(&self) -> std::collections::HashMap<String, String> {
96
34
                self.iter()
97
108
                    .map(|(k, v)| (k.as_ref().to_string(), v.as_ref().to_string()))
98
35
                    .collect()
99
            }
100
        }
101

            
102
        impl<K, V> AsAttributes for &$rust_type
103
        where
104
            K: AsRef<str>,
105
            V: AsRef<str>,
106
        {
107
2
            fn as_attributes(&self) -> std::collections::HashMap<String, String> {
108
2
                self.iter()
109
6
                    .map(|(k, v)| (k.as_ref().to_string(), v.as_ref().to_string()))
110
2
                    .collect()
111
            }
112
        }
113
    };
114
}
115

            
116
impl_as_attributes!([(K, V)]);
117
impl_as_attributes!(HashMap<K, V>);
118
impl_as_attributes!(std::collections::BTreeMap<K, V>);
119
impl_as_attributes!(Vec<(K, V)>);
120

            
121
impl<K, V, const N: usize> AsAttributes for [(K, V); N]
122
where
123
    K: AsRef<str>,
124
    V: AsRef<str>,
125
{
126
54
    fn as_attributes(&self) -> HashMap<String, String> {
127
54
        self.iter()
128
163
            .map(|(k, v)| (k.as_ref().to_string(), v.as_ref().to_string()))
129
            .collect()
130
    }
131
}
132

            
133
// Implementation for references to arrays
134
impl<K, V, const N: usize> AsAttributes for &[(K, V); N]
135
where
136
    K: AsRef<str>,
137
    V: AsRef<str>,
138
{
139
5
    fn as_attributes(&self) -> HashMap<String, String> {
140
5
        self.iter()
141
15
            .map(|(k, v)| (k.as_ref().to_string(), v.as_ref().to_string()))
142
            .collect()
143
    }
144
}