2222from google .auth import _helpers
2323from google .auth .crypt import base
2424from google .auth .exceptions import MissingOptionalDependencyError
25+ from google .auth .crypt import _cryptography_rsa
26+ from google .auth .crypt import _python_rsa
2527
26- try :
27- # Attempt import of module that requires optional `cryptography` dependency
28- from google .auth .crypt import _cryptography_rsa
29- except ImportError : # pragma: NO COVER
30- _cryptography_rsa = None
31-
32- try :
33- # Attempt import of module that requires optional (deprecated) `rsa` dependency
34- from google .auth .crypt import _python_rsa
35- except ImportError : # pragma: NO COVER
36- _python_rsa = None
37-
38- RSA_NOTE = "(Note: `rsa` is also supported for legacy compatibility, but is deprecated)"
39-
28+ RSA_KEY_MODULE_PREFIX = "rsa.key"
29+ CRYPTOGRAPHY_KEY_MODULE_PREFIX = "cryptography."
4030
4131class RSAVerifier (base .Verifier ):
4232 """Verifies RSA cryptographic signatures using public keys.
@@ -51,22 +41,18 @@ class RSAVerifier(base.Verifier):
5141 public_key (Union[rsa.key.PublicKey, cryptography.hazmat.primitives.asymmetric.rsa.RSAPublicKey]):
5242 The public key used to verify signatures.
5343 Raises:
54- ImportError: if neither `cryptography` or `rsa` is installed
5544 ValueError: if an unrecognized public key is provided
5645 """
5746
5847 def __init__ (self , public_key ):
5948 module_str = public_key .__class__ .__module__
60- if "rsa.key" in module_str :
49+ if module_str . startswith ( RSA_KEY_MODULE_PREFIX ) :
6150 impl_lib = _python_rsa
62- elif "cryptography." in module_str :
51+ elif module_str . startswith ( CRYPTOGRAPHY_KEY_MODULE_PREFIX ) :
6352 impl_lib = _cryptography_rsa
6453 else :
6554 raise ValueError (f"unrecognized public key type: { public_key } " )
66- if impl_lib is None :
67- raise MissingOptionalDependencyError .create (self , "cryptography" , RSA_NOTE )
68- else :
69- self ._impl = impl_lib .RSAVerifier (public_key )
55+ self ._impl = impl_lib .RSAVerifier (public_key )
7056
7157 @_helpers .copy_docstring (base .Verifier )
7258 def verify (self , message , signature ):
@@ -86,14 +72,14 @@ def from_string(cls, public_key):
8672
8773 Raises:
8874 ValueError: If the public_key can't be parsed.
89- ImportError: if neither `cryptography` or `rsa` is installe
9075 """
91- if _cryptography_rsa :
92- return _cryptography_rsa .RSAVerifier .from_string (public_key )
93- elif _python_rsa :
94- return _python_rsa .RSAVerifier .from_string (public_key )
95- else :
96- raise MissingOptionalDependencyError .create (cls , "cryptography" , RSA_NOTE )
76+ try :
77+ instance = cls (None )
78+ except ValueError :
79+ # ignore exception when creating instnce without associated key
80+ pass
81+ instance ._impl = _cryptography_rsa .RSAVerifier .from_string (public_key )
82+ return instance
9783
9884
9985class RSASigner (base .Signer , base .FromServiceAccountMixin ):
@@ -113,22 +99,18 @@ class RSASigner(base.Signer, base.FromServiceAccountMixin):
11399 public key or certificate.
114100
115101 Raises:
116- ImportError: if neither `cryptography` or `rsa` is installed
117102 ValueError: if an unrecognized public key is provided
118103 """
119104
120105 def __init__ (self , private_key , key_id = None ):
121106 module_str = private_key .__class__ .__module__
122- if "rsa.key" in module_str :
107+ if module_str . startswith ( RSA_KEY_MODULE_PREFIX ) :
123108 impl_lib = _python_rsa
124- elif "cryptography." in module_str :
125- impl_lib = _cryptography_rsa
109+ elif module_str . startswith ( CRYPTOGRAPHY_KEY_MODULE_PREFIX ) :
110+ impl_lib = _cryptography_rs
126111 else :
127112 raise ValueError (f"unrecognized private key type: { private_key } " )
128- if impl_lib is None :
129- raise MissingOptionalDependencyError .create (self , "cryptography" , RSA_NOTE )
130- else :
131- self ._impl = impl_lib .RSASigner (private_key , key_id = key_id )
113+ self ._impl = impl_lib .RSASigner (private_key , key_id = key_id )
132114
133115 @property # type: ignore
134116 @_helpers .copy_docstring (base .Signer )
@@ -153,11 +135,11 @@ def from_string(cls, key, key_id=None):
153135 Raises:
154136 ValueError: If the key cannot be parsed as PKCS#1 or PKCS#8 in
155137 PEM format.
156- ImportError: if neither `cryptography` or `rsa` is installed
157138 """
158- if _cryptography_rsa :
159- return _cryptography_rsa .RSASigner .from_string (key , key_id = key_id )
160- elif _python_rsa :
161- return _python_rsa .RSASigner .from_string (key , key_id = key_id )
162- else :
163- raise MissingOptionalDependencyError .create (cls , "cryptography" , RSA_NOTE )
139+ try :
140+ instance = cls (None )
141+ except ValueError :
142+ # ignore exception when creating instnce without associated key
143+ pass
144+ instance ._impl = _cryptography_rsa .RSASigner .from_string (key , key_id = key_id )
145+ return instance
0 commit comments