Skip to Content
Building RESTful Web services with Go
book

Building RESTful Web services with Go

by Naren Yellavula
December 2017
Intermediate to advanced
316 pages
6h 58m
English
Packt Publishing
Content preview from Building RESTful Web services with Go

Creating a JWT in Go

The jwt-go package has a function called NewWithClaims that takes two arguments:

  1. Signing method such as HMAC256, RSA, and so on
  2. Claims map

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.

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Hands-On RESTful Web Services with Go - Second Edition

Hands-On RESTful Web Services with Go - Second Edition

Naren Yellavula
Microservices with Go

Microservices with Go

Alexander Shuiskov

Publisher Resources

ISBN: 9781788294287Supplemental Content