JWT Auth for go1.7

This commit is contained in:
Vojtech Vitek (V-Teq) 2016-07-11 17:51:29 -04:00
parent cf1ac5a102
commit c97496f7ab
2 changed files with 25 additions and 23 deletions

View file

@ -12,7 +12,6 @@ import (
"github.com/dgrijalva/jwt-go"
"github.com/goware/jwtauth"
"github.com/pressly/chi"
"golang.org/x/net/context"
)
var (
@ -33,7 +32,7 @@ func TestSimple(t *testing.T) {
r.Use(TokenAuth.Verifier, jwtauth.Authenticator)
r.Get("/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("welcome"))
})
@ -78,8 +77,10 @@ func TestMore(t *testing.T) {
r.Group(func(r chi.Router) {
r.Use(TokenAuth.Verifier)
authenticator := func(next chi.Handler) chi.Handler {
return chi.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
authenticator := func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
if jwtErr, ok := ctx.Value("jwt.err").(error); ok {
switch jwtErr {
default:
@ -103,19 +104,19 @@ func TestMore(t *testing.T) {
}
// Token is authenticated, pass it through
next.ServeHTTPC(ctx, w, r)
next.ServeHTTP(w, r)
})
}
r.Use(authenticator)
r.Get("/admin", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
r.Get("/admin", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("protected"))
})
})
// Public routes
r.Group(func(r chi.Router) {
r.Get("/", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("welcome"))
})
})