mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-07-10 16:27:05 +02:00
add android-keyring
This commit is contained in:
@@ -1,37 +0,0 @@
|
||||
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
|
||||
// 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::Result;
|
||||
|
||||
pub fn init<R: Runtime, C: DeserializeOwned>(
|
||||
app: &AppHandle<R>,
|
||||
_api: PluginApi<R, C>,
|
||||
) -> crate::Result<SecureStorage<R>> {
|
||||
Ok(SecureStorage(app.clone()))
|
||||
}
|
||||
|
||||
/// Access to the secure-storage APIs.
|
||||
pub struct SecureStorage<R: Runtime>(AppHandle<R>);
|
||||
|
||||
impl<R: Runtime> SecureStorage<R> {
|
||||
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,12 +8,8 @@ pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum Error {
|
||||
#[cfg(not(target_os = "android"))]
|
||||
#[error(transparent)]
|
||||
Keyring(#[from] keyring::Error),
|
||||
#[cfg(target_os = "android")]
|
||||
#[error(transparent)]
|
||||
PluginInvoke(#[from] tauri::plugin::mobile::PluginInvokeError),
|
||||
}
|
||||
|
||||
impl Serialize for Error {
|
||||
|
||||
@@ -2,29 +2,17 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
use keyring::Entry;
|
||||
use tauri::{
|
||||
plugin::{Builder, TauriPlugin},
|
||||
Manager, Runtime,
|
||||
AppHandle, Manager, Runtime,
|
||||
};
|
||||
|
||||
pub use models::*;
|
||||
|
||||
#[cfg(not(target_os = "android"))]
|
||||
mod desktop;
|
||||
#[cfg(target_os = "android")]
|
||||
mod mobile;
|
||||
|
||||
mod commands;
|
||||
mod error;
|
||||
mod models;
|
||||
|
||||
pub use error::{Error, Result};
|
||||
|
||||
#[cfg(not(target_os = "android"))]
|
||||
pub use desktop::SecureStorage;
|
||||
#[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.
|
||||
@@ -47,13 +35,33 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
|
||||
commands::set_binary,
|
||||
commands::get_binary
|
||||
])
|
||||
.setup(|app, api| {
|
||||
.setup(|app, _api| {
|
||||
#[cfg(target_os = "android")]
|
||||
let secure_storage = mobile::init(app, api)?;
|
||||
#[cfg(not(target_os = "android"))]
|
||||
let secure_storage = desktop::init(app, api)?;
|
||||
app.manage(secure_storage);
|
||||
android_keyring::set_android_keyring_credential_builder()?;
|
||||
|
||||
app.manage(SecureStorage(app.clone()));
|
||||
Ok(())
|
||||
})
|
||||
.build()
|
||||
}
|
||||
|
||||
/// Access to the secure-storage APIs.
|
||||
pub struct SecureStorage<R: Runtime>(AppHandle<R>);
|
||||
|
||||
impl<R: Runtime> SecureStorage<R> {
|
||||
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()?)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
use serde::de::DeserializeOwned;
|
||||
use tauri::{
|
||||
plugin::{PluginApi, PluginHandle},
|
||||
AppHandle, Runtime,
|
||||
};
|
||||
|
||||
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);
|
||||
|
||||
// initializes the Kotlin or Swift plugin classes
|
||||
pub fn init<R: Runtime, C: DeserializeOwned>(
|
||||
_app: &AppHandle<R>,
|
||||
api: PluginApi<R, C>,
|
||||
) -> 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)?;
|
||||
Ok(SecureStorage(handle))
|
||||
}
|
||||
|
||||
/// Access to the secure_storage APIs.
|
||||
pub struct SecureStorage<R: Runtime>(PluginHandle<R>);
|
||||
|
||||
impl<R: Runtime> SecureStorage<R> {
|
||||
pub fn ping(&self, payload: PingRequest) -> crate::Result<PingResponse> {
|
||||
self.0
|
||||
.run_mobile_plugin("ping", payload)
|
||||
.map_err(Into::into)
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct PingRequest {
|
||||
pub value: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct PingResponse {
|
||||
pub value: Option<String>,
|
||||
}
|
||||
Reference in New Issue
Block a user