Skip to content

Commit

Permalink
Merge pull request #9 from goware/go1.7
Browse files Browse the repository at this point in the history
Go 1.7 + Vendor jwt-go at v2.7
  • Loading branch information
Peter Kieltyka authored Aug 4, 2016
2 parents cf1ac5a + e8c0a67 commit 30d5d5b
Show file tree
Hide file tree
Showing 18 changed files with 1,250 additions and 23 deletions.
33 changes: 17 additions & 16 deletions jwtauth.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package jwtauth

import (
"context"
"encoding/json"
"errors"
"net/http"
"strings"
"time"

"github.com/dgrijalva/jwt-go"
"github.com/pressly/chi"
"golang.org/x/net/context"
jwt "github.com/dgrijalva/jwt-go"
)

var (
Expand Down Expand Up @@ -59,14 +58,14 @@ func NewWithParser(alg string, parser *jwt.Parser, signKey []byte, verifyKey []b
// and respond to the client accordingly. A generic Authenticator
// middleware is provided by this package, that will return a 401
// message for all unverified tokens, see jwtauth.Authenticator.
func (ja *JwtAuth) Verifier(next chi.Handler) chi.Handler {
func (ja *JwtAuth) Verifier(next http.Handler) http.Handler {
return ja.Verify("")(next)
}

func (ja *JwtAuth) Verify(paramAliases ...string) func(chi.Handler) chi.Handler {
return func(next chi.Handler) chi.Handler {
hfn := func(ctx context.Context, w http.ResponseWriter, r *http.Request) {

func (ja *JwtAuth) Verify(paramAliases ...string) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
hfn := func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
var tokenStr string
var err error

Expand Down Expand Up @@ -113,30 +112,30 @@ func (ja *JwtAuth) Verify(paramAliases ...string) func(chi.Handler) chi.Handler
}

ctx = ja.SetContext(ctx, token, err)
next.ServeHTTPC(ctx, w, r)
next.ServeHTTP(w, r.WithContext(ctx))
return
}

if token == nil || !token.Valid || token.Method != ja.signer {
err = ErrUnauthorized
ctx = ja.SetContext(ctx, token, err)
next.ServeHTTPC(ctx, w, r)
next.ServeHTTP(w, r.WithContext(ctx))
return
}

// Check expiry via "exp" claim
if ja.IsExpired(token) {
err = ErrExpired
ctx = ja.SetContext(ctx, token, err)
next.ServeHTTPC(ctx, w, r)
next.ServeHTTP(w, r.WithContext(ctx))
return
}

// Valid! pass it down the context to an authenticator middleware
ctx = ja.SetContext(ctx, token, err)
next.ServeHTTPC(ctx, w, r)
next.ServeHTTP(w, r.WithContext(ctx))
}
return chi.HandlerFunc(hfn)
return http.HandlerFunc(hfn)
}
}

Expand Down Expand Up @@ -194,8 +193,10 @@ func (ja *JwtAuth) IsExpired(t *jwt.Token) bool {
// the Verifier middleware. The Authenticator sends a 401 Unauthorized response for
// all unverified tokens and passes the good ones through. It's just fine until you
// decide to write something similar and customize your client response.
func Authenticator(next chi.Handler) chi.Handler {
return chi.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
func Authenticator(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 {
if jwtErr != nil {
http.Error(w, http.StatusText(401), 401)
Expand All @@ -210,7 +211,7 @@ func Authenticator(next chi.Handler) chi.Handler {
}

// Token is authenticated, pass it through
next.ServeHTTPC(ctx, w, r)
next.ServeHTTP(w, r)
})
}

Expand Down
15 changes: 8 additions & 7 deletions jwtauth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/dgrijalva/jwt-go"
"github.com/goware/jwtauth"
"github.com/pressly/chi"
"golang.org/x/net/context"
)

var (
Expand All @@ -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"))
})

Expand Down Expand Up @@ -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:
Expand All @@ -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"))
})
})
Expand Down
8 changes: 8 additions & 0 deletions vendor/github.com/dgrijalva/jwt-go/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

100 changes: 100 additions & 0 deletions vendor/github.com/dgrijalva/jwt-go/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

88 changes: 88 additions & 0 deletions vendor/github.com/dgrijalva/jwt-go/VERSION_HISTORY.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions vendor/github.com/dgrijalva/jwt-go/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 30d5d5b

Please sign in to comment.