From f44eb3036f7b2a7277a98f2bdb379184da70e008 Mon Sep 17 00:00:00 2001 From: Pietro Mingo Date: Wed, 27 Jul 2022 10:21:02 +0200 Subject: [PATCH] Office365Transport properties --- .idea/.gitignore | 3 ++ .idea/django-mailbox.iml | 14 ++++++ .idea/inspectionProfiles/Project_Default.xml | 41 ++++++++++++++++ .../inspectionProfiles/profiles_settings.xml | 6 +++ .idea/misc.xml | 7 +++ .idea/modules.xml | 8 ++++ .idea/vcs.xml | 6 +++ django_mailbox/models.py | 33 ++++++++++++- django_mailbox/transports/office365.py | 48 ++++++++++--------- 9 files changed, 142 insertions(+), 24 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/django-mailbox.iml create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/django-mailbox.iml b/.idea/django-mailbox.iml new file mode 100644 index 0000000..8e5446a --- /dev/null +++ b/.idea/django-mailbox.iml @@ -0,0 +1,14 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..2d827cd --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,41 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..921287b --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..9b00bcc --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/django_mailbox/models.py b/django_mailbox/models.py index 9ef29ac..52e4c1e 100644 --- a/django_mailbox/models.py +++ b/django_mailbox/models.py @@ -189,6 +189,30 @@ class Mailbox(models.Model): return None return folder[0] + @property + def client_id(self): + """Returns (if specified) the client id for Office365.""" + client_id = self._query_string.get('client_id', None) + if not client_id: + return None + return client_id[0] + + @property + def client_secret(self): + """Returns (if specified) the client secret for Office365.""" + client_secret = self._query_string.get('client_secret', None) + if not client_secret: + return None + return client_secret[0] + + @property + def tenant_id(self): + """Returns (if specified) the tenant id for Office365.""" + tenant_id = self._query_string.get('tentant_id', None) + if not tenant_id: + return None + return tenant_id[0] + def get_connection(self): """Returns the transport instance for this mailbox. @@ -224,7 +248,14 @@ class Mailbox(models.Model): ) conn.connect(self.username, self.password) elif self.type == 'office365': - conn = Office365Transport(self.location) + conn = Office365Transport( + port=self.port if self.port else None, + ssl=True, + archive=self.archive, + client_id=self.client_id, + client_secret=self.client_secret, + tenant_id=self.tenant_id + ) conn.connect(self.username, self.password) elif self.type == 'maildir': conn = MaildirTransport(self.location) diff --git a/django_mailbox/transports/office365.py b/django_mailbox/transports/office365.py index 8ce5554..f98f8c1 100644 --- a/django_mailbox/transports/office365.py +++ b/django_mailbox/transports/office365.py @@ -11,7 +11,7 @@ logger = logging.getLogger(__name__) class Office365Transport(EmailTransport): def __init__( self, hostname, port=None, ssl=False, tls=False, - archive='', folder=None, + archive='', folder=None, client_id=None, client_secret=None, tenant_id=None ): self.max_message_size = getattr( settings, @@ -23,30 +23,32 @@ class Office365Transport(EmailTransport): 'DJANGO_MAILBOX_INTEGRATION_TESTING_SUBJECT', None ) - # self.hostname = hostname - # self.port = port - # self.archive = archive - # self.folder = folder - # self.tls = tls - # if ssl: - # # self.transport = imaplib.IMAP4_SSL - # if not self.port: - # self.port = 993 - # else: - # self.transport = imaplib.IMAP4 - # if not self.port: - # self.port = 143 + self.hostname = hostname + self.port = port + self.archive = archive + self.folder = folder + self.tls = tls + if ssl: + #self.transport = imaplib.IMAP4_SSL + if not self.port: + self.port = 993 + else: + #self.transport = imaplib.IMAP4 + if not self.port: + self.port = 143 + self.client_id = client_id + self.client_secret = client_secret + self.tenant_id = tenant_id def connect(self, username, password): - # self.server = self.transport(self.hostname, self.port) - # if self.tls: - # self.server.starttls() - # typ, msg = self.server.login(username, password) - # - # if self.folder: - # self.server.select(self.folder) - # else: - # self.server.select() + credentials = (self.client_id, self.client_secret) + + # the default protocol will be Microsoft Graph + # the default authentication method will be "on behalf of a user" + + self.server = O365.Account(credentials, auth_flow_type='credentials', tenant_id=self.tenant_id) + if self.server.authenticate(scopes=['mailbox']): + print('Authenticated!') def _get_all_message_ids(self): # # Fetch all the message uids