Securing API end points using Negroni, Gorilla Mux, and JWT Middleware

Baiju Muthukadan
Sep 28, 2017 · 4 min read

Disclaimer: I am not a security expert. Please do not consider this as a recommendation or best practice. Here I am just sharing my learning experience while developing some hobby/pet/side projects.

These days when I develop RESTful APIs, I prefer Go programming language in the back-end. I don’t use any “web framework” , rather I prefer the net/http package in the standard library. Along with the standard library, I also use some third-party libraries. Here is the list:

There are few others, but the above four are most used in my hobby/pet/side projects. You can checkout my current pet project in GitHub to see others.

For this example, Envconfig is not required. You can install the other packages like this:

The JTW Middleware package installs jwt-go package as a dependency.

Here is the complete example code:

You can run the above program like this:

The /api/without-auth can be accessed without JWT token. But to access /api/with-auth you need a JWT token which you can generate from

You can access the /api/with-auth API end point using curl like this:

To avoid typing the JWT bearer token all the time in authorization header, I normally set it as an environment variable. In the above example, I set TKN as an environment variable with the value I generated from website.

Here are few outputs based on different kinds of inputs:

In the above example, the first one is a correct token generated from (When generating token use HS256 algorithm with “secret” as the secret key). That’s why we got auth required message. All other inputs are wrong and so it produced various kinds of error messages.

Code Walk Through

Just like any other Go package, the code starts with package name declaration. In this case the package name is main as we are going to run it directly. To make it an executable program, we also declare a main function below. After the package name declaration, net/http and other third-party packages are imported using factored import style (multiple imports within brackets). As you can see, two import aliases (middleware & jwt) are also specified for the convenience of using them later.

The main function started with the initialization of two routers like this:

The first router (r) will be used to configure API end points without authentication. Whereas the second router (ar) will be used for configuring API end points which requires authentication.

The line number 16–21 is the initialization of the JWT middle-ware which is copied below for easy lookup:

JWT has support for multiple signing methods/algorithms. In the current example we are using HMAC SHA-256 algorithm which is based on a symmetric secret key. The key is provided as the first return value of ValidationKeyGetter function. We chose this particular algorithm for the convenience of demonstration. So that you can easily generate a JWT token from website by providing the secret, which happened to be the string “secret” in this case.

For real use instead of the above HMAC algorithm, you can use RS256 which uses public/private key pair using RSA.

The line number 23–25 register a handler with a router that doesn’t require authentication. The line number 27–29 register another handler with another router that requires authentication. We will see in a moment how these two routers achieving this.

After this, in the line number 31, we are going to create a Negroni middle-ware chain with two middle-wares. Let’s take a closer look:

The mw.HandlerWithNext function is converted to a middle-ware by using the negroni.HandlerFunc helper function. The second middle-ware is constructed using the negroni.Wrap helper function wrapping the ar router. The final object will be conforming the the http.Handler interface. This makes it possible to register a path prefix using the r router as given like this:

This ensures that the API end points that doesn’t exactly match anything directly registered with r router also pass through the middle-ware registered in the an middle-ware chains. To understand this better, consider the two API end points registered in line numbers 23–29. The API end point /api/without-auth is registered with r router. Even though it starts with /api prefix, exact end point is fully matched and so the authentication related middle-wares will not be used. Where as in the case /api/with-auth is registered with ar router and so it’s going to be used.

Then another Negroni middle-ware chain is constructed. And this is used for registering the r router. Instead of Classic,which gives three middle-wares you can use negroni.New and negroni.Use to create middle-ware chains.

Finally, the application is run using the Run helper function.

Instead of the above function call, you can also use: http.ListenAndServe(":7080", n)


Securing API end points is easy in Go with the help of Negroni, Gorilla Mux, and JWT Middleware. These libraries has good documentation which you can see in the GitHub README files or the website.

Baiju Muthukadan

Written by

Software Engineer, Book Author

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade