From 1019cedb97ca9ea5c5a5600ade575c79bdf868e8 Mon Sep 17 00:00:00 2001 From: spsaefan <117728614+spsaefan@users.noreply.github.com> Date: Sat, 27 Jun 2026 13:59:19 +0200 Subject: [PATCH] Add SMTP support for OTK emails --- service/src/main/inse/auth/email_service.py | 29 +++++++++++++++++++++ service/src/main/inse/auth/service.py | 29 ++++++++++++--------- service/src/main/inse/config.py | 8 ++++++ 3 files changed, 54 insertions(+), 12 deletions(-) create mode 100644 service/src/main/inse/auth/email_service.py diff --git a/service/src/main/inse/auth/email_service.py b/service/src/main/inse/auth/email_service.py new file mode 100644 index 00000000..85ef913c --- /dev/null +++ b/service/src/main/inse/auth/email_service.py @@ -0,0 +1,29 @@ +import smtplib +from email.message import EmailMessage + + +def send_email( + recipient: str, + subject: str, + body: str, + smtp_server: str = "localhost", + smtp_port: int = 25, + sender: str = "no-reply@smartguide.local", + username: str | None = None, + password: str | None = None, + use_tls: bool = False, +): + msg = EmailMessage() + msg["From"] = sender + msg["To"] = recipient + msg["Subject"] = subject + msg.set_content(body) + + with smtplib.SMTP(smtp_server, smtp_port) as server: + if use_tls: + server.starttls() + + if username and password: + server.login(username, password) + + server.send_message(msg) diff --git a/service/src/main/inse/auth/service.py b/service/src/main/inse/auth/service.py index d88e5c0c..2845ef62 100644 --- a/service/src/main/inse/auth/service.py +++ b/service/src/main/inse/auth/service.py @@ -27,7 +27,8 @@ from inse.auth.model import ( ) from inse.config import get_settings from inse.database import get_session -from sqlmodel import Session, func, select +from sqlmodel import Session, select, func +from inse.auth.email_service import send_email ID_TOKEN_EXPIRATION_MINUTES = 60 * 24 * 30 # 30 days REFRESH_TOKEN_EXPIRATION_MINUTES = 15 # 15 minutes @@ -210,19 +211,23 @@ def request_otk(session: Session, email: str, password: str): session.commit() - if settings.environment == "prod": - logger.info( - "Saved OTK for account %s with expiration at %s", - account.id, - expires_at, + if settings.send_otk_email: + send_email( + recipient=account.email, + subject="Your SmartGUIDE login code", + body=f"Your login code is: {otk_code}", + smtp_server=settings.smtp_server, + smtp_port=settings.smtp_port, + sender=settings.smtp_sender, + username=settings.smtp_username, + password=settings.smtp_password, + use_tls=settings.smtp_use_tls, ) + logger.info("Sent OTK email for account %s", account.id) + elif settings.environment == "dev": + logger.info("OTK code for account %s: %s", account.id, otk_code) else: - logger.info( - "OTK code for account %s: %s, expires at %s", - account.id, - otk_code, - expires_at, - ) + logger.info("OTK generated for account %s", account.id) return {"status": "ok", "account_id": str(account.id)} diff --git a/service/src/main/inse/config.py b/service/src/main/inse/config.py index 717f01ea..944e0185 100644 --- a/service/src/main/inse/config.py +++ b/service/src/main/inse/config.py @@ -68,6 +68,14 @@ class Settings(BaseSettings): token_hmac_key: Optional[str] = None skip_otk_validation: bool = True + # SMTP / email config + send_otk_email: bool = False + smtp_server: str = "localhost" + smtp_port: int = 25 + smtp_sender: str = "no-reply@smartguide.local" + smtp_username: Optional[str] = None + smtp_password: Optional[str] = None + smtp_use_tls: bool = False settings = Settings()