Skip to content

Commit 6477ba4

Browse files
committed
Update deps, CI flow, README etc.
1 parent 60df66b commit 6477ba4

File tree

8 files changed

+108
-27
lines changed

8 files changed

+108
-27
lines changed

.github/workflows/checks.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ permissions:
1414

1515
env:
1616
# run static analysis only with the latest Go version
17-
LATEST_GO_VERSION: "1.22"
17+
LATEST_GO_VERSION: "1.23"
1818

1919
jobs:
2020
check:

.github/workflows/echo.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
# Echo tests with last four major releases (unless there are pressing vulnerabilities)
2626
# As we depend on `golang.org/x/` libraries which only support last 2 Go releases we could have situations when
2727
# we derive from last four major releases promise.
28-
go: ["1.19", "1.20", "1.21", "1.22"]
28+
go: ["1.20", "1.21", "1.22", "1.23"]
2929
name: ${{ matrix.os }} @ Go ${{ matrix.go }}
3030
runs-on: ${{ matrix.os }}
3131
steps:

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ format: ## Format the source code
3131
help: ## Display this help screen
3232
@grep -h -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
3333

34-
goversion ?= "1.18"
35-
test_version: ## Run tests inside Docker with given version (defaults to 1.18 oldest supported). Example: make test_version goversion=1.18
34+
goversion ?= "1.20"
35+
test_version: ## Run tests inside Docker with given version (defaults to 1.20 oldest supported). Example: make test_version goversion=1.20
3636
@docker run --rm -it -v $(shell pwd):/project golang:$(goversion) /bin/sh -c "cd /project && make race"

README.md

+30
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,36 @@ e.GET("/", func(c echo.Context) error {
6565
})
6666
```
6767

68+
## IMPORTANT: Integration Testing with JWT Library
69+
70+
Ensure that your project includes at least one integration test to detect changes in major versions of the JWT library early.
71+
This is crucial because type assertions like token := c.Get("user").(*jwt.Token) may fail silently if the imported version of the JWT library (e.g., import "github.com/golang-jwt/jwt/v5") differs from the version used internally by dependencies (e.g., echo-jwt may now use v6). Such discrepancies can lead to invalid casts, causing your handlers to panic or throw errors. Integration tests help safeguard against these version mismatches.
72+
73+
```go
74+
func TestIntegrationMiddlewareWithHandler(t *testing.T) {
75+
e := echo.New()
76+
e.Use(echojwt.WithConfig(echojwt.Config{
77+
SigningKey: []byte("secret"),
78+
}))
79+
80+
// use handler that gets token from context to fail your CI flow when JWT library version changes
81+
// a) `token, ok := c.Get("user").(*jwt.Token)`
82+
// b) `token := c.Get("user").(*jwt.Token)`
83+
e.GET("/example", exampleHandler)
84+
85+
req := httptest.NewRequest(http.MethodGet, "/example", nil)
86+
req.Header.Set(echo.HeaderAuthorization, "Bearer <TOKEN>")
87+
res := httptest.NewRecorder()
88+
89+
e.ServeHTTP(res, req)
90+
91+
if res.Code != 200 {
92+
t.Failed()
93+
}
94+
}
95+
```
96+
97+
6898
## Full example
6999

70100
```go

go.mod

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
module github.com/labstack/echo-jwt/v4
22

3-
go 1.18
3+
go 1.20
44

55
require (
6-
github.com/golang-jwt/jwt/v5 v5.2.0
7-
github.com/labstack/echo/v4 v4.11.4
8-
github.com/stretchr/testify v1.8.4
6+
github.com/golang-jwt/jwt/v5 v5.2.1
7+
github.com/labstack/echo/v4 v4.12.0
8+
github.com/stretchr/testify v1.9.0
99
)
1010

1111
require (
@@ -17,10 +17,10 @@ require (
1717
github.com/pmezard/go-difflib v1.0.0 // indirect
1818
github.com/valyala/bytebufferpool v1.0.0 // indirect
1919
github.com/valyala/fasttemplate v1.2.2 // indirect
20-
golang.org/x/crypto v0.19.0 // indirect
21-
golang.org/x/net v0.21.0 // indirect
22-
golang.org/x/sys v0.17.0 // indirect
23-
golang.org/x/text v0.14.0 // indirect
24-
golang.org/x/time v0.5.0 // indirect
20+
golang.org/x/crypto v0.29.0 // indirect
21+
golang.org/x/net v0.31.0 // indirect
22+
golang.org/x/sys v0.27.0 // indirect
23+
golang.org/x/text v0.20.0 // indirect
24+
golang.org/x/time v0.8.0 // indirect
2525
gopkg.in/yaml.v3 v3.0.1 // indirect
2626
)

go.sum

+16-14
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
22
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
33
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
44
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
5-
github.com/golang-jwt/jwt/v5 v5.2.0 h1:d/ix8ftRUorsN+5eMIlF4T6J8CAt9rch3My2winC1Jw=
6-
github.com/golang-jwt/jwt/v5 v5.2.0/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
7-
github.com/labstack/echo/v4 v4.11.4 h1:vDZmA+qNeh1pd/cCkEicDMrjtrnMGQ1QFI9gWN1zGq8=
8-
github.com/labstack/echo/v4 v4.11.4/go.mod h1:noh7EvLwqDsmh/X/HWKPUl1AjzJrhyptRyEbQJfxen8=
5+
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
6+
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
7+
github.com/labstack/echo/v4 v4.12.0 h1:IKpw49IMryVB2p1a4dzwlhP1O2Tf2E0Ir/450lH+kI0=
8+
github.com/labstack/echo/v4 v4.12.0/go.mod h1:UP9Cr2DJXbOK3Kr9ONYzNowSh7HP0aG0ShAyycHSJvM=
99
github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0=
1010
github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU=
1111
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
@@ -17,22 +17,24 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
1717
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1818
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
1919
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
20+
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
21+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
2022
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
2123
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
2224
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
2325
github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
24-
golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo=
25-
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
26-
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
27-
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
26+
golang.org/x/crypto v0.29.0 h1:L5SG1JTTXupVV3n6sUqMTeWbjAyfPwoda2DLX8J8FrQ=
27+
golang.org/x/crypto v0.29.0/go.mod h1:+F4F4N5hv6v38hfeYwTdx20oUvLLc+QfrE9Ax9HtgRg=
28+
golang.org/x/net v0.31.0 h1:68CPQngjLL0r2AlUKiSxtQFKvzRVbnzLwMUn5SzcLHo=
29+
golang.org/x/net v0.31.0/go.mod h1:P4fl1q7dY2hnZFxEk4pPSkDHF+QqjitcnDjUQyMM+pM=
2830
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
2931
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
30-
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
31-
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
32-
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
33-
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
34-
golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk=
35-
golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
32+
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
33+
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
34+
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
35+
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
36+
golang.org/x/time v0.8.0 h1:9i3RxcPv3PZnitoVGMPDKZSq1xW1gK1Xy3ArNOGZfEg=
37+
golang.org/x/time v0.8.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
3638
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
3739
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
3840
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
File renamed without changes.

jwt_integration_test.go

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// SPDX-License-Identifier: MIT
2+
// SPDX-FileCopyrightText: © 2016 LabStack and Echo contributors
3+
4+
package echojwt_test
5+
6+
import (
7+
"errors"
8+
"github.com/golang-jwt/jwt/v5"
9+
echojwt "github.com/labstack/echo-jwt/v4"
10+
"github.com/labstack/echo/v4"
11+
"net/http"
12+
"net/http/httptest"
13+
"testing"
14+
)
15+
16+
func TestIntegrationMiddlewareWithHandler(t *testing.T) {
17+
e := echo.New()
18+
e.Use(echojwt.WithConfig(echojwt.Config{
19+
SigningKey: []byte("secret"),
20+
}))
21+
22+
e.GET("/example", exampleHandler)
23+
24+
req := httptest.NewRequest(http.MethodGet, "/example", nil)
25+
req.Header.Set(echo.HeaderAuthorization, "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ")
26+
res := httptest.NewRecorder()
27+
28+
e.ServeHTTP(res, req)
29+
30+
if res.Code != 200 {
31+
t.Failed()
32+
}
33+
}
34+
35+
func exampleHandler(c echo.Context) error {
36+
// make sure that your imports are correct versions. for example if you use `"github.com/golang-jwt/jwt"` as
37+
// import this cast will fail and `"github.com/golang-jwt/jwt/v5"` will succeed.
38+
// Although `.(*jwt.Token)` looks exactly the same for both packages but this struct is still different
39+
token, ok := c.Get("user").(*jwt.Token)
40+
if !ok {
41+
return errors.New("JWT token missing or invalid")
42+
}
43+
44+
claims, ok := token.Claims.(jwt.MapClaims) // by default claims is of type `jwt.MapClaims`
45+
if !ok {
46+
return errors.New("failed to cast claims as jwt.MapClaims")
47+
}
48+
return c.JSON(http.StatusOK, claims)
49+
}

0 commit comments

Comments
 (0)