Skip to main content

tauri_plugin_auth_session/
lib.rs

1//! Tauri v2 plugin for in-app OAuth authentication on Apple and Android platforms.
2//!
3//! - **macOS / iOS:** ASWebAuthenticationSession (in-app auth sheet)
4//! - **Android:** Chrome Custom Tabs (in-app browser tab)
5//! - **Windows / Linux:** Returns an error (use a desktop OAuth plugin instead)
6//!
7//! # Usage
8//!
9//! ```rust,no_run
10//! tauri::Builder::default()
11//!     .plugin(tauri_plugin_auth_session::init())
12//!     .run(tauri::generate_context!())
13//!     .expect("error while running tauri application");
14//! ```
15
16use tauri::{
17    plugin::{Builder, TauriPlugin},
18    Runtime,
19};
20
21#[cfg(any(target_os = "macos", target_os = "ios"))]
22mod apple;
23
24#[cfg(target_os = "android")]
25mod android;
26
27#[cfg(target_os = "android")]
28const PLUGIN_IDENTIFIER: &str = "app.tauri.auth_session";
29
30#[cfg(target_os = "android")]
31use tauri::{AppHandle, Manager};
32
33/// Holds the mobile plugin handle for Android IPC.
34#[cfg(target_os = "android")]
35struct MobilePluginHandle<R: Runtime>(tauri::plugin::PluginHandle<R>);
36
37/// Initialize the plugin.
38pub fn init<R: Runtime>() -> TauriPlugin<R> {
39    Builder::new("auth-session")
40        .setup(|app, api| {
41            #[cfg(target_os = "android")]
42            {
43                let handle = api.register_android_plugin(PLUGIN_IDENTIFIER, "AuthSessionPlugin")?;
44                app.manage(MobilePluginHandle(handle));
45            }
46            #[cfg(not(target_os = "android"))]
47            {
48                let _ = (app, api);
49            }
50            Ok(())
51        })
52        .invoke_handler(tauri::generate_handler![start])
53        .build()
54}
55
56// ---------------------------------------------------------------------------
57// Apple implementation (macOS + iOS)
58// ---------------------------------------------------------------------------
59
60#[cfg(any(target_os = "macos", target_os = "ios"))]
61#[tauri::command]
62async fn start(
63    auth_url: String,
64    callback_url_scheme: String,
65    ephemeral: Option<bool>,
66) -> Result<String, String> {
67    apple::start_session(auth_url, callback_url_scheme, ephemeral.unwrap_or(false)).await
68}
69
70// ---------------------------------------------------------------------------
71// Android implementation
72// ---------------------------------------------------------------------------
73
74#[cfg(target_os = "android")]
75#[tauri::command]
76async fn start<R: Runtime>(
77    app: AppHandle<R>,
78    auth_url: String,
79    callback_url_scheme: String,
80    _ephemeral: Option<bool>,
81) -> Result<String, String> {
82    let handle = app.state::<MobilePluginHandle<R>>();
83    android::start_session(&handle.0, auth_url, callback_url_scheme).await
84}
85
86// ---------------------------------------------------------------------------
87// Stub (Windows / Linux)
88// ---------------------------------------------------------------------------
89
90#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "android")))]
91#[tauri::command]
92async fn start(
93    _auth_url: String,
94    _callback_url_scheme: String,
95    _ephemeral: Option<bool>,
96) -> Result<String, String> {
97    Err("In-app auth sessions are only available on Apple and Android platforms".to_string())
98}