Karta.SMTP.Policy behaviour (Karta v1.0.0)

Decisions the SMTP session delegates: who may connect, authenticate, send and receive, and what happens to an accepted message. The session itself stays pure; a real policy queries the database.

accept_message/2 receives the message as a Karta.SMTP.SpoolFile. A policy that accepts mail stores it with Karta.Messages.Intake.accept/3, which returns {:ok, queue_id} only once the message is durably stored; the session answers 250 only then.

Authentication: authenticate/4 checks a user name and password (AUTH PLAIN and LOGIN). A policy that also implements scram_credentials/2 gets SCRAM-SHA-256 (RFC 7677) offered: the session runs the exchange with the stored keys the callback returns. For an unknown user name the callback should return made-up keys with identity: nil rather than refuse, so the exchange fails at the proof like a wrong password and does not tell which user names exist. {:error, :temporary} from either callback (the credential store is unavailable) is answered with 454 4.7.0 and not counted as a failed attempt.

IP-authenticated relay: relay_identity/1 is asked once per MAIL on a connection that has not authenticated. A policy that identifies the client by its address (the submission listeners: Karta.SMTP.Relay; the mx listener when policy_opts: [ip_relay: true]) returns {:ok, identity}, and the session treats the connection as identified from then on; :none leaves it unauthenticated, and {:error, :temporary} answers 451 4.3.0. The address is the session's peer, never anything in the message.

Summary

Types

decision()

@type decision() :: :ok | {:reject, Karta.SMTP.Reply.t()}

scram_credentials()

@type scram_credentials() :: %{
  salt: binary(),
  iterations: pos_integer(),
  stored_key: binary(),
  server_key: binary(),
  identity: term() | nil
}

Callbacks

accept_message(t, body)

@callback accept_message(Karta.SMTP.Session.t(), body :: term()) ::
  {:ok, queue_id :: String.t()} | {:reject, Karta.SMTP.Reply.t()}

authenticate(t, mechanism, username, password)

@callback authenticate(
  Karta.SMTP.Session.t(),
  mechanism :: String.t(),
  username :: String.t(),
  password :: String.t()
) :: {:ok, identity :: term()} | :error | {:error, :temporary}

check_connect(t)

@callback check_connect(Karta.SMTP.Session.t()) :: decision()

check_mail(t, reverse_path, params)

@callback check_mail(
  Karta.SMTP.Session.t(),
  reverse_path :: :null | String.t(),
  params :: map()
) ::
  decision()

check_rcpt(t, recipient, params)

@callback check_rcpt(Karta.SMTP.Session.t(), recipient :: String.t(), params :: map()) ::
  decision()

relay_identity(t)

(optional)
@callback relay_identity(Karta.SMTP.Session.t()) ::
  {:ok, identity :: term()} | :none | {:error, :temporary}

scram_credentials(t, username)

(optional)
@callback scram_credentials(Karta.SMTP.Session.t(), username :: String.t()) ::
  {:ok, scram_credentials()} | {:error, :temporary}