Home Uncategorized Golang: API Auth with JWT & Middleware

Golang: API Auth with JWT & Middleware

by komalkumar436


Public APIs are prone to attacks. One way is to impersonate the user and get access to the system resources via APIs. Lot of security methods are in use to protect resources via bad actors; One of the standard way of protecting APIs are to send Auth headers in the APIs. The traditional way to secure APIs are to use basic auth; where the client stores the auth details and send them over http/https for every server request; the server decodes the username and password to authenticate the resource access. This is bad as password is stored at client side and can easily be leaked from there. This technique is prone to attack in the middle and as the same details are sent over the wire repeatedly and the window of attack is very large.

JWT token provide a secure way to transfer the user details over wire for secure communications. For JWT introduction please read (https://jwt.io) . In this article we will discuss on the JWT middleware that decrypts the token and validate the token expiry and other mandatory claims before passing the request to handlers and how to instrument the user details to Golang context.

Choosing JWT Libraries and encryption

Various JWT RFCs implementations are available out there. Please refer (https://jwt.io) for all the open source implementations of the RFC. Out of all I find the https://github.com/lestrrat-go/jwx has implementation of all the JWX Spec. We will be using this library to build a JWT middleware.

JWT – json web tokens are by default not encrypted, these token can hold user claims that needs to be secure over wire. To secure the token and its claims, we will be encrypt the token. There are various encryption methods. Here we will consider to use asymmetric RSA256 encryption. In this mode, the authorisation server generates the JWT token (claims embedded) and encrypts the token with RSA256 private key. One way to generate Asymmetric RSA keys are as follows.

  • openssl genrsa -out privateKey.pem 512; -generates private key of 512bytes
  • openssl rsa -in privateKey.pem -pubout -out publicKey.pem; -generates public key

Authorisation Server will use the private key to encrypt the JWT token and client will send this token to the APIs in their authorisation Bearer token. The token will be decrypted at the Application Server using the public key and token is validated against the expiry of the token.

In comparison to asymmetric, symmetric encryption algorithms are faster and less secure. Based on the application needs choose between the two encryptions.

Assuming you are building a Golang Application using go modules and the http protocol for REST API. With this setup, it will be easy to integrate the following middleware. Let’s dive to build the middleware at application layer.

Step1: Create a type that holds the JWTDetails like public key, algorithm to use decrypt

Step2: Implement Middleware to decrypt and process the token (skipping error checks..)

Step3: Validate token expiry

step4: Instrument user details to request scoped context.

Setting Router Middleware

Step1: Create a Router and Start the server with a “/order” POST endpoint.

On calling curl –location –request POST ‘http://localhost:8080/order&#8217; –header ‘Authorization: Bearer <token>’ \–header ‘Content-Type: application/json’ \–data-raw ‘{ “items_info”:[1,2,3,4]}’.

The request will flow through the jwtMiddleware and then to the /order handler i.e createOrder

Here we come to the end; to summarise we discussed on

  • how to create a jwt middleware
  • how to instrument it in the router middleware
  • how to read the claims set in the request scope context back and use it further.

JWT token authentication is better way than basic auth to authorise and authenticate clients.

You may also like

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: