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.
()
| 145 | // encoding, parsing and validation of standard claims and the function |
| 146 | // CustomValidation is implemented. |
| 147 | func 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. |
| 165 | func ExampleParse_errorChecking() { |
nothing calls this directly
no test coverage detected