Auth0在Go Martini中的使用

huangapple go评论90阅读模式
英文:

Auth0 in Go Martini

问题

我正在尝试在Go的Martini中使用Auth0。我正在使用他们的示例,但无论我尝试什么,似乎都无法使其工作。

这是我的代码:

package main

import (
  "flag"
  "github.com/go-martini/martini"
  "github.com/martini-contrib/render"
  "github.com/auth0/go-jwt-middleware"
  "encoding/base64"
  "github.com/dgrijalva/jwt-go"
  "net/http"
)

func main() {
  m := martini.Classic()
  port := flag.String("port", "8000", "HTTP Port")
  flag.Parse()

  jwtMiddleware := jwtmiddleware.New(jwtmiddleware.Options{
    ValidationKeyGetter: func(token *jwt.Token) (interface{}, error) {
      decoded, err := base64.URLEncoding.DecodeString("<token>")
      if err != nil {
        return nil, err
      }
      return decoded, nil
    },
  })

  m.Use(render.Renderer(render.Options{
    IndentJSON: true, // Output human readable JSON
  }))

  m.Get("/", jwtMiddleware.Handler, func(res http.ResponseWriter, req *http.Request) { // res and req are injected by Martini
    res.WriteHeader(200) // HTTP 200
  })

  // Get the PORT from the environment.
  m.RunOnAddr(":" + *port)

}

当我运行时,我得到一个恐慌,显示Value not found for type http.Handler

如果我将jwtMiddleware.Handler更改为jwtMiddleware.HandlerWithNext,我会得到一个恐慌,显示Value not found for type http.HandlerFunc

有人知道我做错了什么吗?

英文:

I'm trying to use Auth0 with Martini in Go. I'm using their examples but I can't seem to get it working no matter what I try.

Here is my code:

package main

import (
  &quot;flag&quot;
  &quot;github.com/go-martini/martini&quot;
  &quot;github.com/martini-contrib/render&quot;
  &quot;github.com/auth0/go-jwt-middleware&quot;
  &quot;encoding/base64&quot;
  &quot;github.com/dgrijalva/jwt-go&quot;
  &quot;net/http&quot;
)

func main() {
  m := martini.Classic()
  port := flag.String(&quot;port&quot;, &quot;8000&quot;, &quot;HTTP Port&quot;)
  flag.Parse()

  jwtMiddleware := jwtmiddleware.New(jwtmiddleware.Options{
    ValidationKeyGetter: func(token *jwt.Token) (interface{}, error) {
      decoded, err := base64.URLEncoding.DecodeString(&quot;&lt;token&gt;&quot;)
      if err != nil {
        return nil, err
      }
      return decoded, nil
    },
  })

  m.Use(render.Renderer(render.Options{
    IndentJSON: true, // Output human readable JSON
  }))

  m.Get(&quot;/&quot;, jwtMiddleware.Handler, func(res http.ResponseWriter, req *http.Request) { // res and req are injected by Martini
    res.WriteHeader(200) // HTTP 200
  })

  // Get the PORT from the environment.
  m.RunOnAddr(&quot;:&quot; + *port)

}

When I run that, I get a panic that says Value not found for type http.Handler

If I change the jwtMiddleware.Handler to jwtMiddleware.HandlerWithNext, I get a panic for Value not found for type http.HandlerFunc.

Does anyone have any ideas what I'm doing wrong?

答案1

得分: 2

使用jwt-middleware与Martini一起使用,只需使用CheckJWT方法而不是Handler方法。

请参考以下示例:https://github.com/auth0/go-jwt-middleware/blob/master/examples/martini-example/main.go#L27

如果有帮助,请告诉我。

祝好!

英文:

To use the jwt-middleware with Martini, you just have to use the CheckJWT method instead of the Handler method.

Check this example: https://github.com/auth0/go-jwt-middleware/blob/master/examples/martini-example/main.go#L27

Let me know if this helps.

Cheers!

huangapple
  • 本文由 发表于 2015年5月13日 04:45:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/30201084.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定