The Token Gatekeeper
Write the validation step of an API gateway: decide whether a decoded token is valid, expired, not yet valid or simply wrong, with clock-skew leeway.
Statement
Story
The IMBEGNAL gateway has already verified the signature of an access token and decoded its payload (the claims). What is left is the part that causes most real-world outages: the time checks. A server whose clock is a few seconds slow rejects fresh tokens, and one that forgets exp accepts a stolen token forever. You write the function that makes the final decision.
Your task
Implement token_status(claims, now, leeway) in Python or tokenStatus(claims, now, leeway) in JavaScript and return exactly one of these strings:
claimsis a dictionary / object of the decoded payload,nowis the current Unix time in whole seconds,leewayis the allowed clock skew in seconds (an integer, 0 or more).- Apply the rules in this order; the first one that fires decides:
malformed:expis missing or is not an integer, ornbfis present but is not an integer. Strings, booleans,null, lists, objects and numbers with a fractional part are not integers.wrong_issuer:issis not exactly the string"imbegnal"(missing, another case, another type: all wrong).wrong_audience:audis neither the string"api"nor a list that contains the string"api"(missing,"api-v2",[], a number: all wrong).not_yet_valid:nbfis present andnow + leeway < nbf.expired:now >= exp + leeway(at the exact expiry second the token is already expired).valid: none of the above. Any other claim (sub,iat,scope…) is ignored.
How it is run
A test driver calls your function once per case and prints the returned strings, one per line. Write only the function (and helpers); do not read input or print anything yourself.
Example
token_status({"iss": "imbegnal", "aud": "api", "exp": 1000}, 999, 0)
-> "valid"
token_status({"iss": "imbegnal", "aud": "api", "exp": 1000}, 1000, 0)
-> "expired"
token_status({"iss": "imbegnal", "aud": ["web", "api"], "nbf": 500, "exp": 1000}, 495, 4)
-> "not_yet_valid"
Sample input
[{"claims":{"iss":"imbegnal","aud":"api","exp":1000},"now":999,"leeway":0},{"claims":{"iss":"imbegnal","aud":"api","exp":1000},"now":1000,"leeway":0},{"claims":{"iss":"imbegnal","aud":["web","api"],"nbf":500,"exp":1000},"now":495,"leeway":4}]
Allowed languages: python, javascript
Hints
Stuck? Hints are optional, and each one lowers the points you earn for solving this challenge.
- Hint 1Costs 10 points
- Hint 2Costs 15 points
Reveal the previous hint first
- Hint 3Costs 20 points
Reveal the previous hint first
Lessons that teach this
Stuck on the ideas rather than the puzzle? These lessons cover what you need.