forked from castle/castle-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
188 lines (165 loc) · 4.69 KB
/
client.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package castle
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
var castleReqsCounter = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: "iam",
Subsystem: "castle",
Name: "requests_total",
Help: "Number of requests made to castle",
}, []string{"endpoint", "status"})
var (
FilterEndpoint = "https://api.castle.io/v1/filter"
RiskEndpoint = "https://api.castle.io/v1/risk"
)
type APIError struct {
StatusCode int `json:"status_code"`
Message string `json:"message"`
}
func (e *APIError) Error() string {
return fmt.Sprintf("status code: %d, message: %s", e.StatusCode, e.Message)
}
// Castle encapsulates http client
type Castle struct {
client *http.Client
apiSecret string
metricsEnabled bool
}
// New creates a new castle client with default http client
func New(secret string, opts ...Opt) (*Castle, error) {
return NewWithHTTPClient(secret, http.DefaultClient, opts...)
}
// NewWithHTTPClient same as New but allows passing of http.Client with custom config
func NewWithHTTPClient(secret string, client *http.Client, opts ...Opt) (*Castle, error) {
os := &options{
metricsEnabled: true,
}
for _, opt := range opts {
opt(os)
}
return &Castle{
client: client,
apiSecret: secret,
metricsEnabled: os.metricsEnabled,
}, nil
}
// Filter sends a filter request to castle.io
// see https://reference.castle.io/#operation/filter for details
func (c *Castle) Filter(ctx context.Context, req *Request) (RecommendedAction, error) {
if req == nil {
return RecommendedActionNone, errors.New("request cannot be nil")
}
if req.Context == nil {
return RecommendedActionNone, errors.New("request.Context cannot be nil")
}
params := Params{
Email: req.User.Email,
Username: req.User.ID,
}
createdAt := req.CreatedAt
if createdAt.IsZero() {
createdAt = time.Now()
}
r := &castleFilterAPIRequest{
Type: req.Event.EventType,
Name: req.Event.Name,
Status: req.Event.EventStatus,
RequestToken: req.Context.RequestToken,
Params: params,
Context: req.Context,
Properties: req.Properties,
CreatedAt: createdAt,
}
return c.sendCall(ctx, r, FilterEndpoint)
}
// Risk sends a risk request to castle.io
// see https://reference.castle.io/#operation/risk for details
func (c *Castle) Risk(ctx context.Context, req *Request) (RecommendedAction, error) {
if req == nil {
return RecommendedActionNone, errors.New("request cannot be nil")
}
if req.Context == nil {
return RecommendedActionNone, errors.New("request.Context cannot be nil")
}
createdAt := req.CreatedAt
if createdAt.IsZero() {
createdAt = time.Now()
}
r := &castleRiskAPIRequest{
Type: req.Event.EventType,
Name: req.Event.Name,
Status: req.Event.EventStatus,
RequestToken: req.Context.RequestToken,
User: req.User,
Context: req.Context,
Properties: req.Properties,
CreatedAt: createdAt,
}
return c.sendCall(ctx, r, RiskEndpoint)
}
func (c *Castle) sendCall(ctx context.Context, r castleAPIRequest, url string) (_ RecommendedAction, err error) {
defer func() {
if !c.metricsEnabled {
return
}
status := "ok"
if err != nil {
status = "error"
}
castleReqsCounter.WithLabelValues(url, status).Inc()
}()
b := new(bytes.Buffer)
switch request := r.(type) {
case *castleFilterAPIRequest, *castleRiskAPIRequest:
err = json.NewEncoder(b).Encode(request)
default:
err = fmt.Errorf("incorrect request type passed as argument.")
}
if err != nil {
return RecommendedActionNone, err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, b)
if err != nil {
return RecommendedActionNone, err
}
req.SetBasicAuth("", c.apiSecret)
req.Header.Set("content-type", "application/json")
res, err := c.client.Do(req)
if err != nil {
return RecommendedActionNone, err
}
defer res.Body.Close() // nolint: gosec
if res.StatusCode != http.StatusCreated {
b, _ := io.ReadAll(res.Body) // nolint: errcheck
return RecommendedActionNone, &APIError{
StatusCode: res.StatusCode,
Message: string(b),
}
}
resp := &castleAPIResponse{}
if err = json.NewDecoder(res.Body).Decode(resp); err != nil {
return RecommendedActionNone, fmt.Errorf("unable to decode response body: %w", err)
}
return recommendedActionFromString(resp.Policy.Action), nil
}
func recommendedActionFromString(action string) RecommendedAction {
switch action {
case "allow":
return RecommendedActionAllow
case "deny":
return RecommendedActionDeny
case "challenge":
return RecommendedActionChallenge
default:
return RecommendedActionNone
}
}