title: go-restful + JWT authentication
body: <p>I'm trying to plug JWT authentication within a very simple go service written with <a href="http://github.com/emicklei/go-restful" rel="nofollow">go-restful</a>.</p>
  
  <p>The code is very similar to:</p>
  
  <pre><code>package main
  
  import (
      "github.com/emicklei/go-restful"
      "log"
      "net/http"
  )
  
  type User struct {
      Id, Name string
  }
  
  type UserList struct {
      Users []User
  }
  
  func getAllUsers(request *restful.Request, response *restful.Response) {
      log.Printf("getAllUsers")
      response.WriteEntity(UserList{[]User{{"42", "Gandalf"}, {"3.14", "Pi"}}})
  }
  
  func NewUserService() *restful.WebService {
      ws := new(restful.WebService)
      ws.
          Path("/users").
          Consumes(restful.MIME_XML, restful.MIME_JSON).
          Produces(restful.MIME_JSON, restful.MIME_XML)
  
      ws.Route(ws.GET("").To(getAllUsers))
  
      return ws
  }
  
  
  func main() {
      restful.Add(NewUserService())
      log.Printf("start listening on localhost:8080")
      log.Fatal(http.ListenAndServe(":8080", nil))
  }
  </code></pre>
  
  <p>where <code>restful.Request</code> is a wrapper around <code>http.Request</code>.</p>
  
  <p>That being said, it might be possible to use the <a href="https://github.com/auth0/go-jwt-middleware" rel="nofollow">Auth0 jwt middleware</a>.</p>
  
  <p>But as a golang newbie, I'm a bit lost in the plumbing process. I see that I must use a <code>Filter</code> function like </p>
  
  <pre><code>ws.Filter(jwtAuthentication)
  </code></pre>
  
  <p>where</p>
  
  <pre><code>func jwtAuthentication(req *restful.Request, resp *restful.Response, chain *restful.FilterChain) {
      // Jwt Magic goes here \o
      chain.ProcessFilter(req, resp)
  }
  </code></pre>
  
  <p>But I don't figure how and where should I instanciate the JWT middleware.</p>