December 2017
Intermediate to advanced
316 pages
6h 58m
English
The jwt-go package has a function called NewWithClaims that takes two arguments:
For example, it looks like the following code snippet:
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"username": "admin",
"iat":time.Now().Unix(),
})
jwt.SigningMethodHS256 is an encryption algorithm that is available within the package. The second argument is a map with claims such as private (here username) and reserved (issued at). Now we can generate a tokenString using the SignedString function on a token:
tokenString, err := token.SignedString("my_secret_key")
This tokenString then should be passed back to the client.