This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
|
||||
from django.conf import settings
|
||||
|
||||
|
||||
class Microsoft365OAuthError(RuntimeError):
|
||||
"""Safe OAuth boundary error that never includes tokens or client secrets."""
|
||||
|
||||
|
||||
def acquire_exchange_access_token(*, client_factory=None) -> str:
|
||||
tenant_id = settings.M365_TENANT_ID.strip()
|
||||
client_id = settings.M365_CLIENT_ID.strip()
|
||||
client_secret = settings.M365_CLIENT_SECRET.strip()
|
||||
if not all((tenant_id, client_id, client_secret)):
|
||||
raise Microsoft365OAuthError("Microsoft 365 OAuth is niet volledig geconfigureerd.")
|
||||
|
||||
if client_factory is None:
|
||||
import msal
|
||||
|
||||
client_factory = msal.ConfidentialClientApplication
|
||||
factory = client_factory
|
||||
client = factory(
|
||||
client_id,
|
||||
authority=f"https://login.microsoftonline.com/{tenant_id}",
|
||||
client_credential=client_secret,
|
||||
)
|
||||
result = client.acquire_token_for_client(scopes=[settings.M365_EXCHANGE_SCOPE])
|
||||
token = result.get("access_token") if isinstance(result, dict) else None
|
||||
if not isinstance(token, str) or not token:
|
||||
category = "unknown"
|
||||
if isinstance(result, dict):
|
||||
category = str(result.get("error") or "unknown")[:80]
|
||||
raise Microsoft365OAuthError(
|
||||
f"Microsoft 365 OAuth-token kon niet worden verkregen ({category})."
|
||||
)
|
||||
return token
|
||||
|
||||
|
||||
def xoauth2_bytes(username: str, access_token: str) -> bytes:
|
||||
if not username or "\x00" in username or "\r" in username or "\n" in username:
|
||||
raise Microsoft365OAuthError("Ongeldig Microsoft 365-mailboxadres.")
|
||||
return f"user={username}\x01auth=Bearer {access_token}\x01\x01".encode()
|
||||
|
||||
|
||||
def xoauth2_b64(username: str, access_token: str) -> str:
|
||||
return base64.b64encode(xoauth2_bytes(username, access_token)).decode("ascii")
|
||||
@@ -0,0 +1,57 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import smtplib
|
||||
from email.utils import parseaddr
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.mail.backends.base import BaseEmailBackend
|
||||
|
||||
from apps.core.services.microsoft365 import (
|
||||
Microsoft365OAuthError,
|
||||
acquire_exchange_access_token,
|
||||
xoauth2_b64,
|
||||
)
|
||||
from apps.sources.services.tls import trusted_tls_context
|
||||
|
||||
|
||||
class Microsoft365OAuthEmailBackend(BaseEmailBackend):
|
||||
"""Send mail through Exchange Online with app-only SASL XOAUTH2."""
|
||||
|
||||
def send_messages(self, email_messages) -> int:
|
||||
if not email_messages:
|
||||
return 0
|
||||
sent = 0
|
||||
try:
|
||||
token = acquire_exchange_access_token()
|
||||
username = settings.M365_MAILBOX_USER.strip()
|
||||
if not username:
|
||||
raise Microsoft365OAuthError(
|
||||
"M365_MAILBOX_USER is verplicht voor Microsoft 365-mailuitvoer."
|
||||
)
|
||||
with smtplib.SMTP(
|
||||
settings.EMAIL_HOST or "smtp.office365.com",
|
||||
settings.EMAIL_PORT,
|
||||
timeout=settings.M365_SMTP_TIMEOUT_SECONDS,
|
||||
) as client:
|
||||
client.ehlo()
|
||||
client.starttls(context=trusted_tls_context())
|
||||
client.ehlo()
|
||||
code, _ = client.docmd("AUTH", f"XOAUTH2 {xoauth2_b64(username, token)}")
|
||||
if code != 235:
|
||||
raise Microsoft365OAuthError("Microsoft 365 SMTP OAuth geweigerd.")
|
||||
for message in email_messages:
|
||||
recipients = message.recipients()
|
||||
if not recipients:
|
||||
continue
|
||||
sender = parseaddr(message.from_email or settings.DEFAULT_FROM_EMAIL)[1]
|
||||
client.sendmail(
|
||||
sender or username,
|
||||
recipients,
|
||||
message.message().as_bytes(linesep="\r\n"),
|
||||
)
|
||||
sent += 1
|
||||
except Exception:
|
||||
if self.fail_silently:
|
||||
return sent
|
||||
raise
|
||||
return sent
|
||||
@@ -9,7 +9,10 @@ class MailboxConnectionForm(forms.ModelForm):
|
||||
password = forms.CharField(
|
||||
required=False,
|
||||
label="App-wachtwoord",
|
||||
help_text="Laat leeg bij wijzigen om het bestaande app-wachtwoord te behouden.",
|
||||
help_text=(
|
||||
"Alleen voor Gmail of een aangepaste provider. Microsoft 365 gebruikt "
|
||||
"de centrale OAuth-configuratie."
|
||||
),
|
||||
widget=forms.PasswordInput(
|
||||
attrs={"autocomplete": "new-password", "placeholder": "App-wachtwoord"},
|
||||
render_value=False,
|
||||
@@ -50,7 +53,8 @@ class MailboxConnectionForm(forms.ModelForm):
|
||||
data = super().clean()
|
||||
if data.get("provider") != MailboxConnection.Provider.CUSTOM:
|
||||
data["custom_host"] = ""
|
||||
if not self.instance.pk and not data.get("password"):
|
||||
uses_oauth = data.get("provider") == MailboxConnection.Provider.OUTLOOK
|
||||
if not self.instance.pk and not uses_oauth and not data.get("password"):
|
||||
self.add_error("password", "Een app-wachtwoord is verplicht voor een nieuwe koppeling.")
|
||||
return data
|
||||
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [("sources", "0007_alter_mailboxconnection_platform")]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name="mailboxconnection",
|
||||
name="encrypted_password",
|
||||
field=models.TextField(blank=True),
|
||||
)
|
||||
]
|
||||
@@ -357,7 +357,7 @@ class MailboxConnection(TimeStampedModel):
|
||||
custom_host = models.CharField(max_length=255, blank=True)
|
||||
port = models.PositiveIntegerField(default=993)
|
||||
username = models.CharField(max_length=320)
|
||||
encrypted_password = models.TextField()
|
||||
encrypted_password = models.TextField(blank=True)
|
||||
mailbox = models.CharField(max_length=255, default="INBOX")
|
||||
enabled = models.BooleanField(default=True)
|
||||
poll_interval_minutes = models.PositiveIntegerField(
|
||||
|
||||
@@ -74,14 +74,19 @@ def _validate_connected_peer(
|
||||
*,
|
||||
before: ValidatedUrl,
|
||||
after: ValidatedUrl,
|
||||
require_peer: bool,
|
||||
) -> None:
|
||||
peer_ip = _connected_peer_ip(response)
|
||||
if peer_ip is not None:
|
||||
if not peer_ip.is_global:
|
||||
raise FetchError(f"Niet-publiek verbonden IP-adres geblokkeerd: {peer_ip}")
|
||||
return
|
||||
if require_peer:
|
||||
raise FetchError(
|
||||
"Verbonden IP-adres kon niet veilig worden vastgesteld; aanvraag geblokkeerd."
|
||||
)
|
||||
# Mock/custom transports do not always expose the peer socket. Keep the conservative
|
||||
# DNS-overlap fallback for those transports.
|
||||
# DNS-overlap fallback only for explicitly injected test/custom clients.
|
||||
if not set(before.addresses).intersection(after.addresses):
|
||||
raise FetchError("DNS-rebindcontrole faalde bij het benaderen van bron.")
|
||||
|
||||
@@ -175,6 +180,7 @@ def fetch_url(
|
||||
response,
|
||||
before=validation,
|
||||
after=post_validation,
|
||||
require_peer=own_client,
|
||||
)
|
||||
if response.status_code in {301, 302, 303, 307, 308}:
|
||||
location = response.headers.get("location")
|
||||
|
||||
@@ -7,6 +7,7 @@ from typing import Any
|
||||
|
||||
from django.conf import settings
|
||||
|
||||
from apps.core.services.microsoft365 import acquire_exchange_access_token, xoauth2_bytes
|
||||
from apps.sources.models import EmailMessageRecord, MailboxConnection
|
||||
from apps.sources.services.email_import import ingest_email, message_identity
|
||||
from apps.sources.services.mailbox_connections import decrypt_mailbox_password
|
||||
@@ -30,7 +31,11 @@ def poll_imap_mailbox(
|
||||
if client_factory is None:
|
||||
client_factory = imaplib.IMAP4_SSL
|
||||
host_validator(f"https://{connection.imap_host}")
|
||||
password = decrypt_mailbox_password(connection)
|
||||
password = (
|
||||
""
|
||||
if connection.provider == MailboxConnection.Provider.OUTLOOK
|
||||
else decrypt_mailbox_password(connection)
|
||||
)
|
||||
client_options = {"timeout": settings.IMAP_CONNECT_TIMEOUT_SECONDS}
|
||||
if client_factory is imaplib.IMAP4_SSL:
|
||||
client_options["ssl_context"] = trusted_tls_context()
|
||||
@@ -42,7 +47,13 @@ def poll_imap_mailbox(
|
||||
"failed": 0,
|
||||
}
|
||||
try:
|
||||
client.login(connection.username, password)
|
||||
if connection.provider == MailboxConnection.Provider.OUTLOOK:
|
||||
access_token = acquire_exchange_access_token()
|
||||
client.authenticate(
|
||||
"XOAUTH2", lambda _challenge: xoauth2_bytes(connection.username, access_token)
|
||||
)
|
||||
else:
|
||||
client.login(connection.username, password)
|
||||
status, _ = client.select(connection.mailbox, readonly=True)
|
||||
if status != "OK":
|
||||
raise RuntimeError("IMAP-mailbox kon niet worden geopend")
|
||||
|
||||
@@ -48,7 +48,9 @@ def decrypt_mailbox_password(connection: MailboxConnection) -> str:
|
||||
def rotate_mailbox_credentials() -> int:
|
||||
"""Re-encrypt every mailbox password with the first configured key."""
|
||||
rotated = 0
|
||||
for connection in MailboxConnection.objects.select_for_update().all():
|
||||
for connection in MailboxConnection.objects.select_for_update().exclude(
|
||||
provider=MailboxConnection.Provider.OUTLOOK
|
||||
):
|
||||
password = decrypt_mailbox_password(connection)
|
||||
connection.encrypted_password = encrypt_mailbox_password(password)
|
||||
connection.save(update_fields=["encrypted_password", "updated_at"])
|
||||
@@ -79,7 +81,10 @@ def save_mailbox_connection(
|
||||
"poll_interval_minutes",
|
||||
):
|
||||
setattr(connection, field, cleaned_data[field])
|
||||
if password:
|
||||
uses_oauth = connection.provider == MailboxConnection.Provider.OUTLOOK
|
||||
if uses_oauth:
|
||||
connection.encrypted_password = ""
|
||||
elif password:
|
||||
connection.encrypted_password = encrypt_mailbox_password(password)
|
||||
elif not connection.encrypted_password:
|
||||
raise MailboxCredentialError("Een app-wachtwoord is verplicht.")
|
||||
|
||||
Reference in New Issue
Block a user