From be970396b3e89b449a4677e53391f29acdc81524 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Sat, 5 Oct 2024 21:04:08 -0700 Subject: [PATCH] Refactoring --- src/documents/views.py | 27 +++--------------- src/paperless_mail/oauth.py | 55 +++++++++++++++++++++++++++++++++++++ src/paperless_mail/views.py | 23 ++++------------ 3 files changed, 64 insertions(+), 41 deletions(-) create mode 100644 src/paperless_mail/oauth.py diff --git a/src/documents/views.py b/src/documents/views.py index 512a0202a..fea33aa4a 100644 --- a/src/documents/views.py +++ b/src/documents/views.py @@ -160,6 +160,8 @@ from paperless.serialisers import UserSerializer from paperless.views import StandardPagination from paperless_mail.models import MailAccount from paperless_mail.models import MailRule +from paperless_mail.oauth import generate_gmail_oauth_url +from paperless_mail.oauth import generate_outlook_oauth_url from paperless_mail.serialisers import MailAccountSerializer from paperless_mail.serialisers import MailRuleSerializer @@ -1554,27 +1556,6 @@ class UiSettingsView(GenericAPIView): permission_classes = (IsAuthenticated, PaperlessObjectPermissions) serializer_class = UiSettingsViewSerializer - def generate_gmail_oauth_url(self) -> str: - token_request_uri = "https://accounts.google.com/o/oauth2/auth" - response_type = "code" - client_id = settings.GMAIL_OAUTH_CLIENT_ID - redirect_uri = "http://localhost:8000/api/oauth/callback/" - scope = "https://mail.google.com/" - access_type = "offline" - url = f"{token_request_uri}?response_type={response_type}&client_id={client_id}&redirect_uri={redirect_uri}&scope={scope}&access_type={access_type}&prompt=consent" - return url - - def generate_outlook_oauth_url(self) -> str: - token_request_uri = ( - "https://login.microsoftonline.com/common/oauth2/v2.0/authorize" - ) - response_type = "code" - client_id = settings.OUTLOOK_OAUTH_CLIENT_ID - redirect_uri = "http://localhost:8000/api/oauth/callback/" - scope = "offline_access https://outlook.office.com/IMAP.AccessAsUser.All" - url = f"{token_request_uri}?response_type={response_type}&response_mode=query&client_id={client_id}&redirect_uri={redirect_uri}&scope={scope}" - return url - def get(self, request, format=None): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) @@ -1606,10 +1587,10 @@ class UiSettingsView(GenericAPIView): ui_settings["auditlog_enabled"] = settings.AUDIT_LOG_ENABLED if settings.GMAIL_OAUTH_ENABLED: - ui_settings["gmail_oauth_url"] = self.generate_gmail_oauth_url() + ui_settings["gmail_oauth_url"] = generate_gmail_oauth_url() if settings.OUTLOOK_OAUTH_ENABLED: - ui_settings["outlook_oauth_url"] = self.generate_outlook_oauth_url() + ui_settings["outlook_oauth_url"] = generate_outlook_oauth_url() user_resp = { "id": user.id, diff --git a/src/paperless_mail/oauth.py b/src/paperless_mail/oauth.py new file mode 100644 index 000000000..62b34d6c9 --- /dev/null +++ b/src/paperless_mail/oauth.py @@ -0,0 +1,55 @@ +from django.conf import settings + +# Gmail setup guide: https://postmansmtp.com/how-to-configure-post-smtp-with-gmailgsuite-using-oauth/ +# Outlok setup guide: https://medium.com/@manojkumardhakad/python-read-and-send-outlook-mail-using-oauth2-token-and-graph-api-53de606ecfa1 + + +def generate_gmail_oauth_url() -> str: + token_request_uri = "https://accounts.google.com/o/oauth2/auth" + response_type = "code" + client_id = settings.GMAIL_OAUTH_CLIENT_ID + redirect_uri = "http://localhost:8000/api/oauth/callback/" + scope = "https://mail.google.com/" + access_type = "offline" + url = f"{token_request_uri}?response_type={response_type}&client_id={client_id}&redirect_uri={redirect_uri}&scope={scope}&access_type={access_type}&prompt=consent" + return url + + +def generate_outlook_oauth_url() -> str: + token_request_uri = "https://login.microsoftonline.com/common/oauth2/v2.0/authorize" + response_type = "code" + client_id = settings.OUTLOOK_OAUTH_CLIENT_ID + redirect_uri = "http://localhost:8000/api/oauth/callback/" + scope = "offline_access https://outlook.office.com/IMAP.AccessAsUser.All" + url = f"{token_request_uri}?response_type={response_type}&response_mode=query&client_id={client_id}&redirect_uri={redirect_uri}&scope={scope}" + return url + + +def generate_gmail_token_request_data(code: str) -> dict: + client_id = settings.GMAIL_OAUTH_CLIENT_ID + client_secret = settings.GMAIL_OAUTH_CLIENT_SECRET + scope = "https://mail.google.com/" + + return { + "code": code, + "client_id": client_id, + "client_secret": client_secret, + "scope": scope, + "redirect_uri": "http://localhost:8000/api/oauth/callback/", + "grant_type": "authorization_code", + } + + +def generate_outlook_token_request_data(code: str) -> dict: + client_id = settings.OUTLOOK_OAUTH_CLIENT_ID + client_secret = settings.OUTLOOK_OAUTH_CLIENT_SECRET + scope = "offline_access https://outlook.office.com/IMAP.AccessAsUser.All" + + return { + "code": code, + "client_id": client_id, + "client_secret": client_secret, + "scope": scope, + "redirect_uri": "http://localhost:8000/api/oauth/callback/", + "grant_type": "authorization_code", + } diff --git a/src/paperless_mail/views.py b/src/paperless_mail/views.py index 58c5d6f28..dbbd97091 100644 --- a/src/paperless_mail/views.py +++ b/src/paperless_mail/views.py @@ -3,7 +3,6 @@ import logging from datetime import timedelta import httpx -from django.conf import settings from django.http import HttpResponseBadRequest from django.http import HttpResponseRedirect from django.utils import timezone @@ -22,6 +21,8 @@ from paperless_mail.mail import mailbox_login from paperless_mail.mail import refresh_oauth_token from paperless_mail.models import MailAccount from paperless_mail.models import MailRule +from paperless_mail.oauth import generate_gmail_token_request_data +from paperless_mail.oauth import generate_outlook_token_request_data from paperless_mail.serialisers import MailAccountSerializer from paperless_mail.serialisers import MailRuleSerializer @@ -111,7 +112,6 @@ class OauthCallbackView(GenericAPIView): if scope is not None and "google" in scope: # Google - # Gmail setup guide: https://postmansmtp.com/how-to-configure-post-smtp-with-gmailgsuite-using-oauth/ account_type = MailAccount.MailAccountType.GMAIL_OAUTH imap_server = "imap.gmail.com" defaults = { @@ -121,14 +121,11 @@ class OauthCallbackView(GenericAPIView): "imap_port": 993, "account_type": account_type, } - token_request_uri = "https://accounts.google.com/o/oauth2/token" - client_id = settings.GMAIL_OAUTH_CLIENT_ID - client_secret = settings.GMAIL_OAUTH_CLIENT_SECRET - scope = "https://mail.google.com/" + data = generate_gmail_token_request_data(code) + elif scope is None: # Outlook - # Outlok setup guide: https://medium.com/@manojkumardhakad/python-read-and-send-outlook-mail-using-oauth2-token-and-graph-api-53de606ecfa1 account_type = MailAccount.MailAccountType.OUTLOOK_OAUTH imap_server = "outlook.office365.com" defaults = { @@ -142,18 +139,8 @@ class OauthCallbackView(GenericAPIView): token_request_uri = ( "https://login.microsoftonline.com/common/oauth2/v2.0/token" ) - client_id = settings.OUTLOOK_OAUTH_CLIENT_ID - client_secret = settings.OUTLOOK_OAUTH_CLIENT_SECRET - scope = "offline_access https://outlook.office.com/IMAP.AccessAsUser.All" + data = generate_outlook_token_request_data(code) - data = { - "code": code, - "client_id": client_id, - "client_secret": client_secret, - "scope": scope, - "redirect_uri": "http://localhost:8000/api/oauth/callback/", - "grant_type": "authorization_code", - } headers = { "Content-Type": "application/x-www-form-urlencoded", }