> ## 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 Authorization Codes 集合

`oauthAuthorizationCodes` 本用于存放 OAuth 授权码流程中的授权码：每个授权码一个文档，客户端用它换取访问令牌。Schema 位于 `services/web/app/src/models/OauthAuthorizationCode.mjs`，会写入它的代码在 web 模块 `oauth2-server` 中。在 Ayakaleaf Pro 中没有任何东西会执行到这段代码，因此该集合始终为空（见说明）。

#### 字段

| 字段 | 类型 | 含义 | 来源 |
| - | - | - | - |
| `_id` | ObjectId | 文档 id。 | `OauthAuthorizationCode.mjs:6` |
| `authorizationCode` | String | 授权码本身，按原样存储（不做哈希）。 | `OauthAuthorizationCode.mjs:8`, `services/web/modules/oauth2-server/app/src/OAuth2Model.mjs:280` |
| `expiresAt` | Date | 有效期截止时间。过期后 `getAuthorizationCode` 会拒绝该授权码，TTL 索引会将其删除。 | `OauthAuthorizationCode.mjs:9`, `OAuth2Model.mjs:150-153,281` |
| `oauthApplication_id` | ObjectId | 请求该授权码的 [OAuth Applications](https://ayakaleaf-pro.mintlify.site/zh-CN/dev/data-model/oauth-applications-collection.md) 客户端。 | `OauthAuthorizationCode.mjs:10`, `OAuth2Model.mjs:275-284` |
| `redirectUri` | String | 请求的重定向 URI。 | `OauthAuthorizationCode.mjs:11`, `OAuth2Model.mjs:282` |
| `scope` | String | 请求的 scope。 | `OauthAuthorizationCode.mjs:12`, `OAuth2Model.mjs:283` |
| `user_id` | ObjectId | 授予该授权码的用户。 | `OauthAuthorizationCode.mjs:13`, `OAuth2Model.mjs:285` |
| `codeChallenge`, `codeChallengeMethod` | String | PKCE challenge 及其方法。 | `OauthAuthorizationCode.mjs:14-15`, `OAuth2Model.mjs:286-287` |

#### 写入方

| 代码 | 作用 |
| - | - |
| `OAuth2Model.mjs:274-300` (`saveAuthorizationCode`) | 插入授权码。不会被执行，见说明。 |
| `OAuth2Model.mjs:307-312` (`revokeAuthorizationCode`) | 授权码使用后将其删除。不会被执行。 |

#### 读取方

| 代码 | 作用 |
| - | - |
| `OAuth2Model.mjs:137-177` (`getAuthorizationCode`) | 加载授权码及其用户和客户端。不会被执行。 |

#### 索引

| 名称 | 键 | 选项 | 迁移 |
| - | - | - | - |
| `expiresAt_1` | `{ expiresAt: 1 }` | TTL，`expireAfterSeconds: 0`，部分索引：`expiresAt` 存在 | `20230406125632_oauth_tokens_ttl.mjs` |

迁移位于 `tools/migrations/`。唯一索引 `authorizationCode_1` 仅来自标记为 `saas` 和 `server-pro` 的迁移，而 Community Edition 不运行这些迁移。

#### 说明

* 上述模型方法只会被 `@node-oauth/oauth2-server` 的 authorize 和 token 处理器调用。Web 只用这个库校验 bearer 令牌（`services/web/app/src/Features/Authentication/AuthenticationController.mjs:409-450`），也没有路由提供 `/oauth/authorize` 或 `/oauth/token`，因此从不会写入任何授权码。个人访问令牌是直接创建的，见 [OAuth Access Tokens](https://ayakaleaf-pro.mintlify.site/zh-CN/dev/data-model/oauth-access-tokens-collection.md)。


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