minor: move oauth access functions from email_account

This commit is contained in:
phot0n 2022-05-31 19:04:04 +05:30
parent 67730b7b26
commit de6f1326f7
3 changed files with 42 additions and 38 deletions

View file

@ -145,7 +145,7 @@ frappe.ui.form.on("Email Account", {
authorize_api_access: function(frm) {
frappe.call({
method: "frappe.email.doctype.email_account.email_account.oauth_access",
method: "frappe.email.oauth.oauth_access",
args: {
"email_account": frm.doc.name,
"service": frm.doc.service || "",

View file

@ -8,7 +8,6 @@ import socket
import time
from datetime import datetime, timedelta
from poplib import error_proto
from urllib.parse import quote
import frappe
from frappe import _, are_emails_muted, safe_encode
@ -16,13 +15,11 @@ from frappe.desk.form import assign_to
from frappe.email.receive import EmailServer, InboundMail, SentEmailInInboxError
from frappe.email.smtp import SMTPServer
from frappe.email.utils import get_port
from frappe.integrations.google_oauth import GoogleOAuth
from frappe.model.document import Document
from frappe.utils import (
cint,
comma_or,
cstr,
get_request_site_address,
parse_addr,
validate_email_address,
)
@ -929,36 +926,3 @@ def set_email_password(email_account, user, password):
return False
return True
@frappe.whitelist(methods=["POST"])
def oauth_access(email_account: str, reauthorize: bool = False, service: str = None):
doctype = "Email Account"
refresh_token = frappe.db.get_value(doctype, email_account, "refresh_token")
if service == "GMail":
return authorize_google_access(email_account, reauthorize, refresh_token, doctype)
def authorize_google_access(
email_account,
reauthorize: bool = False,
refresh_token: str = None,
doctype: str = "Email Account",
code: str = None,
):
oauth_obj = GoogleOAuth("mail")
if not (refresh_token or code) or reauthorize:
return oauth_obj.get_authentication_url(
get_request_site_address(True),
state={
"method": "frappe.email.doctype.email_account.email_account.authorize_google_access",
"redirect": "/app/Form/{0}/{1}".format(quote(doctype), quote(email_account)),
"email_account": email_account,
},
)
res = oauth_obj.authorize(code, get_request_site_address(True))
frappe.db.set_value(doctype, email_account, "refresh_token", res.get("refresh_token"))
frappe.db.set_value(doctype, email_account, "access_token", res.get("access_token"))

View file

@ -3,9 +3,11 @@ from imaplib import IMAP4
from poplib import POP3
from smtplib import SMTP
from typing import Union
from urllib.parse import quote
import frappe
from frappe.integrations.google_oauth import GoogleOAuth
from frappe.utils import get_request_site_address
class OAuthenticationError(Exception):
@ -107,5 +109,43 @@ class Oauth:
def _get_service_object(self):
return {
"GMail": GoogleOAuth("mail"),
"GMail": GoogleOAuth("mail", validate=False),
}[self.service]
@frappe.whitelist(methods=["POST"])
def oauth_access(email_account: str, reauthorize: bool = False, service: str = None):
doctype = "Email Account"
refresh_token = frappe.db.get_value(doctype, email_account, "refresh_token")
# NOTE: setting this here, since we redirect to the service's auth page,
# we lose the use_oauth value in the emal account form
frappe.db.set_value(doctype, email_account, "use_oauth", 1)
if service:
if service == "GMail":
return authorize_google_access(email_account, reauthorize, refresh_token, doctype)
def authorize_google_access(
email_account,
reauthorize: bool = False,
refresh_token: str = None,
doctype: str = "Email Account",
code: str = None,
):
oauth_obj = GoogleOAuth("mail")
if not (refresh_token or code) or reauthorize:
return oauth_obj.get_authentication_url(
get_request_site_address(True),
state={
"method": "frappe.email.oauth.authorize_google_access",
"redirect": "/app/Form/{0}/{1}".format(quote(doctype), quote(email_account)),
"email_account": email_account,
},
)
res = oauth_obj.authorize(code, get_request_site_address(True))
frappe.db.set_value(doctype, email_account, "refresh_token", res.get("refresh_token"))
frappe.db.set_value(doctype, email_account, "access_token", res.get("access_token"))