go-castle.io is a go library wrapping https://castle.io API.
go get github.com/utilitywarehouse/go-castle.io
castleio.NewWithHTTPClient("secret-api-key", &http.Client{Timeout: time.Second * 2})
castle.Track(
castleio.EventLoginSucceeded,
"user-123",
map[string]string{"prop1": "propValue1"},
map[string]string{"trait1": "traitValue1"},
castleio.ContextFromRequest(req),
)
castle.Track(
castleio.Event("custom-event"),
"user-123",
map[string]string{"prop1": "propValue1"},
map[string]string{"trait1": "traitValue1"},
castleio.ContextFromRequest(req),
)
decision, err := castle.Authenticate(
castleio.EventLoginSucceeded,
"md-1",
map[string]string{"prop1": "propValue1"},
map[string]string{"trait1": "traitValue1"},
castleio.ContextFromRequest(req),
)
package main
import (
"github.com/utilitywarehouse/go-castle.io/castleio"
"net/http"
"log"
)
func main() {
castle, err := castleio.New("secret-api-key")
if err != nil {
log.Fatal(err)
}
http.ListenAndServe(":8080", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// authenticate user then track with castle
decision, err := castle.AuthenticateSimple(
castleio.EventLoginSucceeded,
"user-123",
castleio.ContextFromRequest(r),
)
if err != nil {
log.Println(err)
}
if decision == castleio.RecommendedActionChallenge {
// challenge with MFA and track with castle
err := castle.TrackSimple(
castleio.EventChallengeRequested,
"user-123",
castleio.ContextFromRequest(r),
)
if err != nil {
log.Println(err)
}
// trigger off MFA path
}
w.WriteHeader(http.StatusNoContent)
}))
}