Git Product home page Git Product logo

castle-go's Introduction

castle-go

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

Note: This library is currently a prototype. To see fully supported SDKs, please refer to https://docs.castle.io/baseline/

Install

go get github.com/castle/castle-go

Usage

Providing own http client

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

Tracking properties and traits

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

Tracking custom events

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

Adaptive authentication

decision, err := castle.Authenticate(
  castle.EventLoginSucceeded,
  "md-1",
  map[string]string{"prop1": "propValue1"},
  map[string]string{"trait1": "traitValue1"},
  castle.ContextFromRequest(req),
)

Example

package main

import (
  "net/http"
  "log"
  "github.com/castle/castle-go/castle"
)

func main() {

	cstl, err := castle.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(
			castle.EventLoginSucceeded,
			"user-123",
			castle.ContextFromRequest(r),
		)

		if err != nil {
			log.Println(err)
		}

		if decision == castle.RecommendedActionChallenge {
			// challenge with MFA and track with castle

			err := cstl.TrackSimple(
				castle.EventChallengeRequested,
				"user-123",
				castle.ContextFromRequest(r),
			)

			if err != nil {
				log.Println(err)
			}

			// trigger off MFA path
		}

		w.WriteHeader(http.StatusNoContent)
	}))

}

castle-go's People

Contributors

michael-donat avatar h3xar0n avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.