Replies: 1 comment 5 replies
-
If I understand this correctly - you have 2 versions of This is working example which uses same struct TYPE that is given to package main
import (
"errors"
"github.com/golang-jwt/jwt"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"log"
"net/http"
)
type jwtCustomClaims struct {
Name string `json:"name"`
jwt.StandardClaims
}
func main() {
e := echo.New()
e.Use(middleware.JWTWithConfig(middleware.JWTConfig{
SigningKey: []byte("secret"),
Claims: &jwtCustomClaims{},
}))
e.GET("/", func(c echo.Context) error {
user, ok := c.Get("user").(*jwt.Token)
if !ok {
return errors.New("toke missing or could not cast token to *jwt.Token")
}
claims, ok := user.Claims.(*jwtCustomClaims)
if !ok {
return errors.New("could not cast claims to *jwtCustomClaims")
}
name := claims.Name
return c.String(http.StatusOK, "Welcome "+name+"!")
})
if err := e.Start(":8080"); err != http.ErrServerClosed {
log.Fatal(err)
}
} Test with curl -v -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ" http://localhost:8080 Output * Trying 127.0.0.1:8080...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET / HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.81.0
> Accept: */*
> Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Content-Type: text/plain; charset=UTF-8
< Date: Sat, 26 Nov 2022 07:46:45 GMT
< Content-Length: 17
<
* Connection #0 to host localhost left intact
Welcome John Doe! |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have difficulty retriving JWT in my
controller
package, when the claim is set inroute
package:In my routes.go I have:
And in my controller package I have this:
But when I call
/restricted
route I get 500 error:{"time":"2022-11-26T05:08:41.829715242+01:00","level":"-","prefix":"echo","file":"recover.go","line":"113","message":"[PANIC RECOVER] interface conversion: jwt.Claims is *routes.jwtCustomClaims, not *controller.jwtCustomClaims goroutine 34 [running]:\ngithub.com/labstack/echo/v4/middleware.RecoverWithConfig.func1.1.1()\n\t/home/pc3/go/pkg/mod/github.com/labstack/echo/[email protected]/middleware/recover.
I tried to use
claims := user.Claims.(jwtCustomClaims)
where I defined the samejwtCustomClaims
in the controller package but still get a similar error:{"time":"2022-11-26T05:13:57.279260477+01:00","level":"-","prefix":"echo","file":"recover.go","line":"113","message":"[PANIC RECOVER] interface conversion: jwt.Claims is *routes.jwtCustomClaims, not controller.jwtCustomClaims goroutine 7 [running]:\ngithub.com/labstack/echo/v4/middleware.RecoverWithConfig.
So I'm left clueless and appreciate your help.
Beta Was this translation helpful? Give feedback.
All reactions