mirror of
https://github.com/Threnklyn/jira.git
synced 2026-06-07 13:33:32 +02:00
automatically login when anonymous user detected
This commit is contained in:
@@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
@@ -70,6 +71,16 @@ func main() {
|
|||||||
fig.ConfigDir = ".jira.d"
|
fig.ConfigDir = ".jira.d"
|
||||||
|
|
||||||
o := oreo.New().WithCookieFile(filepath.Join(jiracli.Homedir(), fig.ConfigDir, "cookies.js"))
|
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{
|
registry := []jiracli.CommandRegistry{
|
||||||
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
|
// CmdLogin will attempt to login into jira server
|
||||||
func CmdLogin(o *oreo.Client, opts *GlobalOptions) error {
|
func CmdLogin(o *oreo.Client, opts *GlobalOptions) error {
|
||||||
if session, err := jira.GetSession(o, opts.Endpoint.Value); err != nil {
|
ua := o.WithoutRedirect().WithRetries(0).WithoutCallbacks().WithPostCallback(authCallback)
|
||||||
ua := o.WithoutRedirect().WithRetries(0).WithPostCallback(authCallback)
|
for {
|
||||||
// No active session so try to create a new one
|
if session, err := jira.GetSession(o, opts.Endpoint.Value); err != nil {
|
||||||
_, err := jira.NewSession(ua, opts.Endpoint.Value, opts)
|
// No active session so try to create a new one
|
||||||
if err != nil {
|
_, err := jira.NewSession(ua, opts.Endpoint.Value, opts)
|
||||||
// reset password on failed session
|
if err != nil {
|
||||||
opts.SetPass("")
|
// reset password on failed session
|
||||||
return err
|
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
|
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
|
// CmdLogout will attempt to terminate an active Jira session
|
||||||
func CmdLogout(o *oreo.Client, opts *GlobalOptions) error {
|
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)
|
err := jira.DeleteSession(ua, opts.Endpoint.Value)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
fmt.Println(ansi.Color("OK", "green"), "Terminated session for", opts.User)
|
fmt.Println(ansi.Color("OK", "green"), "Terminated session for", opts.User)
|
||||||
|
|||||||
Reference in New Issue
Block a user