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

Function ExampleNewWithClaims_registeredClaims

example_test.go:17–30  ·  view source on GitHub ↗

Example (atypical) using the RegisteredClaims type by itself to parse a token. The RegisteredClaims type is designed to be embedded into your custom types to provide standard validation features. You can use it alone, but there's no way to retrieve other fields after parsing. See the CustomClaimsTy

()

Source from the content-addressed store, hash-verified

15// no way to retrieve other fields after parsing.
16// See the CustomClaimsType example for intended usage.
17func ExampleNewWithClaims_registeredClaims() {
18 mySigningKey := []byte("AllYourBase")
19
20 // Create the Claims
21 claims := &jwt.RegisteredClaims{
22 ExpiresAt: jwt.NewNumericDate(time.Unix(1516239022, 0)),
23 Issuer: "test",
24 }
25
26 token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
27 ss, err := token.SignedString(mySigningKey)
28 fmt.Println(ss, err)
29 // Output: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ0ZXN0IiwiZXhwIjoxNTE2MjM5MDIyfQ.0XN_1Tpp9FszFOonIBpwha0c_SfnNI22DhTnjMshPg8 <nil>
30}
31
32// Example creating a token using a custom claims type. The RegisteredClaims is embedded
33// in the custom type to allow for easy encoding, parsing and validation of registered claims.

Callers

nothing calls this directly

Calls 1

SignedStringMethod · 0.80

Tested by

no test coverage detected