Skip to content

Commit

Permalink
Move Verifier method to pkg-level func to accept *jwtauth.JwtAuth
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Kieltyka committed Jul 6, 2017
1 parent a5d0d75 commit 6444fb9
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 7 deletions.
15 changes: 15 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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; } || :
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion _example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions jwtauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions jwtauth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 6444fb9

Please sign in to comment.