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

Jwt Protected Endpoints

medium—No data yet

Problem statement

Complete the implementation of MemberAccessSetup and MemberAreaController so the member area answers only callers who arrive with a token the club trusts, and turns everyone else away.

Sample Data

appsettings.json carries the three settings this club trusts, in its MemberAccess section:

"MemberAccess": {
  "Issuer": "https://members.meridianyachtclub.example",
  "Audience": "meridian-member-area",
  "SigningKey": "meridian-member-area-signing-key-2026-0001"
}

A member's 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 member's id, their display name and their home port:

{
  "member_id": "M-4187",
  "display_name": "Ines Farrow",
  "home_port": "Bergen"
}

Service

MemberAccessSetup

AddMemberAccess is handed the application's service collection and its configuration while the application is being put together, and is the only place authentication is registered.

  • 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 MemberAccess section of the configuration handed to the method. Literals in the code are wrong: a second copy of this application, configured for a different club, has to accept that club's tokens and refuse this one's.
  • 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.

MemberAreaController

  • Require an authenticated caller for both of its endpoints, GET /club/membership and GET /club/moorings. Declare that requirement on the endpoints themselves rather than inside the code that answers them, and leave GET /club/bulletin and GET / without it.
  • Accept any caller whose token passes validation, whatever else that token carries. Nothing beyond a valid token is asked of a member.
  • Both endpoint bodies are written already and read the caller's claims out of the request. Leave them as they are: a caller is turned away before a body runs, not by a body. A refused request never reaches them, and an accepted one reaches its endpoint exactly once.

Who the member area answers

Refusing a caller means answering 401 with a WWW-Authenticate header naming the Bearer scheme. The caller was never identified, so it is never 403.

GET /club/bulletin and GET / are outside the member area and answer 200 to a caller carrying no token at all.

Sample Requests & Responses

GET /club/membership with no Authorization header

Response code: 401

WWW-Authenticate: Bearer

GET /club/membership with a valid token for member M-4187

Response code: 200

{
  "memberId": "M-4187",
  "displayName": "Ines Farrow",
  "homePort": "Bergen"
}

GET /club/bulletin with no Authorization header

Response code: 200

{
  "notice": "The clubhouse opens at 07:00 every day."
}

More in Authentication and Authorization

See the module
Policy-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 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.