forked from frappe/frappe
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: email oauth (v13) (frappe#20790)
- Loading branch information
Showing
11 changed files
with
273 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import base64 | ||
from imaplib import IMAP4 | ||
from poplib import POP3 | ||
from smtplib import SMTP | ||
|
||
import frappe | ||
|
||
|
||
class Oauth: | ||
def __init__( | ||
self, | ||
conn, | ||
email_account, | ||
email, | ||
access_token, | ||
mechanism="XOAUTH2", | ||
): | ||
|
||
self.email_account = email_account | ||
self.email = email | ||
self._mechanism = mechanism | ||
self._conn = conn | ||
self._access_token = access_token | ||
|
||
self._validate() | ||
|
||
def _validate(self) -> None: | ||
if not self._access_token: | ||
frappe.throw( | ||
frappe._("Please Authorize OAuth for Email Account {}").format(self.email_account), | ||
title=frappe._("OAuth Error"), | ||
) | ||
|
||
@property | ||
def _auth_string(self) -> str: | ||
return f"user={self.email}\1auth=Bearer {self._access_token}\1\1" | ||
|
||
def connect(self) -> None: | ||
try: | ||
if isinstance(self._conn, POP3): | ||
self._connect_pop() | ||
|
||
elif isinstance(self._conn, IMAP4): | ||
self._connect_imap() | ||
|
||
else: | ||
# SMTP | ||
self._connect_smtp() | ||
|
||
except Exception: | ||
frappe.log_error( | ||
title="Email Connection Error - Authentication Failed", | ||
) | ||
# raising a bare exception here as we have a lot of exception handling present | ||
# where the connect method is called from - hence just logging and raising. | ||
raise | ||
|
||
def _connect_pop(self) -> None: | ||
# NOTE: poplib doesn't have AUTH command implementation | ||
res = self._conn._shortcmd( | ||
"AUTH {} {}".format( | ||
self._mechanism, base64.b64encode(bytes(self._auth_string, "utf-8")).decode("utf-8") | ||
) | ||
) | ||
|
||
if not res.startswith(b"+OK"): | ||
raise | ||
|
||
def _connect_imap(self) -> None: | ||
self._conn.authenticate(self._mechanism, lambda x: self._auth_string) | ||
|
||
def _connect_smtp(self) -> None: | ||
self._conn.auth(self._mechanism, lambda x: self._auth_string, initial_response_ok=False) |
Oops, something went wrong.