# 确保 API 凭据安全

遵循这些最佳做法，确保 API 凭据和令牌安全。

## 选择适当的身份验证方法

应选择适合要完成的任务的身份验证方法。

* 若要使用 API 进行个人使用，可以创建一个 personal access token。
* 若要代表组织或其他用户使用 API，应创建一个 GitHub App。
* 若要在工作流中使用 GitHub Actions API，应使用内置 `GITHUB_TOKEN`API 进行身份验证。

有关详细信息，请参阅“[关于 GitHub 的身份验证](/zh/enterprise-server@3.22/authentication/keeping-your-account-and-data-secure/about-authentication-to-github#authenticating-with-the-api)”。

## 限制凭据的权限

创建 A personal access token时，仅选择所需的最小权限或范围，并为需要使用令牌所需的最短时间设置到期日期。
GitHub 建议使用 fine-grained personal access token 而不是 personal access tokens (classic)。 有关详细信息，请参阅“[管理个人访问令牌](/zh/enterprise-server@3.22/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens#types-of-personal-access-tokens)”。

令牌具有与令牌所有者相同的访问资源和对这些资源执行操作的能力，并且还受到授予令牌的任何范围或权限的限制。 令牌无法向用户授予额外访问权限功能。

在创建GitHub App时，请选择GitHub App所需的最低权限。 有关详细信息，请参阅“[创建GitHub应用的最佳做法](/zh/enterprise-server@3.22/apps/creating-github-apps/about-creating-github-apps/best-practices-for-creating-a-github-app)”。

在 `GITHUB_TOKEN` 工作流中使用 GitHub Actions 进行身份验证时，仅授予所需的最低权限。 有关详细信息，请参阅“[在工作流中使用 GITHUB\_TOKEN 进行身份验证](/zh/enterprise-server@3.22/actions/tutorials/authenticate-with-github_token#modifying-the-permissions-for-the-github_token)”。

## 安全地存储你的身份验证凭据

处理身份验证凭据的方式与处理密码或其他敏感凭据的方式相同。

* 不要使用未加密的消息或电子邮件系统共享身份验证凭据。
* 不要在命令行中传递 personal access token 纯文本。 有关详细信息，请参阅“[管理个人访问令牌](/zh/enterprise-server@3.22/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens#keeping-your-personal-access-tokens-secure)”。
* 不要将未加密的身份验证凭据（如令牌或密钥）推送到任何存储库，即使存储库是专用存储库。 考虑使用 GitHub Actions 密钥。 有关详细信息，请参阅 [在 GitHub Actions 中使用机密](/zh/enterprise-server@3.22/actions/how-tos/write-workflows/choose-what-workflows-do/use-secrets)。
* 可以使用机密扫描来发现已推送到存储库的令牌、私钥和其他机密，或阻止将来包含机密的推送。 有关详细信息，请参阅“[秘密扫描](/zh/enterprise-server@3.22/code-security/concepts/secret-security/secret-scanning)”。

## 限制谁可以访问你的身份验证凭据

不要与他人共享 personal access token 。 请考虑创建一个personal access token，而不是共享GitHub App。 有关详细信息，请参阅“[关于创建GitHub应用](/zh/enterprise-server@3.22/apps/creating-github-apps/about-creating-github-apps/about-creating-github-apps)”。

如果需要与团队共享凭据，请将凭据存储在安全的共享系统中。 例如，可以使用 [1Password](https://1password.com/) 安全地存储和共享密码，也可以将密钥存储在 [Azure KeyVault](https://azure.microsoft.com/en-gb/products/key-vault) 中，并使用 IAM（标识和访问管理）管理访问权限。

如果要创建 GitHub Actions 需要访问 API 的工作流，则可以将凭据存储在加密的机密中，并从工作流访问加密的机密。 有关详细信息，请参阅 [在 GitHub Actions 中使用机密](/zh/enterprise-server@3.22/actions/how-tos/write-workflows/choose-what-workflows-do/use-secrets) 和 [在GitHub Actions工作流中使用GitHub应用发出经过身份验证的 API 请求](/zh/enterprise-server@3.22/apps/creating-github-apps/authenticating-with-a-github-app/making-authenticated-api-requests-with-a-github-app-in-a-github-actions-workflow)。

## 在代码中安全地使用身份验证凭据

切勿将身份验证凭据（如令牌、密钥或与应用相关的机密）硬编码到代码中。 相反，请考虑使用机密管理器，例如 [Azure 密钥保管库](https://azure.microsoft.com/products/key-vault) 或 [HashiCorp Vault](https://www.hashicorp.com/products/vault)。 有关保护 GitHub App 凭据的详细信息，请参阅 [创建GitHub应用的最佳做法](/zh/enterprise-server@3.22/apps/creating-github-apps/about-creating-github-apps/best-practices-for-creating-a-github-app)。

在脚本中使用 personal access token 时，请考虑将令牌存储为 GitHub Actions 密钥，然后通过 GitHub Actions 执行脚本。 有关详细信息，请参阅 [在 GitHub Actions 中使用机密](/zh/enterprise-server@3.22/actions/how-tos/write-workflows/choose-what-workflows-do/use-secrets)。

如果这些选项都不可行，则可以将身份验证凭据存储在 `.env` 文件中。 请确保加密 `.env` 文件，切勿将其推送到任何存储库。

## 准备修正计划

应创建计划，以便及时处理任何安全漏洞。 如果令牌或其他身份验证凭据泄露，则需要：

* 生成新凭据。
* 将旧凭据替换为要存储或访问凭据的任何位置的新凭据。
* 删除已泄露的旧凭据。

如需了解如何轮转 GitHub App 的已泄露凭据，请参阅 [创建GitHub应用的最佳做法](/zh/enterprise-server@3.22/apps/creating-github-apps/about-creating-github-apps/best-practices-for-creating-a-github-app)。

有关创建和删除 personal access tokens 的信息，请参阅 [管理个人访问令牌](/zh/enterprise-server@3.22/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens)。