shared/application/auth/
email_verification_service.rs1use crate::error::Result;
2
3use crate::domain::model::EmailVerificationToken;
4use crate::intern::auth::AuthService;
5
6impl AuthService {
7 #[tracing::instrument(
8 name = "auth.create_verification_email",
9 skip(self),
10 fields(user.id = user_id)
11 )]
12 pub async fn create_verification_email(&self, user_id: &str) -> Result<String> {
13 let token = self.crypto.token.generate()?;
14 let hashed_token = self.crypto.token.hash(&token);
15
16 let expiry = self.configuration.auth.email.verification.token_expires_in;
17 let otp = EmailVerificationToken::default()
18 .with_user_id(user_id)
19 .with_token_hash(&hashed_token)
20 .with_expiray(chrono::Duration::seconds(expiry));
21
22 self.email_verification_token_repository.insert(otp).await?;
23
24 Ok(token)
25 }
26
27 #[tracing::instrument(name = "auth.validate_email_verification_token", skip(self, token))]
28 pub async fn consume_email_verification_token(
29 &self,
30 token: &str,
31 ) -> Result<EmailVerificationToken> {
32 let hash = self.crypto.token.hash(token);
33
34 self.email_verification_token_repository
35 .consume(&hash)
36 .await
37 }
38
39 #[tracing::instrument(name = "auth.invalidate_email_verification_token", skip(self, id))]
40 pub async fn invalidate_email_verification_token(
41 &self,
42 id: &str,
43 ) -> Result<EmailVerificationToken> {
44 self.email_verification_token_repository
45 .invalidate(id)
46 .await
47 }
48
49 #[tracing::instrument(name = "auth.revoke_email_verification_token", skip(self), fields(user.id = user_id))]
50 pub async fn revoke_email_verification_token(&self, user_id: &str) -> Result<()> {
51 self.email_verification_token_repository
52 .revoke(user_id)
53 .await
54 }
55}