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

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


当前回答

在我的用例中,我将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

其他回答

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

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

但是店长却做得很好:

git config --global credential.helper store

在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

我喜欢在存储库中对它们进行加密,并使用.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到项目目录。

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

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

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

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

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

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