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

Multi-Tier Access API

medium100%1 of 1 solved

Problem statement

Complete the implementation of CrewTokenSetup, LongServiceRequirement, LongServiceHandler and AccessRules so that the Northbound Air crew API serves four tiers of caller out of one deployment, each tier with its own rule.

Sample Data

appsettings.json carries the settings this deployment runs on:

"CrewTokens": {
  "Issuer": "https://crew.northboundair.example",
  "Audience": "northbound-crew-services",
  "SigningKey": "northbound-crew-services-signing-key-2024"
},
"Seniority": {
  "Today": "2024-03-01",
  "MinimumYears": "3"
}

A crew token is a JSON Web Token signed with HMAC-SHA256 over the UTF-8 bytes of SigningKey. It names Issuer as its issuer and Audience as its audience, it carries an expiry, and its claims are the crew member's crewId, their crewName, the day they joined as joinedOn written yyyy-MM-dd, and one crewRole claim for every role they hold:

{
  "crewId": "NB-2071",
  "crewName": "Tove Kranz",
  "joinedOn": "2022-06-15",
  "crewRole": "dispatcher"
}

Not every token carries all of that. A token may hold no crewRole at all, or several. joinedOn can be absent, can arrive under another name, can hold something that is not a date written that way, such as 01/03/2021, 2021-3-1 or not-a-date, and can hold a day still to come.

The application reads today's date from the clock the host registers rather than from the system clock. That clock ships reading 2024-03-01 and is not always that date, and Seniority:MinimumYears ships as 3 and is not always 3. None of these settings belongs in the code as a literal: a second deployment, configured for another airline, has to trust its own settings and refuse this one's.

Service

CrewTokenSetup

AddCrewTokens is handed the application's service collection and its configuration while the application is being put together, and is the only place a caller's token becomes an identity.

  • Register bearer token authentication as the application's default scheme, under its usual scheme name Bearer, so a caller the application cannot identify is challenged for a bearer token.
  • Take the issuer, the audience and the signing key from the CrewTokens section of the configuration handed to the method.
  • Accept a token only when all four of these hold: its signature matches the configured signing key, its issuer is the configured issuer, its audience is the configured audience, and its expiry has not passed.
  • Allow no grace period around the expiry. A token that ran out thirty seconds ago is expired and is refused. The framework's default is a five minute allowance, which would accept it.
  • Treat crewRole as the claim that carries the caller's roles, so a rule asking for a role reads that claim and no other.

LongServiceRequirement

The demand the pay tier makes: a minimum number of whole years of service, held on a public int property named Years.

LongServiceHandler

Decides whether a caller meets a LongServiceRequirement. It reaches that verdict from the caller's own claims and the application's clock, so it answers the same whether or not there is a request in front of it.

Service is counted in whole years: adding Years years to joinedOn must land on or before today. The minimum is inclusive, so a caller who joined exactly Years years ago meets it and one who joined a day later does not.

Everything else leaves the requirement unmet, and none of it is an error. A missing joinedOn claim, a value that is not a date written yyyy-MM-dd, and a joining date still to come are all refusals, reached without throwing and without letting a failure escape into the response.

AccessRules

AddAccessRules defines the two named rules and registers the decision behind the second one:

  • OpsConsole, the name the provided OpsPolicy constant holds, is a role requirement allowing the single role dispatcher. A caller holding dispatcher among several roles satisfies it. A caller whose role name merely contains that word, such as dispatcher-trainee or chiefdispatcher, does not.
  • LongService, the name the provided LongServicePolicy constant holds, carries one LongServiceRequirement built from the configured minimum, judged by exactly one LongServiceHandler.

GuardTiers is handed the four route groups this API is built from, and settles what each one asks of a caller. A rule put on a group covers every route the group holds, including routes mapped into it after this method has returned:

  • the status group: open to every caller, carrying no rule at all.
  • the crew group: any caller the application could identify, with nothing further asked of them.
  • the ops group: the OpsConsole rule, by name.
  • the pay group: the LongService rule, by name.

Who each tier answers

Five kinds of caller reach this API: one carrying no token, one carrying a token the application cannot read, a crew member whose token carries no crewRole and too little service, a dispatcher whose service is also too short, and a long serving crew member holding no role. Each endpoint answers them like this:

  • GET /service/status and GET / answer 200 to all five. A token the application cannot read is ignored here rather than refused, because nothing on this tier asks anything of the caller.
  • GET /crew/roster answers 200 to the last three and 401 to the first two.
  • GET /ops/dispatch-log and GET /ops/standby-list answer 200 to the dispatcher, 403 to the plain crew member and to the long serving one, and 401 to the first two.
  • GET /pay/statements answers 200 to the long serving crew member, 403 to the plain crew member and to the dispatcher, and 401 to the first two.

The two rules are independent: the dispatcher role opens nothing on the pay tier, long service opens nothing on the ops tier, and a caller holding both reaches every tier.

Refusing a caller the application never identified is 401, with an empty body and a WWW-Authenticate header naming the Bearer scheme. Refusing a caller it did identify is 403, with an empty body and no such header.

A refused caller never reaches the endpoint behind the tier. Each of the five endpoints records that it ran, and a refusal leaves that tally where it was.

Sample Requests & Responses

GET /service/status with no Authorization header

Response code: 200

{
  "service": "crew-services",
  "state": "available",
  "stations": 3
}

GET /crew/roster with a valid token for crew member NB-2071

Response code: 200

{
  "crewId": "NB-2071",
  "name": "Tove Kranz",
  "joinedOn": "2022-06-15",
  "duties": ["NB417 OSL-TRD", "NB418 TRD-OSL"]
}

GET /crew/roster with no Authorization header

Response code: 401

WWW-Authenticate: Bearer

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 yetmediumPolicy-Gated Admin AreaThis problem covers letting only the people who hold an administrator role into the admin part of an API, while everyone else who is signed in keeps the rest of it and a caller with no sign-in is asked to sign in first.—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 yetmedium

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.