tmuntaner_keyring/lib.rs
1/*
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
5 */
6
7use anyhow::Result;
8
9#[cfg(target_os = "linux")]
10mod secret_service;
11
12#[cfg(target_os = "linux")]
13mod linux;
14
15#[cfg(target_os = "linux")]
16use linux::Keyring;
17
18#[cfg(target_os = "macos")]
19mod mac;
20
21#[cfg(target_os = "macos")]
22use mac::Keyring;
23
24#[cfg(target_os = "windows")]
25mod wincred;
26
27#[cfg(target_os = "windows")]
28mod win;
29
30#[cfg(target_os = "windows")]
31use win::Keyring;
32
33/// This Keyring Client interacts with the OS specific keyring to store a secret.
34///
35/// ## Keyring Backends:
36/// * Windows - [wincred](https://docs.microsoft.com/en-us/windows/win32/api/wincred/)
37/// * Linux - [Secret Service](https://specifications.freedesktop.org/secret-service/latest/)
38/// * Mac - [Security Framework](https://developer.apple.com/documentation/security)
39pub struct KeyringClient<'a> {
40 client: Keyring<'a>,
41}
42
43impl<'a> KeyringClient<'a> {
44 /// Returns a new keyring client
45 ///
46 /// # Arguments
47 ///
48 /// * `username` - The username to store secrets under
49 /// * `service` - A unique identifier within your application
50 /// * `application` - The name of your application
51 pub fn new(username: &'a str, service: &'a str, application: &'a str) -> Result<Self> {
52 let client = Keyring::new(username, service, application)?;
53
54 Ok(KeyringClient { client })
55 }
56
57 pub fn set_password(&self, password: String) -> Result<()> {
58 self.client.set_password(password)
59 }
60
61 pub fn get_password(&self) -> Result<Option<String>> {
62 self.client.get_password()
63 }
64}