From 064ffef8b9f202c1d3db257a26bac722cecc9dec Mon Sep 17 00:00:00 2001 From: phot0n Date: Tue, 31 May 2022 12:25:28 +0530 Subject: [PATCH] minor: throw exception if refresh_token is not present --- frappe/email/oauth.py | 11 +++++++++-- frappe/email/receive.py | 4 ++-- frappe/email/smtp.py | 2 +- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/frappe/email/oauth.py b/frappe/email/oauth.py index 740aefc6a6..c432bcf45a 100644 --- a/frappe/email/oauth.py +++ b/frappe/email/oauth.py @@ -32,14 +32,21 @@ class Oauth: self._access_token = access_token self._refresh_token = refresh_token - self.validate_implementation() + self.validate() - def validate_implementation(self) -> None: + def validate(self) -> None: if self.service != "GMail": raise NotImplementedError( f"Service {self.service} currently doesn't have oauth implementation." ) + if not self._refresh_token: + frappe.throw( + frappe._("Please Authorize OAuth."), + OAuthenticationError, + frappe._("OAuth Error"), + ) + @property def _auth_string(self) -> str: return "user=%s\1auth=Bearer %s\1\1" % (self.email, self._access_token) diff --git a/frappe/email/receive.py b/frappe/email/receive.py index 8bfd177579..e26748dd07 100644 --- a/frappe/email/receive.py +++ b/frappe/email/receive.py @@ -100,7 +100,7 @@ class EmailServer: self.settings.host, self.settings.incoming_port, timeout=frappe.conf.get("pop_timeout") ) - if self.settings.use_oauth and self.settings.refresh_token: + if self.settings.use_oauth: Oauth( self.imap, self.settings.email_account, @@ -133,7 +133,7 @@ class EmailServer: self.settings.host, self.settings.incoming_port, timeout=frappe.conf.get("pop_timeout") ) - if self.settings.use_oauth and self.settings.refresh_token: + if self.settings.use_oauth: Oauth( self.pop, self.settings.email_account, diff --git a/frappe/email/smtp.py b/frappe/email/smtp.py index f5e2af7194..687b8318b6 100644 --- a/frappe/email/smtp.py +++ b/frappe/email/smtp.py @@ -111,7 +111,7 @@ class SMTPServer: self.secure_session(_session) - if self.use_oauth and self.refresh_token: + if self.use_oauth: Oauth( _session, self.email_account, self.login, self.access_token, self.refresh_token, self.service ).connect()