-
Notifications
You must be signed in to change notification settings - Fork 4
/
auth.go
71 lines (59 loc) · 1.86 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package goqradar
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
)
//------------------------------------------------------------------------------
// Structures
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Functions
//------------------------------------------------------------------------------
// Logout by name
func (endpoint *Endpoint) Logout(ctx context.Context, user string) (bool, error) {
// Prepare the URL
var reqURL *url.URL
reqURL, err := url.Parse(endpoint.client.BaseURL)
if err != nil {
return false, fmt.Errorf("Error while parsing the URL : %s", err)
}
reqURL.Path += "/auth/logout"
// Create the data
d, err := json.Marshal(user)
if err != nil {
return false, fmt.Errorf("Error while marshalling the values : %s", err)
}
// Create the request
req, err := http.NewRequest("POST", reqURL.String(), bytes.NewBuffer(d))
if err != nil {
return false, fmt.Errorf("Error while creating the request : %s", err)
}
// Set HTTP headers
req.Header.Set("SEC", endpoint.client.Token)
req.Header.Set("Version", endpoint.client.Version)
req.Header.Set("Content-Type", "application/json")
// Do the request
resp, err := endpoint.client.client.Do(req)
if err != nil {
return false, fmt.Errorf("Error while doing the request : %s", err)
}
defer resp.Body.Close()
// Read the respsonse
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return false, fmt.Errorf("error while reading the request : %s", err)
}
// Prepare the response
var response bool
// Unmarshal the response
err = json.Unmarshal([]byte(body), &response)
if err != nil {
return false, fmt.Errorf("Error while unmarshalling the response : %s. HTTP response is : %s", err, string(body))
}
return response, nil
}