Problem statement
Complete the implementation of AdminAreaPolicy and AdminEndpoints so that the admin area of the support console admits administrators only, and the routes outside it are left as they stand.
Sample Data
A caller reaches the console with a bearer token that carries its user name and one role entry for every role it holds. Reading that token is already wired up: a request carrying a usable one arrives signed in with those roles, and a request carrying none, or one that has expired or cannot be read, arrives as an anonymous caller.
The workspace behind the admin area holds three active sessions, three audit entries and two announcements.
Service
AdminAreaPolicy
The application hands its authorization options to AdminAreaPolicy.Add while it starts. Define one policy there:
- Give it the name AdminArea, which is the value the provided Name constant holds.
- Give it a role requirement, and let it allow the single role admin. A caller holding admin among several roles satisfies it. A caller whose role name merely contains that word, such as admin-support or superadmin, does not.
AdminEndpoints
MapAdminEndpoints creates the /admin group and maps three routes into it. Apply the AdminArea policy, by its name, to the group itself, so that it gates every route the group holds:
- GET /admin/console, GET /admin/audit-entries and POST /admin/announcements, the three this method maps.
- GET /admin/sessions, which another part of the application adds to the same group after MapAdminEndpoints has returned it. Gating the first three one at a time leaves this one open.
A caller the policy turns away never reaches the handler behind the route it asked for.
Who the admin area answers
- A signed-in caller holding the admin role gets the route's own answer, 200.
- A signed-in caller without that role gets 403 and an empty body.
- A caller carrying no token, an expired token or a token that cannot be read gets 401 and an empty body, never 403, and the answer carries the header WWW-Authenticate: Bearer.
Nothing outside the /admin group is gated by the policy. GET /profile goes on answering any signed-in caller, and GET / goes on answering a caller who is not signed in at all.
Sample Requests & Responses
GET /admin/console with a token carrying the admin role
Response code: 200
{ "activeSessions": 3, "auditEntries": 3, "announcements": 2 }
GET /profile with a token carrying the agent role
Response code: 200
{ "name": "marta.vella", "roles": ["agent"] }
