-
Notifications
You must be signed in to change notification settings - Fork 4
/
castle.go
285 lines (225 loc) · 8.19 KB
/
castle.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package castle
import (
"bytes"
"encoding/json"
"net/http"
"github.com/pkg/errors"
"github.com/tomasen/realip"
)
// TrackEndpoint defines the tracking URL castle.io side
var TrackEndpoint = "https://api.castle.io/v1/track"
// AuthenticateEndpoint defines the adaptive authentication URL castle.io side
var AuthenticateEndpoint = "https://api.castle.io/v1/authenticate"
// Event is an enum defining types of event castle tracks
type Event string
// See https://castle.io/docs/events
const (
EventLoginSucceeded Event = "$login.succeeded"
EventLoginFailed Event = "$login.failed"
EventPasswordResetRequestSucceeded Event = "$password_reset_request.succeeded"
EventPasswordResetRequestFailed Event = "$password_reset_request.failed"
EventPasswordResetSucceeded Event = "$password_reset.succeeded"
EventPasswordResetFailed Event = "$password_reset.failed"
EventIncidentMitigated Event = "$incident.mitigated"
EventReviewResolved Event = "$review.resolved"
EventReviewEscalated Event = "$review.escalated"
EventChallengeRequested Event = "$challenge.requested"
EventChallengeSucceeded Event = "$challenge.succeeded"
EventChallengeFailed Event = "$challenge.failed"
)
// AuthenticationRecommendedAction encapsulates the 3 possible responses from auth call (allow, challenge, deny)
type AuthenticationRecommendedAction string
// See https://castle.io/docs/authentication
const (
RecommendedActionNone AuthenticationRecommendedAction = ""
RecommendedActionAllow AuthenticationRecommendedAction = "allow"
RecommendedActionChallenge AuthenticationRecommendedAction = "challenge"
RecommendedActionDeny AuthenticationRecommendedAction = "deny"
)
// New creates a new castle client
func New(secret string) (*Castle, error) {
client := &http.Client{}
return NewWithHTTPClient(secret, client)
}
// HeaderAllowList keeps a list of headers that will be forwarded to castle
var HeaderAllowList = []string{
"Accept",
"Accept-Charset",
"Accept-Datetime",
"Accept-Encoding",
"Accept-Language",
"Cache-Control",
"Connection",
"Content-Length",
"Content-Type",
"Dnt",
"Host",
"Origin",
"Pragma",
"Referer",
"Sec-Fetch-Dest",
"Sec-Fetch-Mode",
"Sec-Fetch-Site",
"Sec-Fetch-User",
"Te",
"Upgrade-Insecure-Requests",
"User-Agent",
"X-Castle-Client-Id",
}
// NewWithHTTPClient same as New but allows passing of http.Client with custom config
func NewWithHTTPClient(secret string, client *http.Client) (*Castle, error) {
return &Castle{client: client, apiSecret: secret}, nil
}
// Castle encapsulates http client
type Castle struct {
client *http.Client
apiSecret string
}
// Context captures data from HTTP request
type Context struct {
ClientID string `json:"client_id"`
IP string `json:"ip"`
Headers map[string]string `json:"headers"`
}
func getClientID(r *http.Request) string {
var clientID string
// ClientID is __cid cookie or X-Castle-Client-Id header
cidCookie, _ := r.Cookie("__cid")
if cidCookie != nil {
clientID = cidCookie.Value
}
cidHeader := r.Header.Get("HTTP_X_CASTLE_CLIENT_ID")
if cidHeader != "" {
clientID = cidHeader
}
return clientID
}
func isHeaderAllowed(header string) bool {
for _, allowedHeader := range HeaderAllowList {
if header == http.CanonicalHeaderKey(allowedHeader) {
return true
}
}
return false
}
// ContextFromRequest builds castle context from current http.Request
func ContextFromRequest(r *http.Request) *Context {
headers := make(map[string]string)
for requestHeader := range r.Header {
if isHeaderAllowed(requestHeader) {
headers[requestHeader] = r.Header.Get(requestHeader)
}
}
return &Context{ClientID: getClientID(r), IP: realip.FromRequest(r), Headers: headers}
}
type castleAPIRequest struct {
Event Event `json:"event"`
UserID string `json:"user_id"`
Context *Context `json:"context"`
Properties map[string]string `json:"properties"`
UserTraits map[string]string `json:"user_traits"`
}
type castleAPIResponse struct {
Error string `json:"error"`
Type string `json:"type"`
Message string `json:"message"`
Action string `json:"action"`
UserID string `json:"user_id"`
DeviceToken string `json:"device_token"`
}
// Track sends a tracking request to castle.io
// see https://castle.io/docs/events for details
func (c *Castle) Track(event Event, userID string, properties map[string]string, userTraits map[string]string, context *Context) error {
e := &castleAPIRequest{Event: event, UserID: userID, Context: context, Properties: properties, UserTraits: userTraits}
return c.SendTrackCall(e)
}
// TrackSimple allows simple tracking of events into castle without specifying traits or properties
func (c *Castle) TrackSimple(event Event, userID string, context *Context) error {
EmptyMap := make(map[string]string)
e := &castleAPIRequest{Event: event, UserID: userID, Context: context, Properties: EmptyMap, UserTraits: EmptyMap}
return c.SendTrackCall(e)
}
// SendTrackCall is a plumbing method constructing the HTTP req/res and interpreting results
func (c *Castle) SendTrackCall(e *castleAPIRequest) error {
b := new(bytes.Buffer)
json.NewEncoder(b).Encode(e)
req, err := http.NewRequest(http.MethodPost, TrackEndpoint, b)
if err != nil {
return err
}
req.SetBasicAuth("", c.apiSecret)
req.Header.Set("content-type", "application/json")
res, err := c.client.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if expected, got := http.StatusNoContent, res.StatusCode; expected != got {
return errors.Errorf("expected %d status but got %d", expected, got)
}
resp := &castleAPIResponse{}
if resp.Error != "" {
// we have an api error
return errors.New(resp.Error)
}
json.NewDecoder(res.Body).Decode(resp)
return err
}
// Authenticate sends an authentication request to castle.io
// see https://castle.io/docs/authentication for details
func (c *Castle) Authenticate(event Event, userID string, properties map[string]string, userTraits map[string]string, context *Context) (AuthenticationRecommendedAction, error) {
e := &castleAPIRequest{Event: event, UserID: userID, Context: context, Properties: properties, UserTraits: userTraits}
return c.SendAuthenticateCall(e)
}
// AuthenticateSimple allows authenticate call into castle without specifying traits or properties
func (c *Castle) AuthenticateSimple(event Event, userID string, context *Context) (AuthenticationRecommendedAction, error) {
EmptyMap := make(map[string]string)
e := &castleAPIRequest{Event: event, UserID: userID, Context: context, Properties: EmptyMap, UserTraits: EmptyMap}
return c.SendAuthenticateCall(e)
}
func authenticationRecommendedActionFromString(action string) AuthenticationRecommendedAction {
switch action {
case "allow":
return RecommendedActionAllow
case "deny":
return RecommendedActionDeny
case "challenge":
return RecommendedActionChallenge
default:
return RecommendedActionNone
}
}
// SendAuthenticateCall is a plumbing method constructing the HTTP req/res and interpreting results
func (c *Castle) SendAuthenticateCall(e *castleAPIRequest) (AuthenticationRecommendedAction, error) {
b := new(bytes.Buffer)
json.NewEncoder(b).Encode(e)
req, err := http.NewRequest(http.MethodPost, AuthenticateEndpoint, 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()
if res.StatusCode != http.StatusCreated {
return RecommendedActionNone, errors.Errorf("expected 201 status but go %s", res.Status)
}
resp := &castleAPIResponse{}
json.NewDecoder(res.Body).Decode(resp)
if resp.Error != "" {
//we have an api error
return RecommendedActionNone, errors.New(resp.Error)
}
if resp.Type != "" {
//we have an api error
return RecommendedActionNone, errors.Errorf("%s: %s", resp.Type, resp.Message)
}
return authenticationRecommendedActionFromString(resp.Action), err
}
// WebhookBody encapsulates body of webhook notificationc coming from castle.io
// see https://castle.io/docs/webhooks
type WebhookBody struct {
}