How JWT claim validation works
masterVerification can include custom logic via claim_spec. A claim_spec is a Lua table where keys match payload keys, and values are validator functions.
Validator Signature:
function(val, claim, jwt_json)
val: The value of the claim being tested (ornil).claim: The name of the claim.jwt_json: The JSON-serialized representation of the object.
Rules:
- Return
trueorfalsefor success/failure. - A validator may raise an error; if it does, validation fails and the error is stored in the
reasonfield of the resulting object. - If a validator returns
nil, it is treated as a success (assuming it would have raised an error if it failed). - Use the special claim
__jwtto access a deep clone of the entire parsed JWT object as thevalparameter.
local claim_spec = {
sub = function(val) return string.match("^[a-z]+$", val) end,
__jwt = function(val, claim, jwt_json)
if val.payload.foo == nil then
error("Missing foo claim")
end
end
}
local jwt_obj = jwt:verify(key, token, claim_spec)