shithub: jirafs

Download patch

ref: e01deb2174a3dad91e6d6dbe0946036a0c0da228
parent: de8eb3b936d18a9214c4ec10fbedc7da40441aaa
author: Romano <dromano@intellisurvey.com>
date: Tue Feb 12 16:42:23 EST 2019

support basic authentication and remove cookie-based authentication

--- a/client.go
+++ b/client.go
@@ -7,8 +7,8 @@
 	"encoding/json"
 	"encoding/pem"
 	"fmt"
-	"io"
-	"io/ioutil"
+    "io"
+    "io/ioutil"
 	"net/http"
 	"net/url"
 
@@ -20,8 +20,7 @@
 
 	user, pass              string
 	jiraURL                 *url.URL
-	cookies                 []*http.Cookie
-	alwaysLogin, usingOAuth bool
+	usingOAuth bool
 
 	maxlisting int
 }
@@ -42,7 +41,7 @@
 		return err
 	}
 
-	var b io.Reader
+    var b io.Reader
 	switch x := body.(type) {
 	case nil:
 	case []byte:
@@ -65,16 +64,10 @@
 	}
 	req.Header.Set("X-Atlassian-Token", "nocheck")
 
-	if c.alwaysLogin && !c.usingOAuth {
-		if err := c.AcquireSessionCookie(c.user, c.pass); err != nil {
-			return err
-		}
+	if !c.usingOAuth {
+		req.SetBasicAuth(c.user, c.pass);
 	}
 
-	for _, cookie := range c.cookies {
-		req.AddCookie(cookie)
-	}
-
 	resp, err := c.Client.Do(req)
 	if err != nil {
 		return err
@@ -103,54 +96,6 @@
 
 	return nil
 
-}
-
-func (c *Client) AcquireSessionCookie(username, password string) error {
-	url, err := c.jiraURL.Parse("/rest/auth/1/session")
-	if err != nil {
-		return err
-	}
-
-	body := struct {
-		Username string `json:"username"`
-		Password string `json:"password"`
-	}{username, password}
-	b, err := json.Marshal(body)
-	if err != nil {
-		return err
-	}
-
-	req, err := http.NewRequest("POST", url.String(), bytes.NewReader(b))
-	if err != nil {
-		return err
-	}
-	req.Header.Set("Content-Type", "application/json")
-
-	resp, err := c.Client.Do(req)
-	if _, err := ioutil.ReadAll(resp.Body); err != nil {
-		return err
-	}
-	resp.Body.Close()
-	c.cookies = resp.Cookies()
-
-	if err != nil {
-		return fmt.Errorf("Auth at JIRA instance failed (HTTP(S) request). %s", err)
-	}
-	if resp != nil && resp.StatusCode != 200 {
-		return fmt.Errorf("Auth at JIRA instance failed (HTTP(S) request). Status code: %d", resp.StatusCode)
-	}
-
-	return nil
-}
-
-func (c *Client) login() error {
-	if c.usingOAuth {
-		return nil
-	}
-	if err := c.AcquireSessionCookie(c.user, c.pass); err != nil {
-		return fmt.Errorf("Could not authenticate to JIRA: %v\n", err)
-	}
-	return nil
 }
 
 func (c *Client) oauth(consumerKey, privateKeyFile string) error {
--- a/jira.go
+++ b/jira.go
@@ -36,7 +36,7 @@
 		t := time.Duration(w.TimeSpentSeconds) * time.Second
 		sf.SetContent([]byte(t.String() + "\n"))
 	case "started":
-		sf.SetContent([]byte(time.Time(w.Started).String() + "\n"))
+		sf.SetContent([]byte(time.Time(*w.Started).String() + "\n"))
 	default:
 		return nil, nil
 	}
@@ -891,7 +891,7 @@
 					jc.user = args[0]
 					jc.pass = args[1]
 				}
-				return jc.login()
+				return nil
 			},
 			"set": func(args []string) error {
 				if len(args) != 2 {
--- a/main.go
+++ b/main.go
@@ -6,7 +6,6 @@
 	"net"
 	"net/http"
 	"net/url"
-	"time"
 
 	"github.com/howeyc/gopass"
 	"github.com/joushou/qp"
@@ -20,8 +19,6 @@
 	pkey        = flag.String("pkey", "", "private key file for OAuth")
 	pass        = flag.Bool("pass", false, "use password for authorization")
 	jiraURLStr  = flag.String("url", "", "jira URL")
-	loginInt    = flag.Int("loginint", 5, "login interval in minutes - 0 disables automatic relogin (password auth only)")
-	alwaysLogin = flag.Bool("alwayslogin", false, "log in on all requests (password auth only)")
 	maxlisting  = flag.Int("maxlisting", 100, "max directory listing length")
 )
 
@@ -36,7 +33,6 @@
 
 	client := &Client{
 		Client:      &http.Client{},
-		alwaysLogin: *alwaysLogin,
 		usingOAuth:  *usingOAuth,
 		jiraURL:     jiraURL,
 		maxlisting:  *maxlisting,
@@ -57,16 +53,6 @@
 
 			client.user = username
 			client.pass = string(password)
-			client.login()
-
-			if *loginInt > 0 {
-				go func() {
-					t := time.NewTicker(time.Duration(*loginInt) * time.Minute)
-					for range t.C {
-						client.login()
-					}
-				}()
-			}
 		} else {
 			fmt.Printf("Continuing without authentication.\n")
 		}