Fixed TG login
Build and Publish / Build and Publish Docker Image (push) Successful in 5m9s

This commit is contained in:
ab committed 2026-07-06 16:30:16 +03:00
1 parent 3b4899f785
commit 2dbbdb0252
10 files changed
+239 -11

No files matched your search

+75
View File
@@ -162,6 +162,11 @@ struct TelegramLinkResponse {
status: TelegramLinkStatusResponse,
}
#[derive(Debug, Serialize, JsonSchema)]
struct TelegramLoginResponse {
redirect_to: String,
}
#[derive(Debug, Deserialize, JsonSchema)]
struct TelegramWebAppLinkRequest {
init_data: String,
@@ -292,6 +297,71 @@ async fn telegram_link_webapp_handler(
.into_response()
}
async fn telegram_login_webapp_handler(
session: Session,
db: Database,
Json(request): Json<TelegramWebAppLinkRequest>,
) -> cot::Result<cot::response::Response> {
let (config, _) = AppConfig::load_with_db(&db).await;
if !config.telegram_bot_enabled {
return Ok(json_error_typed(
cot::http::StatusCode::CONFLICT,
"telegram_disabled",
"Telegram is disabled",
"Telegram integration is disabled by the administrator.",
"",
));
}
let telegram_user = match telegram::validate_web_app_init_data(
&request.init_data,
&config.telegram_bot_token,
TELEGRAM_INIT_DATA_MAX_AGE,
) {
Ok(user) => user,
Err(e) => {
return Ok(json_error_typed(
cot::http::StatusCode::BAD_REQUEST,
"telegram_webapp_auth_failed",
"Telegram verification failed",
"Could not verify Telegram Web App data.",
&e.to_string(),
));
}
};
let telegram_id = telegram_user.id.to_string();
let Some(user) = User::get_by_telegram_id(&db, &telegram_id)
.await
.map_err(|e| cot::Error::internal(format!("failed to load Telegram user: {e}")))?
else {
return Ok(json_error_typed(
cot::http::StatusCode::UNAUTHORIZED,
"telegram_account_not_linked",
"Telegram is not linked",
"This Telegram account is not connected to any VPN account.",
"",
));
};
if !user.is_active() {
return Ok(json_error_typed(
cot::http::StatusCode::FORBIDDEN,
"user_inactive",
"Account is inactive",
"This account is inactive.",
"",
));
}
auth::login(&session, user.id_val()).await?;
Json(TelegramLoginResponse {
redirect_to: "/configs".to_owned(),
})
.into_response()
}
async fn telegram_link_start_handler(
session: Session,
db: Database,
@@ -831,6 +901,11 @@ impl App for ApiApp {
api_post(telegram_link_webapp_handler),
"api_telegram_link_webapp",
),
Route::with_api_handler_and_name(
"/telegram-login/webapp",
api_post(telegram_login_webapp_handler),
"api_telegram_login_webapp",
),
Route::with_api_handler_and_name(
"/telegram-link/start",
api_post(telegram_link_start_handler),