jwt icon indicating copy to clipboard operation
jwt copied to clipboard

Parsing token and getting payload

Open mikolajsemeniuk opened this issue 2 years ago • 1 comments

Parsing token and getting payload

Is there any way to extract claims from token like in this package (any example appreciated)?

mikolajsemeniuk avatar Apr 07 '22 19:04 mikolajsemeniuk

In order to parse the token you'll need to verify it:

import "github.com/gbrlsnchs/jwt/v3"

type CustomPayload struct {
	jwt.Payload
	Foo string `json:"foo,omitempty"`
	Bar int    `json:"bar,omitempty"`
}

var hs = jwt.NewHS256([]byte("secret"))

func main() {
	// ...

	var pl CustomPayload
	hd, err := jwt.Verify(token, hs, &pl)
	if err != nil {
		// ...
	}

	// ...
}

This was done on purpose in order to avoid security flaws where users use data from tokens that weren't validated.

Let me know if this info is enough for you so I can close this issue. :smiley:

gbrlsnchs avatar Apr 08 '22 21:04 gbrlsnchs