Files
mas-admin-rs/src/main.rs
2026-03-06 17:01:50 -05:00

148 lines
4.0 KiB
Rust

// 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 std::io::Write;
use clap::{CommandFactory, Parser, Subcommand};
use colored::{ColoredString, 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());
let mut app = App::new(MATRIX_SERVER_BASE_URL, MAS_SERVER_BASE_URL);
app.get_auth_metadata();
app.register_client();
app.print_status();
print_menu();
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::Setup(sub_args) => {
match sub_args.command {
SetupCommands::GetAuthMetadata => app.get_auth_metadata(),
SetupCommands::RegisterClient => app.register_client(),
SetupCommands::AuthorizeDevice => app.authorize_device(),
SetupCommands::RefreshAuth => app.refresh_auth(),
}
continue;
}
_ => {
if !app.admin_api_ready() {
println!(
"{}",
"Admin API not ready; please use setup subcommand first".red()
);
continue;
}
}
}
match cli.command {
Commands::Setup(_) => unreachable!("Blocked by above match"),
Commands::ShowOauthLinks => app.show_oauth_links(),
}
}
}
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)]
enum Commands {
#[command(subcommand_help_heading = "Setup Auth for Admin API")]
#[command(arg_required_else_help = true)]
Setup(SetupCommandArgs),
ShowOauthLinks,
}
#[derive(Debug, Parser)]
struct SetupCommandArgs {
#[command(subcommand)]
command: SetupCommands,
}
#[derive(Debug, Subcommand, PartialEq, Eq, Sequence)]
enum SetupCommands {
GetAuthMetadata,
RegisterClient,
AuthorizeDevice,
RefreshAuth,
}
fn quick_format_bool(b: bool) -> ColoredString {
match b {
true => "true".bright_green(),
false => "false".red(),
}
}