From 6817cc832940c804d7502d0162ea551f30a546b0 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Mon, 6 Jul 2020 10:52:06 +0100 Subject: [PATCH] Functions for pgp fingerprints --- pgp.py | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 79 insertions(+), 2 deletions(-) diff --git a/pgp.py b/pgp.py index 7f24cc546..a9205606f 100644 --- a/pgp.py +++ b/pgp.py @@ -53,6 +53,28 @@ def getPGPpubKey(actorJson: {}) -> str: return '' +def getPGPfingerprint(actorJson: {}) -> str: + """Returns PGP fingerprint for the given actor + """ + if not actorJson.get('attachment'): + return '' + for propertyValue in actorJson['attachment']: + if not propertyValue.get('name'): + continue + if not propertyValue['name'].lower().startswith('openpgp'): + continue + if not propertyValue.get('type'): + continue + if not propertyValue.get('value'): + continue + if propertyValue['type'] != 'PropertyValue': + continue + if len(propertyValue['value']) < 10: + continue + return propertyValue['value'] + return '' + + def setEmailAddress(actorJson: {}, emailAddress: str) -> None: """Sets the email address for the given actor """ @@ -103,6 +125,13 @@ def setEmailAddress(actorJson: {}, emailAddress: str) -> None: def setPGPpubKey(actorJson: {}, PGPpubKey: str) -> None: """Sets a PGP public key for the given actor """ + removeKey = False + if not PGPpubKey: + removeKey = True + else: + if '--BEGIN PGP PUBLIC KEY' not in PGPpubKey: + removeKey = True + if not actorJson.get('attachment'): actorJson['attachment'] = [] @@ -119,8 +148,7 @@ def setPGPpubKey(actorJson: {}, PGPpubKey: str) -> None: break if propertyFound: actorJson['attachment'].remove(propertyValue) - - if '--BEGIN PGP PUBLIC KEY' not in PGPpubKey: + if removeKey: return for propertyValue in actorJson['attachment']: @@ -141,3 +169,52 @@ def setPGPpubKey(actorJson: {}, PGPpubKey: str) -> None: "value": PGPpubKey } actorJson['attachment'].append(newPGPpubKey) + + +def setPGPfingerprint(actorJson: {}, fingerprint: str) -> None: + """Sets a PGP fingerprint for the given actor + """ + removeFingerprint = False + if not fingerprint: + removeFingerprint = True + else: + if len(fingerprint) < 10: + removeFingerprint = True + + if not actorJson.get('attachment'): + actorJson['attachment'] = [] + + # remove any existing value + propertyFound = None + for propertyValue in actorJson['attachment']: + if not propertyValue.get('name'): + continue + if not propertyValue.get('type'): + continue + if not propertyValue['name'].lower().startswith('openpgp'): + continue + propertyFound = propertyValue + break + if propertyFound: + actorJson['attachment'].remove(propertyValue) + if removeFingerprint: + return + + for propertyValue in actorJson['attachment']: + if not propertyValue.get('name'): + continue + if not propertyValue.get('type'): + continue + if not propertyValue['name'].lower().startswith('openpgp'): + continue + if propertyValue['type'] != 'PropertyValue': + continue + propertyValue['value'] = fingerprint.strip() + return + + newPGPfingerprint = { + "name": "OpenPGP", + "type": "PropertyValue", + "value": fingerprint + } + actorJson['attachment'].append(newPGPfingerprint)