From 6444fb9aef3e6ec3c2c4b28f4f8ceb794509fb69 Mon Sep 17 00:00:00 2001 From: Peter Kieltyka Date: Thu, 6 Jul 2017 18:09:09 -0400 Subject: [PATCH] Move Verifier method to pkg-level func to accept *jwtauth.JwtAuth --- .travis.yml | 15 +++++++++++++++ README.md | 2 +- _example/main.go | 2 +- jwtauth.go | 8 +++++--- jwtauth_test.go | 4 ++-- 5 files changed, 24 insertions(+), 7 deletions(-) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..a026204 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,15 @@ +language: go + +go: + - 1.7.x + - 1.8.x + - tip + +install: + - go get -u golang.org/x/tools/cmd/goimports + +script: + - go get -d -t ./... + - go test ./... + - > + goimports -d -e ./ | grep '.*' && { echo; echo "Aborting due to non-empty goimports output."; exit 1; } || : diff --git a/README.md b/README.md index 424df8a..bcdbc17 100644 --- a/README.md +++ b/README.md @@ -77,7 +77,7 @@ func router() http.Handler { // Protected routes r.Group(func(r chi.Router) { // Seek, verify and validate JWT tokens - r.Use(tokenAuth.Verifier) + r.Use(jwtauth.Verifier(tokenAuth)) // Handle valid / invalid tokens. In this example, we use // the provided authenticator middleware, but you can write your diff --git a/_example/main.go b/_example/main.go index 85e5231..a1a16dd 100644 --- a/_example/main.go +++ b/_example/main.go @@ -89,7 +89,7 @@ func router() http.Handler { // Protected routes r.Group(func(r chi.Router) { // Seek, verify and validate JWT tokens - r.Use(tokenAuth.Verifier) + r.Use(jwtauth.Verifier(tokenAuth)) // Handle valid / invalid tokens. In this example, we use // the provided authenticator middleware, but you can write your diff --git a/jwtauth.go b/jwtauth.go index ed5f1c0..a03442e 100644 --- a/jwtauth.go +++ b/jwtauth.go @@ -66,12 +66,14 @@ func NewWithParser(alg string, parser *jwt.Parser, signKey []byte, verifyKey []b // be the generic `jwtauth.Authenticator` middleware or your own custom handler // which checks the request context jwt token and error to prepare a custom // http response. -func (ja *JwtAuth) Verifier(next http.Handler) http.Handler { - return ja.Verify("")(next) +func Verifier(ja *JwtAuth) func(http.Handler) http.Handler { + return func(next http.Handler) http.Handler { + return Verify(ja, "")(next) + } } // TODO: explain -func (ja *JwtAuth) Verify(paramAliases ...string) func(http.Handler) http.Handler { +func Verify(ja *JwtAuth, 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() diff --git a/jwtauth_test.go b/jwtauth_test.go index feae927..c6a489b 100644 --- a/jwtauth_test.go +++ b/jwtauth_test.go @@ -31,7 +31,7 @@ func init() { func TestSimple(t *testing.T) { r := chi.NewRouter() - r.Use(TokenAuth.Verifier, jwtauth.Authenticator) + r.Use(jwtauth.Verifier(TokenAuth), jwtauth.Authenticator) r.Get("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("welcome")) @@ -76,7 +76,7 @@ func TestMore(t *testing.T) { // Protected routes r.Group(func(r chi.Router) { - r.Use(TokenAuth.Verifier) + r.Use(jwtauth.Verifier(TokenAuth)) authenticator := func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {