在GitHub中生成个人访问令牌后,是否有必要将其存储在机器本地的某个地方?

如果是,是否有首选的存储方式?


好吧,你必须把令牌保存在某个地方,当你不想每次你的应用程序要求输入它时:-)

一个很好的解决方案是使用环境变量,正如在一个评论中已经建议的那样。

但你仍然需要在某处设置环境变量。 在Windows(我正在使用)上,你可以使用系统设置中的对话框(我不知道其他操作系统是否有类似的东西)。

我不这么做,我更喜欢在我的项目中使用脚本。 在私有项目中,您可以将此提交给源代码控制,但这是一个偏好问题。

在我的一个个人项目中,我也使用个人访问令牌调用GitHub API。 这是一个命令行应用程序,最终用户将在配置文件中保存令牌(这是OK的)。

但我也需要开发令牌,因为项目有集成测试,我正在调用GitHub API。

这个项目在GitHub上是公开的,所以我无法在源代码控制中保存令牌。

我所做的是:

我有一个批处理文件(记住,我在Windows上),名为environment-variables.bat,它设置所有必需的环境变量,包括访问令牌 我在构建脚本和用于运行测试的批处理文件中调用此函数 在源代码控制中会忽略Environment-variables.bat 但是在源代码控制中,有environment-variables.bat。相反,其中包含相同的,但假的令牌/密码。

因此,我只需将该文件重命名为environment-variables.bat,将假密码替换为真实密码,就可以正常工作了。


不过,这并不是所有情况下的完美解决方案。

在我的项目中,我有一个问题,我需要在未来为更多的api使用更多的令牌/密码。

因此,我的environment-variables.bat中的令牌数量将会增加,这使得潜在的参与者很难实际执行所有集成测试。我到现在都不知道该怎么办。

基本上我是在我的机器上做的:

https://gist.github.com/bsara/5c4d90db3016814a3d2fe38d314f9c23

我的配置文件脚本与描述略有不同:

env=~/.ssh/agent.env

agent_load_env () { test -f "$env" && . "$env" >| /dev/null ; }

agent_start () {
    (umask 077; ssh-agent >| "$env")
        . "$env" >| /dev/null ; 
}

agent_load_env

# agent_run_state: 0=agent running w/ key; 1=agent w/o key; 2= agent not running
agent_run_state=$(ssh-add -l >| /dev/null 2>&1; echo $?)

if [ ! "$SSH_AUTH_SOCK" ] || [ $agent_run_state = 2 ]; then
    agent_start
    ssh-add
elif [ "$SSH_AUTH_SOCK" ] && [ $agent_run_state = 1 ]; then
    ssh-add
fi

unset env

密码的一半意义在于(理想情况下)你记住它们,系统对它们进行哈希,因此它们永远不会以纯文本的形式存储在任何地方。 然而,GitHub的个人访问令牌系统似乎基本上迫使你以纯文本存储令牌?

首先,PAT(个人访问令牌)不是一个简单的密码,而是一个等价的密码:

你可以生成多个时间(例如,每台机器一个你需要访问GitHub存储库) 你可以在任何时候撤销(从GitHub web界面),这使得PAT过时,即使它徘徊在其中一台机器上。

这与你的密码不同,密码是你账户的唯一密码,如果你在任何地方使用它,都必须修改它,否则无法轻易更改。


由于在命令行或API上使用Git通过HTTPS执行Git操作时,可以使用PAT代替密码,因此可以使用Git凭据帮助器安全地缓存它。 例如,在Windows上,将使用Windows凭据管理器,通过GCM—Git凭据管理器—用于Windows, Mac或Linux:

git config --global credential.helper manager-core
# Git 2.39+
git config --global credential.helper manager

(在2022年第四季度Git 2.39+中,manager-core被替换/重命名为manager)

第一次你推到一个回购,一个弹出窗口将要求你的凭证:用户名和你的PAT。 下一次,它将不会询问,并直接重用该PAT,该PAT仍然安全地存储在凭据管理器中。

类似的想法也适用于Mac和GNOME Keyring(在2021年,它将需要一个DBus会话和libsecret),但在2021年,GCM-Core涵盖了这些用例。 这个想法仍然是:将PAT存储在加密的凭据存储中。


如上所述,更现代的解决方案(2020年第四季度)是Microsoft git -凭据管理器- core,或2022年第四季度,Microsoft git -凭据管理器

git config --global credential.helper manager-core
# Git 2.39+:
git config --global credential.helper manager

在Git 2.39 (Q4 2022)之前,适用于Linux:

您需要安装git-credential-manager-core,下载其最新版本,如gcmcore-linux_amd64.2.0.474.41365.deb

sudo dpkg -i <path-to-package>
git-credential-manager-core configure

不过,正如Mekky Mayata在评论中指出的那样,在Linux上使用GCM (git - credential - manager - core)时,您需要定义一个git config -global credential。credentialStore第一。

参见“Linux上的凭据存储”:

在Linux平台上,Git凭据管理器(GCM)有四个存储凭据的选项: freedesktop.org特勤局API GPG/pass兼容文件 Git内置的凭据缓存 纯文本文件 默认情况下,未配置GCM。 您可以通过设置GCM_CREDENTIAL_STORE环境变量或凭据来选择要使用的凭据存储。credentialStore Git配置设置。

正如agent18在评论中指出的那样,在安装libsecret-1-0和libsecret-1-dev之后使用git-credential-libsecret是很好的第一步。 但是,同样,现在应该由凭证管理器核心包装(在Git 2.39之前)。

我喜欢在存储库中对它们进行加密,并使用.envrc (https://direnv.net/)加载它们。

为了做到这一点,我使用ssh-vault来加密数据,使用我的ssh密钥,GitHub已经公开,例如:

echo MY_TOKEN="secret" | ssh-vault -u <github-user> create > my-encypted-vars.ssh

然后.envrc的内容看起来像这样:

echo "Enter ssh key password"
context=$(ssh-vault view $HOME/projects/my-encrypted.ssh | tail -n +2)
export ${context}

这将解密my-encrypted-vars中的数据。ssh文件,并设置MY_TOKEN到我的环境变量,每次我cd到项目目录。

通过这样做,令牌/变量被“安全地”存储,并且随时可以作为环境变量使用

以我为例,在Ubuntu中,被接受的解决方案不能处理像这样的消息

Git: 'credential-manager'不是Git命令

但是店长却做得很好:

git config --global credential.helper store

您可以缓存您的凭证为一个定义的时间使用:

git config --global credential.helper cache

默认的缓存时间是900秒(15分钟),但可以通过以下方式更改:

git config --global credential.helper 'cache --timeout=3600'

参见以下Github页面:

https://docs.github.com/en/github/using-git/caching-your-github-credentials-in-git

这不是一个永久存储,并且与其他注释一样,凭证不应该以纯文本形式存储,这是一个安全风险。我使用密码管理器(https://bitwarden.com/)存储PAT(个人访问令牌),然后将其复制到第一次使用时,然后将其缓存到其中。如果您在Github帐户上启用2FA,则需要PAT。

在Ubuntu 20.04上测试,几乎是全新安装,使用Git 2.25.1和unity 7.5。

身份验证基础

Github需要一个认证密钥(与该认证密钥绑定的某些权利)。一个特定的认证密钥具有一定的权限(读取私有回购、读写公共回购等),并且“充当密码”,同时具有用户可以随时撤销的权限。

个人访问令牌

我们从PAT开始。即设置——>开发人员设置——> personaonl访问令牌——>生成新的令牌——>注意——>设置权限(repo,repo_hook可能)——>生成令牌 Git推送回购,并在请求时输入生成的令牌(很长的密码)作为密码。

以不同的方式存储密码

Can be done in a file and then using xclip to bring it back to clipboard and paste it everytime (Screw this) Cache with the help of git commands git config credential.helper cache <time-limit-of-cache>. But you still have to somehow clipboard the password after the timelimit. Store it permanently in a file with git commands git config credential.helper store (don't use --global). This is NOT ENCRYPTED. You can open the file and read it. (e.g., If someone gets access to your laptop they can pretty much read the Password using a bootable USB (assuming your whole system is not encrypted)). Or go the encryption route as per here. It is not complicated at all. 3 simple steps.

sudo apt-get install libsecret-1-0 libsecret-1-dev
sudo make --directory=/usr/share/doc/git/contrib/credential/libsecret
    
git config credential.helper /usr/share/doc/git/contrib/credential/libsecret/git-credential-libsecret

这允许以加密格式存储密码/个人访问令牌。git配置文件可以在loca repo的.git/config文件中找到,如这里所示,如果您需要它的话。

注: 有许多地方建议使用Gnome-keyring,但显然不建议使用。

存储多个帐户的密码/ pat

这变得很棘手,@VonC建议我们需要一个git - credentials - manager核心(GCM核心)。这个答案是基于我在这个答案中的发现而加强的。

First install GCM core Download latest .deb package sudo dpkg -i <path-to-package> git-credential-manager-core configure git config --global credential.credentialStore secretservice as we use libsecret Get latest git In my case I had git 2.25 and got error error: unknown option 'show-scope'. It appears that GCM core is using higher git (atleast 2.26). So install the latest and greatest git as per here: sudo add-apt-repository ppa:git-core/ppa sudo apt-get update apt list git # shows the latest git currently 2.31 sudo apt-get install git #or sudo apt-get upgrade Update git remote path with username built in GCM core needs this to identify the different accounts.:( git remote set-url origin https://user1@github.com/user1/myRepo1.git git remote set-url origin https://user2@github.com/user1/myRepo1.git ^^^^^

你的~ /。因此,Gitconfig文件将具有以下内容:

[credential]
   helper = /usr/bin/git-credential-manager-core
   credentialStore = secretservice
[credential "https://dev.azure.com"]
   useHttpPath = true

或者,您可以在主目录中创建一个~/.netrc文件,并将登录凭据保存在其中。

cat ~/.netrc
machine github.com login <login-id> password <token-password>

你可以使用pass存储github https令牌。

将git主机映射到pass条目的两个备选方案:

Bash脚本映射到右传递项:

#!/usr/bin/env bash
# assuming "get" action from git and a config like this
# git config --global credential.helper $XDG_BIN_HOME'/git_credentials_from_pass $@'
while IFS= read -r line
do
  echo "$line"
  if [[ "$line" =~ host=.*github.com.* ]]; then
      echo "username=your_user_name"
      echo "password=$(pass show token_github.com/your_username)"
  #else ...
  fi
done

改变your_username和token_github.com的方式,你设置它通过pass插入。

这将添加令牌传递,无需输入或粘贴两次:

echo your_github_token | sed p | pass add token_github.com/your_username

安装pass-git-helper:

git config --global credential.helper '!pass-git-helper $@'

pass-git-helper需要一个ini文件来映射git请求和pass条目。 $ {XDG_CONFIG_HOME} / pass-git-helper git-pass-mapping.ini例子:

[DEFAULT]
username_extractor=entry_name
[github.com*]
target=token_${host}/your_github_username

尝试启用此功能以帮助跨推/拉持久化

git config credential.helper store

对于正在克隆的repo / macOS用户/安装iTerm2 https://iterm2.com/

使直到

只要在需要时单击该片段即可。 另外,你在用哦,我的zsh,对吧? https://github.com/ohmyzsh/ohmyzsh

为了在缓存中存储您的凭据并避免每次执行git操作时都登录,请遵循以下步骤:

导航到本地存储库文件夹。 在当前文件夹的终端:git config——global——replace-all credential。辅助缓存 执行git push或git pull。 使用用户名和访问令牌登录(访问令牌是您的密码)。令牌可以在GitHub中设置,并可以访问repo,工作流,write:packages和delete:packages。 重复git push或任何git操作,你会发现从现在开始它不再要求登录凭据。

在我的用例中,我将PAT存储在密码管理器中,例如LastPass, KeePass, 1Password。当我在Linux环境(例如Docker)中需要它时,我将PAT保存在一个环境变量中,然后使用git的凭据帮助器设置。例如:

git config --global credential.helper 'cache --timeout 600'

<< eof tr -d ' ' | git credential-cache store 
  protocol=https
  host=github.com
  username=nonce
  password=${GITHUB_PAT}
eof

对于PAT,用户名可以是任何东西,除了空白。以下是详细阐述的要点:

https://gist.github.com/rwcitek/da862e9e27cc28d3e96e62a2ca4b2b64

安装Git凭证管理器!https://github.com/GitCredentialManager/git-credential-manager。GCM支持缓存以及在会话之间持久存在的各种特定于平台的凭据存储。

更棒的是:GCM支持用户友好的安全认证GitHub和GitLab通过web浏览器与OAuth。这意味着您甚至不再需要创建个人访问令牌。当你推送git时,只需按照链接到GitHub并授权应用程序。后续的身份验证不需要交互。

OAuth比个人访问令牌更安全,因为令牌的有效期很短,在需要时使用更长的刷新令牌进行刷新。

curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null
sudo apt update
sudo apt install gh
gh auth login

它将要求输入协议和令牌

然后我再次克隆回购。它不是要信物

使用git代替。基本上,将每个https://github调用替换为访问令牌+ https://

git config——global url."https://<username>:<github-token>@github.com/". insteadof "https://github.com/

现在每次调用github都会自动添加您的凭据。

我在这里找到了很好的答案。 更多关于git insteadOf的信息。