diff --git a/mozilla_django_oidc_db/utils.py b/mozilla_django_oidc_db/utils.py index 32eff07..546c1ad 100644 --- a/mozilla_django_oidc_db/utils.py +++ b/mozilla_django_oidc_db/utils.py @@ -1,7 +1,7 @@ from collections.abc import Collection from copy import deepcopy -from glom import Path, assign, glom +from glom import Path, PathAccessError, assign, glom from requests.utils import _parse_content_type_header # type: ignore from .typing import ClaimPath, JSONObject, JSONValue @@ -32,7 +32,10 @@ def obfuscate_claims( copied_claims = deepcopy(claims) for claim_bits in claims_to_obfuscate: claim_path = Path(*claim_bits) - claim_value = glom(copied_claims, claim_path) + try: + claim_value = glom(copied_claims, claim_path) + except PathAccessError: + continue assign(copied_claims, claim_path, obfuscate_claim_value(claim_value)) return copied_claims diff --git a/tests/test_utils.py b/tests/test_utils.py index 05fba34..65573d6 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,3 +1,4 @@ +from mozilla_django_oidc_db.typing import JSONObject from mozilla_django_oidc_db.utils import obfuscate_claim_value, obfuscate_claims @@ -45,3 +46,11 @@ def test_obfuscate_nested(): result = obfuscate_claims(claims, claims_to_obfuscate) assert result == expected_result + + +def test_obfuscate_with_missing_claims(): + claims: JSONObject = {"present": "12345"} + + result = obfuscate_claims(claims, claims_to_obfuscate=(["missing"], ["present"])) + + assert result == {"present": "****5"}