Skip to main content

cloudillo_auth/
cleanup.rs

1// SPDX-FileCopyrightText: Szilárd Hajba
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
4//! Periodic cleanup task for expired auth data (API keys, verification codes)
5
6use async_trait::async_trait;
7use serde::{Deserialize, Serialize};
8use std::sync::Arc;
9
10use cloudillo_core::scheduler::{Task, TaskId};
11
12use crate::prelude::*;
13
14/// Cleanup task for expired authentication data
15///
16/// Removes expired API keys and verification codes.
17/// Scheduled to run daily at 3 AM via cron.
18#[derive(Clone, Debug, Serialize, Deserialize)]
19pub struct AuthCleanupTask;
20
21#[async_trait]
22impl Task<App> for AuthCleanupTask {
23	fn kind() -> &'static str {
24		"auth.cleanup"
25	}
26
27	fn build(_id: TaskId, _context: &str) -> ClResult<Arc<dyn Task<App>>> {
28		Ok(Arc::new(AuthCleanupTask))
29	}
30
31	fn serialize(&self) -> String {
32		String::new()
33	}
34
35	fn kind_of(&self) -> &'static str {
36		"auth.cleanup"
37	}
38
39	async fn run(&self, app: &App) -> ClResult<()> {
40		info!("Running auth cleanup task");
41
42		// Cleanup expired API keys
43		match app.auth_adapter.cleanup_expired_api_keys().await {
44			Ok(count) => {
45				if count > 0 {
46					info!("Cleaned up {} expired API keys", count);
47				}
48			}
49			Err(e) => {
50				warn!("Failed to cleanup expired API keys: {}", e);
51			}
52		}
53
54		// Cleanup expired verification codes
55		match app.auth_adapter.cleanup_expired_verification_codes().await {
56			Ok(count) => {
57				if count > 0 {
58					info!("Cleaned up {} expired verification codes", count);
59				}
60			}
61			Err(e) => {
62				warn!("Failed to cleanup expired verification codes: {}", e);
63			}
64		}
65
66		// Cleanup expired QR login sessions
67		if let Ok(store) = app.ext::<crate::qr_login::QrLoginStore>() {
68			let count = store.cleanup_expired();
69			if count > 0 {
70				info!("Cleaned up {} expired QR login sessions", count);
71			}
72		}
73
74		Ok(())
75	}
76}
77
78// vim: ts=4