diff --git a/modules/fediverse/__init__.py b/modules/fediverse/__init__.py index dd465d6b..c53f248c 100644 --- a/modules/fediverse/__init__.py +++ b/modules/fediverse/__init__.py @@ -1,6 +1,6 @@ import urllib.parse from src import IRCBot, ModuleManager, utils -from . import ap_actor, ap_server, ap_utils +from . import ap_actor, ap_security, ap_server, ap_utils def _format_username(username, instance): return "@%s@%s" % (username, instance) @@ -25,6 +25,9 @@ class Module(ModuleManager.BaseModule): raise ValueError("`tls-key` not provided in bot config") if not "tls-certificate" in self.bot.config: raise ValueError("`tls-certificate` not provided in bot config") + if not ap_security.has_crypto: + raise ValueError("cyprography library is not installed " + "(https://pypi.org/project/cryptography/)") server_username, instance = ap_utils.split_username(server_username) self.server = ap_server.Server(self.bot, self.exports, diff --git a/modules/fediverse/ap_security.py b/modules/fediverse/ap_security.py index 1ffd3fdb..391c58de 100644 --- a/modules/fediverse/ap_security.py +++ b/modules/fediverse/ap_security.py @@ -1,12 +1,16 @@ import base64, typing -from cryptography.hazmat.primitives import hashes, serialization -from cryptography.hazmat.primitives.asymmetric import padding, rsa -from cryptography.hazmat.backends import default_backend + +try: + from cryptography.hazmat.primitives import hashes, serialization + from cryptography.hazmat.primitives.asymmetric import padding, rsa + from cryptography.hazmat.backends import default_backend + has_crypto = True +except ModuleNotFoundError: + has_crypto = False SIGNATURE_FORMAT = ( "keyId=\"%s\",headers=\"%s\",signature=\"%s\",algorithm=\"rsa-sha256\"") - def _private_key(key_filename: str) -> rsa.RSAPrivateKey: with open(key_filename, "rb") as key_file: return serialization.load_pem_private_key(