flare_core/server/auth/
authenticator.rs1use crate::common::device::DeviceInfo;
6use crate::common::error::Result;
7use async_trait::async_trait;
8
9#[derive(Debug, Clone)]
11pub struct AuthResult {
12 pub authenticated: bool,
14 pub user_id: Option<String>,
16 pub error_message: Option<String>,
18 pub user_metadata: Option<std::collections::HashMap<String, String>>,
20}
21
22impl AuthResult {
23 pub fn success(user_id: Option<String>) -> Self {
25 Self {
26 authenticated: true,
27 user_id,
28 error_message: None,
29 user_metadata: None,
30 }
31 }
32
33 pub fn success_with_metadata(
35 user_id: Option<String>,
36 metadata: std::collections::HashMap<String, String>,
37 ) -> Self {
38 Self {
39 authenticated: true,
40 user_id,
41 error_message: None,
42 user_metadata: Some(metadata),
43 }
44 }
45
46 pub fn failure(error_message: String) -> Self {
48 Self {
49 authenticated: false,
50 user_id: None,
51 error_message: Some(error_message),
52 user_metadata: None,
53 }
54 }
55}
56
57#[async_trait]
62pub trait Authenticator: Send + Sync {
63 async fn authenticate(
75 &self,
76 token: &str,
77 connection_id: &str,
78 device_info: Option<&DeviceInfo>,
79 metadata: Option<&std::collections::HashMap<String, Vec<u8>>>,
80 ) -> Result<AuthResult>;
81
82 async fn is_authenticated(&self, connection_id: &str) -> Result<bool> {
87 let _ = connection_id;
88 Ok(true)
89 }
90
91 async fn revoke_authentication(&self, connection_id: &str) -> Result<()> {
96 let _ = connection_id;
97 Ok(())
98 }
99}