1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
/*!
# Keyring
This is a cross-platform library for searching the platform specific keystore.
[this library's entry on crates.io](https://crates.io/crates/keyring-search).
Currently supported platforms are
Linux,
Windows,
macOS, and iOS.
## Design
This crate, originally planned as a feature for
[keyring](https://crates.io/crates/keyring) provides a broad search of
the platform specific keystores based on user provided search parameters.
### Windows
Windows machines have the option to search by 'user', 'service', or 'target'.
```
use keyring_search::{Search, Limit, List};
fn main() {
let result = Search::new()
.expect("ERROR")
.by("user", "test-user");
let list = List::list_credentials(result, Limit::All)
.expect("Error");
println!("{}", list);
}
```
### Linux - Secret Service
If using the Linux Secret Service platform, the keystore is stored as a HashMap,
and thus is more liberal with the keys that can be searched. The by method will take
any parameter passed and attempt to search for the user defined key.
```
use keyring_search::{Search, Limit, List};
fn main() {
let result = Search::new()
.expect("ERROR")
.by("user", "test-user");
let list = List::list_credentials(result, Limit::All)
.expect("Error");
println!("{}", list);
}
```
### Linux - Keyutils
If using the Linux Keyutils platform, the keystore is non persistent and is used more
as a secure cache. However, this can still be searched. The breadth of the by method is large
and encompasses the different types of keyrings available: "thread", "process", "session,
"user", "user session", and "group". Because of this searching mechanism, the search has to be
rather specific while limiting the different types of data to search, i.e. user, account, service.
```
use keyring_search::{Search, Limit, List};
fn main() {
let result = Search::new()
.expect("ERROR")
.by("session", "test-user@test-service");
let list = List::list_credentials(result, Limit::All)
.expect("Error");
println!("{}", list);
}
```
### MacOS
MacOS machines have the option to search by 'account', 'service', or 'label.
```
use keyring_search::{Search, Limit, List};
fn main() {
let result = Search::new()
.expect("ERROR")
.by("account", "test-user");
let list = List::list_credentials(result, Limit::All)
.expect("Error");
println!("{}", list);
}
```
*/
pub use search::{
CredentialSearch, CredentialSearchResult, Limit,
};
pub use error::{Error, Result};
// Included keystore implementations and default choice thereof.
pub mod mock;
#[cfg(all(target_os = "linux", feature = "linux-keyutils"))]
pub mod keyutils;
#[cfg(all(
target_os = "linux",
feature = "secret-service",
not(feature = "linux-no-secret-service")
))]
pub mod secret_service;
#[cfg(all(
target_os = "linux",
feature = "secret-service",
not(feature = "linux-default-keyutils")
))]
use crate::secret_service as default;
#[cfg(all(
target_os = "linux",
feature = "linux-keyutils",
any(feature = "linux-default-keyutils", not(feature = "secret-service"))
))]
use keyutils as default;
#[cfg(all(
target_os = "linux",
not(feature = "secret-service"),
not(feature = "linux-keyutils")
))]
use mock as default;
#[cfg(all(target_os = "freebsd", feature = "secret-service"))]
pub mod secret_service;
#[cfg(all(target_os = "freebsd", feature = "secret-service"))]
use crate::secret_service as default;
#[cfg(all(target_os = "freebsd", not(feature = "secret-service")))]
use mock as default;
#[cfg(all(target_os = "openbsd", feature = "secret-service"))]
pub mod secret_service;
#[cfg(all(target_os = "openbsd", feature = "secret-service"))]
use crate::secret_service as default;
#[cfg(all(target_os = "openbsd", not(feature = "secret-service")))]
use mock as default;
#[cfg(all(target_os = "macos", feature = "platform-macos"))]
pub mod macos;
#[cfg(all(target_os = "macos", feature = "platform-macos"))]
use macos as default;
#[cfg(all(target_os = "macos", not(feature = "platform-macos")))]
use mock as default;
#[cfg(all(target_os = "windows", feature = "platform-windows"))]
pub mod windows;
#[cfg(all(target_os = "windows", not(feature = "platform-windows")))]
use mock as default;
#[cfg(all(target_os = "windows", feature = "platform-windows"))]
use windows as default;
#[cfg(all(target_os = "ios", feature = "platform-ios"))]
pub mod ios;
#[cfg(all(target_os = "ios", feature = "platform-ios"))]
use ios as default;
#[cfg(all(target_os = "ios", not(feature = "platform-ios")))]
use mock as default;
#[cfg(not(any(
target_os = "linux",
target_os = "freebsd",
target_os = "openbsd",
target_os = "macos",
target_os = "ios",
target_os = "windows",
)))]
use mock as default;
pub mod search;
pub mod error;
fn default_credential_search() -> Result<Search> {
let credentials = default::default_credential_search();
Ok(Search { inner: credentials })
}
pub struct Search {
inner: Box<CredentialSearch>,
}
impl Search {
/// Create a new instance of the Credential Search.
///
/// The default credential search is used.
pub fn new() -> Result<Search> {
default_credential_search()
}
/// Specifies what parameter to search by and the query string
///
/// Can return a [SearchError](Error::SearchError)
/// # Example
/// let search = keyring_search::Search::new().unwrap();
/// let results = search.by("user", "Mr. Foo Bar");
pub fn by(&self, by: &str, query: &str) -> CredentialSearchResult {
self.inner.by(by, query)
}
}
pub struct List {}
impl List {
/// List the credentials with given search result
///
/// Takes CredentialSearchResult type and converts to a string
/// for printing. Matches the Limit type passed to constrain
/// the amount of results added to the string
pub fn list_credentials(search_result: CredentialSearchResult, limit: Limit) -> Result<String> {
match limit {
Limit::All => Self::list_all(search_result),
Limit::Max(max) => Self::list_max(search_result, max),
}
}
/// List all credential search results.
///
/// Is the result of passing the Limit::All type
/// to list_credentials.
fn list_all(result: CredentialSearchResult) -> Result<String> {
let mut output = String::new();
match result {
Ok(search_result) => {
for (outer_key, inner_map) in search_result {
output.push_str(&format!("{}\n", outer_key));
for (key, value) in inner_map {
output.push_str(&format!("\t{}:\t{}\n", key, value));
}
}
Ok(output)
}
Err(err) => Err(Error::SearchError(err.to_string())),
}
}
/// List a certain amount of credential search results.
///
/// Is the result of passing the Limit::Max(i64) type
/// to list_credentials. The 64 bit integer represents
/// the total of the results passed.
/// They are not sorted or filtered.
fn list_max(result: CredentialSearchResult, max: i64) -> Result<String> {
let mut output = String::new();
let mut count = 1;
match result {
Ok(search_result) => {
for (outer_key, inner_map) in search_result {
output.push_str(&format!("{}\n", outer_key));
for (key, value) in inner_map {
output.push_str(&format!("\t{}:\t{}\n", key, value));
}
count += 1;
if count > max {
break;
}
}
Ok(output)
}
Err(err) => Err(Error::SearchError(err.to_string())),
}
}
}
#[cfg(test)]
mod tests {
pub fn generate_random_string_of_len(len: usize) -> String {
// from the Rust Cookbook:
// https://rust-lang-nursery.github.io/rust-cookbook/algorithms/randomness.html
use rand::{distributions::Alphanumeric, thread_rng, Rng};
thread_rng()
.sample_iter(&Alphanumeric)
.take(len)
.map(char::from)
.collect()
}
pub fn generate_random_string() -> String {
generate_random_string_of_len(30)
}
}