---
title: "@xid-kit/tauri"
description: "Tauri v2 桌面 SDK，包含 PKCE S256 流程、deeplink callback 处理器、操作系统 keychain 适配器和 Rust 插件模板。"
locale: "zh-Hans"
---

> Documentation Index
> Fetch the locale documentation index at: https://xid.dev/zh-hans/llms.txt
> Use this file to discover all available pages before exploring further.

# @xid-kit/tauri

## 状态

包状态为 **当前包**。JS bridge、PKCE S256 流程、deeplink callback 处理器、操作系统 keychain 适配器和 Rust 插件模板已实现。在真实生产基础设施上的 IdP 往返验证仍待人工核实。

## Tauri 配置

```json
// tauri.conf.json
{
  "bundle": { "identifier": "com.example.myapp" },
  "plugins": {
"deep-link": { "desktop": { "schemes": ["myapp"] } }
  }
}
```

## Rust 插件

将 `templates/xid-keychain-plugin.rs` 复制到 `src-tauri/src/xid_keychain.rs`，并按照 `templates/tauri-app-setup.rs` 进行注册。将 `keyring = "2"`、`tauri-plugin-deep-link = "2"` 和 `tauri-plugin-shell = "2"` 添加到 `src-tauri/Cargo.toml`。

## JS 集成

```ts
import { createXidTauriClient, createTauriKeychainAdapter } from '@xid-kit/tauri'
import { invoke } from '@tauri-apps/api/core'
import { open } from '@tauri-apps/plugin-shell'
import { onOpenUrl } from '@tauri-apps/plugin-deep-link'

const client = createXidTauriClient({
  issuer: 'https://xid.dev',
  clientId: 'YOUR_CLIENT_ID',
  redirectUri: 'myapp://auth/callback',
  keychain: createTauriKeychainAdapter({ invoke }),
})

// Register deeplink handler (e.g. on App component mount)
await onOpenUrl(async (urls) => {
  for (const url of urls) await client.handleRedirect(url)
})

// Trigger sign-in: opens system browser
await client.signIn({ openUrl: open })
```

## Token 获取与登出

```ts
// Get current access token (refreshes transparently if near expiry)
const token = await client.getAccessToken()

// Get full session (userId, organizationId, expiresAt)
const session = await client.getSession()

// Revoke server session and clear all keychain entries
await client.signOut()

// OIDC RP-initiated logout URL for full IdP sign-out
const logoutUrl = client.buildSignOutUrl({ postLogoutRedirectUri: 'myapp://logout' })
await open(logoutUrl.toString())
```

## 不依赖 Tauri 运行时的开发/测试

```ts
import { createXidTauriClient, createMemoryKeychainAdapter } from '@xid-kit/tauri'

const client = createXidTauriClient({
  issuer: 'http://localhost:8788',
  clientId: 'test-client',
  redirectUri: 'http://localhost:1420/callback',
  keychain: createMemoryKeychainAdapter(),
})
```

## createXidTauriClient 选项

| 选项 | 类型 | 描述 |
| --- | --- | --- |
| `issuer` | string | XID 签发方 URL |
| `clientId` | string | OAuth 2.0 client\_id |
| `redirectUri` | string | 自定义 URI scheme callback |
| `scopes` | readonly string\[\] | 默认：openid, profile, email |
| `keychain` | XidKeychainAdapter | Token 存储适配器；默认为 MemoryKeychainAdapter（生产环境请使用 Tauri 适配器） |

## XidTauriClient 方法

| 方式 | 描述 |
| --- | --- |
| `signIn(options?)` | 构建 PKCE 授权 URL；通过 openUrl callback 打开 |
| `handleRedirect(url)` | 解析 deep link，校验 state，交换 code 换取 token |
| `getSession()` | TauriSession 或 null；接近过期时刷新 token |
| `getAccessToken(options?)` | 访问令牌字符串或 null；接近过期时自动刷新 |
| `signOut()` | 吊销服务端会话并清除 keychain |
| `buildSignOutUrl(options?)` | 构建用于 RP 发起登出的 OIDC end\_session URL |
| `setTokenStorage(adapter)` | 在运行时替换 keychain 适配器 |

## PKCE 和 token 存储

- 始终使用 PKCE S256。不生成 plain challenge。
- verifier 熵为 64 字节；challenge 通过 Web Crypto `crypto.subtle.digest('SHA-256', ...)` 派生。
- 所有密钥的命名空间均在 `xid.*` 下：`xid.access_token`、`xid.refresh_token`、`xid.session`、`xid.pkce_verifier`、`xid.oauth_state`。

Source: https://xid.dev/zh-hans/sdks/tauri/index.mdx
