flare_im_core/server/
auth_handler.rs

1use flare_core::context::AppContext;
2use flare_core::error::{FlareErr, Result};
3use crate::server::handlers::CommandHandler;
4use async_trait::async_trait;
5use log::debug;
6use prost::Message;
7use flare_core::flare_net::net::{LoginReq, LoginResp};
8use flare_core::flare_net::net::{Command, ResCode, Response};
9
10#[async_trait]
11pub trait AuthHandler: Send + Sync {
12    /// 处理登录请求
13    async fn handle_login(&self, ctx:  &AppContext) -> Result<Response>;
14
15    /// 处理登出请求
16    async fn handle_logout(&self, ctx:  &AppContext) -> Result<Response>;
17}
18
19/// 认证命令处理器
20pub struct AuthCommandHandler<T>(pub T);
21
22impl<T> AuthCommandHandler<T> {
23    pub fn new(handler: T) -> Self {
24        Self(handler)
25    }
26}
27
28// 实现 AuthHandler
29#[async_trait]
30impl<T: AuthHandler + Send + Sync> AuthHandler for AuthCommandHandler<T> {
31    async fn handle_login(&self, ctx:  &AppContext) -> Result<Response> {
32        self.0.handle_login(ctx).await
33    }
34
35    async fn handle_logout(&self, ctx:  &AppContext) -> Result<Response> {
36        self.0.handle_logout(ctx).await
37    }
38}
39
40#[async_trait]
41impl<T: AuthHandler + Send + Sync> CommandHandler for AuthCommandHandler<T> {
42    async fn handle_command(&self, ctx:  &AppContext) -> Result<Response> {
43        let command = ctx.command().ok_or_else(|| 
44            FlareErr::invalid_command("Missing command"))?;
45
46        if !self.supports_command(command) {
47            return Ok(Response {
48                code: ResCode::InvalidCommand as i32,
49                message: format!("Unsupported command: {:?}", command),
50                data: Vec::new(),
51            });
52        }
53
54        match command {
55            Command::Login => self.handle_login(ctx).await,
56            Command::LoginOut => self.handle_logout(ctx).await,
57            _ => Ok(Response {
58                code: ResCode::InvalidCommand as i32,
59                message: format!("Unexpected command: {:?}", command),
60                data: Vec::new(),
61            })
62        }
63    }
64    fn supported_commands(&self) -> Vec<Command> {
65        vec![Command::Login , Command::LoginOut]
66    }
67}
68
69/// 默认的认证处理器实现
70#[derive(Default)]
71pub struct DefAuthHandler;
72
73impl DefAuthHandler {
74    pub fn new() -> Self {
75        Self {}
76    }
77}
78
79#[async_trait]
80impl AuthHandler for DefAuthHandler {
81    async fn handle_login(&self, ctx:  &AppContext) -> Result<Response> {
82        let req = ctx.get_data_as::<LoginReq>()?;
83        debug!("处理登录请求 - addr: {}, userid: {}", ctx.remote_addr(), req.user_id);
84
85        // 这里可以添加实际的登录验证逻辑
86        if req.token.is_empty() {
87            return Ok(Response {
88                code: ResCode::Unauthorized as i32,
89                message: "Token is required".into(),
90                data: Vec::new(),
91            });
92        }
93        let resp = LoginResp {
94            user_id:"sss".to_string(),
95            language:"zh".to_string(),
96        };
97        Ok(Response {
98            code: ResCode::Success as i32,
99            message: "登录成功".into(),
100            data: resp.encode_to_vec(),
101        })
102    }
103
104    async fn handle_logout(&self, ctx:  &AppContext) -> Result<Response> {
105        debug!("处理登出请求 - addr: {}", ctx.remote_addr());
106        
107        if let Some(user_id) = ctx.user_id() {
108            debug!("用户 {} 登出", user_id);
109        }
110
111        Ok(Response {
112            code: ResCode::Success as i32,
113            message: "登出成功".into(),
114            data: Vec::new(),
115        })
116    }
117}