diff options
author | Danny Hermes <daniel.j.hermes@gmail.com> | 2015-04-15 15:44:30 -0700 |
---|---|---|
committer | Danny Hermes <daniel.j.hermes@gmail.com> | 2015-04-15 15:44:30 -0700 |
commit | 613ab465d47ad2be03b05c55eff005cb55db7bc5 (patch) | |
tree | 521711e874234f32d42a39d7c4c0d5769eb9b07a /oauth2client/crypt.py | |
parent | 0a6241c792f0c833f2f176292b0c7ea5623eabfd (diff) | |
download | oauth2client-613ab465d47ad2be03b05c55eff005cb55db7bc5.tar.gz |
Deferring OpenSSL import until usage.
This is to speed up import times. In OpenSSL 0.14 the import takes
0.5 seconds due to cffi on-demand build of extensions in the
cryptography library.
For classes and functions which are conditionally defined based on
the existence of OpenSSL.crypto, we check that the module
exists (but don't import it) using imp.find_module.
Diffstat (limited to 'oauth2client/crypt.py')
-rw-r--r-- | oauth2client/crypt.py | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/oauth2client/crypt.py b/oauth2client/crypt.py index 381f389..d66bef1 100644 --- a/oauth2client/crypt.py +++ b/oauth2client/crypt.py @@ -16,8 +16,10 @@ """Crypto-related routines for oauth2client.""" import base64 +import imp import json import logging +import os import sys import time @@ -37,7 +39,9 @@ class AppIdentityError(Exception): try: - from OpenSSL import crypto + _, _package_dir, _ = imp.find_module('OpenSSL') + if not os.path.isfile(os.path.join(_package_dir, 'crypto.py')): + raise ImportError('No module named OpenSSL') class OpenSSLVerifier(object): """Verifies the signature on a message.""" @@ -61,6 +65,7 @@ try: True if message was signed by the private key associated with the public key that this object was constructed with. """ + from OpenSSL import crypto try: if isinstance(message, six.text_type): message = message.encode('utf-8') @@ -84,6 +89,7 @@ try: Raises: OpenSSL.crypto.Error if the key_pem can't be parsed. """ + from OpenSSL import crypto if is_x509_cert: pubkey = crypto.load_certificate(crypto.FILETYPE_PEM, key_pem) else: @@ -111,6 +117,7 @@ try: Returns: string, The signature of the message for the given key. """ + from OpenSSL import crypto if isinstance(message, six.text_type): message = message.encode('utf-8') return crypto.sign(self._key, message, 'sha256') @@ -129,6 +136,7 @@ try: Raises: OpenSSL.crypto.Error if the key can't be parsed. """ + from OpenSSL import crypto parsed_pem_key = _parse_pem_key(key) if parsed_pem_key: pkey = crypto.load_privatekey(crypto.FILETYPE_PEM, parsed_pem_key) @@ -149,6 +157,7 @@ try: Returns: String. PEM contents of ``private_key_text``. """ + from OpenSSL import crypto decoded_body = base64.b64decode(private_key_text) if isinstance(private_key_password, six.string_types): private_key_password = private_key_password.encode('ascii') |