Skip to content

Commit

Permalink
Handle null or empty string values in UserInfo
Browse files Browse the repository at this point in the history
The spec allows null values or empty strings in UserInfo responses.

Make the client accept those values but normalize and drop empty values when parsing the message.
  • Loading branch information
schlenk committed Nov 11, 2022
1 parent 9896e2e commit c8cab2f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The format is based on the [KeepAChangeLog] project.

### Changed
- [#827] Added support for python 3.11
- [#830], [#831] Allow null and empty values in UserInfo responses, but filter them out.

### Fixed
- [#826], [#829] Fixed RP-Initiated Logout To Accept id_token_hint
Expand All @@ -21,6 +22,8 @@ The format is based on the [KeepAChangeLog] project.
[#827]: https://github.com/OpenIDC/pyoidc/issues/827
[#826]: https://github.com/OpenIDC/pyoidc/issues/826
[#829]: https://github.com/OpenIDC/pyoidc/pull/829
[#830]: https://github.com/OpenIDC/pyoidc/issues/830
[#831]: https://github.com/OpenIDC/pyoidc/pull/831

## 1.4.0 [2022-05-23]

Expand Down
12 changes: 11 additions & 1 deletion src/oic/oic/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,10 +561,20 @@ class OpenIDSchema(Message):
"_claim_sources": OPTIONAL_MESSAGE,
}

def from_dict(self, dictionary, **kwargs):
result = super().from_dict(dictionary, **kwargs)
# The spec allows empty fields in the UserInfo/IdToken response, but suggests
# the OP should omit those. So lets drop them here.
for key_ in [
key_ for key_, val in self._dict.items() if val is None or val == ""
]:
del self[key_]
return result

def verify(self, **kwargs):
super().verify(**kwargs)

if "birthdate" in self and self["birthdate"]:
if "birthdate" in self:
# Either YYYY-MM-DD or just YYYY or 0000-MM-DD
try:
time.strptime(self["birthdate"], "%Y-%m-%d")
Expand Down
22 changes: 18 additions & 4 deletions tests/test_oic_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,24 @@ def _eq(l1, l2):
return set(l1) == set(l2)


def test_openidschema():
inp = '{"middle_name":null, "updated_at":"20170328081544", "sub":"abc", "birthdate": null}'
ois = OpenIDSchema().from_json(inp)
assert ois.verify() is False
@pytest.mark.parametrize(
"json_param,claim",
[
(
'{"middle_name":null, "updated_at":"20170328081544", "sub":"abc"}',
"middle_name",
),
('{"birthdate":null, "updated_at":"20170328081544", "sub":"abc"}', "birthdate"),
(
'{"family_name": "", "updated_at":"20170328081544", "sub":"abc"}',
"family_name",
),
],
)
def test_openidschema(json_param, claim):
ois = OpenIDSchema().from_json(json_param)
assert ois.verify() is True
assert claim not in ois


@pytest.mark.parametrize(
Expand Down

0 comments on commit c8cab2f

Please sign in to comment.