44import hmac
55import json
66import os
7+ import sys
78from abc import ABC , abstractmethod
89from typing import (
910 TYPE_CHECKING ,
1314 NoReturn ,
1415 Union ,
1516 cast ,
17+ get_args ,
1618 overload ,
1719)
1820
7577 load_ssh_public_key ,
7678 )
7779
78- # pyjwt-964: we use these both for type checking below, as well as for validating the key passed in.
79- # in Py >= 3.10, we can replace this with the Union types below
80- ALLOWED_RSA_KEY_TYPES = (RSAPrivateKey , RSAPublicKey )
81- ALLOWED_EC_KEY_TYPES = (EllipticCurvePrivateKey , EllipticCurvePublicKey )
82- ALLOWED_OKP_KEY_TYPES = (
83- Ed25519PrivateKey ,
84- Ed25519PublicKey ,
85- Ed448PrivateKey ,
86- Ed448PublicKey ,
87- )
88- ALLOWED_KEY_TYPES = (
89- ALLOWED_RSA_KEY_TYPES + ALLOWED_EC_KEY_TYPES + ALLOWED_OKP_KEY_TYPES
90- )
91- ALLOWED_PRIVATE_KEY_TYPES = (
92- RSAPrivateKey ,
93- EllipticCurvePrivateKey ,
94- Ed25519PrivateKey ,
95- Ed448PrivateKey ,
96- )
97- ALLOWED_PUBLIC_KEY_TYPES = (
98- RSAPublicKey ,
99- EllipticCurvePublicKey ,
100- Ed25519PublicKey ,
101- Ed448PublicKey ,
102- )
80+ if sys .version_info >= (3 , 10 ):
81+ from typing import TypeAlias
82+ else :
83+ # Python 3.9 and lower
84+ from typing_extensions import TypeAlias
85+
86+ # Type aliases for convenience in algorithms method signatures
87+ AllowedRSAKeys : TypeAlias = Union [RSAPrivateKey , RSAPublicKey ]
88+ AllowedECKeys : TypeAlias = Union [EllipticCurvePrivateKey , EllipticCurvePublicKey ]
89+ AllowedOKPKeys : TypeAlias = Union [
90+ Ed25519PrivateKey , Ed25519PublicKey , Ed448PrivateKey , Ed448PublicKey
91+ ]
92+ AllowedKeys : TypeAlias = Union [AllowedRSAKeys , AllowedECKeys , AllowedOKPKeys ]
93+ #: Type alias for allowed ``cryptography`` private keys (requires ``cryptography`` to be installed)
94+ AllowedPrivateKeys : TypeAlias = Union [
95+ RSAPrivateKey , EllipticCurvePrivateKey , Ed25519PrivateKey , Ed448PrivateKey
96+ ]
97+ #: Type alias for allowed ``cryptography`` public keys (requires ``cryptography`` to be installed)
98+ AllowedPublicKeys : TypeAlias = Union [
99+ RSAPublicKey , EllipticCurvePublicKey , Ed25519PublicKey , Ed448PublicKey
100+ ]
103101
104102 if TYPE_CHECKING or bool (os .getenv ("SPHINX_BUILD" , "" )):
105- import sys
106-
107- if sys .version_info >= (3 , 10 ):
108- from typing import TypeAlias
109- else :
110- # Python 3.9 and lower
111- from typing_extensions import TypeAlias
112-
113103 from cryptography .hazmat .primitives .asymmetric .types import (
114104 PrivateKeyTypes ,
115105 PublicKeyTypes ,
116106 )
117107
118- # Type aliases for convenience in algorithms method signatures
119- AllowedRSAKeys : TypeAlias = Union [RSAPrivateKey , RSAPublicKey ]
120- AllowedECKeys : TypeAlias = Union [
121- EllipticCurvePrivateKey , EllipticCurvePublicKey
122- ]
123- AllowedOKPKeys : TypeAlias = Union [
124- Ed25519PrivateKey , Ed25519PublicKey , Ed448PrivateKey , Ed448PublicKey
125- ]
126- AllowedKeys : TypeAlias = Union [AllowedRSAKeys , AllowedECKeys , AllowedOKPKeys ]
127- #: Type alias for allowed ``cryptography`` private keys (requires ``cryptography`` to be installed)
128- AllowedPrivateKeys : TypeAlias = Union [
129- RSAPrivateKey , EllipticCurvePrivateKey , Ed25519PrivateKey , Ed448PrivateKey
130- ]
131- #: Type alias for allowed ``cryptography`` public keys (requires ``cryptography`` to be installed)
132- AllowedPublicKeys : TypeAlias = Union [
133- RSAPublicKey , EllipticCurvePublicKey , Ed25519PublicKey , Ed448PublicKey
134- ]
135-
136108 has_crypto = True
137109except ModuleNotFoundError :
110+ if sys .version_info >= (3 , 11 ):
111+ from typing import Never
112+ else :
113+ from typing_extensions import Never
114+
115+ AllowedRSAKeys = Never # type: ignore[misc]
116+ AllowedECKeys = Never # type: ignore[misc]
117+ AllowedOKPKeys = Never # type: ignore[misc]
118+ AllowedKeys = Never # type: ignore[misc]
119+ AllowedPrivateKeys = Never # type: ignore[misc]
120+ AllowedPublicKeys = Never # type: ignore[misc]
138121 has_crypto = False
139122
140123
@@ -417,7 +400,10 @@ class RSAAlgorithm(Algorithm):
417400 SHA384 : ClassVar [type [hashes .HashAlgorithm ]] = hashes .SHA384
418401 SHA512 : ClassVar [type [hashes .HashAlgorithm ]] = hashes .SHA512
419402
420- _crypto_key_types = ALLOWED_RSA_KEY_TYPES
403+ _crypto_key_types = cast (
404+ tuple [type [AllowedKeys ], ...],
405+ get_args (Union [RSAPrivateKey , RSAPublicKey ]),
406+ )
421407 _MIN_KEY_SIZE : ClassVar [int ] = 2048
422408
423409 def __init__ (self , hash_alg : type [hashes .HashAlgorithm ]) -> None :
@@ -434,7 +420,7 @@ def check_key_length(self, key: AllowedRSAKeys) -> str | None:
434420
435421 def prepare_key (self , key : AllowedRSAKeys | str | bytes ) -> AllowedRSAKeys :
436422 if isinstance (key , self ._crypto_key_types ):
437- return key
423+ return cast ( AllowedRSAKeys , key )
438424
439425 if not isinstance (key , (bytes , str )):
440426 raise TypeError ("Expecting a PEM-formatted key." )
@@ -602,7 +588,10 @@ class ECAlgorithm(Algorithm):
602588 SHA384 : ClassVar [type [hashes .HashAlgorithm ]] = hashes .SHA384
603589 SHA512 : ClassVar [type [hashes .HashAlgorithm ]] = hashes .SHA512
604590
605- _crypto_key_types = ALLOWED_EC_KEY_TYPES
591+ _crypto_key_types = cast (
592+ tuple [type [AllowedKeys ], ...],
593+ get_args (Union [EllipticCurvePrivateKey , EllipticCurvePublicKey ]),
594+ )
606595
607596 def __init__ (
608597 self ,
@@ -625,8 +614,9 @@ def _validate_curve(self, key: AllowedECKeys) -> None:
625614
626615 def prepare_key (self , key : AllowedECKeys | str | bytes ) -> AllowedECKeys :
627616 if isinstance (key , self ._crypto_key_types ):
628- self ._validate_curve (key )
629- return key
617+ ec_key = cast (AllowedECKeys , key )
618+ self ._validate_curve (ec_key )
619+ return ec_key
630620
631621 if not isinstance (key , (bytes , str )):
632622 raise TypeError ("Expecting a PEM-formatted key." )
@@ -840,7 +830,17 @@ class OKPAlgorithm(Algorithm):
840830 This class requires ``cryptography>=2.6`` to be installed.
841831 """
842832
843- _crypto_key_types = ALLOWED_OKP_KEY_TYPES
833+ _crypto_key_types = cast (
834+ tuple [type [AllowedKeys ], ...],
835+ get_args (
836+ Union [
837+ Ed25519PrivateKey ,
838+ Ed25519PublicKey ,
839+ Ed448PrivateKey ,
840+ Ed448PublicKey ,
841+ ]
842+ ),
843+ )
844844
845845 def __init__ (self , ** kwargs : Any ) -> None :
846846 pass
0 commit comments