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
274
275
276
277
278
/*!

# Keyring

This is a cross-platform library for searching the platform specific keystore.
[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.
 */

use std::collections::HashMap;

pub use error::{Error, Result};
pub use search::{CredentialSearch, CredentialSearchResult, Limit};
// 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 error;
pub mod search;

pub fn set_default_credential_search(default_search: Box<CredentialSearch>) -> Result<Search> {
    Ok(Search {
        inner: default_search,
    })
}

fn default_credential_search() -> Result<Search> {
    let credentials = default::default_credential_search();
    Ok(Search { inner: credentials })
}

pub struct Search {
    inner: Box<CredentialSearch>,
}
/// The implementation of the Search structures methods.
///
/// The default search types are: Target, User, and Service.
/// On linux-keyutils these all default to searching the 'session'
/// keyring. If searching in a different keyring, utilize the
/// platform specific `search_by_keyring` function
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 searching by target and the query string
    ///
    /// Can return:
    /// [SearchError](Error::SearchError)
    /// [NoResults](Error::NoResults)
    /// [Unexpected](Error::Unexpected)
    ///
    /// # Example
    ///     let search = keyring_search::Search::new().unwrap();
    ///     let results = search.by_target("Foo.app");
    pub fn by_target(&self, query: &str) -> CredentialSearchResult {
        self.inner.by("target", query)
    }
    /// Specifies searching by user and the query string
    ///
    /// Can return:
    /// [SearchError](Error::SearchError)
    /// [NoResults](Error::NoResults)
    /// [Unexpected](Error::Unexpected)
    ///
    /// # Example
    ///     let search = keyring_search::Search::new().unwrap();
    ///     let results = search.by_user("Mr. Foo Bar");
    pub fn by_user(&self, query: &str) -> CredentialSearchResult {
        self.inner.by("user", query)
    }
    /// Specifies searching by service and the query string
    ///
    /// Can return:
    /// [SearchError](Error::SearchError)
    /// [NoResults](Error::NoResults)
    /// [Unexpected](Error::Unexpected)
    ///
    /// # Example
    ///     let search = keyring_search::Search::new().unwrap();
    ///     let results = search.by_service("Bar inc.");
    pub fn by_service(&self, query: &str) -> CredentialSearchResult {
        self.inner.by("service", query)
    }
}

pub struct List {}

/// Implementation of methods for the `List` structure.
///
/// `list_all`, lists all returned credentials
/// `list_max`, lists a specified max amount of
/// credentials. These are specified by calling [list_credentials](List::list_credentials).
///
/// Linux-keyutils search feature is limited to one result,
/// no matter the `Limit`, one result will be returned.
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) -> 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) -> String {
        let mut output = String::new();
        match result {
            Ok(search_result) => {
                let mut entries: Vec<(String, HashMap<String, String>)> = search_result
                    .iter()
                    .map(|(k, v)| (k.clone(), v.clone()))
                    .collect();
                entries.sort_by_key(|(k, _)| k.parse::<i32>().unwrap_or(0));

                for (outer_key, inner_map) in entries {
                    output.push_str(&format!("{}\n", outer_key));
                    let mut metadata: Vec<(String, String)> = inner_map
                        .iter()
                        .map(|(k, v)| (k.clone(), v.clone()))
                        .collect();
                    metadata.sort_by(|a, b| a.0.cmp(&b.0));
                    for (key, value) in metadata {
                        output.push_str(&format!("{}: {}\n", key, value));
                    }
                }
                println!("Search returned {} results\n", search_result.keys().len());
                output
            }
            Err(err) => 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.
    fn list_max(result: &CredentialSearchResult, max: i64) -> String {
        let mut output = String::new();
        let mut count = 1;
        match result {
            Ok(search_result) => {
                let mut entries: Vec<(String, HashMap<String, String>)> = search_result
                    .iter()
                    .map(|(k, v)| (k.clone(), v.clone()))
                    .collect();
                entries.sort_by_key(|(k, _)| k.parse::<i32>().unwrap_or(0));

                for (outer_key, inner_map) in entries {
                    output.push_str(&format!("{}\n", outer_key));
                    let mut metadata: Vec<(String, String)> = inner_map
                        .iter()
                        .map(|(k, v)| (k.clone(), v.clone()))
                        .collect();
                    metadata.sort_by(|a, b| a.0.cmp(&b.0));
                    for (key, value) in metadata {
                        output.push_str(&format!("{}: {}\n", key, value));
                    }
                    count += 1;
                    if count > max {
                        break;
                    }
                }
                println!("Search returned {} results\n", search_result.keys().len());
                output
            }
            Err(err) => 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)
    }
}