MCPcopy
hub / github.com/golang-jwt/jwt / ExampleParseWithClaims_validationOptions

Function ExampleParseWithClaims_validationOptions

example_test.go:103–123  ·  view source on GitHub ↗

Example creating a token using a custom claims type and validation options. The RegisteredClaims is embedded in the custom type to allow for easy encoding, parsing and validation of standard claims.

()

Source from the content-addressed store, hash-verified

101// Example creating a token using a custom claims type and validation options. The RegisteredClaims is embedded
102// in the custom type to allow for easy encoding, parsing and validation of standard claims.
103func ExampleParseWithClaims_validationOptions() {
104 tokenString := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmb28iOiJiYXIiLCJpc3MiOiJ0ZXN0IiwiYXVkIjoic2luZ2xlIn0.QAWg1vGvnqRuCFTMcPkjZljXHh8U3L_qUjszOtQbeaA"
105
106 type MyCustomClaims struct {
107 Foo string `json:"foo"`
108 jwt.RegisteredClaims
109 }
110
111 token, err := jwt.ParseWithClaims(tokenString, &MyCustomClaims{}, func(token *jwt.Token) (any, error) {
112 return []byte("AllYourBase"), nil
113 }, jwt.WithLeeway(5*time.Second))
114 if err != nil {
115 log.Fatal(err)
116 } else if claims, ok := token.Claims.(*MyCustomClaims); ok {
117 fmt.Println(claims.Foo, claims.Issuer)
118 } else {
119 log.Fatal("unknown claims type, cannot proceed")
120 }
121
122 // Output: bar test
123}
124
125type MyCustomClaims struct {
126 Foo string `json:"foo"`

Callers

nothing calls this directly

Calls 1

ParseWithClaimsMethod · 0.80

Tested by

no test coverage detected