X Tutup
Skip to content

Commit f1c0d04

Browse files
committed
gh auth refresh
1 parent 55a13a3 commit f1c0d04

File tree

3 files changed

+377
-0
lines changed

3 files changed

+377
-0
lines changed

pkg/cmd/auth/logout/logout.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ func NewCmdLogout(f *cmdutil.Factory, runF func(*LogoutOptions) error) *cobra.Co
6262
}
6363

6464
func logoutRun(opts *LogoutOptions) error {
65+
// TODO check for GITHUB_TOKEN and error if found
6566
isTTY := opts.IO.IsStdinTTY() && opts.IO.IsStdoutTTY()
6667

6768
hostname := opts.Hostname

pkg/cmd/auth/refresh/refresh.go

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package refresh
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/AlecAivazis/survey/v2"
8+
"github.com/MakeNowJust/heredoc"
9+
"github.com/cli/cli/internal/config"
10+
"github.com/cli/cli/pkg/cmdutil"
11+
"github.com/cli/cli/pkg/iostreams"
12+
"github.com/cli/cli/pkg/prompt"
13+
"github.com/spf13/cobra"
14+
)
15+
16+
type RefreshOptions struct {
17+
IO *iostreams.IOStreams
18+
Config func() (config.Config, error)
19+
20+
Hostname string
21+
Scopes []string
22+
}
23+
24+
func NewCmdRefresh(f *cmdutil.Factory, runF func(*RefreshOptions) error) *cobra.Command {
25+
opts := &RefreshOptions{
26+
IO: f.IOStreams,
27+
Config: f.Config,
28+
}
29+
30+
cmd := &cobra.Command{
31+
Use: "refresh",
32+
Args: cobra.ExactArgs(0),
33+
Short: "Request new scopes for a token",
34+
Long: heredoc.Doc(`Expand the permission scopes for a given host's token.
35+
36+
This command allows you to add additional scopes to an existing authentication token via a web
37+
browser. This enables gh to access more of the GitHub API, which may be required as gh adds
38+
features or as you use the gh api command.
39+
40+
Unfortunately at this time there is no way to add scopes without a web browser's involvement
41+
due to how GitHub authentication works.
42+
43+
The --hostname flag allows you to operate on a GitHub host other than github.com.
44+
45+
The --scopes flag accepts a comma separated list of scopes you want to add to a token. If
46+
absent, this command ensures that a host's token has the default set of scopes required by gh.
47+
48+
Note that if GITHUB_TOKEN is in the current environment, this command will not work.
49+
`),
50+
Example: heredoc.Doc(`
51+
$ gh auth refresh --scopes write:org,read:public_key
52+
# => open a browser to add write:org and read:public_key scopes for use with gh api
53+
54+
$ gh auth refresh
55+
# => ensure that the required minimum scopes are enabled for a token and open a browser to add if not
56+
`),
57+
RunE: func(cmd *cobra.Command, args []string) error {
58+
if runF != nil {
59+
return runF(opts)
60+
}
61+
62+
return refreshRun(opts)
63+
},
64+
}
65+
66+
cmd.Flags().StringVarP(&opts.Hostname, "hostname", "h", "", "The GitHub host to use for authentication")
67+
cmd.Flags().StringSliceVarP(&opts.Scopes, "scopes", "s", []string{}, "Additional scopes to add to a token")
68+
69+
return cmd
70+
}
71+
72+
func refreshRun(opts *RefreshOptions) error {
73+
if os.Getenv("GITHUB_TOKEN") != "" {
74+
return fmt.Errorf("GITHUB_TOKEN is present in your environment and is incompatible with this command. If you'd like to modify a personal access token, see https://github.com/settings/tokens")
75+
}
76+
77+
isTTY := opts.IO.IsStdinTTY() && opts.IO.IsStdoutTTY()
78+
79+
if !isTTY {
80+
return fmt.Errorf("not attached to a terminal; in headless environments, GITHUB_TOKEN is recommended")
81+
}
82+
83+
cfg, err := opts.Config()
84+
if err != nil {
85+
return err
86+
}
87+
88+
candidates, err := cfg.Hosts()
89+
if err != nil {
90+
return fmt.Errorf("not logged in to any hosts. Use 'gh auth login' to authenticate with a host")
91+
}
92+
93+
hostname := opts.Hostname
94+
if hostname == "" {
95+
if len(candidates) == 1 {
96+
hostname = candidates[0]
97+
} else {
98+
err := prompt.SurveyAskOne(&survey.Select{
99+
Message: "What account do you want to refresh auth for?",
100+
Options: candidates,
101+
}, &hostname)
102+
103+
if err != nil {
104+
return fmt.Errorf("could not prompt: %w", err)
105+
}
106+
}
107+
} else {
108+
var found bool
109+
for _, c := range candidates {
110+
if c == hostname {
111+
found = true
112+
break
113+
}
114+
}
115+
116+
if !found {
117+
return fmt.Errorf("not logged in to %s. use 'gh auth login' to authenticate with this host", hostname)
118+
}
119+
}
120+
121+
return doAuthFlow(cfg, hostname, opts.Scopes)
122+
}
123+
124+
var doAuthFlow = func(cfg config.Config, hostname string, scopes []string) error {
125+
_, err := config.AuthFlowWithConfig(cfg, hostname, "", scopes)
126+
return err
127+
}
Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
package refresh
2+
3+
import (
4+
"bytes"
5+
"os"
6+
"regexp"
7+
"testing"
8+
9+
"github.com/cli/cli/internal/config"
10+
"github.com/cli/cli/pkg/cmdutil"
11+
"github.com/cli/cli/pkg/httpmock"
12+
"github.com/cli/cli/pkg/iostreams"
13+
"github.com/cli/cli/pkg/prompt"
14+
"github.com/google/shlex"
15+
"github.com/stretchr/testify/assert"
16+
)
17+
18+
func Test_NewCmdRefresh(t *testing.T) {
19+
tests := []struct {
20+
name string
21+
cli string
22+
wants RefreshOptions
23+
}{
24+
{
25+
name: "no arguments",
26+
wants: RefreshOptions{
27+
Hostname: "",
28+
Scopes: []string{},
29+
},
30+
},
31+
{
32+
name: "hostname",
33+
cli: "-h aline.cedrac",
34+
wants: RefreshOptions{
35+
Hostname: "aline.cedrac",
36+
Scopes: []string{},
37+
},
38+
},
39+
{
40+
name: "one scope",
41+
cli: "--scopes repo:invite",
42+
wants: RefreshOptions{
43+
Scopes: []string{"repo:invite"},
44+
},
45+
},
46+
{
47+
name: "scopes",
48+
cli: "--scopes repo:invite,read:public_key",
49+
wants: RefreshOptions{
50+
Scopes: []string{"repo:invite", "read:public_key"},
51+
},
52+
},
53+
}
54+
55+
for _, tt := range tests {
56+
t.Run(tt.name, func(t *testing.T) {
57+
io, _, _, _ := iostreams.Test()
58+
f := &cmdutil.Factory{
59+
IOStreams: io,
60+
}
61+
62+
argv, err := shlex.Split(tt.cli)
63+
assert.NoError(t, err)
64+
65+
var gotOpts *RefreshOptions
66+
cmd := NewCmdRefresh(f, func(opts *RefreshOptions) error {
67+
gotOpts = opts
68+
return nil
69+
})
70+
// TODO cobra hack-around
71+
cmd.Flags().BoolP("help", "x", false, "")
72+
73+
cmd.SetArgs(argv)
74+
cmd.SetIn(&bytes.Buffer{})
75+
cmd.SetOut(&bytes.Buffer{})
76+
cmd.SetErr(&bytes.Buffer{})
77+
78+
_, err = cmd.ExecuteC()
79+
assert.NoError(t, err)
80+
assert.Equal(t, tt.wants.Hostname, gotOpts.Hostname)
81+
assert.Equal(t, tt.wants.Scopes, gotOpts.Scopes)
82+
})
83+
84+
}
85+
}
86+
87+
type authArgs struct {
88+
hostname string
89+
scopes []string
90+
}
91+
92+
func Test_refreshRun(t *testing.T) {
93+
tests := []struct {
94+
name string
95+
opts *RefreshOptions
96+
askStubs func(*prompt.AskStubber)
97+
cfgHosts []string
98+
wantErr *regexp.Regexp
99+
ghtoken string
100+
nontty bool
101+
wantAuthArgs authArgs
102+
}{
103+
{
104+
name: "GITHUB_TOKEN set",
105+
opts: &RefreshOptions{},
106+
ghtoken: "abc123",
107+
wantErr: regexp.MustCompile(`GITHUB_TOKEN is present in your environment`),
108+
},
109+
{
110+
name: "non tty",
111+
opts: &RefreshOptions{},
112+
nontty: true,
113+
wantErr: regexp.MustCompile(`not attached to a terminal;`),
114+
},
115+
{
116+
name: "no hosts configured",
117+
opts: &RefreshOptions{},
118+
wantErr: regexp.MustCompile(`not logged in to any hosts`),
119+
},
120+
{
121+
name: "hostname given but dne",
122+
cfgHosts: []string{
123+
"github.com",
124+
"aline.cedrac",
125+
},
126+
opts: &RefreshOptions{
127+
Hostname: "obed.morton",
128+
},
129+
wantErr: regexp.MustCompile(`not logged in to obed.morton`),
130+
},
131+
{
132+
name: "hostname provided and is configured",
133+
cfgHosts: []string{
134+
"obed.morton",
135+
"github.com",
136+
},
137+
opts: &RefreshOptions{
138+
Hostname: "obed.morton",
139+
Scopes: []string{},
140+
},
141+
wantAuthArgs: authArgs{
142+
hostname: "obed.morton",
143+
scopes: []string{},
144+
},
145+
},
146+
{
147+
name: "no hostname, one host configured",
148+
cfgHosts: []string{
149+
"github.com",
150+
},
151+
opts: &RefreshOptions{
152+
Hostname: "",
153+
Scopes: []string{},
154+
},
155+
wantAuthArgs: authArgs{
156+
hostname: "github.com",
157+
scopes: []string{},
158+
},
159+
},
160+
{
161+
name: "no hostname, multiple hosts configured",
162+
cfgHosts: []string{
163+
"github.com",
164+
"aline.cedrac",
165+
},
166+
opts: &RefreshOptions{
167+
Hostname: "",
168+
Scopes: []string{},
169+
},
170+
askStubs: func(as *prompt.AskStubber) {
171+
as.StubOne("github.com")
172+
},
173+
wantAuthArgs: authArgs{
174+
hostname: "github.com",
175+
scopes: []string{},
176+
},
177+
},
178+
{
179+
name: "scopes provided",
180+
cfgHosts: []string{
181+
"github.com",
182+
},
183+
opts: &RefreshOptions{
184+
Scopes: []string{"repo:invite", "public_key:read"},
185+
},
186+
wantAuthArgs: authArgs{
187+
hostname: "github.com",
188+
scopes: []string{"repo:invite", "public_key:read"},
189+
},
190+
},
191+
}
192+
for _, tt := range tests {
193+
t.Run(tt.name, func(t *testing.T) {
194+
aa := authArgs{}
195+
doAuthFlow = func(_ config.Config, hostname string, scopes []string) error {
196+
aa.hostname = hostname
197+
aa.scopes = scopes
198+
return nil
199+
}
200+
201+
ghtoken := os.Getenv("GITHUB_TOKEN")
202+
defer func() {
203+
os.Setenv("GITHUB_TOKEN", ghtoken)
204+
}()
205+
os.Setenv("GITHUB_TOKEN", tt.ghtoken)
206+
io, _, _, _ := iostreams.Test()
207+
208+
io.SetStdinTTY(!tt.nontty)
209+
io.SetStdoutTTY(!tt.nontty)
210+
211+
tt.opts.IO = io
212+
cfg := config.NewBlankConfig()
213+
tt.opts.Config = func() (config.Config, error) {
214+
return cfg, nil
215+
}
216+
for _, hostname := range tt.cfgHosts {
217+
_ = cfg.Set(hostname, "oauth_token", "abc123")
218+
}
219+
reg := &httpmock.Registry{}
220+
reg.Register(
221+
httpmock.GraphQL(`query UserCurrent\b`),
222+
httpmock.StringResponse(`{"data":{"viewer":{"login":"cybilb"}}}`))
223+
224+
mainBuf := bytes.Buffer{}
225+
hostsBuf := bytes.Buffer{}
226+
defer config.StubWriteConfig(&mainBuf, &hostsBuf)()
227+
228+
as, teardown := prompt.InitAskStubber()
229+
defer teardown()
230+
if tt.askStubs != nil {
231+
tt.askStubs(as)
232+
}
233+
234+
err := refreshRun(tt.opts)
235+
assert.Equal(t, tt.wantErr == nil, err == nil)
236+
if err != nil {
237+
if tt.wantErr != nil {
238+
assert.True(t, tt.wantErr.MatchString(err.Error()))
239+
return
240+
} else {
241+
t.Fatalf("unexpected error: %s", err)
242+
}
243+
}
244+
245+
assert.Equal(t, aa.hostname, tt.wantAuthArgs.hostname)
246+
assert.Equal(t, aa.scopes, tt.wantAuthArgs.scopes)
247+
})
248+
}
249+
}

0 commit comments

Comments
 (0)
X Tutup