Search code examples
gomartiniauth0

Auth0 in Go Martini


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 (
  "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)

}

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?


Solution

  • 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!