Skip to content

Commit 8a206e9

Browse files
committed
Start versioning from v4 to track Echo versions
1 parent c84d41a commit 8a206e9

File tree

3 files changed

+93
-5
lines changed

3 files changed

+93
-5
lines changed

README.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
11
# Echo JWT middleware
22

3-
JWT middleware for [Echo](https://github.com/labstack/echo) framework. By default uses [golang-jwt/jwt/v4](https://github.com/golang-jwt/jwt)
3+
JWT middleware for [Echo](https://github.com/labstack/echo) framework. This middleware uses by default [golang-jwt/jwt/v4](https://github.com/golang-jwt/jwt)
44
as JWT implementation.
55

6+
## Versioning
7+
8+
This repository does not use semantic versioning. MAJOR version tracks which Echo version should be used. MINOR version
9+
tracks API changes (possibly backwards incompatible) and PATCH version is incremented for fixes.
10+
11+
For Echo `v4` use `v4.x.y` releases.
12+
13+
`main` branch is compatible with the latest Echo version.
14+
615
## Usage
716

817
Add JWT middleware dependency with go modules
918
```bash
10-
go get github.com/labstack/echo-jwt
19+
go get github.com/labstack/echo-jwt/v4
1120
```
1221

1322
Use as import statement
1423
```go
15-
import "github.com/labstack/echo-jwt"
24+
import "github.com/labstack/echo-jwt/v4"
1625
```
1726

1827
Add middleware in simplified form, by providing only the secret key
@@ -31,6 +40,10 @@ e.Use(echojwt.WithConfig(echojwt.Config{
3140

3241
Extract token in handler
3342
```go
43+
import "github.com/golang-jwt/jwt/v4"
44+
45+
// ...
46+
3447
e.GET("/", func(c echo.Context) error {
3548
token, ok := c.Get("user").(*jwt.Token) // by default token is stored under `user` key
3649
if !ok {
@@ -52,7 +65,7 @@ package main
5265
import (
5366
"errors"
5467
"github.com/golang-jwt/jwt/v4"
55-
"github.com/labstack/echo-jwt"
68+
"github.com/labstack/echo-jwt/v4"
5669
"github.com/labstack/echo/v4"
5770
"github.com/labstack/echo/v4/middleware"
5871
"log"

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module github.com/labstack/echo-jwt
1+
module github.com/labstack/echo-jwt/v4
22

33
go 1.17
44

jwt_extranal_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// SPDX-License-Identifier: MIT
2+
// SPDX-FileCopyrightText: © 2016 LabStack and Echo contributors
3+
4+
package echojwt_test
5+
6+
import (
7+
"errors"
8+
"fmt"
9+
"github.com/golang-jwt/jwt/v4"
10+
echojwt "github.com/labstack/echo-jwt/v4"
11+
"github.com/labstack/echo/v4"
12+
"io"
13+
"log"
14+
"net"
15+
"net/http"
16+
"time"
17+
)
18+
19+
func ExampleJWTWithConfig_usage() {
20+
e := echo.New()
21+
22+
e.Use(echojwt.WithConfig(echojwt.Config{
23+
SigningKey: []byte("secret"),
24+
}))
25+
26+
e.GET("/", func(c echo.Context) error {
27+
// make sure that your imports are correct versions. for example if you use `"github.com/golang-jwt/jwt"` as
28+
// import this cast will fail and `"github.com/golang-jwt/jwt/v4"` will succeed.
29+
// Although `.(*jwt.Token)` looks exactly the same for both packages but this struct is still different
30+
token, ok := c.Get("user").(*jwt.Token) // by default token is stored under `user` key
31+
if !ok {
32+
return errors.New("JWT token missing or invalid")
33+
}
34+
claims, ok := token.Claims.(jwt.MapClaims) // by default claims is of type `jwt.MapClaims`
35+
if !ok {
36+
return errors.New("failed to cast claims as jwt.MapClaims")
37+
}
38+
return c.JSON(http.StatusOK, claims)
39+
})
40+
41+
// ----------------------- start server on random port -----------------------
42+
l, err := net.Listen("tcp", ":0")
43+
if err != nil {
44+
log.Fatal(err)
45+
}
46+
go func(e *echo.Echo, l net.Listener) {
47+
s := http.Server{Handler: e}
48+
if err := s.Serve(l); err != http.ErrServerClosed {
49+
log.Fatal(err)
50+
}
51+
}(e, l)
52+
time.Sleep(100 * time.Millisecond)
53+
54+
// ----------------------- execute HTTP request with valid token and check the response -----------------------
55+
requestURL := fmt.Sprintf("http://%v", l.Addr().String())
56+
req, err := http.NewRequest(http.MethodGet, requestURL, nil)
57+
if err != nil {
58+
log.Fatal(err)
59+
}
60+
req.Header.Set(echo.HeaderAuthorization, "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ")
61+
62+
res, err := http.DefaultClient.Do(req)
63+
if err != nil {
64+
log.Fatal(err)
65+
}
66+
67+
body, err := io.ReadAll(res.Body)
68+
if err != nil {
69+
log.Fatal(err)
70+
}
71+
72+
fmt.Printf("Response: status code: %d, body: %s\n", res.StatusCode, body)
73+
74+
// Output: Response: status code: 200, body: {"admin":true,"name":"John Doe","sub":"1234567890"}
75+
}

0 commit comments

Comments
 (0)