mirror of
https://github.com/Threnklyn/jira.git
synced 2026-05-19 04:33:28 +02:00
automatically login when anonymous user detected
This commit is contained in:
@@ -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
@@ -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
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user