Skip to content
This repository was archived by the owner on Mar 6, 2026. It is now read-only.

Commit 6aa7f41

Browse files
committed
rewrote classes to assume cryptography and rsa are both present
1 parent d6ec87f commit 6aa7f41

2 files changed

Lines changed: 26 additions & 59 deletions

File tree

google/auth/crypt/rsa.py

Lines changed: 25 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,11 @@
2222
from google.auth import _helpers
2323
from google.auth.crypt import base
2424
from 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

4131
class 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

9985
class 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

google/auth/exceptions.py

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,4 @@ class TimeoutError(GoogleAuthError):
105105

106106

107107
class ResponseError(GoogleAuthError):
108-
"""Used to indicate an error occurred when reading an HTTP response."""
109-
110-
111-
class MissingOptionalDependencyError(ImportError):
112-
"""Raised when a user attempts to use a class that requires an optional dependency"""
113-
114-
@classmethod
115-
def create(cls, caller, dependency_name, suffix_str=None):
116-
"""Creates an instance referencing the required dependency and the triggering class"""
117-
caller_cls = caller if isinstance(caller, type) else type(caller)
118-
msg_str = (
119-
f"{caller_cls.__name__} requires `{dependency_name}` optional dependency."
120-
)
121-
if suffix_str:
122-
msg_str = f"{msg_str} {suffix_str}"
123-
return cls(msg_str)
108+
"""Used to indicate an error occurred when reading an HTTP response."""

0 commit comments

Comments
 (0)