summaryrefslogtreecommitdiff
path: root/service/pixelated
diff options
context:
space:
mode:
authorFolker Bernitt <fbernitt@thoughtworks.com>2015-11-04 10:11:19 +0100
committerFolker Bernitt <fbernitt@thoughtworks.com>2015-11-04 12:02:53 +0100
commitd79aa00e3c24c5bf5e5ed5ba5a9b976f93034362 (patch)
tree87a32a2e85d04b3bebc2ceb4aa56b447488940b6 /service/pixelated
parentffa355c46eda5d05f25890420218845ac3e53f71 (diff)
Instantiate new MailSender in Services
- Issue #499 - Some smaller refactorings - Extract smtp cert download to own class
Diffstat (limited to 'service/pixelated')
-rw-r--r--service/pixelated/adapter/services/mail_sender.py14
-rw-r--r--service/pixelated/bitmask_libraries/provider.py7
-rw-r--r--service/pixelated/bitmask_libraries/session.py61
-rw-r--r--service/pixelated/bitmask_libraries/smtp.py33
-rw-r--r--service/pixelated/config/services.py10
5 files changed, 68 insertions, 57 deletions
diff --git a/service/pixelated/adapter/services/mail_sender.py b/service/pixelated/adapter/services/mail_sender.py
index a3315b53..ca1e99d7 100644
--- a/service/pixelated/adapter/services/mail_sender.py
+++ b/service/pixelated/adapter/services/mail_sender.py
@@ -30,12 +30,9 @@ class SMTPDownException(Exception):
class MailSender(object):
- def __init__(self, account_email_address, keymanager, cert_path, remote_smtp_host, remote_smtp_port):
- self._from = account_email_address
+ def __init__(self, smtp_config, keymanager):
+ self._smtp_config = smtp_config
self._keymanager = keymanager
- self._cert_path = cert_path
- self._remote_smtp_host = remote_smtp_host
- self._remote_smtp_port = remote_smtp_port
def sendmail(self, mail):
recipients = flatten([mail.to, mail.cc, mail.bcc])
@@ -48,7 +45,12 @@ class MailSender(object):
return defer.gatherResults(deferreds)
def _create_outgoing_mail(self):
- return OutgoingMail(str(self._from), self._keymanager, unicode(self._cert_path), unicode(self._cert_path), str(self._remote_smtp_host), int(self._remote_smtp_port))
+ return OutgoingMail(str(self._smtp_config.account_email),
+ self._keymanager,
+ self._smtp_config.cert_path,
+ self._smtp_config.cert_path,
+ str(self._smtp_config.remote_smtp_host),
+ int(self._smtp_config.remote_smtp_port))
class LocalSmtpMailSender(object):
diff --git a/service/pixelated/bitmask_libraries/provider.py b/service/pixelated/bitmask_libraries/provider.py
index 071b0bbf..a529208d 100644
--- a/service/pixelated/bitmask_libraries/provider.py
+++ b/service/pixelated/bitmask_libraries/provider.py
@@ -138,10 +138,3 @@ class LeapProvider(object):
def address_for(self, username):
return '%s@%s' % (username, self.domain)
-
- def _client_cert_path(self):
- return os.path.join(
- self.config.leap_home,
- "providers",
- self.domain,
- "keys", "client", "smtp.pem")
diff --git a/service/pixelated/bitmask_libraries/session.py b/service/pixelated/bitmask_libraries/session.py
index 4a503628..3f8e6de6 100644
--- a/service/pixelated/bitmask_libraries/session.py
+++ b/service/pixelated/bitmask_libraries/session.py
@@ -26,7 +26,7 @@ from leap.mail.incoming.service import IncomingMail
from leap.auth import SRPAuth
from leap.mail.imap.account import IMAPAccount
from .nicknym import NickNym
-from .smtp import LeapSmtp
+from .smtp import LeapSmtp, LeapSMTPConfig
from .soledad import SoledadSessionFactory
from leap.common.events import (
@@ -123,6 +123,36 @@ class LeapSession(object):
raise
+class SmtpCertDownloader(object):
+
+ def __init__(self, provider, auth):
+ self._provider = provider
+ self._auth = auth
+
+ def download(self):
+ cert_url = '%s/%s/cert' % (self._provider.api_uri, self._provider.api_version)
+ cookies = {"_session_id": self._auth.session_id}
+ headers = {}
+ headers["Authorization"] = 'Token token="{0}"'.format(self._auth.token)
+ response = requests.get(
+ cert_url,
+ verify=LeapCertificate(self._provider).provider_api_cert,
+ cookies=cookies,
+ timeout=self._provider.config.timeout_in_s,
+ headers=headers)
+ response.raise_for_status()
+
+ client_cert = response.content
+
+ return client_cert
+
+ def download_to(self, target_file):
+ client_cert = self.download()
+
+ with open(target_file, 'w') as f:
+ f.write(client_cert)
+
+
class LeapSessionFactory(object):
def __init__(self, provider):
self._provider = provider
@@ -151,34 +181,27 @@ class LeapSessionFactory(object):
nicknym = self._create_nicknym(account_email, auth.token, auth.uuid, soledad)
self._download_smtp_cert(auth)
- smtp = LeapSmtp(self._provider, auth, nicknym.keymanager)
- # TODO: Create the new mail sender based on what we have in available LeapSmtp, e.g. the certs
+ smtp_host, smtp_port = self._provider.smtp_info()
+ smtp_config = LeapSMTPConfig(account_email, self._smtp_client_cert_path(), smtp_host, smtp_port)
+ smtp = LeapSmtp(smtp_config, nicknym.keymanager)
return LeapSession(self._provider, auth, mail_store, soledad, nicknym, smtp)
def _download_smtp_cert(self, auth):
- cert_path = self._provider._client_cert_path()
+ cert_path = self._smtp_client_cert_path()
if not os.path.exists(os.path.dirname(cert_path)):
os.makedirs(os.path.dirname(cert_path))
- cert_url = '%s/%s/cert' % (self._provider.api_uri, self._provider.api_version)
- cookies = {"_session_id": auth.session_id}
- headers = {}
- headers["Authorization"] = 'Token token="{0}"'.format(auth.token)
- response = requests.get(
- cert_url,
- verify=LeapCertificate(self._provider).provider_api_cert,
- cookies=cookies,
- timeout=self._provider.config.timeout_in_s,
- headers=headers)
- response.raise_for_status()
-
- client_cert = response.content
+ SmtpCertDownloader(self._provider, auth).download_to(cert_path)
- with open(cert_path, 'w') as f:
- f.write(client_cert)
+ def _smtp_client_cert_path(self):
+ return os.path.join(
+ self._config.leap_home,
+ "providers",
+ self._provider.domain,
+ "keys", "client", "smtp.pem")
def _lookup_session(self, key):
global SESSIONS
diff --git a/service/pixelated/bitmask_libraries/smtp.py b/service/pixelated/bitmask_libraries/smtp.py
index 63d2d310..f4ab00f7 100644
--- a/service/pixelated/bitmask_libraries/smtp.py
+++ b/service/pixelated/bitmask_libraries/smtp.py
@@ -14,41 +14,40 @@
# You should have received a copy of the GNU Affero General Public License
# along with Pixelated. If not, see <http://www.gnu.org/licenses/>.
import logging
-import os
-import requests
import random
from leap.mail.smtp import setup_smtp_gateway
-from pixelated.bitmask_libraries.certs import LeapCertificate
logger = logging.getLogger(__name__)
+class LeapSMTPConfig(object):
+
+ def __init__(self, account_email, cert_path, remote_smtp_host, remote_smtp_port):
+ self.account_email = account_email
+ self.cert_path = cert_path
+ self.remote_smtp_host = remote_smtp_host
+ self.remote_smtp_port = remote_smtp_port
+
+
class LeapSmtp(object):
- def __init__(self, provider, auth, keymanager=None):
+ def __init__(self, smtp_config, keymanager=None):
self.local_smtp_port_number = random.randrange(12000, 16000)
- self._provider = provider
- self.username = auth.username
- self.session_id = auth.session_id
- self.user_token = auth.token
+ self._smtp_config = smtp_config
self._keymanager = keymanager
- self._remote_hostname, self._remote_port = provider.smtp_info()
self._local_smtp_service_socket = None
self._local_smtp_service = None
def start(self):
- cert_path = self._provider._client_cert_path()
- email = '%s@%s' % (self.username, self._provider.domain)
-
self._local_smtp_service, self._local_smtp_service_socket = setup_smtp_gateway(
port=self.local_smtp_port_number,
- userid=str(email),
+ userid=str(self._smtp_config.account_email),
keymanager=self._keymanager,
- smtp_host=self._remote_hostname.encode('UTF-8'),
- smtp_port=self._remote_port,
- smtp_cert=cert_path,
- smtp_key=cert_path,
+ smtp_host=self._smtp_config.remote_smtp_host.encode('UTF-8'),
+ smtp_port=self._smtp_config.remote_smtp_port,
+ smtp_cert=self._smtp_config.cert_path,
+ smtp_key=self._smtp_config.cert_path,
encrypted_only=False
)
diff --git a/service/pixelated/config/services.py b/service/pixelated/config/services.py
index 7c08d286..cd475228 100644
--- a/service/pixelated/config/services.py
+++ b/service/pixelated/config/services.py
@@ -1,7 +1,7 @@
from pixelated.adapter.mailstore.searchable_mailstore import SearchableMailStore
from pixelated.adapter.services.mail_service import MailService
from pixelated.adapter.model.mail import InputMail
-from pixelated.adapter.services.mail_sender import LocalSmtpMailSender # , MailSender
+from pixelated.adapter.services.mail_sender import LocalSmtpMailSender, MailSender # , MailSender
from pixelated.adapter.search import SearchEngine
from pixelated.adapter.services.draft_service import DraftService
from pixelated.adapter.listeners.mailbox_indexer_listener import listen_all_mailboxes
@@ -55,17 +55,11 @@ class Services(object):
self.search_engine = search_engine
def setup_mail_service(self, leap_session, search_engine):
- smtp_host, smtp_port = leap_session.provider.smtp_info()
pixelated_mail_sender = LocalSmtpMailSender(
leap_session.account_email(),
leap_session.smtp)
- # pixelated_mail_sender = MailSender(
- # leap_session.account_email(),
- # leap_session.nicknym,
- # leap_session.provider.local_ca_crt,
- # smtp_host,
- # smtp_port)
+ MailSender(leap_session.smtp._smtp_config, leap_session.nicknym.keymanager)
return MailService(
pixelated_mail_sender,