gbrlsnchs / jwt

Go JWT signing, verifying and validating
MIT License
452 stars 34 forks source link

Parsing token and getting payload #53

Open mikolajsemeniuk opened 2 years ago

mikolajsemeniuk commented 2 years ago

Parsing token and getting payload

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

gbrlsnchs commented 2 years ago

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: