From b559b7a75f9e33a0d6cadd4989805d56bb204287 Mon Sep 17 00:00:00 2001 From: Martyn Smith Date: Tue, 1 Aug 2023 11:25:28 +1200 Subject: [PATCH 1/2] Support both v1 and v2 of Pydantic One of the nice parts about pydantic v2 is that they provide backward compability by importing from `pydantic.v1`. This adds support so that regardless of which version of pydantic the project is using, py_webauthn will work. --- requirements.txt | 6 ++++-- setup.py | 2 +- webauthn/helpers/decode_credential_public_key.py | 5 ++++- webauthn/helpers/parse_backup_flags.py | 5 ++++- webauthn/helpers/parse_client_data_json.py | 5 ++++- webauthn/helpers/structs.py | 8 ++++++-- 6 files changed, 23 insertions(+), 8 deletions(-) diff --git a/requirements.txt b/requirements.txt index 2399d93..0d48191 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ +annotated-types==0.5.0 asn1crypto==1.4.0 black==21.9b0 cbor2==5.4.2.post1 @@ -11,11 +12,12 @@ pathspec==0.9.0 platformdirs==2.4.0 pycodestyle==2.8.0 pycparser==2.20 -pydantic==1.10.11 +pydantic==2.1.1 +pydantic_core==2.4.0 pyflakes==2.4.0 pyOpenSSL==23.2.0 regex==2021.10.8 six==1.16.0 toml==0.10.2 tomli==1.2.1 -typing-extensions==4.2.0 +typing_extensions==4.7.1 diff --git a/setup.py b/setup.py index 0bbeed3..98d2120 100644 --- a/setup.py +++ b/setup.py @@ -51,7 +51,7 @@ def find_version(*file_paths): 'asn1crypto>=1.4.0', 'cbor2>=5.4.2.post1', 'cryptography>=41.0.1', - 'pydantic>=1.10.11,<2.0a0', + 'pydantic>=1.10.11', 'pyOpenSSL>=23.2.0', ] ) diff --git a/webauthn/helpers/decode_credential_public_key.py b/webauthn/helpers/decode_credential_public_key.py index 15ed499..4d09aad 100644 --- a/webauthn/helpers/decode_credential_public_key.py +++ b/webauthn/helpers/decode_credential_public_key.py @@ -1,7 +1,10 @@ from typing import Union from cbor2 import decoder -from pydantic import BaseModel +try: + from pydantic.v1 import BaseModel +except ImportError: + from pydantic import BaseModel from .cose import COSECRV, COSEKTY, COSEAlgorithmIdentifier, COSEKey from .exceptions import InvalidPublicKeyStructure, UnsupportedPublicKeyType diff --git a/webauthn/helpers/parse_backup_flags.py b/webauthn/helpers/parse_backup_flags.py index 9ca0bf7..1b0adae 100644 --- a/webauthn/helpers/parse_backup_flags.py +++ b/webauthn/helpers/parse_backup_flags.py @@ -1,5 +1,8 @@ from enum import Enum -from pydantic import BaseModel +try: + from pydantic.v1 import BaseModel +except ImportError: + from pydantic import BaseModel from .structs import AuthenticatorDataFlags, CredentialDeviceType from .exceptions import InvalidBackupFlags diff --git a/webauthn/helpers/parse_client_data_json.py b/webauthn/helpers/parse_client_data_json.py index 152b517..986234e 100644 --- a/webauthn/helpers/parse_client_data_json.py +++ b/webauthn/helpers/parse_client_data_json.py @@ -1,7 +1,10 @@ import json from json.decoder import JSONDecodeError -from pydantic import ValidationError +try: + from pydantic.v1 import ValidationError +except ImportError: + from pydantic import ValidationError from .base64url_to_bytes import base64url_to_bytes from .exceptions import InvalidClientDataJSONStructure diff --git a/webauthn/helpers/structs.py b/webauthn/helpers/structs.py index 5aaac61..93190f9 100644 --- a/webauthn/helpers/structs.py +++ b/webauthn/helpers/structs.py @@ -1,8 +1,12 @@ from enum import Enum from typing import List, Literal, Optional -from pydantic import BaseModel, validator -from pydantic.fields import ModelField +try: + from pydantic.v1 import BaseModel, validator + from pydantic.v1.fields import ModelField +except: + from pydantic import BaseModel, validator + from pydantic.fields import ModelField from .bytes_to_base64url import bytes_to_base64url from .cose import COSEAlgorithmIdentifier From d6fcc57b048a92ee10d12482edd25a2c488939ab Mon Sep 17 00:00:00 2001 From: Martyn Smith Date: Tue, 1 Aug 2023 12:17:43 +1200 Subject: [PATCH 2/2] Not sure this is the best solution, but seems to work --- mypy.ini | 2 +- webauthn/helpers/decode_credential_public_key.py | 2 +- webauthn/helpers/parse_backup_flags.py | 2 +- webauthn/helpers/parse_client_data_json.py | 2 +- webauthn/helpers/structs.py | 4 ++-- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/mypy.ini b/mypy.ini index 10e9fbf..28b95a7 100644 --- a/mypy.ini +++ b/mypy.ini @@ -1,5 +1,5 @@ [mypy] -plugins = pydantic.mypy +plugins = pydantic.v1.mypy python_version = 3.8 diff --git a/webauthn/helpers/decode_credential_public_key.py b/webauthn/helpers/decode_credential_public_key.py index 4d09aad..d9535d0 100644 --- a/webauthn/helpers/decode_credential_public_key.py +++ b/webauthn/helpers/decode_credential_public_key.py @@ -4,7 +4,7 @@ try: from pydantic.v1 import BaseModel except ImportError: - from pydantic import BaseModel + from pydantic import BaseModel # type: ignore from .cose import COSECRV, COSEKTY, COSEAlgorithmIdentifier, COSEKey from .exceptions import InvalidPublicKeyStructure, UnsupportedPublicKeyType diff --git a/webauthn/helpers/parse_backup_flags.py b/webauthn/helpers/parse_backup_flags.py index 1b0adae..7b9274a 100644 --- a/webauthn/helpers/parse_backup_flags.py +++ b/webauthn/helpers/parse_backup_flags.py @@ -2,7 +2,7 @@ try: from pydantic.v1 import BaseModel except ImportError: - from pydantic import BaseModel + from pydantic import BaseModel # type: ignore from .structs import AuthenticatorDataFlags, CredentialDeviceType from .exceptions import InvalidBackupFlags diff --git a/webauthn/helpers/parse_client_data_json.py b/webauthn/helpers/parse_client_data_json.py index 986234e..47c2eb6 100644 --- a/webauthn/helpers/parse_client_data_json.py +++ b/webauthn/helpers/parse_client_data_json.py @@ -4,7 +4,7 @@ try: from pydantic.v1 import ValidationError except ImportError: - from pydantic import ValidationError + from pydantic import ValidationError # type: ignore from .base64url_to_bytes import base64url_to_bytes from .exceptions import InvalidClientDataJSONStructure diff --git a/webauthn/helpers/structs.py b/webauthn/helpers/structs.py index 93190f9..3004980 100644 --- a/webauthn/helpers/structs.py +++ b/webauthn/helpers/structs.py @@ -5,8 +5,8 @@ from pydantic.v1 import BaseModel, validator from pydantic.v1.fields import ModelField except: - from pydantic import BaseModel, validator - from pydantic.fields import ModelField + from pydantic import BaseModel, validator # type: ignore + from pydantic.fields import ModelField # type: ignore from .bytes_to_base64url import bytes_to_base64url from .cose import COSEAlgorithmIdentifier