reddit / emacs
Magit and authentication
shooki7 · 2022-05-20 07:42
I am trying to use Magit as my main git client, while having repos in GitHub and GitLab.
Using Doom Emacs, I installed `ghub` and `forge` and wrote in my config file:
(setq auth-sources '("~/.authinfo.gpg"))
I created a GitHub token, pasted it in the required format in `authinfo.gpg` :
machine api.github.com login my_user^forge password TOKEN
But still each time I try any git interaction using Magit it asks me for username and password. I wouldn't complain usually, but now if you want to go through it you'll have to type the token instead of your password, which is not really practical.
I try to fix this problem for some time using the `ghub` docs and other, with no success.
Did it happen to anyone?
BettaGeorge · 2022-05-20 08:02
I just use ssh to connect to github, that is much easier than going through all the hassle with their stupid 2FA.
In particular, once you have an ssh agent running, magit just uses that and never asks you anything.
shooki7 · 2022-05-20 08:11
If it works, I'll do that.
I couldn't find anything recent about integrating ssh key to magit.
Do you have any link that explains how to set it up?
edit: Followed the GH guide (generating the key, upload it to GH) and the njust cloning through SSH.. OMG it was so simple
flexibeast · 2022-05-20 09:08
i use a password manager (KeepassXC) and store my GitHub PAT in that, so i don't have to type it, and instead just copy and paste.
BettaGeorge · 2022-05-20 09:13
Glad to help!
Remember to secure your ssh key with a reasonably long passphrase. Using an ssh agent, you only have to unlock it once when you log in, then it stays unlocked (also for magit). Keepassxc for example even integrates an ssh agent so you only have to unlock your password manager and everything else is automatic.
shooki7 · 2022-05-20 10:26
Do I need to load the ssh agent each time my system boots?
Can the ssh agent run automatically whenever I try a git command using ssh?
Thanks for the help!
yigitemres · 2022-05-20 12:16
I think there is a problem with you magit or forge configuration.
1. Is your magit (without forge) works without asking username and password?
1. If yes, you should add this line `machine api.github.com login my_user\^forge password TOKEN` to `.authinfo` and then use `epa-encrypt-file` in order to create `.authinfo.gpg`. ~~After that remove `.authinfo` itself~~ (see below) and forge continue to use `.authinfo.gpg`
2. If no, you have to either install `ssh-agency` package through emacs or you have to follow this [link](https://interworks.com/blog/2021/09/15/setting-up-ssh-agent-in-windows-for-passwordless-git-authentication/).
By the way, `forge` is only needed for issues and pr management wtih the comfort of `magit` (see the [link](https://magit.vc/manual/forge.html#Introduction)). You can use `magit` without `forge` if you set properly.
(Actually we really need to remove `.authinfo` file but somehow I cannot run forge with only `.authinfo.gpg`.)
TheSnowIsCold-46 · 2022-05-20 12:49
This is how I run with Magit. When my system boots I start my ssh agent, and add my git key (unlock with password). Then I start up emacs daemon and Magit will just use the key until I stop the agent of shut down the system.
You don't need to do anything other than the initial start of the agent and unlocking of the key. Just make sure you do it before you start emacs (or restart the emacs server if you run in daemon mode). Magit will automatically see the agent running and use the keys in it as another user mentioned.
BettaGeorge · 2022-05-20 16:32
You can just start the agent in your .profile, then it always runs before your emacs server is up.
Source: I run exwm, and this setup works.
Schievel1 · 2022-05-21 09:00
Why not using ssh?
CIAvash · 2022-05-21 19:44
I didn't have this problem when I was using authinfo. Are you sure you are running a gpg-agent?
FWIW I now use [KeePassXC](https://github.com/GitCredentialManager/git-credential-manager)(With secret service enabled)+[Git Credential Manager](https://github.com/GitCredentialManager/git-credential-manager):
`(setq-default auth-sources '(default "secrets:SecretDatabaseName" "~/.authinfo.gpg"))`
`(add-hook 'magit-process-prompt-functions #'magit-process-git-credential-manager-core)`