diff --git a/backend/mail.py b/backend/mail.py index 524623da8ddb91ad46910202f035c4e148aa0b5c..6df13a17eaa4a2a1e2f9d65042830c6c3e901dc7 100644 --- a/backend/mail.py +++ b/backend/mail.py @@ -5,15 +5,10 @@ import imaplib from rich.console import Console from cryptography import x509 import tls_utils -from tls_utils import TLSDetails +from generic_handler import GenericHandler from abc import ABC, abstractmethod -class MailHandler(ABC): - def __init__(self, host: str, port: int, context: ssl.SSLContext): - self.host = host - self.port = port - self.context = context - +class MailHandler(GenericHandler): def connect(self, verification: bool) -> int: connection = self.protocol_init(self.host, self.port) if verification: @@ -22,7 +17,7 @@ class MailHandler(ABC): connection.starttls() cert = connection.sock.getpeercert() self.protocol_close(connection) - return tls_utils.get_validity_days(cert)[1] + return tls_utils.check_cert_validity(cert)[1] @abstractmethod def protocol_init(self, host, port): @@ -34,15 +29,6 @@ class MailHandler(ABC): def protocol_starttls_args(self): raise NotImplementedError() - @staticmethod - def create_handler(protocol: str): - if protocol == "smtp": - return SMTPHandler - elif protocol == "imap": - return IMAPHandler - else: - raise ValueError("Invalid protocol") - class IMAPHandler(MailHandler): def protocol_init(self, host, port): return imaplib.IMAP4(host, port) @@ -57,21 +43,4 @@ class SMTPHandler(MailHandler): def protocol_close(self, connection): connection.quit() def protocol_starttls_args(self): - return {"context": self.context} - -class MailVerificator: - def __init__(self, context: ssl.SSLContext): - self.context = context - - def connect(self, domain: str, port: int, protocol: str) -> TLSDetails: - mail = MailHandler.create_handler(protocol)(domain, port, self.context) - try: - expiry = mail.connect(True) - return TLSDetails(domain_name=domain, expires_in_days=expiry) - except ssl.SSLCertVerificationError as e: - if (e.verify_code == tls_utils.EXPIRED_VERIFY_CODE): - expiry = mail.connect(False) - return TLSDetails(domain_name=domain, expires_in_days=expiry) - else: - error = "failed verification:", e.verify_message + "." - return TLSDetails(domain_name=domain, error_message=error) \ No newline at end of file + return {"context": self.context} \ No newline at end of file