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