Files
mas-admin-rs/src/main.rs

160 lines
4.7 KiB
Rust
Raw Normal View History

2026-03-06 14:55:28 -05:00
// these were for synapse, disabled with MAS
// const USERS_ENDPOINT_V3: &str = "/_synapse/admin/v3/users";
// const USERS_ENDPOINT_V2: &str = "/_synapse/admin/v2/users";
// const REGISTRATION_TOKENS_ENDPOINT: &str = "/_synapse/admin/v1/registration_tokens";
// q25W9kalf3EvMvJbLCkxByXym7OukM1F
//01KJZ6KYHQRRPMG3M49CZSBTM4Z
//01KJZ4RN8HAXYMHBC7F7H608EP
mod app;
pub mod json_types;
use json_types::*;
use std::io::Write;
use clap::{CommandFactory, Parser, Subcommand};
use colored::Colorize;
use enum_iterator::Sequence;
use crate::app::App;
// google oauth
// 754241279547-kk48of2t6eu5vol85hn6op3ofpp3em76.apps.googleusercontent.com
const MATRIX_SERVER_BASE_URL: &str = "https://matrix.supersaturn.space";
const MAS_SERVER_BASE_URL: &str = "https://mas.supersaturn.space";
const CLIENT_SECRET: &str = "asdjfja2392ijf23923ndflkas01812j312k3j_18127";
fn main() {
//curl --header "Authorization: Bearer syt_anA_YRbjQUlvKVDuAfIDIdPW_0k2VVC" -X GET http://127.0.0.1:8008/_synapse/admin/v2/users/@jp:matrix.supersaturn.space
// let access_token = "Bearer mat_zA80YL1OafucuRGgdZSPIILCSquto2_nT9b73";
print!("\x1B[2J\x1B[2;5H");
println!("{}", "Welcome to auth tools CLI".bright_cyan());
println!("{} {MATRIX_SERVER_BASE_URL}\n\n", "Homeserver:".green());
print_menu();
let mut app = App::new(MATRIX_SERVER_BASE_URL, MAS_SERVER_BASE_URL);
app.get_auth_metadata();
app.register_client();
app.print_status();
loop {
let input = readline().unwrap();
let input = input.trim();
if input.is_empty() {
continue;
}
let Some(args) = shlex::split(input) else {
continue;
};
let cli = match Cli::try_parse_from(args) {
Ok(c) => c,
Err(e) => {
println!("{}", e.to_string());
continue;
}
};
match cli.command {
Commands::GetAuthMetadata => app.get_auth_metadata(),
Commands::RegisterClient => app.register_client(),
Commands::AuthorizeDevice => app.authorize_device(),
Commands::RefreshAuth => app.refresh_auth(),
Commands::ShowOauthLinks => app.show_oauth_links(),
2026-03-06 14:55:28 -05:00
}
}
//
// todo!("nope");
// let req = client
// .post(format!(
// "{base_url}{USERS_ENDPT}/01KJXZRB1FSNNQ22DDC0368V9G/deactivate"
// ))
// .header("Accept", "application/json")
// .header("Authorization", access_token)
// .header("Content-Type", "application/json")
// .body(include_str!("../deactivate.json"));
// let req = client
// .post(format!("{base_url}{REGISTRATION_TOKENS_ENDPOINT}/new"))
// .header("Authorization", format!("Bearer {access_token}"))
// .body("{}");
// let req = client.get(format!("{base_url}{USERS_ENDPOINT_V3}")).header(
// "Authorization",
// "Bearer syt_anA_YRbjQUlvKVDuAfIDIdPW_0k2VVC",
// );
// let req = client
// .put(format!(
// "{base_url}{USERS_ENDPOINT_V2}/@afriend:supersaturn.space"
// ))
// .header("Authorization", "))
// .body(include_str!("../postbody.json"));
// println!("{:?}", req);
// let req = req.send().unwrap();
// match req.status() {
// StatusCode::OK | StatusCode::NO_CONTENT | StatusCode::CREATED => (),
// StatusCode::UNAUTHORIZED => {
// if !req.text().unwrap().contains("token expired") {
// panic!("Not authorized: {}", req.text().unwrap());
// }
// let req = client.get(format!("{MAS_SERVER_BASE_URL}{TOKEN_ENDPT}"));
// }
// sc => panic!("Bad response?\n{}\n{:?}", sc, req.text()),
// };
// let req_txt = req.text().unwrap();
// // println!("{}", &req_txt);
// let txt = prettify_json_str(&req_txt);
// println!("Blocking req status\n\n{}\n\n", txt.unwrap());
//
}
const MENU_HELP_TEMPLATE: &str = "\
{about-with-newline}\n\
{all-args}{after-help}\
";
fn print_menu() {
let mut cli = Cli::command().help_template(MENU_HELP_TEMPLATE);
cli.print_long_help().unwrap();
}
fn readline() -> Result<String, String> {
write!(std::io::stdout(), "> ").map_err(|e| e.to_string())?;
std::io::stdout().flush().map_err(|e| e.to_string())?;
let mut buffer = String::new();
std::io::stdin()
.read_line(&mut buffer)
.map_err(|e| e.to_string())?;
Ok(buffer)
}
#[derive(Debug, clap::Parser)]
#[command(multicall = true)]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Debug, Subcommand, PartialEq, Eq, Sequence)]
enum Commands {
GetAuthMetadata,
RegisterClient,
AuthorizeDevice,
RefreshAuth,
2026-03-06 14:55:28 -05:00
ShowOauthLinks,
}