Refactoring
This commit is contained in:
parent
843ad5bdd6
commit
be970396b3
@ -160,6 +160,8 @@ from paperless.serialisers import UserSerializer
|
|||||||
from paperless.views import StandardPagination
|
from paperless.views import StandardPagination
|
||||||
from paperless_mail.models import MailAccount
|
from paperless_mail.models import MailAccount
|
||||||
from paperless_mail.models import MailRule
|
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 MailAccountSerializer
|
||||||
from paperless_mail.serialisers import MailRuleSerializer
|
from paperless_mail.serialisers import MailRuleSerializer
|
||||||
|
|
||||||
@ -1554,27 +1556,6 @@ class UiSettingsView(GenericAPIView):
|
|||||||
permission_classes = (IsAuthenticated, PaperlessObjectPermissions)
|
permission_classes = (IsAuthenticated, PaperlessObjectPermissions)
|
||||||
serializer_class = UiSettingsViewSerializer
|
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):
|
def get(self, request, format=None):
|
||||||
serializer = self.get_serializer(data=request.data)
|
serializer = self.get_serializer(data=request.data)
|
||||||
serializer.is_valid(raise_exception=True)
|
serializer.is_valid(raise_exception=True)
|
||||||
@ -1606,10 +1587,10 @@ class UiSettingsView(GenericAPIView):
|
|||||||
ui_settings["auditlog_enabled"] = settings.AUDIT_LOG_ENABLED
|
ui_settings["auditlog_enabled"] = settings.AUDIT_LOG_ENABLED
|
||||||
|
|
||||||
if settings.GMAIL_OAUTH_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:
|
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 = {
|
user_resp = {
|
||||||
"id": user.id,
|
"id": user.id,
|
||||||
|
55
src/paperless_mail/oauth.py
Normal file
55
src/paperless_mail/oauth.py
Normal file
@ -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",
|
||||||
|
}
|
@ -3,7 +3,6 @@ import logging
|
|||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
from django.conf import settings
|
|
||||||
from django.http import HttpResponseBadRequest
|
from django.http import HttpResponseBadRequest
|
||||||
from django.http import HttpResponseRedirect
|
from django.http import HttpResponseRedirect
|
||||||
from django.utils import timezone
|
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.mail import refresh_oauth_token
|
||||||
from paperless_mail.models import MailAccount
|
from paperless_mail.models import MailAccount
|
||||||
from paperless_mail.models import MailRule
|
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 MailAccountSerializer
|
||||||
from paperless_mail.serialisers import MailRuleSerializer
|
from paperless_mail.serialisers import MailRuleSerializer
|
||||||
|
|
||||||
@ -111,7 +112,6 @@ class OauthCallbackView(GenericAPIView):
|
|||||||
|
|
||||||
if scope is not None and "google" in scope:
|
if scope is not None and "google" in scope:
|
||||||
# Google
|
# Google
|
||||||
# Gmail setup guide: https://postmansmtp.com/how-to-configure-post-smtp-with-gmailgsuite-using-oauth/
|
|
||||||
account_type = MailAccount.MailAccountType.GMAIL_OAUTH
|
account_type = MailAccount.MailAccountType.GMAIL_OAUTH
|
||||||
imap_server = "imap.gmail.com"
|
imap_server = "imap.gmail.com"
|
||||||
defaults = {
|
defaults = {
|
||||||
@ -121,14 +121,11 @@ class OauthCallbackView(GenericAPIView):
|
|||||||
"imap_port": 993,
|
"imap_port": 993,
|
||||||
"account_type": account_type,
|
"account_type": account_type,
|
||||||
}
|
}
|
||||||
|
|
||||||
token_request_uri = "https://accounts.google.com/o/oauth2/token"
|
token_request_uri = "https://accounts.google.com/o/oauth2/token"
|
||||||
client_id = settings.GMAIL_OAUTH_CLIENT_ID
|
data = generate_gmail_token_request_data(code)
|
||||||
client_secret = settings.GMAIL_OAUTH_CLIENT_SECRET
|
|
||||||
scope = "https://mail.google.com/"
|
|
||||||
elif scope is None:
|
elif scope is None:
|
||||||
# Outlook
|
# 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
|
account_type = MailAccount.MailAccountType.OUTLOOK_OAUTH
|
||||||
imap_server = "outlook.office365.com"
|
imap_server = "outlook.office365.com"
|
||||||
defaults = {
|
defaults = {
|
||||||
@ -142,18 +139,8 @@ class OauthCallbackView(GenericAPIView):
|
|||||||
token_request_uri = (
|
token_request_uri = (
|
||||||
"https://login.microsoftonline.com/common/oauth2/v2.0/token"
|
"https://login.microsoftonline.com/common/oauth2/v2.0/token"
|
||||||
)
|
)
|
||||||
client_id = settings.OUTLOOK_OAUTH_CLIENT_ID
|
data = generate_outlook_token_request_data(code)
|
||||||
client_secret = settings.OUTLOOK_OAUTH_CLIENT_SECRET
|
|
||||||
scope = "offline_access https://outlook.office.com/IMAP.AccessAsUser.All"
|
|
||||||
|
|
||||||
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 = {
|
headers = {
|
||||||
"Content-Type": "application/x-www-form-urlencoded",
|
"Content-Type": "application/x-www-form-urlencoded",
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user