Skip to content

uw-labs/go-castle.io

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-castle.io Build Status

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

Install

go get github.com/utilitywarehouse/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.EventLoginSucceeded,
		"md-1",
		map[string]string{"prop1": "propValue1"},
		map[string]string{"trait1": "traitValue1"},
		castleio.ContextFromRequest(req),
	)

Example

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)
	}))

}

Releases

No releases published

Packages

No packages published