Skip to content

Commit b3723c7

Browse files
author
Andrew Somerville
committed
wrap token comparisons in a function, proliferate it virtually everywhere apk-token was, and fix some related tab/space issues
1 parent ccb5fad commit b3723c7

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

jiracli/cli.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ const (
5555
)
5656

5757
type GlobalOptions struct {
58-
// AuthenticationMethod is the method we use to authenticate with the jira serivce. Possible values are "api-token" or "session".
58+
// AuthenticationMethod is the method we use to authenticate with the jira serivce.
59+
// Possible values are "api-token", "bearer-token" or "session".
5960
// The default is "api-token" when the service endpoint ends with "atlassian.net", otherwise it "session". Session authentication
6061
// will promt for user password and use the /auth/1/session-login endpoint.
6162
AuthenticationMethod figtree.StringOption `yaml:"authentication-method,omitempty" json:"authentication-method,omitempty"`
@@ -154,6 +155,10 @@ func (o *GlobalOptions) AuthMethod() string {
154155
return o.AuthenticationMethod.Value
155156
}
156157

158+
func (o *GlobalOptions) AuthMethodIsToken() bool{
159+
return o.AuthMethod() == "api-token" || o.AuthMethod() == "bearer-token";
160+
}
161+
157162
func register(app *kingpin.Application, o *oreo.Client, fig *figtree.FigTree) {
158163
globals := GlobalOptions{
159164
User: figtree.NewStringOption(os.Getenv("USER")),
@@ -177,7 +182,7 @@ func register(app *kingpin.Application, o *oreo.Client, fig *figtree.FigTree) {
177182
token := globals.GetPass()
178183
authHeader := fmt.Sprintf("Bearer %s", token)
179184
req.Header.Add("Authorization", authHeader)
180-
}
185+
}
181186
return req, nil
182187
})
183188

@@ -198,7 +203,7 @@ func register(app *kingpin.Application, o *oreo.Client, fig *figtree.FigTree) {
198203
// rerun the original request
199204
return o.Do(req)
200205
}
201-
} else if globals.AuthMethod() == "api-token" && resp.StatusCode == 401 {
206+
} else if globals.AuthMethodIsToken() && resp.StatusCode == 401 {
202207
globals.SetPass("")
203208
return o.Do(req)
204209
}
@@ -236,7 +241,7 @@ func register(app *kingpin.Application, o *oreo.Client, fig *figtree.FigTree) {
236241
} else if globals.SocksProxy.Value != "" {
237242
o = o.WithTransport(socksProxy(globals.SocksProxy.Value))
238243
}
239-
if globals.AuthMethod() == "api-token" {
244+
if globals.AuthMethodIsToken() {
240245
o = o.WithCookieFile("")
241246
}
242247
if globals.Login.Value == "" {

jiracli/password.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func (o *GlobalOptions) ProvideAuthParams() *jiradata.AuthParams {
2121

2222
func (o *GlobalOptions) keyName() string {
2323
user := o.Login.Value
24-
if o.AuthMethod() == "api-token" {
24+
if o.AuthMethodIsToken() {
2525
user = "api-token:" + user
2626
}
2727

@@ -133,14 +133,14 @@ func (o *GlobalOptions) GetPass() string {
133133
return o.cachedPassword
134134
}
135135

136-
if o.cachedPassword = os.Getenv("JIRA_API_TOKEN"); o.cachedPassword != "" && o.AuthMethod() == "api-token" {
136+
if o.cachedPassword = os.Getenv("JIRA_API_TOKEN"); o.cachedPassword != "" && o.AuthMethodIsToken() {
137137
return o.cachedPassword
138138
}
139139

140140
prompt := fmt.Sprintf("Jira Password [%s]: ", o.Login)
141141
help := ""
142142

143-
if o.AuthMethod() == "api-token" {
143+
if o.AuthMethodIsToken() {
144144
prompt = fmt.Sprintf("Jira API-Token [%s]: ", o.Login)
145145
help = "API Tokens may be required by your Jira service endpoint: https://developer.atlassian.com/cloud/jira/platform/deprecation-notice-basic-auth-and-cookie-based-auth/"
146146
}

jiracmd/logout.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func CmdLogoutRegistry() *jiracli.CommandRegistryEntry {
3030

3131
// CmdLogout will attempt to terminate an active Jira session
3232
func CmdLogout(o *oreo.Client, globals *jiracli.GlobalOptions, opts *jiracli.CommonOptions) error {
33-
if (globals.AuthMethod() == "api-token" || globals.AuthMethod() == "bearer-token") {
33+
if globals.AuthMethodIsToken() {
3434
log.Noticef("No need to logout when using api-token or bearer-token authentication method")
3535
if globals.GetPass() != "" && terminal.IsTerminal(int(os.Stdin.Fd())) && terminal.IsTerminal(int(os.Stdout.Fd())) {
3636
delete := false

0 commit comments

Comments
 (0)