appguard_client_authentication/
lib.rs1mod heartbeat;
2
3use nullnet_libappguard::AppGuardGrpcInterface;
4use std::sync::Arc;
5use tokio::sync::RwLock;
6
7#[derive(Clone)]
8pub struct AuthHandler {
9 app_id: String,
10 app_secret: String,
11 token: Arc<RwLock<String>>,
12 client: AppGuardGrpcInterface,
13}
14
15impl AuthHandler {
16 #[must_use]
17 pub async fn new(client: AppGuardGrpcInterface) -> Self {
18 let app_id = std::env::var("APP_ID").unwrap_or_default();
19 let app_secret = std::env::var("APP_SECRET").unwrap_or_default();
20 let auth = Self {
21 app_id,
22 app_secret,
23 client: client.clone(),
24 token: Arc::new(RwLock::new(String::new())),
25 };
26
27 let auth_2 = auth.clone();
28 tokio::spawn(async move { heartbeat::routine(auth_2).await });
29
30 log::info!("Waiting for the first server heartbeat");
31 while auth.token.read().await.is_empty() {
32 tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
33 }
34 log::info!("Received the first server heartbeat");
35
36 auth
37 }
38
39 pub async fn get_token(&self) -> String {
40 self.token.read().await.clone()
41 }
42}