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

Commit 7fa0a84

Browse files
committed
improved init logic
1 parent 754a401 commit 7fa0a84

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

google/auth/crypt/rsa.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@
1919
for implmentations using different third party libraries
2020
"""
2121

22+
from cryptography.hazmat.primitives.asymmetric.rsa import RSAPrivateKey
23+
from cryptography.hazmat.primitives.asymmetric.rsa import RSAPublicKey
2224
from google.auth import _helpers
2325
from google.auth.crypt import base
2426
from google.auth.crypt import _cryptography_rsa
2527
from google.auth.crypt import _python_rsa
2628

2729
RSA_KEY_MODULE_PREFIX = "rsa.key"
28-
CRYPTOGRAPHY_KEY_MODULE_PREFIX = "cryptography."
30+
2931

3032
class RSAVerifier(base.Verifier):
3133
"""Verifies RSA cryptographic signatures using public keys.
@@ -45,10 +47,10 @@ class RSAVerifier(base.Verifier):
4547

4648
def __init__(self, public_key):
4749
module_str = public_key.__class__.__module__
48-
if module_str.startswith(RSA_KEY_MODULE_PREFIX):
49-
impl_lib = _python_rsa
50-
elif module_str.startswith(CRYPTOGRAPHY_KEY_MODULE_PREFIX):
50+
if isinstance(public_key, RSAPublicKey):
5151
impl_lib = _cryptography_rsa
52+
elif module_str.startswith(RSA_KEY_MODULE_PREFIX):
53+
impl_lib = _python_rsa
5254
else:
5355
raise ValueError(f"unrecognized public key type: {type(public_key)}")
5456
self._impl = impl_lib.RSAVerifier(public_key)
@@ -97,10 +99,10 @@ class RSASigner(base.Signer, base.FromServiceAccountMixin):
9799

98100
def __init__(self, private_key, key_id=None):
99101
module_str = private_key.__class__.__module__
100-
if module_str.startswith(RSA_KEY_MODULE_PREFIX):
101-
impl_lib = _python_rsa
102-
elif module_str.startswith(CRYPTOGRAPHY_KEY_MODULE_PREFIX):
102+
if isinstance(private_key, RSAPrivateKey):
103103
impl_lib = _cryptography_rsa
104+
elif module_str.startswith(RSA_KEY_MODULE_PREFIX):
105+
impl_lib = _python_rsa
104106
else:
105107
raise ValueError(f"unrecognized private key type: {type(private_key)}")
106108
self._impl = impl_lib.RSASigner(private_key, key_id=key_id)

0 commit comments

Comments
 (0)