项目作者: otraore

项目描述 :
Hydra middleware for golang web frameworks
高级语言: Go
项目地址: git://github.com/otraore/gatekeeper.git
创建时间: 2016-08-26T00:20:29Z
项目社区:https://github.com/otraore/gatekeeper

开源协议:MIT License

下载


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

  1. go get -u github.com/otraore/gatekeeper

Gin

  1. import (
  2. "github.com/gin-gonic/gin"
  3. "github.com/ory-am/hydra/firewall"
  4. hydra "github.com/ory-am/hydra/sdk"
  5. "github.com/otraore/gatekeeper/gin"
  6. )
  7. func handler(c *gin.Context) {
  8. ctx := c.Get("hydra").(*firewall.Context)
  9. // Now you can access ctx.Subject etc.
  10. }
  11. func main(){
  12. // Initialize Hydra
  13. hc, err := hydra.Connect(
  14. hydra.ClientID("..."),
  15. hydra.ClientSecret("..."),
  16. hydra.ClusterURL("..."),
  17. )
  18. if err != nil {
  19. panic(err)
  20. }
  21. // Create a gatekeeper instance for Gin
  22. gk := gatekeeper.New(hc)
  23. r := gin.Default()
  24. r.GET("/protected", gk.ScopesRequired("scope1", "scope2"), handler)
  25. r.Run()
  26. }

Echo

  1. import (
  2. "github.com/labstack/echo"
  3. "github.com/labstack/echo/engine/standard"
  4. "github.com/ory-am/hydra/firewall"
  5. hydra "github.com/ory-am/hydra/sdk"
  6. "github.com/otraore/gatekeeper/echo"
  7. )
  8. func handler(c echo.Context) {
  9. ctx := c.Get("hydra").(*firewall.Context)
  10. // Now you can access ctx.Subject etc.
  11. }
  12. func main(){
  13. // Initialize Hydra
  14. hc, err := hydra.Connect(
  15. hydra.ClientID("..."),
  16. hydra.ClientSecret("..."),
  17. hydra.ClusterURL("..."),
  18. )
  19. if err != nil {
  20. panic(err)
  21. }
  22. // Create a gatekeeper instance for Echo
  23. gk := gatekeeper.New(hc)
  24. e := echo.Default()
  25. e.GET("/protected", handler, gk.ScopesRequired("scope1", "scope2"),)
  26. e.Run(standard.New(":8080"))
  27. }

Goa

Example coming soon

License

MIT