Commit graph

86 commits

Author SHA1 Message Date
Des Preston
94a640bd2a
Add auth setup-git for setting up gh as a git credential helper (#4246)
Adds a new command `gh auth setup-git [<hostname>]` that sets up git to
use the GitHub CLI as a credential helper.

The gist is that it runs these two git commands for each hostname the
user is authenticated with.

```
git config --global --replace-all 'credential.https://github.com.helper' ''
git config --global --add 'credential.https://github.com.helper' '!gh auth git-credential'
```

If a hostname flag is given, it'll setup GH CLI as a credential helper
for only that hostname.

If the user is not authenticated with any git hostnames, or the user is
not authenticated with the hostname given as a flag, it'll print an
error.

Co-authored-by: Mislav Marohnić <mislav@github.com>
2021-12-02 16:38:34 +00:00
Alan Donovan
f4491c7a80 Add FlagErrorf; encapsulate FlagError.error 2021-10-21 11:40:20 -04:00
Mislav Marohnić
2c86e671f3 Enable gh auth login for github.localhost 2021-10-18 20:01:18 +02:00
Mislav Marohnić
89ad870190 auth refresh: preserve existing scopes when requesting new ones
When there was a previously valid token that was granted some scopes,
ensure all those scopes will be re-requested when doing the
authentication flow for the new token.
2021-10-14 19:52:59 +02:00
Mislav Marohnić
b38ce24497 Ensure correct path to gh after gh auth refresh git credential setup 2021-09-24 14:43:48 +02:00
Mislav Marohnić
d853ce5bc9 Avoid resolving executable() until requested at runtime
This is to avoid hitting the filesystem and resolving symlinks
unnecessarily. The value of executable is just used conditionally by a
handful of commands.
2021-09-24 14:42:41 +02:00
Mislav Marohnić
11fbb60ae7 Rename the module to "github.com/cli/cli/v2" 2021-08-25 12:41:30 +02:00
Mislav Marohnić
21521b06b9 Check git presence during auth login only if it's going to be needed 2021-08-11 19:22:16 +02:00
Shaharyar Ahmed
e28236a447 Check path for git executable before auth
There was a bug where if git was not installed then gh would do its
authentication and try to configure git but would then find out that the
git executable was not in PATH.

Now gh checks to see if the git executable is in PATH before
authenticating the user. If the git executable is in PATH the
authentication continues as normal, if it is not in PATH then it prints
out an error to the console:

$ git executable not found in $PATH

Resolves: #3818
2021-08-11 19:22:16 +02:00
Mislav Marohnić
c598a1edc2 Fix detecting cases when cfg.Hosts() is empty 2021-07-20 15:50:58 +02:00
Mislav Marohnić
98f1f5ec0d Use absolute path when configuring gh as git credential
This keeps git operations working even when PATH is modified, e.g. `brew
update` will work even though Homebrew runs the command explicitly
without `/usr/local/bin` in PATH.

Additionally, this inserts a blank value for `credential.*.helper` to
instruct git to ignore previously configured credential helpers, i.e.
those that might have been set up in system configuration files. We do
this because otherwise, git will store the credential obtained from gh
in every other credential helper in the chain, which we want to avoid.

Before:

    git config --global credential.https://github.com.helper '!gh auth git-credential'

After:

    git config --global credential.https://github.com.helper ''
    git config --global --add credential.https://github.com.helper '!/path/to/gh auth git-credential'
2021-03-03 16:20:21 +01:00
Mislav Marohnić
c5af4ddfdc
Merge pull request #3009 from cli/git-credential-env
Fix `auth git-credential` when the token comes from environment
2021-02-23 16:32:23 +01:00
Mislav Marohnić
cfddda8829 Indicate workflow scope is GHE 3.0+ only during auth login 2021-02-23 10:52:29 +01:00
Mislav Marohnić
f807795491 Fix pasting Personal Access Token to auth login for GHE 2021-02-23 10:19:11 +01:00
Mislav Marohnić
9bf1668b3f Fix auth git-credential when the token comes from environment
When a token such as GH_TOKEN is set through environment variables and
`~/.config/gh/hosts.yml` is non-existent, the `auth git-credential get`
command used to fail due to missing username.

Since GitHub username isn't at all required for token authentication,
use the `x-access-token` faux username instead of trying to obtain one
from a config file.
2021-02-19 15:37:11 +01:00
Mislav Marohnić
1a9e42ed55 Add ssh-key add command and publish ssh-key 2021-02-17 19:46:59 +01:00
Mislav Marohnić
4a49e3526c Merge remote-tracking branch 'origin' into writeorg-oauth-scope 2021-02-17 17:11:24 +01:00
Mislav Marohnić
b4bf8cda8d Close pubkey file after reading 2021-02-17 17:01:14 +01:00
Mislav Marohnić
87fcda5fbc Add tests for SSH login flow 2021-02-17 16:52:40 +01:00
Mislav Marohnić
9550ad0159
Tweak prompt for SSH passphrase
Co-authored-by: Amanda Pinsker <ampinsk@github.com>
2021-02-05 10:16:42 +01:00
Mislav Marohnić
1bd6d7b36c Extract parts of HTTPS-related setup shared between login and refresh 2021-02-04 18:36:42 +01:00
Mislav Marohnić
5a110c8e42 Add SSH key generation & uploading to gh auth login flow 2021-02-01 23:34:00 +01:00
Sam Coe
71fd2fa24c
Fix up test 2021-01-22 08:50:09 -08:00
Sam Coe
5e10638b0e
Merge branch 'trunk' into auth-status-fail 2021-01-22 08:46:24 -08:00
Nate Smith
5430728a0a
Merge pull request #2813 from cli/token-from-env-err
More descriptive error when aborting auth due to environment variables
2021-01-20 15:06:52 -08:00
Mislav Marohnić
4a2cc8d2a4 Simplify ssh-key list
Do not require nor request `read:public_key` scope by default.
2021-01-20 20:23:57 +01:00
edualb
e26a1b98a1 add ssh-key command 2021-01-20 20:23:38 +01:00
Mislav Marohnić
c9407b2629 More descriptive error when aborting auth due to environment variables
Old message:

    read-only token in GH_TOKEN cannot be modified

This message was vague and some users did not understand that this
refers to the value that is read from environment variables.

New message:

    $ GH_TOKEN=123 ghd auth login -h github.com
    The value of the GH_TOKEN environment variable is being used for authentication.
    To have GitHub CLI store credentials instead, first clear the value from the environment.
2021-01-20 18:27:35 +01:00
Mislav Marohnić
75ebb863e3 Use testify assertions for error matching 2021-01-19 13:59:37 +01:00
Nilesh Singh
f3fcaf6c9c Fix error message text & add test case 2021-01-17 14:42:49 +05:30
Nilesh Singh
2f5ffbd60a Add fail message for non-existent hostname 2021-01-15 13:28:57 +05:30
Mislav Marohnić
38ea595ce2 Fix refresh test 2020-12-07 20:07:20 +01:00
Mislav Marohnić
3c76eb15a4 Add tests for auth git-credential command 2020-12-07 20:01:53 +01:00
Mislav Marohnić
381e83e6e5 Extend git credential prompt to auth refresh 2020-12-07 20:01:16 +01:00
Mislav Marohnić
c39dc28fa1 Rename auth/client to auth/shared 2020-12-07 17:08:42 +01:00
Mislav Marohnić
d56d92c908 If git credential helper is non-defined, set gh as credential helper 2020-11-23 20:20:05 +01:00
Mislav Marohnić
e36c9029d3 Fix broken tests 2020-11-20 20:33:08 +01:00
Mislav Marohnić
91d2adc134 Avoid re-requesting username if we already have it 2020-11-20 19:36:26 +01:00
Mislav Marohnić
67672fa88c Prime user's git HTTPS credentials on auth login 2020-11-20 19:36:04 +01:00
vilmibm
a2aa154794 port entirely to ColorScheme 2020-10-29 12:37:45 -07:00
Sam
edecb2e4f7
Merge pull request #2207 from cli/codespaces
[Codespaces] Support "integration" tokens
2020-10-27 15:37:02 +03:00
Mislav Marohnić
460d55f723 Move HostnameValidator to ghinstance 2020-10-21 16:31:20 +00:00
wilso199
3ecb9de1a7 Adding a hostname flag option for use with gh api 2020-10-21 16:31:20 +00:00
Sam
f8fd61cc6c
Merge pull request #2157 from rista404/print-auth-token-1514
Add ability to print auth token
2020-10-20 08:46:45 +02:00
Mislav Marohnić
4bda6fceec auth status: fix reporting the token source 2020-10-16 17:11:14 +00:00
Ryan Nystrom
02883a89e3 add tests 2020-10-16 15:59:41 +00:00
Ryan Nystrom
93bc389269 Add scopes flag to login 2020-10-15 22:04:18 +00:00
rista404
2bdfc10e0a
Add ability to print auth token 2020-10-11 12:55:34 +02:00
Mislav Marohnić
49909b5123 Tweak gh auth login examples 2020-10-01 19:56:56 +02:00
Mislav Marohnić
cc2b86f3a6 Specify minimum required token scopes in gh help auth login 2020-10-01 19:56:33 +02:00