| 
									
										
										
										
											2020-04-03 18:52:18 +00:00
										 |  |  | __filename__ = "pgp.py" | 
					
						
							|  |  |  | __author__ = "Bob Mottram" | 
					
						
							|  |  |  | __license__ = "AGPL3+" | 
					
						
							|  |  |  | __version__ = "1.1.0" | 
					
						
							|  |  |  | __maintainer__ = "Bob Mottram" | 
					
						
							|  |  |  | __email__ = "bob@freedombone.net" | 
					
						
							|  |  |  | __status__ = "Production" | 
					
						
							| 
									
										
										
										
											2019-12-17 20:44:18 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def getEmailAddress(actorJson: {}) -> str: | 
					
						
							|  |  |  |     """Returns the email address 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('email'): | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  |         if not propertyValue.get('type'): | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  |         if not propertyValue.get('value'): | 
					
						
							|  |  |  |             continue | 
					
						
							| 
									
										
										
										
											2020-04-03 18:52:18 +00:00
										 |  |  |         if propertyValue['type'] != 'PropertyValue': | 
					
						
							| 
									
										
										
										
											2019-12-17 20:44:18 +00:00
										 |  |  |             continue | 
					
						
							|  |  |  |         if '@' not in propertyValue['value']: | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  |         if '.' not in propertyValue['value']: | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  |         return propertyValue['value'] | 
					
						
							|  |  |  |     return '' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-03 18:52:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-17 20:44:18 +00:00
										 |  |  | def getPGPpubKey(actorJson: {}) -> str: | 
					
						
							|  |  |  |     """Returns PGP public key 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('pgp'): | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  |         if not propertyValue.get('type'): | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  |         if not propertyValue.get('value'): | 
					
						
							|  |  |  |             continue | 
					
						
							| 
									
										
										
										
											2020-04-03 18:52:18 +00:00
										 |  |  |         if propertyValue['type'] != 'PropertyValue': | 
					
						
							| 
									
										
										
										
											2019-12-17 20:44:18 +00:00
										 |  |  |             continue | 
					
						
							| 
									
										
										
										
											2019-12-17 20:58:50 +00:00
										 |  |  |         if '--BEGIN PGP PUBLIC KEY' not in propertyValue['value']: | 
					
						
							| 
									
										
										
										
											2019-12-17 20:44:18 +00:00
										 |  |  |             continue | 
					
						
							|  |  |  |         return propertyValue['value'] | 
					
						
							|  |  |  |     return '' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-03 18:52:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-06 09:52:06 +00:00
										 |  |  | 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 '' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-03 18:52:18 +00:00
										 |  |  | def setEmailAddress(actorJson: {}, emailAddress: str) -> None: | 
					
						
							| 
									
										
										
										
											2019-12-17 20:44:18 +00:00
										 |  |  |     """Sets the email address for the given actor
 | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2020-07-06 10:25:18 +00:00
										 |  |  |     notEmailAddress = False | 
					
						
							|  |  |  |     if '@' not in emailAddress: | 
					
						
							|  |  |  |         notEmailAddress = True | 
					
						
							|  |  |  |     if '.' not in emailAddress: | 
					
						
							|  |  |  |         notEmailAddress = True | 
					
						
							| 
									
										
										
										
											2020-12-12 15:31:28 +00:00
										 |  |  |     if '<' in emailAddress: | 
					
						
							|  |  |  |         notEmailAddress = True | 
					
						
							| 
									
										
										
										
											2020-07-06 10:25:18 +00:00
										 |  |  |     if emailAddress.startswith('@'): | 
					
						
							|  |  |  |         notEmailAddress = True | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-17 20:44:18 +00:00
										 |  |  |     if not actorJson.get('attachment'): | 
					
						
							| 
									
										
										
										
											2020-04-03 18:52:18 +00:00
										 |  |  |         actorJson['attachment'] = [] | 
					
						
							| 
									
										
										
										
											2019-12-17 20:44:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-17 23:35:59 +00:00
										 |  |  |     # remove any existing value | 
					
						
							| 
									
										
										
										
											2020-04-03 18:52:18 +00:00
										 |  |  |     propertyFound = None | 
					
						
							| 
									
										
										
										
											2019-12-17 23:35:59 +00:00
										 |  |  |     for propertyValue in actorJson['attachment']: | 
					
						
							|  |  |  |         if not propertyValue.get('name'): | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  |         if not propertyValue.get('type'): | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  |         if not propertyValue['name'].lower().startswith('email'): | 
					
						
							|  |  |  |             continue | 
					
						
							| 
									
										
										
										
											2020-04-03 18:52:18 +00:00
										 |  |  |         propertyFound = propertyValue | 
					
						
							| 
									
										
										
										
											2019-12-17 23:35:59 +00:00
										 |  |  |         break | 
					
						
							|  |  |  |     if propertyFound: | 
					
						
							|  |  |  |         actorJson['attachment'].remove(propertyFound) | 
					
						
							| 
									
										
										
										
											2020-07-06 10:25:18 +00:00
										 |  |  |     if notEmailAddress: | 
					
						
							| 
									
										
										
										
											2019-12-17 20:44:18 +00:00
										 |  |  |         return | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for propertyValue in actorJson['attachment']: | 
					
						
							|  |  |  |         if not propertyValue.get('name'): | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  |         if not propertyValue.get('type'): | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  |         if not propertyValue['name'].lower().startswith('email'): | 
					
						
							|  |  |  |             continue | 
					
						
							| 
									
										
										
										
											2020-04-03 18:52:18 +00:00
										 |  |  |         if propertyValue['type'] != 'PropertyValue': | 
					
						
							| 
									
										
										
										
											2019-12-17 20:44:18 +00:00
										 |  |  |             continue | 
					
						
							| 
									
										
										
										
											2020-04-03 18:52:18 +00:00
										 |  |  |         propertyValue['value'] = emailAddress | 
					
						
							| 
									
										
										
										
											2019-12-17 20:44:18 +00:00
										 |  |  |         return | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-03 18:52:18 +00:00
										 |  |  |     newEmailAddress = { | 
					
						
							| 
									
										
										
										
											2019-12-17 20:44:18 +00:00
										 |  |  |         "name": "Email", | 
					
						
							|  |  |  |         "type": "PropertyValue", | 
					
						
							|  |  |  |         "value": emailAddress | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     actorJson['attachment'].append(newEmailAddress) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-03 18:52:18 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def setPGPpubKey(actorJson: {}, PGPpubKey: str) -> None: | 
					
						
							| 
									
										
										
										
											2019-12-17 20:44:18 +00:00
										 |  |  |     """Sets a PGP public key for the given actor
 | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2020-07-06 09:52:06 +00:00
										 |  |  |     removeKey = False | 
					
						
							|  |  |  |     if not PGPpubKey: | 
					
						
							|  |  |  |         removeKey = True | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         if '--BEGIN PGP PUBLIC KEY' not in PGPpubKey: | 
					
						
							|  |  |  |             removeKey = True | 
					
						
							| 
									
										
										
										
											2020-12-12 15:31:28 +00:00
										 |  |  |         if '<' in PGPpubKey: | 
					
						
							|  |  |  |             removeKey = True | 
					
						
							| 
									
										
										
										
											2020-07-06 09:52:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-17 20:44:18 +00:00
										 |  |  |     if not actorJson.get('attachment'): | 
					
						
							| 
									
										
										
										
											2020-04-03 18:52:18 +00:00
										 |  |  |         actorJson['attachment'] = [] | 
					
						
							| 
									
										
										
										
											2019-12-17 20:44:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-17 23:35:59 +00:00
										 |  |  |     # remove any existing value | 
					
						
							| 
									
										
										
										
											2020-04-03 18:52:18 +00:00
										 |  |  |     propertyFound = None | 
					
						
							| 
									
										
										
										
											2019-12-17 23:35:59 +00:00
										 |  |  |     for propertyValue in actorJson['attachment']: | 
					
						
							|  |  |  |         if not propertyValue.get('name'): | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  |         if not propertyValue.get('type'): | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  |         if not propertyValue['name'].lower().startswith('pgp'): | 
					
						
							|  |  |  |             continue | 
					
						
							| 
									
										
										
										
											2020-04-03 18:52:18 +00:00
										 |  |  |         propertyFound = propertyValue | 
					
						
							| 
									
										
										
										
											2019-12-17 23:35:59 +00:00
										 |  |  |         break | 
					
						
							|  |  |  |     if propertyFound: | 
					
						
							|  |  |  |         actorJson['attachment'].remove(propertyValue) | 
					
						
							| 
									
										
										
										
											2020-07-06 09:52:06 +00:00
										 |  |  |     if removeKey: | 
					
						
							| 
									
										
										
										
											2019-12-17 20:44:18 +00:00
										 |  |  |         return | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for propertyValue in actorJson['attachment']: | 
					
						
							|  |  |  |         if not propertyValue.get('name'): | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  |         if not propertyValue.get('type'): | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  |         if not propertyValue['name'].lower().startswith('pgp'): | 
					
						
							|  |  |  |             continue | 
					
						
							| 
									
										
										
										
											2020-04-03 18:52:18 +00:00
										 |  |  |         if propertyValue['type'] != 'PropertyValue': | 
					
						
							| 
									
										
										
										
											2019-12-17 20:44:18 +00:00
										 |  |  |             continue | 
					
						
							| 
									
										
										
										
											2020-04-03 18:52:18 +00:00
										 |  |  |         propertyValue['value'] = PGPpubKey | 
					
						
							| 
									
										
										
										
											2019-12-17 20:44:18 +00:00
										 |  |  |         return | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-03 18:52:18 +00:00
										 |  |  |     newPGPpubKey = { | 
					
						
							| 
									
										
										
										
											2019-12-17 20:44:18 +00:00
										 |  |  |         "name": "PGP", | 
					
						
							|  |  |  |         "type": "PropertyValue", | 
					
						
							|  |  |  |         "value": PGPpubKey | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     actorJson['attachment'].append(newPGPpubKey) | 
					
						
							| 
									
										
										
										
											2020-07-06 09:52:06 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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) |