From e692fac905c6639b5cd9a440d44dccb3b6b843ec Mon Sep 17 00:00:00 2001 From: Lorenz Diener Date: Fri, 31 May 2019 14:12:04 +0200 Subject: Make some dependencies optional --- docs/index.rst | 3 +++ mastodon/Mastodon.py | 23 ++++++++++++++++++++--- setup.py | 23 ++++++++++++++++++----- 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/docs/index.rst b/docs/index.rst index 52bac0f..45d8202 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -1135,6 +1135,9 @@ displayed. Mastodon allows an application to have one webpush subscription per user at a time. +All crypto utilities require Mastodon.pys optional "webpush" feature dependencies +(specifically, the "cryptography" and "http_ece" packages). + .. automethod:: Mastodon.push_subscription .. automethod:: Mastodon.push_subscription_set .. automethod:: Mastodon.push_subscription_update diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 7b6c801..37ca999 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py @@ -20,9 +20,20 @@ import threading import sys import six from decorator import decorate -from cryptography.hazmat.backends import default_backend -from cryptography.hazmat.primitives.asymmetric import ec -import http_ece + +IMPL_HAS_CRYPTO = True +try: + from cryptography.hazmat.backends import default_backend + from cryptography.hazmat.primitives.asymmetric import ec +except: + IMPL_HAS_CRYPTO = False + +IMPL_HAS_ECE = True +try: + import http_ece +except: + IMPL_HAS_ECE = False + import base64 import json import blurhash @@ -2306,6 +2317,9 @@ class Mastodon: Returns two dicts: One with the private key and shared secret and another with the public key and shared secret. """ + if not IMPL_HAS_CRYPTO: + raise NotImplementedError('To use the crypto tools, please install the webpush feature dependencies.') + push_key_pair = ec.generate_private_key(ec.SECP256R1(), default_backend()) push_key_priv = push_key_pair.private_numbers().private_value push_key_pub = push_key_pair.public_key().public_numbers().encode_point() @@ -2331,6 +2345,9 @@ class Mastodon: Returns the decoded webpush as a `push notification dict`_. """ + if (not IMPL_HAS_ECE) or (not IMPL_HAS_CRYPTO): + raise NotImplementedError('To use the crypto tools, please install the webpush feature dependencies.') + salt = self.__decode_webpush_b64(encryption_header.split("salt=")[1].strip()) dhparams = self.__decode_webpush_b64(crypto_key_header.split("dh=")[1].split(";")[0].strip()) p256ecdsa = self.__decode_webpush_b64(crypto_key_header.split("p256ecdsa=")[1].strip()) diff --git a/setup.py b/setup.py index 6b8c682..1bc3a17 100644 --- a/setup.py +++ b/setup.py @@ -1,12 +1,27 @@ from setuptools import setup -test_deps = ['pytest', 'pytest-runner', 'pytest-cov', 'vcrpy', 'pytest-vcr', 'pytest-mock', 'requests-mock'] +test_deps = [ + 'pytest', + 'pytest-runner', + 'pytest-cov', + 'vcrpy', + 'pytest-vcr', + 'pytest-mock', + 'requests-mock' +] + +webpush_deps = [ + 'http_ece>=1.0.5', + 'cryptography>=1.6.0', +] + extras = { - "test": test_deps + "test": test_deps, + "webpush": webpush_deps, } setup(name='Mastodon.py', - version='1.4.2', + version='1.4.3', description='Python wrapper for the Mastodon API', packages=['mastodon'], install_requires=[ @@ -16,8 +31,6 @@ setup(name='Mastodon.py', 'pytz', 'python-magic', 'decorator>=4.0.0', - 'http_ece>=1.0.5', - 'cryptography>=1.6.0', 'blurhash>=1.1.3', ], tests_require=test_deps, -- cgit v1.2.3