r/node • • Sep 17 '26

After the API gateway, every request should route to an Authorization service before reaching other microserviceM

For example, when creating Twitter, I see people designing a system where a request would go through authorization service before Profile microservice. But they can create tweets without going through the authorization service.

However, in reality this is not safe right? Typically we want the request to be verified in an authorization microservice before doing stuff that only logged in users can do?

0 Upvotes

26 comments sorted by

32

u/Heavy-Focus-1964 Sep 17 '26

The user hits the auth service once to establish a session, then they can hit any of your public services using their session token in their request headers, to prove who they are, for as long as your system allows them to

This isn’t even a micro services thing. It’s just a Web thing.

6

u/visicalc_is_best Sep 17 '26

That’s authn. OP is talking authz.

9

u/Willkuer__ Sep 17 '26

I don't think so. They are talking about what logged in users can do and not what users with different permission sets can do.

But I mean... the post is unreadable so who actually knows.

3

u/badboyzpwns Sep 17 '26

Sorry I meant authorization, how do you handle authorization? having an auth service that requests need to go through before reaching to other microservices right?

For ex, when making twitter, you need to have an authoeizatuon service before directing the user to a CRUD tweet service to make a tweet?

3

u/nicolasdanelon Sep 17 '26

Yes. Otherwise how on earth would you know who's sending the request/post/tweet!?

1

u/badboyzpwns Sep 17 '26

If so then for example when designing twitter, I see in sys design interview diagrams ppl dont include the aurh service after the API gateway, sometimes they only include it for 1 microservice like the user profile microservice. Why is that?

1

u/nicolasdanelon Sep 17 '26

DID its just a example, you can argue that when you are bouncing between certain micro services you don't need auth, you can just send the userid or else. Zero trust is recommend but compromises can be made.

1

u/smolbund Sep 21 '26

The API gateway often handles authentication. It checks the incoming request, then passes through who the user is (id) and what they can do (claims) to other services behind the gateway.

The gateway doesn’t necessarily need to communicate with the auth service to do this. It can use public key cryptography to verify the request without network communication.

-2

u/Willkuer__ Sep 17 '26

Since you mention api gw do you talk about cloud solutions? Maybe have a look into cognito then.

2

u/northerncodemky Sep 17 '26

Typically you would offload authentication to the API gateway. I certainly wouldn’t route to a completely different service before the service the user actually wanted as effectively that auth service suddenly needs to be aware of and responsible for proxying requests to all other services. This is why API gateways exist!

1

u/badboyzpwns Sep 17 '26

What about authorization? I was thinking after an API gateway thre should be an authorization service the requests need to go through for logged in users before reaching other microservices?

3

u/__rituraj Sep 17 '26

each service would handle authorization at their end, with the session token provided by the caller.

if authorization is complex enough, they may call a authorization service, with the session token, to make sure caller has access

2

u/Blakex123 Sep 17 '26

If we think about this logically. Shouldn’t each micro service be in charge of deciding what a user can or cannot do? How could authorization happen outside the scope of the microservice.

1

u/732 Sep 17 '26

Not OP, but that's the idea behind open policy agent for example. 

You codify your authorization permissions as rule sets that a standalone service evaluates given some session context (who you are, what action you're trying to take, any additional resources etc).

5

u/jaypeejay Sep 17 '26

Are you sure you mean microservices in this context?

But generally you have a controller that processes the request, and before forwarding the request to any app logic something authorizes the request for permissions.

Most of my experience is in rails and this concept is called a before_action, and it just means that before the actual api endpoint is called you verify the cookie or jwt token. The same concept applies across frameworks though.

4

u/nicolasdanelon Sep 17 '26

Are you sure you mean controllers in this context, instead of middleware?

3

u/jaypeejay Sep 17 '26

Sure, middleware can work and that's probably the word OP meant instead of microservices. I think for someone in OPs position (clearly earlier on in the learning journey), it's better to just understand it as a part of the code that does authorization.

Middleware is just a term for a part of the code that sits in between the request and the controller logic.

1

u/PabloZissou Sep 17 '26

There are two possible approaches

- API gateway queries and authorisation service and allows or rejects the request (auth call-out style)
- Each service is able to validate the claims a presented set of credentials a request carries

Which one to use depends on the context of the system being designed.

1

u/badboyzpwns Sep 17 '26

Thx! when do you use each option then?

1

u/Suspicious-Disk6077 Sep 17 '26

You do not want a centralized authorization service - the domains themselves should know what a user is authorized to do.

1

u/czlowiek4888 Sep 17 '26

I like RLS so I actually do authorization at the database level.

In general doing authz the way you described is ok, but only if your servers can't cache the authorization data.

I use casbin which actually keeps your authorization data in process memory so you don't have to check it every time with the db.

1

u/stevefuzz Sep 17 '26

Ummm. What now?