Skip to content

Commit

Permalink
Split logic into two files
Browse files Browse the repository at this point in the history
  • Loading branch information
Nesze committed Apr 17, 2024
1 parent dd57394 commit 029ecc3
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 88 deletions.
88 changes: 0 additions & 88 deletions castle.go → client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,47 +16,6 @@ var FilterEndpoint = "https://api.castle.io/v1/filter"
// RiskEndpoint defines the risk URL castle.io side
var RiskEndpoint = "https://api.castle.io/v1/risk"

type Event struct {
EventType EventType
EventStatus EventStatus
}

// EventType is an enum defining types of event castle tracks
type EventType string

// See https://docs.castle.io/docs/events
const (
EventTypeLogin EventType = "$login"
EventTypeRegistration EventType = "$registration"
EventTypeProfileUpdate EventType = "$profile_update"
EventTypeProfileReset EventType = "$profile_reset"
EventTypePasswordResetRequest EventType = "$password_reset_request"
EventTypeChallenge EventType = "$challenge"
EventTypeLogout EventType = "&logout"
)

// EventStatus is an enum defining the statuses for a given event.
type EventStatus string

// See https://docs.castle.io/docs/events
const (
EventStatusAttempted EventStatus = "$attempted"
EventStatusSucceeded EventStatus = "$succeeded"
EventStatusFailed EventStatus = "$failed"
EventStatusRequested EventStatus = "$requested"
)

// RecommendedAction encapsulates the 3 possible responses from auth call (allow, challenge, deny)
type RecommendedAction string

// See https://castle.io/docs/authentication
const (
RecommendedActionNone RecommendedAction = ""
RecommendedActionAllow RecommendedAction = "allow"
RecommendedActionChallenge RecommendedAction = "challenge"
RecommendedActionDeny RecommendedAction = "deny"
)

// New creates a new castle client
func New(secret string) (*Castle, error) {
client := &http.Client{}
Expand All @@ -75,53 +34,6 @@ type Castle struct {
apiSecret string
}

// Context captures data from HTTP request
type Context struct {
IP string `json:"ip"`
Headers map[string]string `json:"headers"`
RequestToken string `json:"request_token"`
}

type Request struct {
Context *Context
Event Event
User User
Properties map[string]string
}

type User struct {
ID string `json:"id"`
Email string `json:"email,omitempty"`
Phone string `json:"phone,omitempty"`
Name string `json:"name,omitempty"`
RegisteredAt string `json:"registered_at,omitempty"`
Traits map[string]string `json:"traits,omitempty"`
}

type castleAPIRequest struct {
Type EventType `json:"type"`
Status EventStatus `json:"status"`
RequestToken string `json:"request_token"`
User User `json:"user"`
Context *Context `json:"context"`
Properties map[string]string `json:"properties,omitempty"`
}

type castleAPIResponse struct {
Type string `json:"type"`
Message string `json:"message"`
Risk float32 `json:"risk"`
Policy struct {
Name string `json:"name"`
ID string `json:"id"`
RevisionID string `json:"revision_id"`
Action string `json:"action"`
} `json:"policy"`
Device struct {
Token string `json:"token"`
} `json:"device"`
}

// 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) {
Expand Down
File renamed without changes.
90 changes: 90 additions & 0 deletions model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package castle

type Event struct {
EventType EventType
EventStatus EventStatus
}

// EventType is an enum defining types of event castle tracks.
type EventType string

// See https://docs.castle.io/docs/events
const (
EventTypeLogin EventType = "$login"
EventTypeRegistration EventType = "$registration"
EventTypeProfileUpdate EventType = "$profile_update"
EventTypeProfileReset EventType = "$profile_reset"
EventTypePasswordResetRequest EventType = "$password_reset_request"
EventTypeChallenge EventType = "$challenge"
EventTypeLogout EventType = "&logout"
)

// EventStatus is an enum defining the statuses for a given event.
type EventStatus string

// See https://docs.castle.io/docs/events
const (
EventStatusAttempted EventStatus = "$attempted"
EventStatusSucceeded EventStatus = "$succeeded"
EventStatusFailed EventStatus = "$failed"
EventStatusRequested EventStatus = "$requested"
)

// RecommendedAction encapsulates the 3 possible responses from auth call (allow, challenge, deny).
type RecommendedAction string

// See https://castle.io/docs/authentication
const (
RecommendedActionNone RecommendedAction = ""
RecommendedActionAllow RecommendedAction = "allow"
RecommendedActionChallenge RecommendedAction = "challenge"
RecommendedActionDeny RecommendedAction = "deny"
)

// Request wraps the Castle required data for to the pkg's Risk and Filter methods.
type Request struct {
Context *Context
Event Event
User User
Properties map[string]string
}

// Context captures data from HTTP request.
type Context struct {
IP string `json:"ip"`
Headers map[string]string `json:"headers"`
RequestToken string `json:"request_token"`
}

type User struct {
ID string `json:"id"`
Email string `json:"email,omitempty"`
Phone string `json:"phone,omitempty"`
Name string `json:"name,omitempty"`
RegisteredAt string `json:"registered_at,omitempty"`
Traits map[string]string `json:"traits,omitempty"`
}

type castleAPIRequest struct {
Type EventType `json:"type"`
Status EventStatus `json:"status"`
RequestToken string `json:"request_token"`
User User `json:"user"`
Context *Context `json:"context"`
Properties map[string]string `json:"properties,omitempty"`
}

type castleAPIResponse struct {
Type string `json:"type"`
Message string `json:"message"`
Risk float32 `json:"risk"`
Policy struct {
Name string `json:"name"`
ID string `json:"id"`
RevisionID string `json:"revision_id"`
Action string `json:"action"`
} `json:"policy"`
Device struct {
Token string `json:"token"`
} `json:"device"`
}

0 comments on commit 029ecc3

Please sign in to comment.