@fabianlars/tauri-plugin-oauth

中文文档
2.1.0发布于 2026/07/07MIT OR Apache-2.0TypeScriptESM + CJS近 30 天下载 9.1万

用于产入本地主机服务器的 Tauri 插件 。 需要一些欧斯流 (Login with X).

中文使用文档
文档目录10 节
  1. 为什么是这个插件?
  2. 安装
  3. 使用量
  4. 锈
  5. 类型脚本
  6. 配置
  7. 回调后重定向浏览器
  8. 安全考虑
  9. 捐款
  10. 许可证

Tauri 插件 OAuth 设备

在桌面上处理基于浏览器的 OAuth 流的最小化 Rust 库和 Tauri 插件 应用。 此插件将生成一个临时本地主机服务器, 以捕获 OAuth 重定向, 解析 使用桌面应用程序 OAuth 的挑战.

为什么是这个插件?

许多OAuth供应商(如Google和GitHub)不允许自定义的URI计划("深链接")作为 重定向 URL 。 此插件提供了一个解决方案 :

  1. 复制临时本地服务器
  2. 抓取 OAuth 重定向
  3. 将授权数据传送回您的应用程序

说明: 关于采用深层联系的替代办法, 见 tauri-plugin-深链路. 深层联系 如果没有打开实例, 插件可以自动启动您的应用程序 .

安装

# Cargo.toml
[dependencies]
tauri-plugin-oauth = "2"

对于使用 npm 或 纱线的 Tauri 项目 :

npm install @fabianlars/tauri-plugin-oauth@2
# or
yarn add @fabianlars/tauri-plugin-oauth@2

使用量

锈

use tauri::{command, Emitter, Window};
use tauri_plugin_oauth::start;

#[command]
async fn start_server(window: Window) -> Result<u16, String> {
    start(move |url| {
        // Because of the unprotected localhost port, you must verify the URL here.
        // Preferebly send back only the token, or nothing at all if you can handle everything else in Rust.
        let _ = window.emit("redirect_uri", url);
    })
        .map_err(|err| err.to_string())
}

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
    tauri::Builder::default()

        .plugin(tauri_plugin_oauth::init())
        .invoke_handler(tauri::generate_handler![start_server])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

类型脚本

import {
  start,
  cancel,
  onUrl,
  onInvalidUrl,
} from "@fabianlars/tauri-plugin-oauth";

async function startOAuthFlow() {
  try {
    const port = await start();
    console.log(`OAuth server started on port ${port}`);

    // Set up listeners for OAuth results
    await onUrl((url) => {
      console.log("Received OAuth URL:", url);
      // Handle the OAuth redirect
    });

    // Initiate your OAuth flow here
    // ...
  } catch (error) {
    console.error("Error starting OAuth server:", error);
  }
}

// Don't forget to stop the server when you're done
async function stopOAuthServer() {
  try {
    await cancel(port);
    console.log("OAuth server stopped");
  } catch (error) {
    console.error("Error stopping OAuth server:", error);
  }
}

配置

您可以使用 OauthConfig 结构 :

use tauri_plugin_oauth::OauthConfig;

let config = OauthConfig {
    ports: Some(vec![8000, 8001, 8002]),
    response: Some("OAuth process completed. You can close this window.".into()),
    ..Default::default()
};

start_with_config(config, |url| {
    // Handle OAuth URL
})
.expect("Failed to start OAuth server");

回调后重定向浏览器

如果你设定 redirect_uri 字段中,插件将以一个 302 Found 用于该 URL 而不是服务 HTML 响应。 这是有用的,当你想要 浏览器, 一旦供应商被击中, 即可登陆您的应用程序的真实页面( 例如 Tauri 窗口 URL) 本地主机服务器.

let config = OauthConfig {
    redirect_uri: Some("http://tauri.localhost/oauth/done".into()),
    ..Default::default()
};

说明: 何时 redirect_uri 设置, 处理器关闭接收本地主机 URL 供应商击出( 例如 ). http://127.0.0.1:<port>/?code=...&state=...(b) —— 没有 完整 URL 带有 碎片(#...),只有浏览器看到。 如果您的 OAuth 流量返回一个符号 URL 片段( 暗流), 剖析 window.location 并转发 返回您的应用程序; 默认值( 无) redirect_uri) 行为使用内线 脚本已经为你做了这个.

安全考虑

  • 总是在您服务器侧验证收到的 OAuth URL , 然后再认为它是真实的 .
  • 在 OAuth 流中使用 HTTPS 来防止中层人的攻击 .
  • 在您的应用程序中执行适当的代号存储并刷新机制 .

捐款

总是欢迎捐款! 请随意提交拉力请求.

许可证

这个项目根据下列任何一种许可证持有双重许可证,可自行选择:

版本与发布记录

@fabianlars/tauri-plugin-oauth 共发布 2 个版本,首次发布于 2024/11/05;过去 12 个月发布了 1 个版本。

版本发布日期距今
2.1.0latest2026/07/074 个月前
2.0.02024/11/052 年前

关于 @fabianlars/tauri-plugin-oauth 的常见问题

@fabianlars/tauri-plugin-oauth 是什么?

用于产入本地主机服务器的 Tauri 插件 。 需要一些欧斯流 (Login with X).

如何安装 @fabianlars/tauri-plugin-oauth?

在项目目录运行 npm install @fabianlars/tauri-plugin-oauth,或使用 pnpm add / yarn add / bun add。

@fabianlars/tauri-plugin-oauth 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@fabianlars/tauri-plugin-oauth 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@fabianlars/tauri-plugin-oauth 可以商用吗?

@fabianlars/tauri-plugin-oauth 使用 MIT OR Apache-2.0 许可。可在 MIT、Apache-2.0 中任选其一遵守。选择 MIT 时,宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@fabianlars/tauri-plugin-oauth 的下载量有多少?

根据 npm 官方下载统计,@fabianlars/tauri-plugin-oauth 在 2026/10/14 – 2026/11/12 这 30 天内被下载 91,019 次,日均约 3,034 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@fabianlars/tauri-plugin-oauth 还在维护吗?

最新版本 2.1.0 发布于 2026/07/07(4 个月前);至今共发布 2 个版本,其中过去 12 个月发布了 1 个;首次发布于 2024/11/05。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。