from typing import Awaitable, Callable, Optional, Tuple from smtplib import SMTP_SSL as SMTP import synapse from synapse import module_api class SMTPAuthProvider: def __init__(self, config: dict, api: module_api): self.api = api api.register_password_auth_provider_callbacks( auth_checkers={ ("m.login.password", ("password",)): self.check_pass, }, ) async def check_pass( self, username: str, login_type: str, login_dict: "synapse.module_api.JsonDict", ): if login_type != "m.login.password": return None result = False with SMTP(config.smtp_host) as smtp: password = login_dict.get("password") try: smtp.login(username, password) except: return None result = True if result == True: return (self.api.get_qualified_user_id(username), None) else: return None