Go is becoming very popular for backend web development, and JWT's are one of the most popular ways to handle authentication on API requests. In this article, we are going to go over the basics of JWT's and how to implement a secure authentication strategy in Go!
What is a JWT?
JSON Web Tokens are an open, industry standard RFC 7519 method for representing claims securely between two parties.
More simply put, JWT's are encoded JSON objects that have been signed by the server, verifying authenticity.
For example, when a user logs in to a website secured via JWTs, the flow should look something like this:
{"username":"wagslane"}
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IndhZ3NsYW5lIn0.ov6d8XtwQoKUwsYahk9UwH333NICElFSs6ag6pINyPQ
wagslane
"Create a JWT
We are going to use a popular library for dealing with JSON Web Tokens's in Go, jwt-go. Make sure you have the code cloned locally:
go get github.com/dgrijalva/jwt-go
For simplicity, we are building a symmetric encryption scheme. This means we assume that the server that creates the JWTs will also be the only server that will be verifying them.
First, define a struct that will be used to represent claims to identify our users:
type customClaims struct {
Username string `json:"username"`
jwt.StandardClaims
}
The jwt.StandardClaims struct contains useful fields like expiry and issuer name. Now we will create some actual claims for the user that just logged in:
claims := customClaims{
Username: username,
StandardClaims: jwt.StandardClaims{
ExpiresAt: 15000,
Issuer: "nameOfWebsiteHere",
},
}
Create an unsigned token from the claims:
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
Sign the token using a secure private key. In production make sure you use a real private key, preferably at least 256 bits in length:
signedToken, err := token.SignedString([]byte("secureSecretText"))
Now the signed token can be sent back to the client.
Validating a JWT
When the client makes a request to a protected endpoint we can verify the JWT is authentic using the following steps.
*Note: It is idiomatic to use the Authorization HTTP header:
Authorization: Bearer {jwt}
After receiving the JWT, validate the claims and verify the signature using the same private key:
token, err := jwt.ParseWithClaims(
jwtFromHeader,
&customClaims{},
func(token *jwt.Token) (interface{}, error) {
return []byte("secureSecretText"), nil
},
)
If the claims have been tampered with then the err variable will not be nil.
Parse the claims from the token:
claims, ok := token.Claims.(*customClaims)
if !ok {
return errors.New("Couldn't parse claims")
}
Check if the token is expired:
if claims.ExpiresAt < time.Now().UTC().Unix() {
return errors.New("JWT is expired")
}
You now know the username of the authenticated user!
username := claims.Username
For full examples take a look at the package's tests.
By Lane Wagner
Previously published at https://qvault.io/2020/02/20/how-to-build-jwts-in-go-golang/