epicyon/enigma.py

75 lines
2.2 KiB
Python
Raw Normal View History

2021-12-11 10:53:38 +00:00
__filename__ = "enigma.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "1.2.0"
__maintainer__ = "Bob Mottram"
__email__ = "bob@libreserver.org"
__status__ = "Production"
__module_group__ = "Profile Metadata"
2021-12-28 17:20:43 +00:00
def get_enigma_pub_key(actor_json: {}) -> str:
2021-12-11 10:53:38 +00:00
"""Returns Enigma public key for the given actor
"""
2021-12-26 10:29:52 +00:00
if not actor_json.get('attachment'):
2021-12-11 10:53:38 +00:00
return ''
2021-12-26 10:32:45 +00:00
for property_value in actor_json['attachment']:
if not property_value.get('name'):
2021-12-11 10:53:38 +00:00
continue
2021-12-26 10:32:45 +00:00
if not property_value['name'].lower().startswith('enigma'):
2021-12-11 10:53:38 +00:00
continue
2021-12-26 10:32:45 +00:00
if not property_value.get('type'):
2021-12-11 10:53:38 +00:00
continue
2021-12-26 10:32:45 +00:00
if not property_value.get('value'):
2021-12-11 10:53:38 +00:00
continue
2021-12-26 10:32:45 +00:00
if property_value['type'] != 'PropertyValue':
2021-12-11 10:53:38 +00:00
continue
2021-12-26 10:32:45 +00:00
return property_value['value']
2021-12-11 10:53:38 +00:00
return ''
2022-01-02 12:40:33 +00:00
def set_enigma_pub_key(actor_json: {}, enigma_pub_key: str) -> None:
2021-12-11 10:53:38 +00:00
"""Sets a Enigma public key for the given actor
"""
2022-01-02 12:40:33 +00:00
remove_key = False
if not enigma_pub_key:
remove_key = True
2021-12-11 10:53:38 +00:00
2021-12-26 10:29:52 +00:00
if not actor_json.get('attachment'):
actor_json['attachment'] = []
2021-12-11 10:53:38 +00:00
# remove any existing value
2022-01-02 12:40:33 +00:00
property_found = None
2021-12-26 10:32:45 +00:00
for property_value in actor_json['attachment']:
if not property_value.get('name'):
2021-12-11 10:53:38 +00:00
continue
2021-12-26 10:32:45 +00:00
if not property_value.get('type'):
2021-12-11 10:53:38 +00:00
continue
2021-12-26 10:32:45 +00:00
if not property_value['name'].lower().startswith('enigma'):
2021-12-11 10:53:38 +00:00
continue
2022-01-02 12:40:33 +00:00
property_found = property_value
2021-12-11 10:53:38 +00:00
break
2022-01-02 12:40:33 +00:00
if property_found:
2021-12-26 10:32:45 +00:00
actor_json['attachment'].remove(property_value)
2022-01-02 12:40:33 +00:00
if remove_key:
2021-12-11 10:53:38 +00:00
return
2021-12-26 10:32:45 +00:00
for property_value in actor_json['attachment']:
if not property_value.get('name'):
2021-12-11 10:53:38 +00:00
continue
2021-12-26 10:32:45 +00:00
if not property_value.get('type'):
2021-12-11 10:53:38 +00:00
continue
2021-12-26 10:32:45 +00:00
if not property_value['name'].lower().startswith('enigma'):
2021-12-11 10:53:38 +00:00
continue
2021-12-26 10:32:45 +00:00
if property_value['type'] != 'PropertyValue':
2021-12-11 10:53:38 +00:00
continue
2022-01-02 12:40:33 +00:00
property_value['value'] = enigma_pub_key
2021-12-11 10:53:38 +00:00
return
2022-01-02 12:40:33 +00:00
new_enigma_pub_key = {
2021-12-11 10:53:38 +00:00
"name": "Enigma",
"type": "PropertyValue",
2022-01-02 12:40:33 +00:00
"value": enigma_pub_key
2021-12-11 10:53:38 +00:00
}
2022-01-02 12:40:33 +00:00
actor_json['attachment'].append(new_enigma_pub_key)