From 9c63f2becc0caa1f684852224375b54f828cc42e Mon Sep 17 00:00:00 2001 From: drebs Date: Mon, 3 Dec 2012 11:28:21 -0200 Subject: LeapDocument can set and get 'valid' encrypted json --- src/leap/soledad/leap.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/leap/soledad/leap.py b/src/leap/soledad/leap.py index 08330618..863e63f8 100644 --- a/src/leap/soledad/leap.py +++ b/src/leap/soledad/leap.py @@ -26,21 +26,27 @@ class LeapDocument(Document): Returns document's json serialization encrypted with user's public key. """ # TODO: replace for openpgp encryption with users's pub key. - return base64.b64encode(self.get_json()) + return json.dumps({'cyphertext':base64.b64encode(self.get_json())}) - def set_encrypted_json(self): + def set_encrypted_json(self, encrypted_json): """ Set document's content based on encrypted version of json string. """ # TODO: # - replace for openpgp decryption using user's priv key. # - raise error if unsuccessful. - return self.set_json(base64.b64decode(self.get_json())) + cyphertext = json.loads(encrypted_json)['cyphertext'] + plaintext = base64.b64decode(cyphertext) + return self.set_json(plaintext) class LeapSyncTarget(HTTPSyncTarget): def _parse_sync_stream(self, data, return_doc_cb, ensure_callback=None): + """ + Does the same as parent's method but ensures incoming content will be + decrypted. + """ parts = data.splitlines() # one at a time if not parts or parts[0] != '[': raise BrokenSyncStream @@ -75,6 +81,9 @@ class LeapSyncTarget(HTTPSyncTarget): def sync_exchange(self, docs_by_generations, source_replica_uid, last_known_generation, last_known_trans_id, return_doc_cb, ensure_callback=None): + """ + Does the same as parent's method but encrypts content before syncing. + """ self._ensure_connection() if self._trace_hook: # for tests self._trace_hook('sync_exchange') -- cgit v1.2.3