Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of optional fields such as user_handle. #180

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ target/
# Jupyter Notebook
.ipynb_checkpoints

# PyCharm
.idea

# pyenv
.python-version

Expand Down
11 changes: 6 additions & 5 deletions webauthn/helpers/structs.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,14 @@ def _pydantic_v2_validate_bytes_fields(
"""
field = cls.model_fields[info.field_name] # type: ignore[attr-defined]

if field.annotation != bytes:
# UserHandle is defined as Optional[bytes] which is represented as:
# field.annotation = typing.Optional[bytes]. So we handle that explicitly here.
if field.annotation != bytes and info.field_name != 'user_handle': # type: ignore[attr-defined]
return v

if isinstance(v, str):
# NOTE:
# Ideally we should only do this when info.mode == "json", but
# that does not work when using the deprecated parse_raw method
# base64 encoding is the standard used when serializing bytes for JSON
# requests. For direct python the field value is assumed to already be bytes.
if isinstance(v, str) and info.mode == "json": # type: ignore[attr-defined]
MasterKale marked this conversation as resolved.
Show resolved Hide resolved
return base64url_to_bytes(v)

return _to_bytes(v)
Expand Down