use crate::{LocalResetDelivery, MemoryResetDelivery, ResetDelivery, ResetMessage, Secret};
use capy_core::Result;
use std::{future::Future, pin::Pin};
pub trait VerificationDelivery: Send + Sync + 'static {
fn deliver(
&self,
message: VerificationMessage,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + '_>>;
fn development_only(&self) -> bool {
false
}
}
pub struct VerificationMessage {
pub email: String,
pub token: Secret,
pub expires_at: i64,
}
impl std::fmt::Debug for VerificationMessage {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("VerificationMessage([REDACTED])")
}
}
impl From<VerificationMessage> for ResetMessage {
fn from(message: VerificationMessage) -> Self {
Self {
email: message.email,
token: message.token,
expires_at: message.expires_at,
}
}
}
#[derive(Clone, Debug)]
pub struct LocalVerificationDelivery(LocalResetDelivery);
impl LocalVerificationDelivery {
pub fn new(directory: impl Into<std::path::PathBuf>) -> Self {
Self(LocalResetDelivery::new(directory))
}
}
impl VerificationDelivery for LocalVerificationDelivery {
fn deliver(
&self,
message: VerificationMessage,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + '_>> {
self.0.deliver(message.into())
}
fn development_only(&self) -> bool {
true
}
}
#[derive(Clone, Default, Debug)]
pub struct MemoryVerificationDelivery(MemoryResetDelivery);
impl MemoryVerificationDelivery {
pub fn take(&self) -> Vec<VerificationMessage> {
self.0
.take()
.into_iter()
.map(|message| VerificationMessage {
email: message.email,
token: message.token,
expires_at: message.expires_at,
})
.collect()
}
}
impl VerificationDelivery for MemoryVerificationDelivery {
fn deliver(
&self,
message: VerificationMessage,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + '_>> {
self.0.deliver(message.into())
}
fn development_only(&self) -> bool {
true
}
}
#[derive(Debug, serde::Deserialize, schemars::JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct VerifyEmailRequest {
#[schemars(with = "String")]
pub token: Secret,
}
#[derive(Debug, serde::Serialize, schemars::JsonSchema)]
pub struct VerificationStatus {
pub verified: bool,
}
pub async fn prune(db: &sea_orm::DatabaseConnection) -> Result<u64> {
use sea_orm::{ColumnTrait, EntityTrait, QueryFilter};
Ok(crate::models::verification::Entity::delete_many()
.filter(crate::models::verification::Column::ExpiresAt.lte(crate::runtime::now()))
.exec(db)
.await?
.rows_affected)
}
pub mod migration {
use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct EmailVerificationMigration;
#[async_trait::async_trait]
impl MigrationTrait for EmailVerificationMigration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(Alias::new("email_verification_tokens"))
.col(
ColumnDef::new(Alias::new("user_id"))
.big_integer()
.not_null()
.primary_key(),
)
.col(
ColumnDef::new(Alias::new("token_hash"))
.string_len(64)
.not_null(),
)
.col(
ColumnDef::new(Alias::new("email"))
.string_len(254)
.not_null(),
)
.col(
ColumnDef::new(Alias::new("expires_at"))
.big_integer()
.not_null(),
)
.foreign_key(
ForeignKey::create()
.from_col(Alias::new("user_id"))
.to(Alias::new("users"), Alias::new("id"))
.on_delete(ForeignKeyAction::Cascade),
)
.to_owned(),
)
.await
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(
Table::drop()
.table(Alias::new("email_verification_tokens"))
.to_owned(),
)
.await
}
}
}