-
-
Notifications
You must be signed in to change notification settings - Fork 163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simple custom field on a JWT token #1033
Comments
I don't understand your question: You show snippets allegedly declaring a field, but I have no idea how you are using them, which gives me zero information as to exactly what you are expecting to happen. This is exactly why we have an issue template that explicitly asks for a stand alone Go test code. This module provides objects that can handle custom fields as is, without having to declare them. If your JWT has {
"foo": "bar",
... other standard JWT claims ...
} Then you can just parse the JWT without any setup, and access foo by: token, _ := jwt.Parse(...)
v, ok := token.Get(`foo`) The only annoyance is that since we (this module) does not know what the type of this field is, it's going to just give you back whatever encoding/json gave us. In this case, probably a foo := v.(string)
|
I'm sorry if the question is confusing to you and perhaps the echo middleware is the culprit here. I understand that in this module adding custom fields will yield them back from a parsed token if I were to use As for the issue template and standalone working code, given that the echo middleware is not compatible with v2 (there is an outstanding PR for it since March, putting it together was not very feasible, but I'll try. |
Here is a sample code that shows what I mean (I had to build it against https://github.com/khash/echo-middleware-jwx so it works with v2) but it is identical to the sample code here https://github.com/lestrrat-go/echo-middleware-jwx Obviously you'd need a valid JWKS endpoint that you can issue tokens for to make it run, but I have those already and the JWKs infrastructure is working as expected. package main
import (
"context"
"fmt"
"net/http"
"time"
jwx "github.com/khash/echo-middleware-jwx"
"github.com/labstack/echo/v4"
"github.com/lestrrat-go/jwx/v2/jwk"
"github.com/lestrrat-go/jwx/v2/jwt"
)
type customToken struct {
jwt.Token
}
func main() {
const certs = `CHANGE_THIS`
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
e := echo.New()
c := jwk.NewCache(ctx)
c.Register(certs, jwk.WithMinRefreshInterval(15*time.Minute))
ks, err := c.Refresh(ctx, certs)
if err != nil {
panic(fmt.Sprintf("failed to refresh google JWKS: %s\n", err))
}
works := e.Group("/works")
doesntWork := e.Group("/doesnt-work")
works.Use(jwx.JWX(ks))
doesntWork.Use(jwx.JWXWithConfig(ks, jwx.Config{
TokenFactory: func(c echo.Context) jwt.Token {
return &customToken{}
},
ContextKey: "token",
}))
works.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})
doesntWork.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})
e.Start(":8000")
} You will notice that added In this example, I haven't added any custom fields to the
|
I'm going to step back about adding custom fields, and will explain what I can tell you about the panic. See:
THIS is where the panic is occurring. Upon looking at your code, see where you define type customToken struct {
jwt.Token
} and how you initialize it: return &customToken{} Your return &customToken{ Token: nil } Then at jwt/validate.go, Q.E.D. :) |
Ah! I think as I was trying to migrate from the basic JWT echo middleware to this library I was looking around and saw that in custom fields there is a |
I'm trying what I think is a simple scenario. I have an extra field (custom claims) on my JWT token and I'd like it to be made available in a custom JWT struct. I'm using this in conjunction with JWKS and the echo middleware, but I don't think those details matter in the outcome.
Here is a quick example:
In using the JWX middleware, I'm also using
TokenFactory
to create a newcustomToken
:I've also registered the custom field:
All of the above, leads to getting a nil pointer error during the parsing of the token as part of the first check in
isIssueAtValid
, since thetoken
is nil.I've worked around the problem by not using a custom token and converting
(c *customToken) CheckMyField()
toCheckMyField(token jwt.Token)
which works, but I would like to know what I'm doing wrong in the setup, please.Thank you
The text was updated successfully, but these errors were encountered: