mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-07-10 16:27:05 +02:00
initial rough impl
This commit is contained in:
@@ -2,14 +2,26 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
use tauri::{AppHandle, command, Runtime, Window};
|
||||
use tauri::{command, AppHandle, Runtime};
|
||||
|
||||
use crate::Result;
|
||||
use crate::{Result, SecureStorageExt};
|
||||
|
||||
#[command]
|
||||
pub(crate) async fn execute<R: Runtime>(
|
||||
_app: AppHandle<R>,
|
||||
_window: Window<R>,
|
||||
) -> Result<String> {
|
||||
Ok("success".to_string())
|
||||
pub(crate) fn set_string<R: Runtime>(app: AppHandle<R>, key: &str, value: &str) -> Result<()> {
|
||||
app.secure_storage().set_string(key, value)
|
||||
}
|
||||
|
||||
#[command]
|
||||
pub(crate) fn get_string<R: Runtime>(app: AppHandle<R>, key: &str) -> Result<String> {
|
||||
app.secure_storage().get_string(key)
|
||||
}
|
||||
|
||||
#[command]
|
||||
pub(crate) fn set_binary<R: Runtime>(app: AppHandle<R>, key: &str, value: &[u8]) -> Result<()> {
|
||||
app.secure_storage().set_binary(key, value)
|
||||
}
|
||||
|
||||
#[command]
|
||||
pub(crate) fn get_binary<R: Runtime>(app: AppHandle<R>, key: &str) -> Result<Vec<u8>> {
|
||||
app.secure_storage().get_binary(key)
|
||||
}
|
||||
|
||||
@@ -2,10 +2,11 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
use keyring::Entry;
|
||||
use serde::de::DeserializeOwned;
|
||||
use tauri::{plugin::PluginApi, AppHandle, Runtime};
|
||||
|
||||
use crate::models::*;
|
||||
use crate::Result;
|
||||
|
||||
pub fn init<R: Runtime, C: DeserializeOwned>(
|
||||
app: &AppHandle<R>,
|
||||
@@ -18,9 +19,19 @@ pub fn init<R: Runtime, C: DeserializeOwned>(
|
||||
pub struct SecureStorage<R: Runtime>(AppHandle<R>);
|
||||
|
||||
impl<R: Runtime> SecureStorage<R> {
|
||||
pub fn ping(&self, payload: PingRequest) -> crate::Result<PingResponse> {
|
||||
Ok(PingResponse {
|
||||
value: payload.value,
|
||||
})
|
||||
pub fn set_string(&self, key: &str, value: &str) -> Result<()> {
|
||||
Ok(Entry::new(&self.0.config().identifier, key)?.set_password(value)?)
|
||||
}
|
||||
|
||||
pub fn get_string(&self, key: &str) -> Result<String> {
|
||||
Ok(Entry::new(&self.0.config().identifier, key)?.get_password()?)
|
||||
}
|
||||
|
||||
pub fn set_binary(&self, key: &str, value: &[u8]) -> Result<()> {
|
||||
Ok(Entry::new(&self.0.config().identifier, key)?.set_secret(value)?)
|
||||
}
|
||||
|
||||
pub fn get_binary(&self, key: &str) -> Result<Vec<u8>> {
|
||||
Ok(Entry::new(&self.0.config().identifier, key)?.get_secret()?)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,9 +8,10 @@ pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum Error {
|
||||
#[cfg(not(target_os = "android"))]
|
||||
#[error(transparent)]
|
||||
Io(#[from] std::io::Error),
|
||||
#[cfg(mobile)]
|
||||
Keyring(#[from] keyring::Error),
|
||||
#[cfg(target_os = "android")]
|
||||
#[error(transparent)]
|
||||
PluginInvoke(#[from] tauri::plugin::mobile::PluginInvokeError),
|
||||
}
|
||||
|
||||
@@ -9,9 +9,9 @@ use tauri::{
|
||||
|
||||
pub use models::*;
|
||||
|
||||
#[cfg(desktop)]
|
||||
#[cfg(not(target_os = "android"))]
|
||||
mod desktop;
|
||||
#[cfg(mobile)]
|
||||
#[cfg(target_os = "android")]
|
||||
mod mobile;
|
||||
|
||||
mod commands;
|
||||
@@ -20,11 +20,13 @@ mod models;
|
||||
|
||||
pub use error::{Error, Result};
|
||||
|
||||
#[cfg(desktop)]
|
||||
#[cfg(not(target_os = "android"))]
|
||||
pub use desktop::SecureStorage;
|
||||
#[cfg(mobile)]
|
||||
#[cfg(target_os = "android")]
|
||||
pub use mobile::SecureStorage;
|
||||
|
||||
// TODO: Consider using a worker thread to handle caveats mentioned by keyring-rs
|
||||
|
||||
/// Extensions to [`tauri::App`], [`tauri::AppHandle`], [`tauri::WebviewWindow`], [`tauri::Webview`] and [`tauri::Window`] to access the secure-storage APIs.
|
||||
pub trait SecureStorageExt<R: Runtime> {
|
||||
fn secure_storage(&self) -> &SecureStorage<R>;
|
||||
@@ -39,11 +41,16 @@ impl<R: Runtime, T: Manager<R>> crate::SecureStorageExt<R> for T {
|
||||
/// Initializes the plugin.
|
||||
pub fn init<R: Runtime>() -> TauriPlugin<R> {
|
||||
Builder::new("secure-storage")
|
||||
.invoke_handler(tauri::generate_handler![commands::execute])
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
commands::set_string,
|
||||
commands::get_string,
|
||||
commands::set_binary,
|
||||
commands::get_binary
|
||||
])
|
||||
.setup(|app, api| {
|
||||
#[cfg(mobile)]
|
||||
#[cfg(target_os = "android")]
|
||||
let secure_storage = mobile::init(app, api)?;
|
||||
#[cfg(desktop)]
|
||||
#[cfg(not(target_os = "android"))]
|
||||
let secure_storage = desktop::init(app, api)?;
|
||||
app.manage(secure_storage);
|
||||
Ok(())
|
||||
|
||||
@@ -13,8 +13,8 @@ use crate::models::*;
|
||||
#[cfg(target_os = "android")]
|
||||
const PLUGIN_IDENTIFIER: &str = "app.tauri.secure_storage";
|
||||
|
||||
#[cfg(target_os = "ios")]
|
||||
tauri::ios_plugin_binding!(init_plugin_secure_storage);
|
||||
//#[cfg(target_os = "ios")]
|
||||
//tauri::ios_plugin_binding!(init_plugin_secure_storage);
|
||||
|
||||
// initializes the Kotlin or Swift plugin classes
|
||||
pub fn init<R: Runtime, C: DeserializeOwned>(
|
||||
@@ -23,8 +23,8 @@ pub fn init<R: Runtime, C: DeserializeOwned>(
|
||||
) -> crate::Result<SecureStorage<R>> {
|
||||
#[cfg(target_os = "android")]
|
||||
let handle = api.register_android_plugin(PLUGIN_IDENTIFIER, "ExamplePlugin")?;
|
||||
#[cfg(target_os = "ios")]
|
||||
let handle = api.register_ios_plugin(init_plugin_secure_storage)?;
|
||||
//#[cfg(target_os = "ios")]
|
||||
//let handle = api.register_ios_plugin(init_plugin_secure_storage)?;
|
||||
Ok(SecureStorage(handle))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user