-
Notifications
You must be signed in to change notification settings - Fork 492
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
XOAUTH2
support for Outlook SMTP (#15064)
(cherry picked from commit 56fd2c9) Co-authored-by: themylogin <[email protected]>
- Loading branch information
1 parent
254bcae
commit bc57695
Showing
4 changed files
with
119 additions
and
11 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
src/middlewared/middlewared/alembic/versions/25.04/2024-12-02_13-45_mail_oauth_provider.py
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,42 @@ | ||
"""Mail OAuth provider | ||
Revision ID: bda3a0ff206e | ||
Revises: bb352e66987f | ||
Create Date: 2024-12-02 13:45:00.262906+00:00 | ||
""" | ||
import json | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
from middlewared.plugins.pwenc import encrypt, decrypt | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = 'bda3a0ff206e' | ||
down_revision = 'bb352e66987f' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
conn = op.get_bind() | ||
for id, em_oauth in conn.execute("SELECT id, em_oauth FROM system_email").fetchall(): | ||
if em_oauth := decrypt(em_oauth): | ||
em_oauth = json.loads(em_oauth) | ||
if em_oauth: | ||
em_oauth["provider"] = "gmail" | ||
conn.execute( | ||
"UPDATE system_email SET em_oauth = ? WHERE id = ?", | ||
(encrypt(json.dumps(em_oauth)), id) | ||
) | ||
|
||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
pass | ||
# ### end Alembic commands ### |
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,67 @@ | ||
import base64 | ||
from dataclasses import dataclass | ||
from smtplib import SMTP | ||
import time | ||
|
||
import requests | ||
|
||
from middlewared.service import CallError, private, Service | ||
|
||
|
||
@dataclass | ||
class OutlookToken: | ||
token: str | ||
expires_at: float | ||
|
||
|
||
class MailService(Service): | ||
outlook_tokens: dict[str, OutlookToken] = {} | ||
|
||
@private | ||
def outlook_xoauth2(self, server: SMTP, config: dict): | ||
server.ehlo() | ||
|
||
if token := self._get_outlook_token(config["fromemail"], config["oauth"]["refresh_token"]): | ||
code, response = self._do_xoauth2(server, config["fromemail"], token) | ||
if 200 <= code <= 299: | ||
return | ||
|
||
self.logger.warning("Outlook XOAUTH2 failed: %r %r. Refreshing access token", code, response) | ||
|
||
self.logger.debug("Requesting Outlook access token") | ||
r = requests.post( | ||
"https://login.microsoftonline.com/common/oauth2/v2.0/token", | ||
data={ | ||
"grant_type": "refresh_token", | ||
"client_id": config["oauth"]["client_id"], | ||
"client_secret": config["oauth"]["client_secret"], | ||
"refresh_token": config["oauth"]["refresh_token"], | ||
"scope": "https://outlook.office.com/SMTP.Send openid offline_access", | ||
} | ||
) | ||
r.raise_for_status() | ||
response = r.json() | ||
|
||
token = response["access_token"] | ||
self._set_outlook_token(config["fromemail"], config["oauth"]["refresh_token"], token, response["expires_in"]) | ||
|
||
code, response = self._do_xoauth2(server, config["fromemail"], token) | ||
if 200 <= code <= 299: | ||
return | ||
|
||
raise CallError("Outlook XOAUTH2 failed: %r %r" % (code, response)) | ||
|
||
def _get_outlook_token(self, email: str, refresh_token: str) -> str | None: | ||
for key, token in list(self.outlook_tokens.items()): | ||
if token.expires_at < time.monotonic() - 5: | ||
self.outlook_tokens.pop(key) | ||
|
||
if token := self.outlook_tokens.get(email + refresh_token): | ||
return token.token | ||
|
||
def _set_outlook_token(self, email: str, refresh_token: str, token: str, expires_in: int): | ||
self.outlook_tokens[email + refresh_token] = OutlookToken(token, time.monotonic() + expires_in) | ||
|
||
def _do_xoauth2(self, server: SMTP, email: str, access_token: str): | ||
auth_string = f"user={email}\1auth=Bearer {access_token}\1\1" | ||
return server.docmd("AUTH XOAUTH2", base64.b64encode(auth_string.encode()).decode()) |