Files
bot/src/token.rs
syui d0746a7af6
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Failing after 11m36s
update handle
2025-06-16 20:41:03 +09:00

48 lines
1.6 KiB
Rust

use crate::http_client::HttpClient;
use std::collections::HashMap;
use std::io::{self, Write};
pub async fn post_request(handle: String, pass: String, host: String, auth_factor_token: Option<String>) -> String {
// First attempt with provided 2FA code (if any)
let response = create_session_request(&handle, &pass, &host, auth_factor_token.as_deref()).await;
// Check if 2FA is required
if response.contains("AuthFactorTokenRequired") {
println!("🔐 2FA authentication required");
println!("📧 A sign-in code has been sent to your email address");
// Prompt for 2FA code
print!("Enter 2FA code: ");
io::stdout().flush().unwrap();
let mut code = String::new();
io::stdin().read_line(&mut code).unwrap();
let code = code.trim();
// Retry with 2FA code
println!("🔄 Retrying authentication with 2FA code...");
return create_session_request(&handle, &pass, &host, Some(code)).await;
}
response
}
async fn create_session_request(handle: &str, pass: &str, host: &str, auth_factor_token: Option<&str>) -> String {
let url = format!("https://{}/xrpc/com.atproto.server.createSession", host);
let mut map = HashMap::new();
map.insert("identifier", handle);
map.insert("password", pass);
// Add 2FA code if provided
if let Some(code) = auth_factor_token {
map.insert("authFactorToken", code);
}
let client = HttpClient::new();
match client.post_json(&url, &map).await {
Ok(response) => response,
Err(e) => format!("Error: {}", e),
}
}