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.
()
| 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. |
| 103 | func 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 | |
| 125 | type MyCustomClaims struct { |
| 126 | Foo string `json:"foo"` |
nothing calls this directly
no test coverage detected