automatically login when anonymous user detected

This commit is contained in:
Cory Bennett
2017-08-27 13:55:48 -07:00
parent 1f345cedee
commit 21add544dc
3 changed files with 28 additions and 12 deletions
+11
View File
@@ -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{
+16 -11
View File
@@ -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
}
+1 -1
View File
@@ -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)