X Tutup
Skip to content

Commit 2bdfc10

Browse files
committed
Add ability to print auth token
1 parent a2aa07d commit 2bdfc10

File tree

2 files changed

+55
-18
lines changed

2 files changed

+55
-18
lines changed

pkg/cmd/auth/status/status.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ type StatusOptions struct {
1919
IO *iostreams.IOStreams
2020
Config func() (config.Config, error)
2121

22-
Hostname string
22+
Hostname string
23+
ShowToken bool
2324
}
2425

2526
func NewCmdStatus(f *cmdutil.Factory, runF func(*StatusOptions) error) *cobra.Command {
@@ -48,6 +49,7 @@ func NewCmdStatus(f *cmdutil.Factory, runF func(*StatusOptions) error) *cobra.Co
4849
}
4950

5051
cmd.Flags().StringVarP(&opts.Hostname, "hostname", "h", "", "Check a specific hostname's auth status")
52+
cmd.Flags().BoolVarP(&opts.ShowToken, "show-token", "t", false, "Display the auth token")
5153

5254
return cmd
5355
}
@@ -84,7 +86,7 @@ func statusRun(opts *StatusOptions) error {
8486
continue
8587
}
8688

87-
_, tokenSource, _ := cfg.GetWithSource(hostname, "oauth_token")
89+
token, tokenSource, _ := cfg.GetWithSource(hostname, "oauth_token")
8890
tokenIsWriteable := cfg.CheckWriteable(hostname, "oauth_token") == nil
8991

9092
statusInfo[hostname] = []string{}
@@ -124,6 +126,11 @@ func statusRun(opts *StatusOptions) error {
124126
addMsg("%s Git operations for %s configured to use %s protocol.",
125127
utils.GreenCheck(), hostname, utils.Bold(proto))
126128
}
129+
tokenDisplay := "*******************"
130+
if opts.ShowToken {
131+
tokenDisplay = token
132+
}
133+
addMsg("%s Token: %s", utils.GreenCheck(), tokenDisplay)
127134
}
128135
addMsg("")
129136

pkg/cmd/auth/status/status_test.go

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ func Test_NewCmdStatus(t *testing.T) {
3434
Hostname: "ellie.williams",
3535
},
3636
},
37+
{
38+
name: "show token",
39+
cli: "--show-token",
40+
wants: StatusOptions{
41+
ShowToken: true,
42+
},
43+
},
3744
}
3845

3946
for _, tt := range tests {
@@ -92,62 +99,64 @@ func Test_statusRun(t *testing.T) {
9299
wantErrOut: regexp.MustCompile(`Logged in to joel.miller as.*tess`),
93100
},
94101
{
95-
name: "hostname set",
96-
opts: &StatusOptions{
97-
Hostname: "joel.miller",
98-
},
102+
name: "missing scope",
103+
opts: &StatusOptions{},
99104
cfg: func(c config.Config) {
100105
_ = c.Set("joel.miller", "oauth_token", "abc123")
101106
_ = c.Set("github.com", "oauth_token", "abc123")
102107
},
103108
httpStubs: func(reg *httpmock.Registry) {
104-
reg.Register(httpmock.REST("GET", "api/v3/"), httpmock.ScopesResponder("repo,read:org,"))
109+
reg.Register(httpmock.REST("GET", "api/v3/"), httpmock.ScopesResponder("repo,"))
110+
reg.Register(httpmock.REST("GET", ""), httpmock.ScopesResponder("repo,read:org,"))
105111
reg.Register(
106112
httpmock.GraphQL(`query UserCurrent\b`),
107113
httpmock.StringResponse(`{"data":{"viewer":{"login":"tess"}}}`))
108114
},
109-
wantErrOut: regexp.MustCompile(`Logged in to joel.miller as.*tess`),
115+
wantErrOut: regexp.MustCompile(`joel.miller: missing required.*Logged in to github.com as.*tess`),
116+
wantErr: regexp.MustCompile(``),
110117
},
111118
{
112-
name: "missing scope",
119+
name: "bad token",
113120
opts: &StatusOptions{},
114121
cfg: func(c config.Config) {
115122
_ = c.Set("joel.miller", "oauth_token", "abc123")
116123
_ = c.Set("github.com", "oauth_token", "abc123")
117124
},
118125
httpStubs: func(reg *httpmock.Registry) {
119-
reg.Register(httpmock.REST("GET", "api/v3/"), httpmock.ScopesResponder("repo,"))
126+
reg.Register(httpmock.REST("GET", "api/v3/"), httpmock.StatusStringResponse(400, "no bueno"))
120127
reg.Register(httpmock.REST("GET", ""), httpmock.ScopesResponder("repo,read:org,"))
121128
reg.Register(
122129
httpmock.GraphQL(`query UserCurrent\b`),
123130
httpmock.StringResponse(`{"data":{"viewer":{"login":"tess"}}}`))
124131
},
125-
wantErrOut: regexp.MustCompile(`joel.miller: missing required.*Logged in to github.com as.*tess`),
132+
wantErrOut: regexp.MustCompile(`joel.miller: authentication failed.*Logged in to github.com as.*tess`),
126133
wantErr: regexp.MustCompile(``),
127134
},
128135
{
129-
name: "bad token",
136+
name: "all good",
130137
opts: &StatusOptions{},
131138
cfg: func(c config.Config) {
132139
_ = c.Set("joel.miller", "oauth_token", "abc123")
133140
_ = c.Set("github.com", "oauth_token", "abc123")
134141
},
135142
httpStubs: func(reg *httpmock.Registry) {
136-
reg.Register(httpmock.REST("GET", "api/v3/"), httpmock.StatusStringResponse(400, "no bueno"))
143+
reg.Register(httpmock.REST("GET", "api/v3/"), httpmock.ScopesResponder("repo,read:org,"))
137144
reg.Register(httpmock.REST("GET", ""), httpmock.ScopesResponder("repo,read:org,"))
138145
reg.Register(
139146
httpmock.GraphQL(`query UserCurrent\b`),
140147
httpmock.StringResponse(`{"data":{"viewer":{"login":"tess"}}}`))
148+
reg.Register(
149+
httpmock.GraphQL(`query UserCurrent\b`),
150+
httpmock.StringResponse(`{"data":{"viewer":{"login":"tess"}}}`))
141151
},
142-
wantErrOut: regexp.MustCompile(`joel.miller: authentication failed.*Logged in to github.com as.*tess`),
143-
wantErr: regexp.MustCompile(``),
152+
wantErrOut: regexp.MustCompile(`(?s)Logged in to github.com as.*tess.*Logged in to joel.miller as.*tess`),
144153
},
145154
{
146-
name: "all good",
155+
name: "hide token",
147156
opts: &StatusOptions{},
148157
cfg: func(c config.Config) {
149158
_ = c.Set("joel.miller", "oauth_token", "abc123")
150-
_ = c.Set("github.com", "oauth_token", "abc123")
159+
_ = c.Set("github.com", "oauth_token", "xyz456")
151160
},
152161
httpStubs: func(reg *httpmock.Registry) {
153162
reg.Register(httpmock.REST("GET", "api/v3/"), httpmock.ScopesResponder("repo,read:org,"))
@@ -159,7 +168,28 @@ func Test_statusRun(t *testing.T) {
159168
httpmock.GraphQL(`query UserCurrent\b`),
160169
httpmock.StringResponse(`{"data":{"viewer":{"login":"tess"}}}`))
161170
},
162-
wantErrOut: regexp.MustCompile(`(?s)Logged in to github.com as.*tess.*Logged in to joel.miller as.*tess`),
171+
wantErrOut: regexp.MustCompile(`(?s)Token: \*{19}.*Token: \*{19}`),
172+
},
173+
{
174+
name: "show token",
175+
opts: &StatusOptions{
176+
ShowToken: true,
177+
},
178+
cfg: func(c config.Config) {
179+
_ = c.Set("joel.miller", "oauth_token", "abc123")
180+
_ = c.Set("github.com", "oauth_token", "xyz456")
181+
},
182+
httpStubs: func(reg *httpmock.Registry) {
183+
reg.Register(httpmock.REST("GET", "api/v3/"), httpmock.ScopesResponder("repo,read:org,"))
184+
reg.Register(httpmock.REST("GET", ""), httpmock.ScopesResponder("repo,read:org,"))
185+
reg.Register(
186+
httpmock.GraphQL(`query UserCurrent\b`),
187+
httpmock.StringResponse(`{"data":{"viewer":{"login":"tess"}}}`))
188+
reg.Register(
189+
httpmock.GraphQL(`query UserCurrent\b`),
190+
httpmock.StringResponse(`{"data":{"viewer":{"login":"tess"}}}`))
191+
},
192+
wantErrOut: regexp.MustCompile(`(?s)Token: xyz456.*Token: abc123`),
163193
},
164194
}
165195

0 commit comments

Comments
 (0)
X Tutup