From 72582abb2d33aff03f08918d006a20142bc7aaca Mon Sep 17 00:00:00 2001 From: Manuel Gomez <34234948+lologf@users.noreply.github.com> Date: Tue, 9 Apr 2019 12:07:00 +0200 Subject: [PATCH 1/6] Update admin.py --- django_mailbox/admin.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/django_mailbox/admin.py b/django_mailbox/admin.py index c586b40..9ee4fbc 100644 --- a/django_mailbox/admin.py +++ b/django_mailbox/admin.py @@ -8,6 +8,7 @@ console. import logging +from django import forms from django.conf import settings from django.contrib import admin from django.utils.translation import ugettext_lazy as _ @@ -39,8 +40,15 @@ resend_message_received_signal.short_description = ( _('Re-send message received signal') ) +class MailboxForm(forms.ModelForm): + uri = forms.CharField(widget=forms.PasswordInput) + + class Meta: + model = Mailbox + fields = ('name', 'uri', 'from_email', 'active',) class MailboxAdmin(admin.ModelAdmin): + form = MailboxForm list_display = ( 'name', 'uri', @@ -51,6 +59,10 @@ class MailboxAdmin(admin.ModelAdmin): readonly_fields = ['last_polling', ] actions = [get_new_mail] + def save_model(self, request, obj, form, change): + if request: + obj.uri = obj.encrypt_uri() + obj.save() class MessageAttachmentAdmin(admin.ModelAdmin): raw_id_fields = ('message', ) From 11a05a8e0980c876c7a69b29cf4af3ecd146c596 Mon Sep 17 00:00:00 2001 From: Manuel Gomez <34234948+lologf@users.noreply.github.com> Date: Tue, 9 Apr 2019 12:09:55 +0200 Subject: [PATCH 2/6] Encrypt, decrypt and padding in model methods --- django_mailbox/models.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/django_mailbox/models.py b/django_mailbox/models.py index a343989..221c410 100644 --- a/django_mailbox/models.py +++ b/django_mailbox/models.py @@ -17,6 +17,7 @@ import os.path import sys import uuid from tempfile import NamedTemporaryFile +from Crypto.Cipher import AES import six from six.moves.urllib.parse import parse_qs, unquote, urlparse @@ -113,6 +114,33 @@ class Mailbox(models.Model): objects = models.Manager() active_mailboxes = ActiveMailboxManager() + def pad(self, key): + length = 32 - (len(key) % 32) + return key + chr(length).encode('utf-8') * length + + def unpad(self, key): + return key[0:-ord(key[-1])] + + def encrypt_uri(self): + secret_key = self.pad(django_settings.SECRET_KEY) + self.uri = unicode(self.pad(self.uri)).encode('utf-8') + + cipher = AES.new(secret_key) + self.uri = cipher.encrypt(self.uri) + self.uri = base64.b64encode(self.uri) + + return self.uri + + def decrypt_uri(self): + secret_key = self.pad(django_settings.SECRET_KEY) + self.uri = self.pad(self.uri) + + cipher = AES.new(secret_key) + self.uri = base64.b64decode(self.uri) + self.uri = cipher.decrypt(self.uri) + + return self.unpad(self.uri) + @property def _protocol_info(self): return urlparse(self.uri) From 850f86aef70f1939f098e9107057cfeeb85897cf Mon Sep 17 00:00:00 2001 From: Manuel Gomez <34234948+lologf@users.noreply.github.com> Date: Tue, 9 Apr 2019 12:11:06 +0200 Subject: [PATCH 3/6] Pycryto added in requirements --- rtd_requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/rtd_requirements.txt b/rtd_requirements.txt index 18684b7..8e20bd2 100644 --- a/rtd_requirements.txt +++ b/rtd_requirements.txt @@ -1,2 +1,3 @@ django>=1.9,<1.10 six +pycrypto==2.6.1 From e073a8c32c36fa757a17f7d654c7d7df805078f9 Mon Sep 17 00:00:00 2001 From: Manuel Gomez <34234948+lologf@users.noreply.github.com> Date: Tue, 9 Apr 2019 13:54:08 +0200 Subject: [PATCH 4/6] Remove URI from list_display --- django_mailbox/admin.py | 1 - 1 file changed, 1 deletion(-) diff --git a/django_mailbox/admin.py b/django_mailbox/admin.py index 9ee4fbc..857bf21 100644 --- a/django_mailbox/admin.py +++ b/django_mailbox/admin.py @@ -51,7 +51,6 @@ class MailboxAdmin(admin.ModelAdmin): form = MailboxForm list_display = ( 'name', - 'uri', 'from_email', 'active', 'last_polling', From cd1e4de42b5b09dc4576201c43c18f47a7c2dcdb Mon Sep 17 00:00:00 2001 From: Manuel Gomez <34234948+lologf@users.noreply.github.com> Date: Tue, 9 Apr 2019 14:06:01 +0200 Subject: [PATCH 5/6] Help text in uri form --- django_mailbox/admin.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/django_mailbox/admin.py b/django_mailbox/admin.py index 857bf21..b953947 100644 --- a/django_mailbox/admin.py +++ b/django_mailbox/admin.py @@ -41,7 +41,19 @@ resend_message_received_signal.short_description = ( ) class MailboxForm(forms.ModelForm): - uri = forms.CharField(widget=forms.PasswordInput) + uri = forms.CharField(widget=forms.PasswordInput, + help_text="For security, the URI will not be shown and will " + "be encrypted in database " + "
" + "Example: imap+ssl://myusername:mypassword@someserver
" + "
" + "Internet transports include 'imap' and 'pop3'; " + "common local file transports include 'maildir', 'mbox', " + "and less commonly 'babyl', 'mh', and 'mmdf'.
" + "
" + "Be sure to urlencode your username and password should they " + "contain illegal characters (like @, :, etc)." + ) class Meta: model = Mailbox From 6f23c59ca8305aa93b87596702670be77c1173f1 Mon Sep 17 00:00:00 2001 From: Manuel Gomez <34234948+lologf@users.noreply.github.com> Date: Tue, 9 Apr 2019 15:18:24 +0200 Subject: [PATCH 6/6] Decrypt URI in _protocol_info() method --- django_mailbox/models.py | 1 + 1 file changed, 1 insertion(+) diff --git a/django_mailbox/models.py b/django_mailbox/models.py index 221c410..9d60a78 100644 --- a/django_mailbox/models.py +++ b/django_mailbox/models.py @@ -143,6 +143,7 @@ class Mailbox(models.Model): @property def _protocol_info(self): + self.uri = self.decrypt_uri() return urlparse(self.uri) @property