Skip to main content

AuthMailDelivery

Trait AuthMailDelivery 

Source
pub trait AuthMailDelivery:
    Send
    + Sync
    + 'static {
    // Required method
    fn deliver(
        &self,
        cx: &Cx,
        ex: &mut dyn Executor,
        letter: AuthLetter,
    ) -> impl Future<Output = Result<()>> + Send;
}
Expand description

Takes each AuthLetter a panel’s auth sends, installed with AuthMail::delivery, so a durable queue sends it in place of the router’s Mailer.

Without one, a panel sends each letter through the Mailer once the token it carries commits: a crash or a mail-server outage then loses it. A delivery writes the letter to a queue through ex, the transaction that records its token, so the letter and the token commit together or not at all, and a worker sends it later.

use tablo_core::{AuthLetter, AuthMailDelivery};
use topcoat::context::Cx;

#[derive(Debug, toasty::Model)]
struct QueuedMail {
    #[key]
    #[auto]
    id: u64,
    #[column(type = text)]
    sealed: String,
}

struct Outbox;

impl AuthMailDelivery for Outbox {
    async fn deliver(
        &self,
        _cx: &Cx,
        ex: &mut dyn toasty::Executor,
        letter: AuthLetter,
    ) -> topcoat::Result<()> {
        let json = serde_json::to_string(&letter)?;
        toasty::create!(QueuedMail {
            sealed: seal(&json)
        })
        .exec(ex)
        .await?;
        Ok(())
    }
}
  • deliver runs inside the transaction recording the letter’s token, through ex. An AuthLetterKind::TwoFactorAlert records no token: ex is then the database itself.
  • An error rolls the transaction back: no token is recorded and no letter goes out. A password-reset or verification link is mailed after the response, so its failure is logged; a two-factor code’s answers the request with the outage; an email change’s fails the save.
  • It may run more than once for one letter when the database aborts the transaction for a concurrent one, so it writes only through ex: a mail sent from inside it goes out once per run, and before the token commits.
  • The letter carries a live link or code. Seal it before writing it, and drop a letter past its expires_at rather than send it late.

Required Methods§

Source

fn deliver( &self, cx: &Cx, ex: &mut dyn Executor, letter: AuthLetter, ) -> impl Future<Output = Result<()>> + Send

Takes letter for sending, writing through ex.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§