Files
telegram-tui/src/tdlib/auth.rs
Mikhail Kilin bba5cbd22d fixes
2026-01-30 23:55:01 +03:00

73 lines
2.4 KiB
Rust

use tdlib_rs::enums::{AuthorizationState, Update};
use tdlib_rs::functions;
#[derive(Debug, Clone, PartialEq)]
#[allow(dead_code)]
pub enum AuthState {
WaitTdlibParameters,
WaitPhoneNumber,
WaitCode,
WaitPassword,
Ready,
Closed,
Error(String),
}
/// Менеджер авторизации TDLib
pub struct AuthManager {
pub state: AuthState,
client_id: i32,
}
impl AuthManager {
pub fn new(client_id: i32) -> Self {
Self {
state: AuthState::WaitTdlibParameters,
client_id,
}
}
pub fn is_authenticated(&self) -> bool {
self.state == AuthState::Ready
}
/// Обработать обновление авторизации
pub fn handle_auth_update(&mut self, update: &Update) {
if let Update::AuthorizationState(auth_update) = update {
self.state = match &auth_update.authorization_state {
AuthorizationState::WaitTdlibParameters => AuthState::WaitTdlibParameters,
AuthorizationState::WaitPhoneNumber => AuthState::WaitPhoneNumber,
AuthorizationState::WaitCode(_) => AuthState::WaitCode,
AuthorizationState::WaitPassword(_) => AuthState::WaitPassword,
AuthorizationState::Ready => AuthState::Ready,
AuthorizationState::Closed => AuthState::Closed,
_ => return,
};
}
}
/// Отправить номер телефона
pub async fn send_phone_number(&self, phone: String) -> Result<(), String> {
functions::set_authentication_phone_number(phone, None, self.client_id)
.await
.map(|_| ())
.map_err(|e| format!("Ошибка отправки номера: {:?}", e))
}
/// Отправить код подтверждения
pub async fn send_code(&self, code: String) -> Result<(), String> {
functions::check_authentication_code(code, self.client_id)
.await
.map(|_| ())
.map_err(|e| format!("Ошибка проверки кода: {:?}", e))
}
/// Отправить пароль 2FA
pub async fn send_password(&self, password: String) -> Result<(), String> {
functions::check_authentication_password(password, self.client_id)
.await
.map(|_| ())
.map_err(|e| format!("Ошибка проверки пароля: {:?}", e))
}
}