Skip to content

Commit 9b5d78b

Browse files
cuervoluFabianLars
andauthored
feat: Add TypeScript Bindings and Improve Documentation (#23)
Co-authored-by: Fabian-Lars <[email protected]> Co-authored-by: Fabian-Lars <[email protected]>
1 parent 3537dd0 commit 9b5d78b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+6247
-102
lines changed

.gitignore

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,18 @@
11
/target
22
/Cargo.lock
3+
4+
/.vs
5+
.DS_Store
6+
.Thumbs.db
7+
*.sublime*
8+
.idea/
9+
debug.log
10+
package-lock.json
11+
.vscode/settings.json
12+
yarn.lock
13+
14+
/.tauri
15+
node_modules/
16+
17+
dist-js
18+
dist

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ repository = "https://github.com/FabianLars/tauri-plugin-oauth"
1212
links = "tauri-plugin-oauth"
1313

1414
[dependencies]
15-
tauri = { version = "2.0.0-rc.0" }
15+
tauri = { version = "2.0.0-rc.10" }
1616
httparse = "1"
1717
log = "0.4"
1818
serde = "1"
1919
url = "2"
2020
thiserror = "1.0"
2121

2222
[build-dependencies]
23-
tauri-plugin = { version = "2.0.0-rc.0", features = ["build"] }
23+
tauri-plugin = { version = "2.0.0-rc.9", features = ["build"] }

README.md

Lines changed: 134 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,137 @@
1-
# Tauri Plugin oauth
1+
# Tauri Plugin OAuth
22

3-
WIP
3+
![WIP Badge](https://img.shields.io/badge/status-WIP-yellow)
44

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.
78

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)

examples/vanilla/.gitignore

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,24 @@
1-
node_modules/
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
dist
12+
dist-ssr
13+
*.local
14+
15+
# Editor directories and files
16+
.vscode/*
17+
!.vscode/extensions.json
18+
.idea
19+
.DS_Store
20+
*.suo
21+
*.ntvs*
22+
*.njsproj
23+
*.sln
24+
*.sw?
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"recommendations": ["tauri-apps.tauri-vscode", "rust-lang.rust-analyzer"]
3+
}

examples/vanilla/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Tauri + Vanilla TS
2+
3+
This template should help get you started developing with Tauri in vanilla HTML, CSS and Typescript.
4+
5+
## Recommended IDE Setup
6+
7+
- [VS Code](https://code.visualstudio.com/) + [Tauri](https://marketplace.visualstudio.com/items?itemName=tauri-apps.tauri-vscode) + [rust-analyzer](https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer)

examples/vanilla/index.html

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8"/>
5+
<link rel="stylesheet" href="/src/styles.css"/>
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
7+
<title>Tauri OAuth Test</title>
8+
<script type="module" src="/src/main.ts" defer></script>
9+
</head>
10+
<body>
11+
<div class="container">
12+
<h1>Welcome to Tauri OAuth Test!</h1>
13+
<div class="row">
14+
<a href="https://vitejs.dev" target="_blank">
15+
<img src="/src/assets/vite.svg" class="logo vite" alt="Vite logo"/>
16+
</a>
17+
<a href="https://tauri.app" target="_blank">
18+
<img
19+
src="/src/assets/tauri.svg"
20+
class="logo tauri"
21+
alt="Tauri logo"
22+
/>
23+
</a>
24+
<a href="https://www.typescriptlang.org/docs" target="_blank">
25+
<img
26+
src="/src/assets/typescript.svg"
27+
class="logo typescript"
28+
alt="typescript logo"
29+
/>
30+
</a>
31+
</div>
32+
<p>Test the OAuth plugin using Rust or TypeScript</p>
33+
<div class="row">
34+
<button id="start-rust">Start OAuth Server (Rust)</button>
35+
<button id="start-ts">Start OAuth Server (TypeScript)</button>
36+
</div>
37+
<div id="result" class="result-box"></div>
38+
</div>
39+
</body>
40+
</html>

examples/vanilla/package.json

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
{
2-
"name": "app",
3-
"version": "1.0.0",
4-
"description": "",
5-
"main": "index.js",
2+
"name": "vanilla",
3+
"private": true,
4+
"version": "0.1.0",
5+
"type": "module",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"dev": "vite",
8+
"build": "tsc && vite build",
9+
"preview": "vite preview",
10+
"tauri": "tauri"
811
},
9-
"author": "",
10-
"license": "MIT",
1112
"dependencies": {
12-
"@tauri-apps/cli": "^1.5"
13+
"@tauri-apps/api": ">=2.0.0-rc.0",
14+
"@tauri-apps/plugin-shell": ">=2.0.0-rc.0",
15+
"@fabianlars/tauri-plugin-oauth": "file:../../"
16+
},
17+
"devDependencies": {
18+
"@tauri-apps/cli": ">=2.0.0-rc.0",
19+
"vite": "^5.3.1",
20+
"typescript": "^5.2.2"
1321
}
1422
}

0 commit comments

Comments
 (0)