DEV Community

Cover image for JWT Says ‘Admin’. Should Your API Believe It?

JWT Says ‘Admin’. Should Your API Believe It?

Hadil Ben Abdallah on November 25, 2026

A JWT authorization check can look simple: your API receives a JSON Web Token, reads the user's roles from the Keycloak access token and grants acc...
Collapse
 
userxnom profile image
Abdullah Naseer •

The tenant-isolation example is the part that lands hardest — a valid signature plus an admin role in realm_access.roles still doesn't prove the user is an admin of org-456. One gap I noticed: the jose example validates the signature, issuer and audience, but I didn't see a check on the typ claim. Section 5 says to reject ID tokens used as API credentials — does the snippet actually enforce typ == "Bearer", or is that left to the reader? And how would you handle a token where the organization claim is missing entirely, say a legacy user created before Organizations was enabled — deny by default, or fall back to a database lookup?

Collapse
 
hadil profile image
Hadil Ben Abdallah •

You're right about the typ check. The jose example validates the signature, issuer, audience, and allowed algorithm, but those checks alone don't enforce the access-token type. I included that check as a separate step in the article, and it should definitely be reflected in the code example too.

For the missing organization claim, I'd avoid automatically granting access or assuming the user belongs to the requested organization. A database lookup can be a reasonable fallback for legacy users, provided it verifies their actual membership and permissions on the server. If neither the token nor the database can establish that relationship, the safest approach is to deny access by default.

Collapse
 
cryptostrapon profile image
cryptostrapon •

The multi-org case is where the example gets tricky. In the sample token, realm_access.roles is realm-wide and the organization claim only lists memberships. So the token can say "admin" and "member of org-456 and org-789", but not in which of the two the user is admin.

If UserContext is built from those two claims, someone who is admin in Acme and a plain member in Globex passes canManageMembers for Globex. I'd key permissions by (user, org) on the server, or carry org-specific roles in the token (a group per org works), and treat a realm-level admin as platform admin only.

Collapse
 
hadil profile image
Hadil Ben Abdallah •

You're right: organization membership alone doesn't tell us what role someone has in each organization. If we rely only on a realm-level admin role, we could easily end up granting someone permissions they shouldn't have in another workspace. I'd keep organization-specific permissions on the server or include properly scoped roles in the token. A realm-level admin should also be treated as a platform admin, with a clearly defined scope.

Collapse
 
kielltampubolon profile image
Kiell Tampubolon •

The Audience mapper step is where I'd add one caveat. Once the mapper puts saas-api into aud, every client that gets that client scope can mint tokens your API accepts, so aud alone no longer tells you which app is calling. Keycloak also sets azp to the client the token was issued to, and checking it against a short allowlist closes that gap cheaply. It also gives you a clean place to say the public SPA can read members while only the back-office client can remove them. Would you put azp in the jose example next to the typ check?

Collapse
 
hadil profile image
Hadil Ben Abdallah •

You're right that once multiple clients share the same audience through a client scope, checking aud alone doesn't tell us which client obtained the token. Adding an azp check against an explicit allowlist would make the example stronger, especially when different clients should have different levels of access. I'd definitely consider including it alongside the typ check, while keeping the actual permission checks on the API side.

Collapse
 
caboulicam profile image
Christophe ABOULICAM •

One case worth adding for teams with a gateway in front: it's tempting to validate the JWT at the gateway and pass a clean X-User-Role header down to the service. That only holds if the gateway strips any incoming header with the same name, and the service only accepts traffic from the gateway. Otherwise, a client sets the header directly and skips the token entirely.

Collapse
 
hadil profile image
Hadil Ben Abdallah •

This is one of those shortcuts that can introduce a serious security hole. Validating the JWT at the gateway doesn't help much if a client can send its own X-User-Role header directly to the service. Stripping untrusted headers and restricting service access to the gateway are both essential here. The trust boundary needs to be enforced all the way through, not just at the first layer.

Collapse
 
caboulicam profile image
Christophe ABOULICAM •

Exactly. When the network can't fully guarantee that only the gateway reaches the service, I'd rather not forward a plain header at all. The gateway can pass a short-lived token it signs itself, and the service checks that signature. Then a forged header is simply rejected, even if someone finds a path around the gateway.

Collapse
 
ascendra_ventures_92c21f2 profile image
Ascendra Ventures •

Token validity and permission being separate checks is the line that matters most here, and the org-456 example shows why. A perfectly valid token with an admin role can still be the wrong answer for a resource that belongs to a different organization.

Ascendra Ventures described a commercial version of the same problem in a recent post about a mobility manufacturer. Its site was a brochure and dealers still ordered by phone and fax, and the requirement turned out to be one storefront where retail customers and authorized dealers each see their own catalog and price, and neither ever sees the other's. The post called it an access problem rather than a website problem. The store decides what to show based on who is looking: consumers get consumer prices, authenticated dealers get dealer pricing and dealer stock. It runs around 4,000 transactions a day with catalog search under a tenth of a second, and the lesson in the post was to ask, before buying a second system for a second audience, whether one storefront can know who is looking at it.

For the multi-org case raised in the comments, would you put per-organization roles into the token, or keep them in the API's own database and trust the token only for identity?

Collapse
 
hadil profile image
Hadil Ben Abdallah •

Great example, especially the distinction between treating it as a website problem and recognizing it as an access-control problem. The same thinking applies to multi-tenant APIs: knowing who the user is doesn't automatically tell us what they should be able to see or manage.

Personally, I'd lean toward keeping organization-specific permissions in the API's database when the authorization rules are complex or change frequently. The token can establish the user's identity, while the backend checks their current role and permissions for the organization involved in each request. This also avoids relying on potentially outdated permissions in an already-issued token.

Putting per-organization roles in the token can work well too, especially when memberships and permissions are relatively stable and you want to avoid an extra database lookup. The important thing is to make sure every permission is tied to the correct organization.

I think the right choice depends on how frequently permissions change and how strict the application needs to be about reflecting those changes immediately.

Collapse
 
arslan_ah profile image
Arslan Ahmad •

"Token validity and permission are separate checks" should be on a poster in every backend team's room.

The org boundary check is the one I see missed most. In multi-tenant APIs, the token is perfectly valid... just for the wrong tenant. The signature passes, the role says admin, and suddenly you're an admin of someone else's data.

The authn vs authz split is one of the things we hammer on in Grokking the API Design Interview at DesignGurus, because interviewers love poking right at this gap. Great writeup.

Collapse
 
hadil profile image
Hadil Ben Abdallah •

Haha, I love that poster idea! 😂 And you're right about the wrong-tenant scenario. That's what makes this so dangerous: everything can look fine during token validation, while the actual access decision is completely wrong.

Collapse
 
the_daily_flare profile image
The daily flare •

I think the tenant isolation example makes the risk click for me.

Collapse
 
hadil profile image
Hadil Ben Abdallah •

Glad that example helped! That's the kind of risk I wanted to highlight. It's easy to think about roles in isolation, but once multiple organizations share the same API, the scope of each permission becomes just as important as the permission itself.