Skip to content

Latest commit

 

History

History
89 lines (72 loc) · 2.1 KB

README.md

File metadata and controls

89 lines (72 loc) · 2.1 KB

gatekeeper

GoDoc Go Report Card License

Hydra middleware for golang web frameworks. Inspired by gin-hydra, it current has support for gin, echo, and goa. More framework support is planned.

Install

go get -u github.com/otraore/gatekeeper

Gin

import (
    "github.com/gin-gonic/gin"
    "github.com/ory-am/hydra/firewall"
    hydra "github.com/ory-am/hydra/sdk"
    "github.com/otraore/gatekeeper/gin"
)

func handler(c *gin.Context) {
	ctx := c.Get("hydra").(*firewall.Context)
	// Now you can access ctx.Subject etc.
}

func main(){
	// Initialize Hydra
	hc, err := hydra.Connect(
		hydra.ClientID("..."),
		hydra.ClientSecret("..."),
		hydra.ClusterURL("..."),
	)

	if err != nil {
		panic(err)
	}
	
	// Create a gatekeeper instance for Gin
	gk := gatekeeper.New(hc)

 	r := gin.Default()
	r.GET("/protected", gk.ScopesRequired("scope1", "scope2"), handler)
	r.Run()
}

Echo

import (
    "github.com/labstack/echo"
    "github.com/labstack/echo/engine/standard"
    "github.com/ory-am/hydra/firewall"
    hydra "github.com/ory-am/hydra/sdk"
    "github.com/otraore/gatekeeper/echo"
)

func handler(c echo.Context) {
	ctx := c.Get("hydra").(*firewall.Context)
	// Now you can access ctx.Subject etc.
}

func main(){
	// Initialize Hydra
	hc, err := hydra.Connect(
		hydra.ClientID("..."),
		hydra.ClientSecret("..."),
		hydra.ClusterURL("..."),
	)

	if err != nil {
		panic(err)
	}
	
	// Create a gatekeeper instance for Echo
	gk := gatekeeper.New(hc)

 	e := echo.Default()
	e.GET("/protected", handler, gk.ScopesRequired("scope1", "scope2"),)

	e.Run(standard.New(":8080"))
}

Goa

Example coming soon

License

MIT