StaffCoder
.NET
.NET.NET 10
12 modules · 48 challengesSign up
Module 12 · Authentication and Authorization

Policy-Gated Admin Area

medium—No data yet

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"] }

More in Authentication and Authorization

See the module
Jwt Protected EndpointsThis problem covers letting a web application answer only the callers who arrive with a signed pass it trusts, turning everyone else away, and handing the caller's own details to the code that answers them.—No data yetmediumMinimum Tenure RequirementThis problem covers letting a set of payroll pages be opened only by staff who have been with the company long enough, working that out from the hire date the caller's sign-in carries and turning everyone else away without an error.—No data yetmediumMulti-Tier Access APIThis problem covers letting one API serve four kinds of page at once, one open to everybody, one to anybody signed in, one to a named group of staff and one only to staff who have been there long enough, and answering every other caller with the right refusal.100%1 of 1 solvedmedium

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.