Skip to content

[WIP] Authorization#5518

Draft
ramonsmits wants to merge 15 commits into
masterfrom
auth
Draft

[WIP] Authorization#5518
ramonsmits wants to merge 15 commits into
masterfrom
auth

Conversation

@ramonsmits

Copy link
Copy Markdown
Member

No description provided.

ramonsmits and others added 12 commits June 4, 2026 14:45
Adds Authentication.RolesClaim (default "realm_access.roles") and a
RolesClaimsTransformation that normalises per-IdP role claim shapes —
Keycloak's nested realm_access.roles, flat repeated claims (Entra app
roles, Keycloak with a User Realm Role mapper, Cognito groups), and
JSON-array-as-string values — into canonical "roles" claims that
PermissionVerbHandler reads unchanged.

The parsing lives in a pure static RolesClaimExtractor in Infrastructure
so it can be unit-tested without an ASP.NET host; the IClaimsTransformation
implementation in Hosting is a thin wrapper that adds idempotency via a
sentinel claim. Removes the TODO on PermissionVerbHandler that the
transformation now resolves.
Without RequireAuthenticatedUser() in the policy, an unauthenticated
request reaches PermissionVerbHandler, finds no roles, and fails the
requirement — which ASP.NET classifies as FailedRequirements and turns
into a 403 Forbid. The OIDC acceptance tests expect 401 Challenge
(Should_reject_requests_without_bearer_token, ..._with_invalid/expired
/wrong_audience/wrong_issuer). Adding the auth requirement first ensures
the failure is classified as FailedAuthentication when no valid
principal is present.
The Primary, Audit, and Monitoring acceptance-test runners called
AddServiceControlAuthentication but not AddServiceControlAuthorization,
while production RunCommand wires both. Without the authorization
provider, ASP.NET cannot resolve the policy names emitted by the
Permissions catalogue and any request to a controller carrying
[Authorize(Policy = Permissions.X)] throws "AuthorizationPolicy named
'...' was not found" — which breaks the test hosts and times out the
audit acceptance tests that exercise authorized endpoints.
Should_accept_requests_with_valid_bearer_token previously passed in CI
by accident: AddServiceControlAuthorization was missing from the test
runners, so the policy provider could not resolve the [Authorize] policy
name and threw, returning 500 — which the assertion "not 401 and not
403" accepted. With the policy provider now wired up, a valid token
without any role claim correctly fails the permission requirement and
returns 403. The test must therefore supply the canonical "roles" claim
with a value that maps to a role granting the endpoint's permission.

"reader" grants every *:*:view permission, which covers the endpoints
the three acceptance-test variants exercise (error:messages:view,
audit:message:view, monitoring:endpoint:view).
The PlatformSampleSettings approval tests in SC, Audit, and Monitoring
unit-test projects serialise the settings object and diff it against an
approved JSON snapshot. The earlier Authentication.RolesClaim addition
needs the snapshots updated to include the new property.
The recent refactor moved Authentication.RoleBasedAuthorizationEnabled
to a separate master switch (default false) and made the entire
authorization registration short-circuit when it was off. That left the
permission policy provider unregistered in every default deployment, so
ASP.NET could not resolve the policy names emitted by [Authorize(Policy
= Permissions.X)] attributes — every annotated endpoint returned 500
with "AuthorizationPolicy named '...' was not found", which is what was
timing out the audit acceptance tests at 90s each.

PermissionPolicyProvider already returns allow-all policies for every
known permission when its oidcEnabled flag is false. Registering it
unconditionally and passing RoleBasedAuthorizationEnabled to that flag
gives the right behaviour in all three combinations: RBAC off →
allow-all (controllers reachable, no permission check), RBAC on →
require auth + the permission requirement evaluated by
PermissionVerbHandler. The handler itself remains gated on
RoleBasedAuthorizationEnabled since it has nothing to evaluate when
RBAC is off.
Authentication.RoleBasedAuthorizationEnabled defaults to false, so
without an explicit opt-in the policy provider returns allow-all and
unauthenticated requests reach the controller — breaking every
Should_reject_requests_* test in the three When_authentication_is_enabled
classes. Add WithRoleBasedAuthorizationEnabled() to the test
configuration helper and call it alongside WithAuthenticationEnabled()
in all three OIDC enabled fixtures.
Two settings shifted in the OpenIdConnectSettings serialisation:
RolesClaim moved after the ServicePulse block and its default changed
from "realm_access.roles" to "roles", and the new
RoleBasedAuthorizationEnabled property (default false) was added. Sync
the three approved snapshots so the approval tests reflect the current
shape.
Make the name of the claim for roles configurable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants