-
Notifications
You must be signed in to change notification settings - Fork 4
/
access.go
80 lines (68 loc) · 2.43 KB
/
access.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
72
73
74
75
76
77
78
79
80
package goqradar
import (
"context"
"encoding/json"
"fmt"
"net/http"
)
//------------------------------------------------------------------------------
// Structures
//------------------------------------------------------------------------------
// LoginAttempt is a login attempt
type LoginAttempt struct {
AttemptResult string `json:"attempt_result"`
AttemptTime int `json:"attempt_time"`
RemoteIP string `json:"remote_ip"`
UserID int `json:"user_id"`
AttemptMethod string `json:"attempt_method"`
}
// LoginAttemptPaginatedResponse is the paginated response.
type LoginAttemptPaginatedResponse struct {
Total int `json:"total"`
Min int `json:"min"`
Max int `json:"max"`
LoginAttempts []*LoginAttempt `json:"offense_types"`
}
//------------------------------------------------------------------------------
// Functions
//------------------------------------------------------------------------------
// ListAccessAttempts returns the list of the login attempts with given fields, filters and sort.
func (endpoint *Endpoint) ListAccessAttempts(ctx context.Context, fields, filter, sort string, min, max int) (*LoginAttemptPaginatedResponse, error) {
// Options
options := []Option{}
if fields != "" {
options = append(options, WithParam("fields", fields))
}
if filter != "" {
options = append(options, WithParam("filter", filter))
}
if sort != "" {
options = append(options, WithParam("sort", sort))
}
options = append(options, WithHeader("Range", fmt.Sprintf("items=%d-%d", min, max)))
// Do the request
resp, err := endpoint.client.do(ctx, http.MethodGet, "/access/login_attempts", options...)
if err != nil {
return nil, fmt.Errorf("error while calling the endpoint: %s", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("error with the status code: %d", resp.StatusCode)
}
// Process the Content-Range
min, max, total, err := parseContentRange(resp.Header.Get("Content-Range"))
if err != nil {
return nil, fmt.Errorf("error while parsing the content-range [%s]: %s", resp.Header.Get("Content-Range"), err)
}
// Prepare the response
response := &LoginAttemptPaginatedResponse{
Total: total,
Min: min,
Max: max,
}
// Decode the response
err = json.NewDecoder(resp.Body).Decode(&response.LoginAttempts)
if err != nil {
return nil, fmt.Errorf("error while decoding the response: %s", err)
}
return response, nil
}