#4388·gofr

RBAC: authorize on a token's scope/permissions claim, and add the role/permission extractor hook agreed in #2144

Author: akshat-kumar-singhalCreated Sep 26, 2026Updated Oct 1, 2026

Is your feature request related to a problem? Please describe.

EnableRBAC decides every request from one role-name string, read from roleHeader or a single jwtClaimPath, and maps it to permissions through the static roles section of the config. Most identity providers don't put authority in a token that way. They send it as:

Claim Shape Emitted by (examples)
scope space-delimited string (RFC 8693 §4.2, RFC 9068 §2.2.3) Auth0, Cognito, most OAuth2 servers
scp space-delimited string Entra ID
permissions array of strings Auth0 (RBAC "Add Permissions in the Access Token"), Clerk, WorkOS
roles / groups (RFC 9068 §2.2.3.1) array of strings Entra ID, Keycloak, Zitadel

None of these can drive GoFr's RBAC today:

jwtClaimPath: "scope"   token {"scope":"orders:read orders:write"}  -> role = "orders:read orders:write" -> no match -> 403
jwtClaimPath: "roles"   token {"roles":["admin","viewer"]}          -> role = "[admin viewer]"          -> no match -> 403 (#3979 item 3)

Separately, there is no hook to supply the permissions yourself, for example from a live store so a revoked grant stops working before the token expires. LoadPermissions accepts only a file path, and role extraction is hard-coded in extractRole.

The result is that an app whose identity provider issues scopes can't use EnableRBAC at all. It has to write its own authorization middleware, which re-implements the endpoint matching and the specificity-based rule resolution just fixed in #3934, and it misses future fixes to them.

Describe the solution you'd like

Two additive, backward-compatible changes. Existing configs behave exactly as today.

  1. Read a multi-valued claim. When the configured claim holds an array or a space-delimited string, split it into values instead of formatting it into one string. Two ways to use the values:

    • As roles: union the permissions of every named role. This also settles the decision #3979 item 3 asks for.
    • As permissions, directly: a new key (name illustrative), e.g. "permissionsClaimPath": "scope". The values are matched against requiredPermissions with today's ANY semantics, and the roles section becomes optional.
  2. An extractor hook. Let the app supply func(r *http.Request) (permissions []string, err error), or a roles variant, for anything the built-in claim reader can't express. The middleware keeps doing endpoint matching, public-route handling, audit logging and metrics; only "what does this caller hold?" is delegated. An error from the hook denies the request.

    This is the shape discussed in #2144's review, where a maintainer agreed: "We can have the interface in GoFr and implementation outside" (https://github.com/gofr-dev/gofr/pull/2144#issuecomment-3205885020). The PR merged without it. Issue #2000 also listed "custom extractors" as goal 3.

Since EnableRBAC(configPath ...string) is variadic on a string, the hook probably needs either an options form (EnableRBAC(path, rbac.WithExtractor(fn))) or a separate constructor. Happy to follow whatever convention you prefer.

Describe alternatives you've considered

  • Writing our own middleware. This is what we do today. It works, but it duplicates the matcher and resolver, and it gives up the audit log, metrics and tracing the built-in provides.
  • Only fixing #3979 item 3 (role arrays). That helps role-list tokens, but not scope/scp tokens, where the values are permissions rather than role names. It also doesn't cover a live lookup.
  • A custom middleware that rewrites the token into a single-role header before EnableRBAC runs. This loses information whenever a caller holds more than one role or scope, and hides the real authority from the audit log.

Additional context

  • Other frameworks with built-in authorization read these claims natively and expose a customization point. Spring Security's JwtGrantedAuthoritiesConverter maps scope/scp to SCOPE_* authorities and can be replaced; ASP.NET Core has RequireScope/RequireClaim plus pluggable authorization handlers.
  • Related: #2000 (original RBAC proposal, goal 3), #2144 (review discussion above), #3979 item 3 (role arrays), #3935 (RBAC's own route table).
  • If the direction is acceptable, I'm happy to send a PR.