Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update castle sdk remove deprecated fields #26

Merged
merged 9 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"io"
"net/http"
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
Expand Down Expand Up @@ -71,14 +72,19 @@ func (c *Castle) Filter(ctx context.Context, req *Request) (RecommendedAction, e
if req.Context == nil {
return RecommendedActionNone, errors.New("request.Context cannot be nil")
}
r := &castleAPIRequest{
user_params := UserParams{
pansbro12 marked this conversation as resolved.
Show resolved Hide resolved
Email: req.User.Email,
Username: req.User.Name,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the name necessarily the username? I wonder if we're reusing the name field in our generic request type in a way that wasn't intended by the underlying API, or are they actually the same values?

Copy link
Author

@pansbro12 pansbro12 Nov 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No so i actually was just thinking about this now, I remember us using either the account number or the email as username in a service somewhere but i can't think where.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really sure whether to include this now, looking at the code from the castle-processor the username is seems to be either the email or account number depending on what the user used to sign in. The username value is contained in the request properties so I've just set that as the username, might be that the username and the email are duplicates in some cases but I don't think that will be an issue.

}
r := &castleFilterAPIRequest{
Type: req.Event.EventType,
Name: req.Event.Name,
Status: req.Event.EventStatus,
RequestToken: req.Context.RequestToken,
User: req.User,
Params: user_params,
Context: req.Context,
Properties: req.Properties,
CreatedAt: time.Now(),
}
return c.sendCall(ctx, r, FilterEndpoint)
}
Expand All @@ -92,19 +98,20 @@ func (c *Castle) Risk(ctx context.Context, req *Request) (RecommendedAction, err
if req.Context == nil {
return RecommendedActionNone, errors.New("request.Context cannot be nil")
}
r := &castleAPIRequest{
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: time.Now(),
pansbro12 marked this conversation as resolved.
Show resolved Hide resolved
}
return c.sendCall(ctx, r, RiskEndpoint)
}

func (c *Castle) sendCall(ctx context.Context, r *castleAPIRequest, url string) (_ RecommendedAction, err error) {
func (c *Castle) sendCall(ctx context.Context, r castleAPIRequest, url string) (_ RecommendedAction, err error) {
defer func() {
if !c.metricsEnabled {
return
Expand All @@ -118,7 +125,13 @@ func (c *Castle) sendCall(ctx context.Context, r *castleAPIRequest, url string)
}()

b := new(bytes.Buffer)
err = json.NewEncoder(b).Encode(r)

switch request := r.(type) {
case *castleRiskAPIRequest:
err = json.NewEncoder(b).Encode(request)
case *castleFilterAPIRequest:
err = json.NewEncoder(b).Encode(request)
pansbro12 marked this conversation as resolved.
Show resolved Hide resolved
}
if err != nil {
return RecommendedActionNone, err
}
Expand Down
6 changes: 3 additions & 3 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func configureRequest(httpReq *http.Request) *castle.Request {
},
User: castle.User{
ID: "user-id",
Email: "[email protected]",
Traits: map[string]string{"trait1": "traitValue1"},
},
Properties: map[string]string{"prop1": "propValue1"},
Expand Down Expand Up @@ -150,7 +151,7 @@ func TestCastle_Filter(t *testing.T) {
Type castle.EventType `json:"type"`
Status castle.EventStatus `json:"status"`
RequestToken string `json:"request_token"`
User castle.User `json:"user"`
Params castle.UserParams `json:"params"`
Context *castle.Context `json:"context"`
Properties map[string]string `json:"properties"`
}
Expand All @@ -168,9 +169,8 @@ func TestCastle_Filter(t *testing.T) {

assert.Equal(t, castle.EventTypeLogin, reqData.Type)
assert.Equal(t, castle.EventStatusSucceeded, reqData.Status)
assert.Equal(t, "user-id", reqData.User.ID)
assert.Equal(t, "user@test.com", reqData.Params.Email)
assert.Equal(t, map[string]string{"prop1": "propValue1"}, reqData.Properties)
assert.Equal(t, map[string]string{"trait1": "traitValue1"}, reqData.User.Traits)
assert.Equal(t, castle.FromHTTPRequest(httpReq), reqData.Context)

executed = true
Expand Down
34 changes: 33 additions & 1 deletion model.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package castle

import "time"

type Event struct {
EventType EventType
EventStatus EventStatus
Expand Down Expand Up @@ -68,14 +70,44 @@ type User struct {
Traits map[string]string `json:"traits,omitempty"`
}

type castleAPIRequest struct {
type UserParams struct {
pansbro12 marked this conversation as resolved.
Show resolved Hide resolved
Email string `json:"email,omitempty"`
Phone string `json:"phone,omitempty"`
Username string `json:"username,omitempty"`
}

type castleAPIRequest interface {
GetEventType() EventType
}

type castleFilterAPIRequest struct {
Type EventType `json:"type"`
Name string `json:"name,omitempty"`
Status EventStatus `json:"status"`
RequestToken string `json:"request_token"`
Params UserParams `json:"params"`
Context *Context `json:"context"`
Properties map[string]string `json:"properties,omitempty"`
CreatedAt time.Time `json:"created_at"`
}

func (r *castleFilterAPIRequest) GetEventType() EventType {
return r.Type
}

type castleRiskAPIRequest struct {
Type EventType `json:"type"`
Name string `json:"name,omitempty"`
Status EventStatus `json:"status"`
RequestToken string `json:"request_token"`
User User `json:"user"`
Context *Context `json:"context"`
Properties map[string]string `json:"properties,omitempty"`
CreatedAt time.Time `json:"created_at"`
}

func (r *castleRiskAPIRequest) GetEventType() EventType {
return r.Type
}

type castleAPIResponse struct {
Expand Down
Loading