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

Function ExampleParseWithClaims_customValidation

example_test.go:147–162  ·  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 and the function CustomValidation is implemented.

()

Source from the content-addressed store, hash-verified

145// encoding, parsing and validation of standard claims and the function
146// CustomValidation is implemented.
147func ExampleParseWithClaims_customValidation() {
148 tokenString := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmb28iOiJiYXIiLCJpc3MiOiJ0ZXN0IiwiYXVkIjoic2luZ2xlIn0.QAWg1vGvnqRuCFTMcPkjZljXHh8U3L_qUjszOtQbeaA"
149
150 token, err := jwt.ParseWithClaims(tokenString, &MyCustomClaims{}, func(token *jwt.Token) (any, error) {
151 return []byte("AllYourBase"), nil
152 }, jwt.WithLeeway(5*time.Second))
153 if err != nil {
154 log.Fatal(err)
155 } else if claims, ok := token.Claims.(*MyCustomClaims); ok {
156 fmt.Println(claims.Foo, claims.Issuer)
157 } else {
158 log.Fatal("unknown claims type, cannot proceed")
159 }
160
161 // Output: bar test
162}
163
164// An example of parsing the error types using errors.Is.
165func ExampleParse_errorChecking() {

Callers

nothing calls this directly

Calls 1

ParseWithClaimsMethod · 0.80

Tested by

no test coverage detected