Skip to content

Commit

Permalink
Update py_webauthn and avoid pydantic deprecation warnings (refs #634)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpaniagualaconich authored and claudep committed Oct 2, 2023
1 parent 8b6171c commit bc9dea1
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
extras_require={
'call': ['twilio>=6.0'],
'sms': ['twilio>=6.0'],
'webauthn': ['webauthn>=1.6.0,<1.99'],
'webauthn': ['webauthn>=1.11.0,<1.99'],
'yubikey': ['django-otp-yubikey'],
'phonenumbers': ['phonenumbers>=7.0.9,<8.99'],
'phonenumberslite': ['phonenumberslite>=7.0.9,<8.99'],
Expand Down
17 changes: 12 additions & 5 deletions two_factor/plugins/webauthn/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@
from django.utils.module_loading import import_string
from django.utils.translation import gettext_lazy as _
from pydantic import ValidationError as PydanticValidationError
from webauthn.helpers.exceptions import InvalidAuthenticationResponse
from webauthn.helpers.exceptions import (
InvalidAuthenticationResponse, InvalidRegistrationResponse,
)
from webauthn.helpers.parse_authentication_credential_json import (
parse_authentication_credential_json,
)
from webauthn.helpers.parse_registration_credential_json import (
parse_registration_credential_json,
)
from webauthn.helpers.structs import (
PublicKeyCredentialRpEntity, PublicKeyCredentialUserEntity,
)
Expand All @@ -16,7 +24,6 @@

from .models import WebauthnDevice
from .utils import (
AuthenticationCredential, RegistrationCredential,
make_credential_creation_options, make_credential_request_options,
verify_authentication_response,
)
Expand Down Expand Up @@ -79,7 +86,7 @@ def _verify_token(self, user, token, device=None):
del self.request.session['webauthn_request_options']

try:
credential_id = AuthenticationCredential.parse_raw(token).id
credential_id = parse_authentication_credential_json(token).id
device = WebauthnDevice.objects.get(user=user, key_handle=credential_id)

new_sign_count = verify_authentication_response(
Expand Down Expand Up @@ -128,8 +135,8 @@ def clean_token(self):
token = self.cleaned_data['token']

try:
RegistrationCredential.parse_raw(token)
except PydanticValidationError as exc:
parse_registration_credential_json(token)
except InvalidRegistrationResponse as exc:
raise forms.ValidationError(_('Entered token is not valid.'), code='invalid_token') from exc

self.cleaned_data = {
Expand Down
2 changes: 1 addition & 1 deletion two_factor/plugins/webauthn/tests/test_views_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def test_setup_webauthn(self):
{'token': ['This field is required.']})

with mock.patch(
"two_factor.plugins.webauthn.forms.RegistrationCredential.parse_raw"
"two_factor.plugins.webauthn.forms.parse_registration_credential_json"
), mock.patch(
"two_factor.plugins.webauthn.method.verify_registration_response"
) as verify_registration_response:
Expand Down
11 changes: 5 additions & 6 deletions two_factor/plugins/webauthn/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@
)
from webauthn.helpers import base64url_to_bytes, bytes_to_base64url
from webauthn.helpers.structs import (
AttestationConveyancePreference, AuthenticationCredential,
AuthenticatorAttachment, AuthenticatorSelectionCriteria,
AuthenticatorTransport, PublicKeyCredentialDescriptor,
RegistrationCredential, UserVerificationRequirement,
AttestationConveyancePreference, AuthenticatorAttachment,
AuthenticatorSelectionCriteria, AuthenticatorTransport,
PublicKeyCredentialDescriptor, UserVerificationRequirement,
)


Expand Down Expand Up @@ -62,7 +61,7 @@ def verify_registration_response(expected_rp_id, expected_origin, expected_chall
:return: a tuple with the credential public key, id and current sign count
"""
verified_registration = webauthn_verify_registration_response(
credential=RegistrationCredential.parse_raw(registration_token),
credential=registration_token,
expected_challenge=base64url_to_bytes(expected_challenge),
expected_origin=expected_origin,
expected_rp_id=expected_rp_id,
Expand Down Expand Up @@ -130,7 +129,7 @@ def verify_authentication_response(
:return: the new sign count for the WebauthnDevice instance
"""
verified_authentication = webauthn_verify_authentication_response(
credential=AuthenticationCredential.parse_raw(authentication_token),
credential=authentication_token,
expected_challenge=base64url_to_bytes(expected_challenge),
expected_rp_id=expected_rp.id,
expected_origin=expected_origin,
Expand Down

0 comments on commit bc9dea1

Please sign in to comment.