Skip to content

About

https://castle.io integration library

Resources

Stars

3 stars

Watchers

5 watching

Forks

Latest commit

 

History

28 Commits

Folders and files

Repository files navigation

go-castle.io CI

go-castle.io is a go library wrapping https://castle.io API.

Install

go get github.com/uw-labs/go-castle.io

Usage

Providing own http client

castleio.NewWithHTTPClient("secret-api-key", &http.Client{Timeout: time.Second * 2})

Tracking properties and traits

castle.Track(
		castleio.EventLoginSucceeded,
		"user-123",
		map[string]string{"prop1": "propValue1"},
		map[string]string{"trait1": "traitValue1"},
		castleio.ContextFromRequest(req),
	)

Tracking custom events

castle.Track(
		castleio.Event("custom-event"),
		"user-123",
		map[string]string{"prop1": "propValue1"},
		map[string]string{"trait1": "traitValue1"},
		castleio.ContextFromRequest(req),
	)

Adaptive authentication

decision, err := castle.Authenticate(
		castleio.CastleAPIRequest{
			Event:      castleio.EventLoginSucceeded,
			UserID:     "user-123",
			Properties: map[string]string{"prop1": "propValue1"},
			UserTraits: map[string]string{"trait1": "traitValue1"},
			Context:    castleio.ContextFromRequest(req),
		},
	)

Example

package main

import (
	"github.com/uw-labs/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)
	}))

}

About

https://castle.io integration library

Resources

Stars

3 stars

Watchers

5 watching

Forks

Releases

Packages

Contributors

Languages