-
Notifications
You must be signed in to change notification settings - Fork 2
/
auth.go
56 lines (47 loc) · 1.14 KB
/
auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package common
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/http/cookiejar"
cli "gopkg.in/urfave/cli.v1"
)
type auth struct {
Username string `json:"username"`
Password string `json:"password"`
}
// AuthUser authenticates user with provided username/pw in JIRA using basicauth
func AuthUser(c *cli.Context) (http.Client, string, error) {
jar, _ := cookiejar.New(nil)
client := http.Client{Jar: jar}
user, pw, host, err := defineCredentials(c)
if err != nil {
return client, host, err
}
auth, _ := json.Marshal(auth{
Username: user,
Password: pw,
})
resp, _ := client.Post(
host+"/rest/auth/1/session",
"application/json",
bytes.NewBuffer(auth),
)
b, _ := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if resp.StatusCode != 200 {
return client, host, errors.New(string(b))
}
return client, host, nil
}
func defineCredentials(c *cli.Context) (string, string, string, error) {
host := fmt.Sprintf("https://%s.atlassian.net", c.GlobalString("domain"))
_, err := http.Get(host)
if err != nil {
return "", "", "", err
}
return c.GlobalString("username"), c.GlobalString("password"), host, nil
}