From 994eaa79b274c3c37af42cb343c41b5dec6e8d19 Mon Sep 17 00:00:00 2001 From: drebs Date: Sun, 18 Dec 2016 21:27:02 -0200 Subject: [feat] use cookies in the client syncer --- .../leap/soledad/client/http_target/__init__.py | 26 +++++++++++++++++----- client/src/leap/soledad/client/sqlcipher.py | 9 ++++++-- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/client/src/leap/soledad/client/http_target/__init__.py b/client/src/leap/soledad/client/http_target/__init__.py index 0e250bf1..590ae8f6 100644 --- a/client/src/leap/soledad/client/http_target/__init__.py +++ b/client/src/leap/soledad/client/http_target/__init__.py @@ -24,10 +24,14 @@ after receiving. import os -from leap.soledad.common.log import getLogger -from leap.common.certs import get_compatible_ssl_context_factory +from cookielib import CookieJar + from twisted.web.client import Agent +from twisted.web.client import CookieAgent from twisted.internet import reactor + +from leap.common.certs import get_compatible_ssl_context_factory +from leap.soledad.common.log import getLogger from leap.soledad.client.http_target.send import HTTPDocSender from leap.soledad.client.http_target.api import SyncTargetAPI from leap.soledad.client.http_target.fetch import HTTPDocFetcher @@ -43,6 +47,14 @@ if os.environ.get('SOLEDAD_STATS'): DO_STATS = True +def newCookieAgent(cert_file): + _factory = get_compatible_ssl_context_factory(cert_file) + _agent = Agent(reactor, _factory) + _cookieJar = CookieJar() + agent = CookieAgent(_agent, _cookieJar) + return agent + + class SoledadHTTPSyncTarget(SyncTargetAPI, HTTPDocSender, HTTPDocFetcher): """ @@ -54,7 +66,8 @@ class SoledadHTTPSyncTarget(SyncTargetAPI, HTTPDocSender, HTTPDocFetcher): the parsed documents that the remote send us, before being decrypted and written to the main database. """ - def __init__(self, url, source_replica_uid, creds, crypto, cert_file): + def __init__(self, url, source_replica_uid, creds, crypto, cert_file, + agent=None): """ Initialize the sync target. @@ -72,6 +85,8 @@ class SoledadHTTPSyncTarget(SyncTargetAPI, HTTPDocSender, HTTPDocFetcher): the SSL certificate used by the remote soledad server. :type cert_file: str + :param agent: an http agent + :type agent: twisted.web.client.Agent """ if url.endswith("/"): url = url[:-1] @@ -86,8 +101,9 @@ class SoledadHTTPSyncTarget(SyncTargetAPI, HTTPDocSender, HTTPDocFetcher): self._insert_doc_cb = None # Twisted default Agent with our own ssl context factory - self._http = Agent(reactor, - get_compatible_ssl_context_factory(cert_file)) + if not agent: + agent = newCookieAgent(cert_file) + self._http = agent if DO_STATS: self.sync_exchange_phase = [0] diff --git a/client/src/leap/soledad/client/sqlcipher.py b/client/src/leap/soledad/client/sqlcipher.py index c9a9444e..9b352bbf 100644 --- a/client/src/leap/soledad/client/sqlcipher.py +++ b/client/src/leap/soledad/client/sqlcipher.py @@ -59,6 +59,7 @@ from leap.soledad.common.l2db.backends import sqlite_backend from leap.soledad.common.errors import DatabaseAccessError from leap.soledad.client.http_target import SoledadHTTPSyncTarget +from leap.soledad.client.http_target import newCookieAgent from leap.soledad.client.sync import SoledadSynchronizer from leap.soledad.client import pragmas @@ -397,7 +398,6 @@ class SQLCipherU1DBSync(SQLCipherDatabase): ENCRYPT_LOOP_PERIOD = 1 def __init__(self, opts, soledad_crypto, replica_uid, cert_file): - self._opts = opts self._path = opts.path self._crypto = soledad_crypto @@ -407,6 +407,10 @@ class SQLCipherU1DBSync(SQLCipherDatabase): # storage for the documents received during a sync self.received_docs = [] + # setup an http agent capable of storing cookies, so we can use + # server's session persistence feature + self._agent = newCookieAgent(cert_file) + self.running = False self._db_handle = None @@ -491,7 +495,8 @@ class SQLCipherU1DBSync(SQLCipherDatabase): self._replica_uid, creds=creds, crypto=self._crypto, - cert_file=self._cert_file)) + cert_file=self._cert_file, + agent=self._agent)) # # Symmetric encryption of syncing docs -- cgit v1.2.3