# 驗證

> 驗證 — Turso 文件。
> Canonical HTML: https://tursodb.cn/zh-tw/docs/api-reference/authentication/
> Upstream Source (MIT): https://docs.turso.tech/api-reference/authentication
> Language: zh-TW

---
Turso API 使用 API 權杖來驗證請求。你可以使用 [Turso CLI](https://docs.turso.tech/cli) 與[驗證 API](/zh-tw/docs/api-reference/tokens/create/) 來建立與撤銷 API 權杖。

權杖可以在三個限制層級上簽發：

| 限制層級 | 可執行的操作 | 簽發方式 |
| - | - | - |
| 組織範圍 | 僅限於單一組織。可選擇性地限制於一組[權限範圍](#organization-scoped-tokens-with-scopes)。 | `turso auth api-tokens mint my-token --org my-org` |
| 群組範圍 | 固定於組織內的單一群組，並具有所選的一組權限。 | `turso auth api-tokens mint my-token --org my-org --group default --read-only` |
| 無限制 *（已棄用）* | 可作用於呼叫者所屬的每一個組織。 | `turso auth api-tokens mint my-token` |

<div class="doc-callout doc-callout-warning"><div class="doc-callout-title">警告</div><div class="doc-callout-body">

**無限制（跨組織）權杖已棄用，並將於未來的版本中移除。** 新權杖應一律使用 `--org` 限定於單一組織。現有的無限制權杖目前仍可運作，但你應儘早將其輪替為具範圍限制的權杖。

</div></div>


```bash
# Token scoped to a single organization
turso auth api-tokens mint my-token --org my-org

# Group-scoped token, read-only (tighter scope, for automations that only need one group)
turso auth api-tokens mint my-token --org my-org --group default --read-only

# Token with access to all organizations — deprecated
turso auth api-tokens mint my-token
```

* 使用 `--org` 將權杖限定於單一組織，尤其是在 CI/CD 與生產環境中。
* 使用 API 權杖時，請使用環境變數。
* 切勿在公開場合分享你的 API 權杖，包括儲存庫與 CI/CD Actions。

Turso 使用 Bearer 驗證，並要求在所有受保護的請求中透過 `Authorization` 標頭傳遞你的 API 權杖：

```bash
Authorization: Bearer TOKEN
```

## 具權限範圍的組織範圍權杖

未具權限範圍的組織範圍權杖，可以執行簽發它的使用者在該組織內所能執行的所有操作。若要加以限制，請在簽發時指定權限範圍。如此一來，該權杖便只能在該組織的每個群組中執行所列出的操作。

```bash
# Read the audit logs, members, and billing data of the organization
turso auth api-tokens mint audit-reader --org my-org --scope org:read

# Create databases and mint SQL tokens in every group
turso auth api-tokens mint deploy-bot --org my-org --scope db:create --scope db:mint-token
```

組織範圍權杖可以擁有所有的[群組權限範圍](#scope-vocabulary)。它也可以擁有下列組織權限範圍，而群組範圍權杖則無法擁有：

| 權限範圍 | 允許的操作 |
| - | - |
| `group:create` | 在組織中建立群組。 |
| `group:delete` | 刪除組織中的群組。 |
| `org:read` | 讀取組織資料：成員、邀請、帳務、發票、稽核日誌、SSO、JWKS 與 API 權杖。 |

讀取邀請與稽核日誌另外要求簽發該權杖的使用者必須是組織的管理員或擁有者。

具權限範圍的權杖無法執行沒有對應權限範圍的操作。這些操作包括管理成員與邀請、變更組織設定、轉移群組或資料庫，以及簽發 API 權杖。這些操作請使用未具權限範圍的權杖。

`full-access` 預設集不包含組織權限範圍。若要授予這些權限，請以 `--scope` 逐一列出每個權限範圍。

## 群組範圍權杖

群組範圍權杖是一種 Platform API 憑證，固定於組織內的單一群組，並具有明確的允許操作清單。對於應該能在單一群組內佈建與管理資料庫、而無法觸及組織其餘部分的自動化而言，這正是合適的形式。

簽發群組範圍權杖的呼叫者必須是組織的管理員或擁有者。

<div class="doc-callout doc-callout-info"><div class="doc-callout-title">備註</div><div class="doc-callout-body">

群組範圍權杖是控制平面的憑證——它們授權對 Turso Platform API 的請求。它們與你的應用程式用來查詢資料庫的 [SQL 引擎權杖](/zh-tw/docs/sdk/authorization/tokens/) 彼此獨立。兩套系統不共用金鑰材料或詞彙。

</div></div>


### 權限範圍詞彙

平台會在伺服器端將 `read-only` 與 `full-access` 預設集展開為對應的個別權限範圍，因此權杖建立後你所看到的值一律是個別權限範圍。

| 權限範圍 | 允許的操作 |
| - | - |
| `read` | 所有 GET 形式的路由：列出/取得資料庫與群組、設定、用量、執行個體、位置。 |
| `db:create` | 在群組內建立資料庫（`POST /databases`）、從傾印檔 seeded、還原。 |
| `db:delete` | 刪除群組內的資料庫。 |
| `db:configure` | 修補資料庫設定、喚醒、新增或移除執行個體。 |
| `db:mint-token` | 為群組內的資料庫簽發 SQL 引擎權杖或 TLS 用戶端憑證。 |
| `db:rotate-creds` | 輪替資料庫簽章金鑰，使先前為其簽發的所有 SQL 權杖失效。 |
| `group:configure` | 設定、重新命名、更新、取消封存群組，或新增與移除位置。 |
| `group:mint-token` | 簽發群組層級的 SQL 引擎權杖。 |
| `group:rotate-creds` | 輪替群組簽章金鑰，使群組權杖失效。在 AWS 上，資料庫範圍的權杖不會失效。 |

<div class="doc-callout doc-callout-warning"><div class="doc-callout-title">警告</div><div class="doc-callout-body">

`db:mint-token` 與 `db:rotate-creds` 是刻意區分開來的權限範圍。簽發新的 SQL 憑證是累加性的；輪替則會使目前使用中的所有憑證失效，可能導致執行中的應用程式中斷。僅將輪替權限授予真正需要它的自動化。

</div></div>


### 預設集

| 預設集 | 展開為 |
| - | - |
| `read-only` | `read` |
| `full-access` | 上方列出的每一個個別權限範圍。 |

群組建立、群組刪除、群組轉移與 AWS 遷移在任何權限範圍下都刻意無法透過群組範圍權杖執行——這些操作僅限於組織層級的憑證。

### 生命週期

* 群組範圍權杖是以群組的 UUID 固定，而非其名稱。若某個群組被刪除，並以相同名稱建立新的群組，舊權杖不會移轉——它們會隨該群組一併被撤銷。
* 刪除群組會串聯撤銷所有以該群組為範圍的權杖。
* 將群組轉移至不同的組織會串聯撤銷所有以該群組為範圍的權杖。

### 管理組織權杖

有兩個端點讓管理員能在單一位置管理所有以組織為範圍的權杖——它們正是儀表板組織層級權杖表格的後端：

* [`GET /v1/organizations/{organizationSlug}/api-tokens`](/zh-tw/docs/api-reference/tokens/list-organization/) — 列出所有組織範圍與群組範圍的權杖，並附上簽發使用者。
* [`DELETE /v1/organizations/{organizationSlug}/api-tokens/{tokenId}`](/zh-tw/docs/api-reference/tokens/revoke-organization/) — 依 ID 撤銷。

成員與檢視者可以呼叫相同的端點，但只能看見並撤銷他們自己簽發的權杖。

## 基礎 URL

Turso API 位於下列 URL：

```bash
https://api.turso.tech
```
