Workspace/Coding labs
Loading progress

Enforce a scoped action grant

Intermediate45 min

Implement authorize(request, grant, now) -> bool. Request must have exactly tenant, audience, action, resource, destination, units. Grant must have exactly tenant, audience, actions, resources, destinations, max_units, expires. Identity and names are nonempty strings; units and max_units are nonnegative integers excluding booleans; now/expires are integers. Grant scopes are lists of nonempty strings. Allow only matching tenant/audience, allowed action/resource/destination, units <= max_units, and now < expires. Return False on malformed data. This assumes the grant and identity context are already authenticated by a trusted caller; it is not token validation.

Your task

  1. Complete the starter function using the contract above.
  2. Use the examples and visible tests to check normal inputs, boundaries, and rejected inputs.
  3. Run tests to record your result, then compare with the explained reference solution.

Examples

EXAMPLE 1

Inputrequest tenant A, action draft, resource D1, destination local, units 3; grant A allows draft/D1/local with max_units 5 and expires 100; now 99

OutputTrue

Every material field is within scope.
EXAMPLE 2

Inputsame request at now 100

OutputFalse

Expiry is exclusive.
solution.pyPython 3.12