Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ package echojwt
import (
"errors"
"fmt"
"net/http"

"github.com/golang-jwt/jwt/v4"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"net/http"
)

// Config defines the config for JWT middleware.
Expand Down Expand Up @@ -224,7 +225,7 @@ func (config Config) ToMiddleware() (echo.MiddlewareFunc, error) {
}

// prioritize token errors over extracting errors
err = lastTokenErr
err := lastTokenErr
if err == nil {
err = lastExtractorErr
}
Expand Down
29 changes: 28 additions & 1 deletion jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ package echojwt
import (
"errors"
"fmt"
"github.com/golang-jwt/jwt/v4"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"sync"
"testing"

"github.com/golang-jwt/jwt/v4"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -751,3 +752,29 @@ func TestWithConfig_panic(t *testing.T) {
},
)
}

func TestToMiddlewareRace(t *testing.T) {
e := echo.New()
mw, err := Config{
ParseTokenFunc: func(c echo.Context, auth string) (interface{}, error) {
return auth, nil
},
SuccessHandler: func(c echo.Context) {
c.Set("success", "yes")
},
}.ToMiddleware()
assert.NoError(t, err)

dummyHandler := func(_ echo.Context) error { return nil }
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < 10; j++ {
mw(dummyHandler)(e.NewContext(httptest.NewRequest("", "/", nil), httptest.NewRecorder()))
}
}()
}
wg.Wait()
}