-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbasic_auth.go
40 lines (36 loc) · 960 Bytes
/
basic_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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
)
type AuthenticationData struct {
Data struct {
Ticket string `json:"ticket"`
CSRFPreventionToken string `json:"CSRFPreventionToken"`
} `json:"data"`
}
var credentials AuthenticationData
func authenticate() AuthenticationData {
credentials = AuthenticationData{}
addr := fmt.Sprintf("%s/api2/json/access/ticket", pmURL)
data := url.Values{
"username": {pmUsername},
"password": {pmPassword},
}
requestBody := bytes.NewBufferString(data.Encode()).Bytes()
response, _ := http.Post(addr, "application/x-www-form-urlencoded", bytes.NewReader(requestBody))
if response.StatusCode > 299 {
log.Fatal(fmt.Errorf("authentication returned error %d", response.StatusCode))
}
responseBody, _ := ioutil.ReadAll(response.Body)
err := json.Unmarshal(responseBody, &credentials)
if err != nil {
log.Fatal(err)
}
return credentials
}