From ab9a577474cb26f22559392d83c2c7070c0b45bf Mon Sep 17 00:00:00 2001 From: phot0n Date: Wed, 1 Jun 2022 17:57:51 +0530 Subject: [PATCH] minor: better oauth flow --- .../doctype/email_account/email_account.js | 43 +++++++++++++------ .../doctype/email_account/email_account.json | 2 +- .../doctype/email_account/email_account.py | 21 +++++---- frappe/integrations/google_oauth.py | 2 +- 4 files changed, 43 insertions(+), 25 deletions(-) diff --git a/frappe/email/doctype/email_account/email_account.js b/frappe/email/doctype/email_account/email_account.js index fda4966b90..d553b58e28 100644 --- a/frappe/email/doctype/email_account/email_account.js +++ b/frappe/email/doctype/email_account/email_account.js @@ -67,6 +67,21 @@ frappe.email_defaults_pop = { }; +function oauth_access(frm) { + return frappe.call({ + method: "frappe.email.oauth.oauth_access", + args: { + "email_account": frm.doc.name, + "service": frm.doc.service || "" + }, + callback: function(r) { + if (!r.exc) { + window.open(r.message.url); + } + } + }); +} + frappe.ui.form.on("Email Account", { service: function(frm) { $.each(frappe.email_defaults[frm.doc.service], function(key, value) { @@ -126,6 +141,7 @@ frappe.ui.form.on("Email Account", { frm.events.enable_incoming(frm); frm.events.notify_if_unreplied(frm); frm.events.show_gmail_message_for_less_secure_apps(frm); + frm.events.show_oauth_authorization_message(frm); if (frappe.route_flags.delete_user_from_locals && frappe.route_flags.linked_user) { delete frappe.route_flags.delete_user_from_locals; @@ -133,6 +149,12 @@ frappe.ui.form.on("Email Account", { } }, + after_save(frm) { + if (frm.doc.use_oauth && !frm.doc.refresh_token) { + oauth_access(frm); + } + }, + show_gmail_message_for_less_secure_apps: function(frm) { frm.dashboard.clear_headline(); let msg = __("GMail will only work if you enable 2-step authentication and use app-specific password OR use OAuth."); @@ -143,19 +165,16 @@ frappe.ui.form.on("Email Account", { } }, + show_oauth_authorization_message(frm) { + let msg = __("Oauth Enabled but not Authorized. Please use `Authorize API Access` Button to do the same."); + if (frm.doc.use_oauth && !frm.doc.refresh_token) { + frm.dashboard.clear_headline(); + frm.dashboard.set_headline_alert(msg, "yellow"); + } + }, + authorize_api_access: function(frm) { - frappe.call({ - method: "frappe.email.oauth.oauth_access", - args: { - "email_account": frm.doc.name, - "service": frm.doc.service || "" - }, - callback: function(r) { - if (!r.exc) { - window.open(r.message.url); - } - } - }); + oauth_access(frm); }, email_id:function(frm) { diff --git a/frappe/email/doctype/email_account/email_account.json b/frappe/email/doctype/email_account/email_account.json index 72be4f2e42..f76bacd044 100644 --- a/frappe/email/doctype/email_account/email_account.json +++ b/frappe/email/doctype/email_account/email_account.json @@ -613,7 +613,7 @@ "icon": "fa fa-inbox", "index_web_pages_for_search": 1, "links": [], - "modified": "2022-05-30 15:45:05.282867", + "modified": "2022-06-01 17:51:37.878446", "modified_by": "Administrator", "module": "Email", "name": "Email Account", diff --git a/frappe/email/doctype/email_account/email_account.py b/frappe/email/doctype/email_account/email_account.py index e17c021e52..71cc16bb21 100755 --- a/frappe/email/doctype/email_account/email_account.py +++ b/frappe/email/doctype/email_account/email_account.py @@ -83,8 +83,16 @@ class EmailAccount(Document): if getattr(self, "service", "") != "GMail" and self.use_oauth: self.use_oauth = 0 + if not self.use_oauth and self.refresh_token: + # clear access & refresh token + self.refresh_token = self.access_token = None + if not frappe.local.flags.in_install and (self.use_oauth or not self.awaiting_password): - if self.refresh_token or self.password or self.smtp_server in ("127.0.0.1", "localhost"): + if ( + (self.use_oauth and self.refresh_token) + or self.password + or self.smtp_server in ("127.0.0.1", "localhost") + ): if self.enable_incoming: self.get_incoming_server() self.no_failed = 0 @@ -93,10 +101,7 @@ class EmailAccount(Document): self.validate_smtp_conn() else: if self.enable_incoming or (self.enable_outgoing and not self.no_smtp_authentication): - if self.use_oauth: - if not self.is_new(): - frappe.throw(_("Please Enable OAuth by using `Authorize API access` button")) - else: + if not self.use_oauth: frappe.throw(_("Password is required or select Awaiting Password")) if self.notify_if_unreplied: @@ -151,12 +156,6 @@ class EmailAccount(Document): enable_outgoing=self.enable_outgoing, ) - def after_insert(self): - if self.use_oauth and not self.refresh_token: - frappe.msgprint( - _("Please Enable OAuth by using `Authorize API access` button"), indicator="orange" - ) - def there_must_be_only_one_default(self): """If current Email Account is default, un-default all other accounts.""" for field in ("default_incoming", "default_outgoing"): diff --git a/frappe/integrations/google_oauth.py b/frappe/integrations/google_oauth.py index c720a22d28..c920fc31fa 100644 --- a/frappe/integrations/google_oauth.py +++ b/frappe/integrations/google_oauth.py @@ -145,7 +145,7 @@ def handle_response( ) if raise_err: - frappe.throw(frappe._(error_title), frappe._(error_message)) + frappe.throw(frappe._(error_title), GoogleAuthenticationError, frappe._(error_message)) return {}