> ## 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-TW/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-TW/dev/data-model/oauth-access-tokens-collection.md)。


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