From 21add544dc9677fb0f72e9876700eaa600fbb30e Mon Sep 17 00:00:00 2001 From: Cory Bennett Date: Sun, 27 Aug 2017 13:55:48 -0700 Subject: [PATCH] automatically login when anonymous user detected --- cmd/jira/main.go | 11 +++++++++++ jiracli/login.go | 27 ++++++++++++++++----------- jiracli/logout.go | 2 +- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/cmd/jira/main.go b/cmd/jira/main.go index b74dcd6..bb48c20 100644 --- a/cmd/jira/main.go +++ b/cmd/jira/main.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "net/http" "os" "path/filepath" "runtime/debug" @@ -70,6 +71,16 @@ func main() { fig.ConfigDir = ".jira.d" o := oreo.New().WithCookieFile(filepath.Join(jiracli.Homedir(), fig.ConfigDir, "cookies.js")) + o = o.WithPostCallback( + func(req *http.Request, resp *http.Response) (*http.Response, error) { + if resp.Header.Get("X-Ausername") == "anonymous" { + // we are not logged in, so force login now by running the "login" command + app.Parse([]string{"login"}) + return o.Do(req) + } + return resp, nil + }, + ) registry := []jiracli.CommandRegistry{ jiracli.CommandRegistry{ diff --git a/jiracli/login.go b/jiracli/login.go index ef7c980..5fcfa9b 100644 --- a/jiracli/login.go +++ b/jiracli/login.go @@ -45,18 +45,23 @@ func authCallback(req *http.Request, resp *http.Response) (*http.Response, error // CmdLogin will attempt to login into jira server func CmdLogin(o *oreo.Client, opts *GlobalOptions) error { - if session, err := jira.GetSession(o, opts.Endpoint.Value); err != nil { - ua := o.WithoutRedirect().WithRetries(0).WithPostCallback(authCallback) - // No active session so try to create a new one - _, err := jira.NewSession(ua, opts.Endpoint.Value, opts) - if err != nil { - // reset password on failed session - opts.SetPass("") - return err + ua := o.WithoutRedirect().WithRetries(0).WithoutCallbacks().WithPostCallback(authCallback) + for { + if session, err := jira.GetSession(o, opts.Endpoint.Value); err != nil { + // No active session so try to create a new one + _, err := jira.NewSession(ua, opts.Endpoint.Value, opts) + if err != nil { + // reset password on failed session + opts.SetPass("") + log.Errorf("%s", err) + continue + } + fmt.Println(ansi.Color("OK", "green"), "New session for", opts.User) + break + } else { + fmt.Println(ansi.Color("OK", "green"), "Found session for", session.Name) + break } - fmt.Println(ansi.Color("OK", "green"), "New session for", opts.User) - } else { - fmt.Println(ansi.Color("OK", "green"), "Found session for", session.Name) } return nil } diff --git a/jiracli/logout.go b/jiracli/logout.go index 0aa2a6d..fa93151 100644 --- a/jiracli/logout.go +++ b/jiracli/logout.go @@ -26,7 +26,7 @@ func CmdLogoutRegistry(fig *figtree.FigTree, o *oreo.Client) *CommandRegistryEnt // CmdLogout will attempt to terminate an active Jira session func CmdLogout(o *oreo.Client, opts *GlobalOptions) error { - ua := o.WithoutRedirect().WithRetries(0) + ua := o.WithoutRedirect().WithRetries(0).WithoutCallbacks() err := jira.DeleteSession(ua, opts.Endpoint.Value) if err == nil { fmt.Println(ansi.Color("OK", "green"), "Terminated session for", opts.User)