|
1 |
| -# Tauri Plugin oauth |
| 1 | +# Tauri Plugin OAuth |
2 | 2 |
|
3 |
| -WIP |
| 3 | + |
4 | 4 |
|
5 |
| -Minimalistic rust library and Tauri plugin(soon) to spawn a temporary localhost server which you redirect to from browser based oauth flows ("Login with X"). |
6 |
| -Needed because many sites such as Google and GitHub don't allow custom URI schemes ("deep link") as redirect URLs. |
| 5 | +A minimalistic Rust library and Tauri plugin for handling browser-based OAuth flows in desktop |
| 6 | +applications. This plugin spawns a temporary localhost server to capture OAuth redirects, solving |
| 7 | +the challenge of using OAuth with desktop apps. |
7 | 8 |
|
8 |
| -See https://github.com/FabianLars/tauri-plugin-deep-link for an alternative based on deep linking. This one will automatically start your app if there is no open instance. |
| 9 | +## Why This Plugin? |
| 10 | + |
| 11 | +Many OAuth providers (like Google and GitHub) don't allow custom URI schemes ("deep links") as |
| 12 | +redirect URLs. This plugin provides a solution by: |
| 13 | + |
| 14 | +1. Spawning a temporary local server |
| 15 | +2. Capturing the OAuth redirect |
| 16 | +3. Passing the authorization data back to your app |
| 17 | + |
| 18 | +> **Note**: For an alternative approach using deep linking, |
| 19 | +> see [tauri-plugin-deep-link](https://github.com/tauri-apps/plugins-workspace/tree/v2/plugins/deep-link). The deep-link |
| 20 | +> plugin can automatically start your app if there's no open instance. |
| 21 | +
|
| 22 | +## Installation |
| 23 | + |
| 24 | +```toml |
| 25 | +# Cargo.toml |
| 26 | +[dependencies] |
| 27 | +tauri-plugin-oauth = "2.0.0-rc.0" |
| 28 | +``` |
| 29 | + |
| 30 | +For Tauri projects using npm or yarn: |
| 31 | + |
| 32 | +```bash |
| 33 | +npm install @fabianlars/ [email protected] |
| 34 | +# or |
| 35 | +yarn add @fabianlars/ [email protected] |
| 36 | +``` |
| 37 | + |
| 38 | +## Usage |
| 39 | + |
| 40 | +### Rust |
| 41 | + |
| 42 | +```rust |
| 43 | +use tauri::{command, Emitter, Window}; |
| 44 | +use tauri_plugin_oauth::start; |
| 45 | + |
| 46 | +#[command] |
| 47 | +async fn start_server(window: Window) -> Result<u16, String> { |
| 48 | + start(move |url| { |
| 49 | + // Because of the unprotected localhost port, you must verify the URL here. |
| 50 | + // Preferebly send back only the token, or nothing at all if you can handle everything else in Rust. |
| 51 | + let _ = window.emit("redirect_uri", url); |
| 52 | + }) |
| 53 | + .map_err(|err| err.to_string()) |
| 54 | +} |
| 55 | + |
| 56 | +#[cfg_attr(mobile, tauri::mobile_entry_point)] |
| 57 | +pub fn run() { |
| 58 | + tauri::Builder::default() |
| 59 | + |
| 60 | + .plugin(tauri_plugin_oauth::init()) |
| 61 | + .invoke_handler(tauri::generate_handler![start_server]) |
| 62 | + .run(tauri::generate_context!()) |
| 63 | + .expect("error while running tauri application"); |
| 64 | +} |
| 65 | + |
| 66 | +``` |
| 67 | + |
| 68 | +### TypeScript |
| 69 | + |
| 70 | +```typescript |
| 71 | +import { start, cancel, onUrl, onInvalidUrl } from '@fabianlars/tauri-plugin-oauth'; |
| 72 | + |
| 73 | +async function startOAuthFlow() { |
| 74 | + try { |
| 75 | + const port = await start(); |
| 76 | + console.log(`OAuth server started on port ${port}`); |
| 77 | + |
| 78 | + // Set up listeners for OAuth results |
| 79 | + await onUrl((url) => { |
| 80 | + console.log('Received OAuth URL:', url); |
| 81 | + // Handle the OAuth redirect |
| 82 | + }); |
| 83 | + |
| 84 | + // Initiate your OAuth flow here |
| 85 | + // ... |
| 86 | + |
| 87 | + } catch (error) { |
| 88 | + console.error('Error starting OAuth server:', error); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +// Don't forget to stop the server when you're done |
| 93 | +async function stopOAuthServer() { |
| 94 | + try { |
| 95 | + await cancel(port); |
| 96 | + console.log('OAuth server stopped'); |
| 97 | + } catch (error) { |
| 98 | + console.error('Error stopping OAuth server:', error); |
| 99 | + } |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +## Configuration |
| 104 | + |
| 105 | +You can configure the plugin behavior using the `OauthConfig` struct: |
| 106 | + |
| 107 | +```rust |
| 108 | +use tauri_plugin_oauth::OauthConfig; |
| 109 | + |
| 110 | +let config = OauthConfig { |
| 111 | + ports: Some(vec![8000, 8001, 8002]), |
| 112 | + response: Some("OAuth process completed. You can close this window.".into()), |
| 113 | +}; |
| 114 | + |
| 115 | +start_with_config(config, |url| { |
| 116 | + // Handle OAuth URL |
| 117 | +}) |
| 118 | +.await |
| 119 | +.expect("Failed to start OAuth server"); |
| 120 | +``` |
| 121 | + |
| 122 | +## Security Considerations |
| 123 | + |
| 124 | +- Always validate the received OAuth URL on your server-side before considering it authentic. |
| 125 | +- Use HTTPS for your OAuth flow to prevent man-in-the-middle attacks. |
| 126 | +- Implement proper token storage and refresh mechanisms in your application. |
| 127 | + |
| 128 | +## Contributing |
| 129 | + |
| 130 | +Contributions are always welcome! Please feel free to submit a Pull Request. |
| 131 | + |
| 132 | +## License |
| 133 | + |
| 134 | +This project is dual-licensed under either of the following licenses, at your option: |
| 135 | + |
| 136 | +- [Apache License, Version 2.0](LICENSE_APACHE-2.0) |
| 137 | +- [MIT License](LICENSE_MIT) |
0 commit comments