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

Minimum Tenure Requirement

medium—No data yet

Problem statement

Complete the implementation of MinimumTenureRequirement, MinimumTenureHandler and PayrollAuthorization so that the payroll views of Larkspur Mills open only to staff who have served long enough.

Sample Data

Callers arrive with a signed token. A token that belongs to somebody on the payroll carries a hireDate claim holding that person's first day, written as yyyy-MM-dd and nothing else:

{
  "sub": "e-4417",
  "name": "Rosa Iversen",
  "hireDate": "2010-05-04"
}

Not every token carries one. The claim can be absent, it can arrive under some other name, and it can hold something that is not a date in that form, such as 01/03/2015, 2015-3-1 or not-a-date.

The host is configured with the minimum number of whole years of service at Payroll:MinimumTenureYears. It ships as 2 and is not always 2. The application reads today's date from the TimeProvider the host registers rather than from the system clock; that clock ships reading 2019-04-10 and is not always that date either.

The payroll book is pre-seeded with three payslips, two tax documents and a two entry pay calendar.

Service

MinimumTenureRequirement

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

MinimumTenureHandler

Decides whether a caller meets a MinimumTenureRequirement. 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 the hire date must land on or before today. The minimum is inclusive, so a caller hired exactly Years years ago meets it and one hired a day later does not.

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

PayrollAuthorization

  • AddPayrollAuthorization: register exactly one MinimumTenureHandler as the decision that judges the requirement, and define a policy named TenuredStaff carrying one MinimumTenureRequirement built from the configured minimum.
  • GuardPayrollGroup: put the TenuredStaff policy on the payroll group it is handed, so that it covers every view the group holds, including views mapped into the group after this method has returned.

Who a guarded view answers

  • 401 to a caller whose token is missing, expired or otherwise unusable, carrying a WWW-Authenticate header that names the Bearer scheme.
  • 403, and no WWW-Authenticate header, to a caller whose token is good but whose service is too short.
  • 200 to a caller who meets the minimum.

A refused call never reaches a payroll view. Each view records that it ran, and a refusal leaves that tally where it was.

GET /payroll/calendar is not part of the group. It stays open to anyone signed in, however short their service, and answers 401 to a caller with no token at all.

Sample Requests & Responses

Today reads 2019-04-10 and the minimum is 2 years throughout these samples.

GET /payroll/payslips with a token carrying "hireDate": "2010-05-04"

Response code: 200

[
  { "reference": "PS-2019-01", "period": "2019-01", "gross": 4200.00, "net": 3010.75 },
  { "reference": "PS-2019-02", "period": "2019-02", "gross": 4200.00, "net": 3010.75 },
  { "reference": "PS-2019-03", "period": "2019-03", "gross": 4600.00, "net": 3268.25 }
]

GET /payroll/calendar with a token carrying "hireDate": "2018-08-01"

Response code: 200

[
  { "period": "2019-04", "paidOn": "2019-04-26" },
  { "period": "2019-05", "paidOn": "2019-05-28" }
]

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 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.