Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use JWT to authenticate separate API Microservice

I am developing an app using microservices in NodeJS. I have built an auth api which handles the usual registration login etc and it issues JWT's

How do I use these to protect routes in a separate API microservice written with Express?

Do I need to use JWT with the secret to decrypt the token in the API app?

like image 829
Luke Avatar asked May 15 '19 10:05

Luke


People also ask

How do I use JWT tokens in microservices?

For Authorization, the Microservice would need the JWT access token to be passed to it. It can then verify the JWT token & extract the user roles from the claims & accordingly allow/deny the request for the concerned endpoint.

Should authentication be a separate microservice?

Central dependency—authentication and authorization logic must be handled separately by each microservice. You could use the same code in all microservices, but this requires that all microservices support a specific language or framework.

How do I authenticate API using JWT?

To authenticate a user, a client application must send a JSON Web Token (JWT) in the authorization header of the HTTP request to your backend API. API Gateway validates the token on behalf of your API, so you don't have to add any code in your API to process the authentication.

How does JWT authentication work in microservices?

Incoming requests for authentication would be routed to the appropriate microservice. If the credentials provided be correct, a new JWT would be returned to the gateway, which would then forward to the caller.

How to set up JWT authentication with Kong Gateway?

The approach to JWT authentication is quite simple: Set up a basic Node.js Express server with a single endpoint. Set up Kong Gateway as an API gateway to your server. Enable the JWT plugin to protect your server endpoint with JWT authentication. Lastly, I’ll cover advanced use cases for the plugin.

How JWT token is generated in web server?

Server generates a Jwt token at server side. After token generation, the server returns a token in response. Now, the client sends a copy of the token to validate the token. The server checks JWT token to see if it's valid or not.

How does JWT work?

How Does JWT Work? Step 1. Client logs in with his/her credentials. Step 2. Server generates a Jwt token at server side. Step 3. After token generation, the server returns a token in response. Step 4. Now, the client sends a copy of the token to validate the token. Step 5. The server checks JWT ...


2 Answers

You could write a library that you import into your other microservices that requires all routes by default to require authentication. This library could have a mechanism to validate JWT's at the microservice level, so you never need to talk to your auth api to see if a JWT is valid or not. See the description and diagram below:

Your auth server will will need to be the single issuer of JWTs to your microservices. So, when a user logs in and successfully authenticates, your auth server will issue a JWT signed with a private key (signing MUST be asymmetric - RS256 is one example) you keep on the auth server only; do not give this private key to other microservices that you wish to validate JWTs inside of. What you can do is derive a public key based on the private key you sign your tokens with and publish that to an endpoint on your auth server that requires no authentication - the public key will be represented in the form of a JWK (see link to spec). Google does something similar here. Then, in each of your microservices, your library will need to devise a way to make a GET request to the public key endpoint on your auth server every X minutes to see if there are any changes and cache the public key in each microservice. By having the public key cached in your microservice, you will be able to validate the requesting JWT inside the service that is being requested.

Then whenever a request comes into one of your microservices, the library you import will examine the requesting JWT, check its validity, and grant access/authorization if the token is valid. The beauty of using a private/public key pair and asymmetric key signing is that you can validate a token based on the public key alone, but not sign it. So as long as each service has the public key from your /cert endpoint, they can validate a token without ever needing to talk to the auth server or knowing the private key.

This will require a little more work up front, but will yield you massive amount of ease, flexibility, and peace of mind in the future knowing only one source knows your private key.

enter image description here

like image 113
Joe Berg Avatar answered Nov 09 '22 05:11

Joe Berg


One common pattern here would be to use an API gateway as the entry point to your entire microservice architecture. Incoming requests for authentication would be routed to the appropriate microservice. If the credentials provided be correct, a new JWT would be returned to the gateway, which would then forward to the caller. For the actual microservice APIs which comprise your application, the gateway would check that the incoming JWT be valid before allowing the request to hit the microservice.

This answer leaves out a few things, for simplicity. For instance, often you would want to have an authorization microservice, which decides what a user is allowed to do. Also, implementing JWT can be involved. You might need a cache layer to keep track of whitelisted and/or blacklisted JWT.

like image 38
Tim Biegeleisen Avatar answered Nov 09 '22 06:11

Tim Biegeleisen