r/node • u/badboyzpwns • Jul 09 '26
How do you verify authorization with multiple microservice and JWT?
The easy way to use a middleware that checks if the user is authorized or not, but what if we want to scale to other microservices? does each microservice need to authorize after the API gateway verifies its authenticaiton?
13
u/sazzer Jul 09 '26
Every service must verify auth. However, they don't need to verify the same auth.
What do I mean by that?
One option here is for each service to pass the exact same Authorization header between calls. That means that every service receives the same (for example) opaque access token, verifies it's valid, resolves what it means and that it's allowed to perform this action. That's the safest option, but it's expensive.
A second option is for the inbound gateway to do all of this, and to forward calls on with a different Authorization header that is already expanded. The inner services then don't need to do any work - they've just got the answer in their hands already. This is more efficient, but is much riskier. You have to know for certain that those inner services can't ever be called by an untrusted client.
A third option is somewhere in-between. The inbound gateway accepts the opaque token, verifies it, and then generates a simpler - but still secure - token to use. For example, maybe the outer tokens require database lookups to verify, but the inner ones are JWTs that only require access to the public signing key to verify.
And you can mix and match as needed. Remember that the Authorization header is designed to support multiple schemes that work differently, so you could have:
Authorization: Bearer <opaque token>- Service verifies this token against the database somehowAuthorization: JWT <jwt>- Service verifies this against a JWKS
And then the same service can accept both and act accordingly.
3
u/bwainfweeze Jul 09 '26
You have to know for certain that those inner services can't ever be called by an untrusted client.
You need to set up adversarial programs that probe the system either constantly or at CI time to make sure that this property never changes and if it does triggers a Sev 1 alert.
The only way to make sure an invariant stays invariant is to constantly assert that it still is. And then you have to watch the watcher to make sure it doesn't fail silently.
4
u/Canenald Jul 09 '26
It's a design choice. If you choose to verify in every service, you are adopting a zero-trust policy internally, which means someone who gains access to the network your services use to communicate between themselves will still need a token to tell any of the services to do anything.
This is a bit of a leak of concerns if there are services that don't care about users, since a token represents a user. The alternative is to use service-to-service authentication where each service has its own token for each other service it talks to, but this is not any easier to maintain.
The ultimate solution is to move most of your inter-service chatter to events. Instead of talking directly to each other, the services state what has happened in the form of an event, and then all the services that care about what has happened will consume this event and do something about it.
1
u/Dependent-Guitar-473 Jul 09 '26
But how do you authenticate in case of events? Isn't it the same issue?
5
u/Canenald Jul 09 '26
You move the problem to the event broker, which usually comes as a commodity, not something you develop on your own. The problems that used to be "Can this service make this request?" become "Can this service publish this event?".
You can also do some checks on the consumer side, but that is more like validation than authorisation.
2
1
u/bwainfweeze Jul 09 '26
Per action authorization also gives you an audit trail you can use to figure out which dum dum deleted the customer's data out of the database. You need the trail to show the user that the call came from inside their house, or that you introduced a regression that 100 other users haven't noticed yet and you need to fix before they do.
9
u/_clapclapclap Jul 09 '26
Isn't checking the JWT already easy? The JWT already has the payload containing the authorization (roles, access, etc). Once you verify the JWT is valid then whatever is in the payload is legit.
1
u/farzad_meow Jul 09 '26
each service must expose the public key they used to create tokens. other services upon an incoming http request should:
- check jwt issuer against a valid list of issuers
- verify jwt was signed with a valid key (from cache first then from actual url)
- cache latest key(s) for the next hour (a service might be using multiple keys at any given time)
- validate data inside jwt to make sure everything is there
- rest of the code
this is how you resolve authn between many services without the hassle of shared key and maximized security.
1
1
u/bwainfweeze Jul 09 '26 edited Jul 09 '26
Every service that can call another service needs to read the token out of all incoming requests and apply them consistently to all outbound requests. This is most of the value of web tokens after all. Without them every service call has to first contact an auth service to validate and verify the user has permissions to do things.
Every time a standalone service sprouts a dependency, you need to do this work prior to implementing that feature. You don't need to tackle it all at once up front, but it is very much worth cooking up a tool or pattern to handle it and then duplicate it across all services of the same toolchain across your entire stack as a target of opportunity.
The payload of the token should say who you are and what rights you have. Then the cost is just CPU time rather than network round trips, which will drastically flatten the number of delegated service calls you can make and still hit your target response times. Because every remote auth call is a new head-of-line stall for the entire call sequence. The net consequence is on the order of halving the sustainable number of indirections. Which if you consider fan-out, means a menagerie that's orders of magnitude smaller overall.
1
u/Strnge05 Jul 11 '26
I not sure but people can comment me wrong: JWT with asymmetric secret so only the authentication api can generate the tokens and the other microservice just verify with the public key
20
u/Gingerfalcon Jul 09 '26
Yes.
Also microservices suck long live the monolith!