← All Exercises
Validate Token Claims Against a Policy
intermediatejavascript
Background
A signature check alone is not authentication. Claims must still be time-bounded and bound to the expected audience and issuer, or a token from another service is accepted.
Problem
Check a decoded token payload against a validation policy and return a sorted list of violation codes covering expiry, not-before, maximum token age, audience, and issuer.
Input
JSON object: { claims, now, policy: { maxAgeSeconds, audience?, issuer? } }Output
JSON array of violation codes (sorted, empty when valid)
Sample tests
Sample 1
Input: {"now":1700000100,"claims":{"exp":1700000000,"iat":1699990000,"aud":"api","iss":"auth"},"policy":{"maxAgeSeconds":86400,"audience":"api","issuer":"auth"}}Expected: ["expired"]
Sample 2
Input: {"now":1700000000,"claims":{"exp":1700003600,"nbf":1700000600,"iat":1700000000,"aud":"other","iss":"auth"},"policy":{"maxAgeSeconds":86400,"audience":"api","issuer":"auth"}}Expected: ["audience-mismatch","not-yet-valid"]
Your solution
Learning objective
Enforce claim-level token policy beyond signature verification.