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

Function ExampleNewWithClaims_hmac

hmac_example_test.go:27–39  ·  view source on GitHub ↗

Example creating, signing, and encoding a JWT token using the HMAC signing method

()

Source from the content-addressed store, hash-verified

25
26// Example creating, signing, and encoding a JWT token using the HMAC signing method
27func ExampleNewWithClaims_hmac() {
28 // Create a new token object, specifying signing method and the claims
29 // you would like it to contain.
30 token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
31 "foo": "bar",
32 "nbf": time.Date(2015, 10, 10, 12, 0, 0, 0, time.UTC).Unix(),
33 })
34 // Sign and get the complete encoded token as a string using the secret
35 tokenString, err := token.SignedString(hmacSampleSecret)
36
37 fmt.Println(tokenString, err)
38 // Output: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmb28iOiJiYXIiLCJuYmYiOjE0NDQ0Nzg0MDB9.u1riaD1rW97opCoAuRCTy4w58Br-Zk-bh7vLiRIsrpU <nil>
39}
40
41// Example parsing and validating a token using the HMAC signing method
42func ExampleParse_hmac() {

Callers

nothing calls this directly

Calls 1

SignedStringMethod · 0.80

Tested by

no test coverage detected