> ## Documentation Index
> Fetch the complete documentation index at: https://ayakaleaf-pro.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# OAuth Applications 集合

`oauthApplications` 儲存 web 所知道的 OAuth 用戶端。一份文件對應一個用戶端。在 Ayakaleaf Pro 中，唯一寫入它的是 web 模組 `git-bridge`：web 啟動時，它會建立一個名為 `Overleaf Git Bridge` 的用戶端。Schema 位於 `services/web/app/src/models/OauthApplication.mjs`。

#### 欄位

| 欄位 | 型別 | 含義 | 來源 |
| - | - | - | - |
| `_id` | ObjectId | 用戶端文件 id。`oauthAccessTokens.oauthApplication_id` 與 `oauthAuthorizationCodes.oauthApplication_id` 指向它。 | `OauthApplication.mjs:5` |
| `id` | String | 用戶端 id：64 個隨機小寫字母與數字。 | `OauthApplication.mjs:7`, `services/web/modules/git-bridge/index.mjs:25` |
| `clientSecret` | String | 用戶端密鑰，寫入格式為 `v1.` 加 32 個隨機字元。只有 `getClient` 會讀取它，而沒有任何程式碼呼叫 `getClient`（見備註）。 | `OauthApplication.mjs:8`, `git-bridge/index.mjs:26` |
| `grants` | Array of String | 授權類型。git-bridge 用戶端為 `['password']`。驗證權杖時隨用戶端一併回傳（`services/web/modules/oauth2-server/app/src/OAuth2Model.mjs:67-72`）。 | `OauthApplication.mjs:9`, `git-bridge/index.mjs:27` |
| `name` | String | 用戶端名稱。git-bridge 模組與個人存取權杖程式碼透過名稱 `Overleaf Git Bridge` 尋找該用戶端。 | `OauthApplication.mjs:10`, `git-bridge/index.mjs:15-16,28` |
| `redirectUris` | Array of String | 允許的重新導向 URI。git-bridge 用戶端為 `[]`。 | `OauthApplication.mjs:11`, `git-bridge/index.mjs:29` |
| `scopes` | Array of String | 用戶端可使用的 scope。git-bridge 用戶端為 `['git_bridge']`。 | `OauthApplication.mjs:12`, `git-bridge/index.mjs:30` |
| `pkceEnabled` | Boolean | 僅存在於 schema 中：本儲存庫沒有程式碼寫入它，只有未被使用的 `getClient` 會讀取它（`OAuth2Model.mjs:207`）。 | `OauthApplication.mjs:13` |

#### 寫入方

| 程式碼 | 作用 |
| - | - |
| `git-bridge/index.mjs:9-35` (module `git-bridge`) | 當 `GIT_BRIDGE_ENABLED` 為 `true` 時，若尚無同名用戶端，則在 web 啟動時建立 `Overleaf Git Bridge` 用戶端。 |

#### 讀取方

| 程式碼 | 作用 |
| - | - |
| `services/web/modules/oauth2-server/app/src/OAuthPersonalAccessTokenManager.mjs:53-61` (module `oauth2-server`) | 依名稱尋找 `Overleaf Git Bridge` 用戶端，並將其 `_id` 存入每個新的個人存取權杖。沒有該用戶端就無法建立權杖。 |
| `OAuth2Model.mjs:31-39,67-72` (`getAccessToken`) | 透過 `oauthApplication_id` 載入權杖對應的用戶端，並回傳其 `id` 與 `grants`。 |
| `OAuth2Model.mjs:185-209,241,275` (`getClient`, `saveToken`, `saveAuthorizationCode`) | 依 `id` 查詢用戶端。在本儲存庫中從不會執行，見備註。 |

#### 索引

只有預設的 `_id` 索引。唯一索引 `id_1` 僅來自標記為 `saas`（`20190912145016_create_oauthApplications_indexes.mjs`）與 `server-pro`（`20230502180757_server_pro_oauth_indexes.mjs`）的遷移，而 Community Edition 不會執行這些遷移。

#### 備註

* Web 只用 `@node-oauth/oauth2-server` 驗證 bearer 權杖（`services/web/app/src/Features/Authentication/AuthenticationController.mjs:409-450` 中的 `server.authenticate`），這只會呼叫模型的 `getAccessToken` 與 `verifyScope`。沒有路由提供 `/oauth/token` 或 `/oauth/authorize`，因此 `getClient`、`saveToken` 與 `saveAuthorizationCode` 從不會被呼叫。
* 該模組先依名稱尋找用戶端再建立它，而 Community Edition 在 `name` 與 `id` 上都沒有唯一索引，所以沒有任何機制阻止出現第二個同名用戶端。需要該用戶端的程式碼會使用 `findOne` 回傳的那一個。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.