2022-12-28 10:18:13 +00:00
|
|
|
__filename__ = "keys.py"
|
|
|
|
__author__ = "Bob Mottram"
|
|
|
|
__license__ = "AGPL3+"
|
2024-01-21 19:01:20 +00:00
|
|
|
__version__ = "1.5.0"
|
2022-12-28 10:18:13 +00:00
|
|
|
__maintainer__ = "Bob Mottram"
|
|
|
|
__email__ = "bob@libreserver.org"
|
|
|
|
__status__ = "Production"
|
|
|
|
__module_group__ = "ActivityPub"
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
|
|
def _get_local_private_key(base_dir: str, nickname: str, domain: str) -> str:
|
|
|
|
"""Returns the private key for a local account
|
|
|
|
"""
|
|
|
|
if not domain or not nickname:
|
|
|
|
return None
|
|
|
|
handle = nickname + '@' + domain
|
|
|
|
key_filename = base_dir + '/keys/private/' + handle.lower() + '.key'
|
|
|
|
if not os.path.isfile(key_filename):
|
|
|
|
return None
|
|
|
|
with open(key_filename, 'r', encoding='utf-8') as pem_file:
|
|
|
|
return pem_file.read()
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def _get_local_public_key(base_dir: str, nickname: str, domain: str) -> str:
|
|
|
|
"""Returns the public key for a local account
|
|
|
|
"""
|
|
|
|
if not domain or not nickname:
|
|
|
|
return None
|
|
|
|
handle = nickname + '@' + domain
|
|
|
|
key_filename = base_dir + '/keys/public/' + handle.lower() + '.key'
|
|
|
|
if not os.path.isfile(key_filename):
|
|
|
|
return None
|
|
|
|
with open(key_filename, 'r', encoding='utf-8') as pem_file:
|
|
|
|
return pem_file.read()
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def get_instance_actor_key(base_dir: str, domain: str) -> str:
|
|
|
|
"""Returns the private key for the instance actor used for
|
|
|
|
signing GET posts
|
|
|
|
"""
|
|
|
|
return _get_local_private_key(base_dir, 'inbox', domain)
|
|
|
|
|
|
|
|
|
|
|
|
def get_person_key(nickname: str, domain: str, base_dir: str,
|
2024-02-19 13:33:11 +00:00
|
|
|
key_type: str, debug: bool):
|
2022-12-28 10:18:13 +00:00
|
|
|
"""Returns the public or private key of a person
|
2024-02-19 13:33:11 +00:00
|
|
|
key_type can be private or public
|
2022-12-28 10:18:13 +00:00
|
|
|
"""
|
|
|
|
if key_type == 'private':
|
|
|
|
key_pem = _get_local_private_key(base_dir, nickname, domain)
|
|
|
|
else:
|
|
|
|
key_pem = _get_local_public_key(base_dir, nickname, domain)
|
|
|
|
if not key_pem:
|
|
|
|
if debug:
|
|
|
|
print('DEBUG: ' + key_type + ' key file not found')
|
|
|
|
return ''
|
|
|
|
if len(key_pem) < 20:
|
|
|
|
if debug:
|
|
|
|
print('DEBUG: private key was too short: ' + key_pem)
|
|
|
|
return ''
|
|
|
|
return key_pem
|