Move Verifier method to pkg-level func to accept *jwtauth.JwtAuth

This commit is contained in:
Peter Kieltyka 2017-07-06 18:09:09 -04:00
parent a5d0d75313
commit 6444fb9aef
5 changed files with 24 additions and 7 deletions

15
.travis.yml Normal file
View file

@ -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; } || :

View file

@ -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

View file

@ -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

View file

@ -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()

View file

@ -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) {