tauri-plugin-keystore 2.1.0-alpha.2

Interact with the device-native key storage (Android Keystore, iOS Keychain).
use serde::de::DeserializeOwned;
use tauri::{
    plugin::{PluginApi, PluginHandle},
    AppHandle, Runtime,
};

use crate::models::*;

#[cfg(target_os = "ios")]
tauri::ios_plugin_binding!(init_plugin_keystore);

// initializes the Kotlin or Swift plugin classes
pub fn init<R: Runtime, C: DeserializeOwned>(
    _app: &AppHandle<R>,
    api: PluginApi<R, C>,
) -> crate::Result<Keystore<R>> {
    #[cfg(target_os = "android")]
    let handle = api.register_android_plugin("app.tauri.keystore", "KeystorePlugin")?;
    #[cfg(target_os = "ios")]
    let handle = api.register_ios_plugin(init_plugin_keystore)?;
    Ok(Keystore(handle))
}

/// Access to the keystore APIs.
pub struct Keystore<R: Runtime>(PluginHandle<R>);

impl<R: Runtime> Keystore<R> {
    // Each of these blocks on an authentication prompt for as long as the user
    // takes to respond, so they use the async plugin API rather than occupying a
    // runtime thread for the duration.
    pub async fn store(&self, payload: StoreRequest) -> crate::Result<()> {
        self.0
            .run_mobile_plugin_async("store", payload)
            .await
            .map_err(Into::into)
    }

    pub async fn retrieve(&self, payload: RetrieveRequest) -> crate::Result<RetrieveResponse> {
        self.0
            .run_mobile_plugin_async("retrieve", payload)
            .await
            .map_err(Into::into)
    }

    pub async fn remove(&self, payload: RemoveRequest) -> crate::Result<()> {
        self.0
            .run_mobile_plugin_async("remove", payload)
            .await
            .map_err(Into::into)
    }
}