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