Skip to content

Commit

Permalink
fix(jwt): update claims validation logic, allow value to be False or 0
Browse files Browse the repository at this point in the history
ref: #23
  • Loading branch information
lepture committed Jun 15, 2024
1 parent 18ae2e2 commit ab7d909
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/joserfc/rfc7519/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ def check_value(self, claim_name: str, value: Any) -> None:
option = self.options.get(claim_name)
if option:
allow_blank = option.get("allow_blank")
if not allow_blank and not value:
if not allow_blank and value == "":
raise InvalidClaimError(claim_name)

option_value = option.get("value")
if option_value and value != option_value:
if option_value is not None and value != option_value:
raise InvalidClaimError(claim_name)

option_values = option.get("values")
if option_values and value not in option_values:
if option_values is not None and value not in option_values:
raise InvalidClaimError(claim_name)

def validate(self, claims: Dict[str, Any]) -> None:
Expand Down
5 changes: 5 additions & 0 deletions tests/jwt/test_claims.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ def test_essential_empty_value(self):
self.assertRaises(MissingClaimError, claims_requests.validate, {"sub": None})
claims_requests.validate({"sub": ""})

def test_essential_false_value(self):
claims_requests = jwt.JWTClaimsRegistry(foo={"essential": True})
claims_requests.validate({"foo": False})
claims_requests.validate({"foo": 0})

def test_option_value(self):
claims_requests = jwt.JWTClaimsRegistry(sub={"essential": True, "value": "123"})
self.assertRaises(InvalidClaimError, claims_requests.validate, {"sub": "a"})
Expand Down

0 comments on commit ab7d909

Please sign in to comment.