Pacer/Program.cs
2026-04-22 22:28:17 -04:00

160 lines
5.9 KiB
C#

using System;
using System.Net.Http;
using System.Net.Mail;
using System.Text;
using System.Text.Json;
using OtpNet;
using DotNetEnv;
namespace PacerConsole
{
class Program
{
// Put credentials here
static string username;
static string password;
static async Task Main(string[] args)
{
DotNetEnv.Env.Load();
//string secret = "2PUSNHLSWJTMBEPQUFUJHWPBCYKJHIGI";
string secret = Environment.GetEnvironmentVariable("OTP_SECRET");
username = Environment.GetEnvironmentVariable("USERNAME");
password = Environment.GetEnvironmentVariable("PASSWORD");
byte[] base32Bytes = Base32Encoding.ToBytes(secret);
Totp otp = new Totp(base32Bytes, step: 30);
string otpCode = otp.ComputeTotp();
Console.WriteLine("OTP Code: " + otpCode);
string token = await PacerLogin(otpCode); // await it
if (token != "1" && token != "2")
{
await PacerLogout(token);
}
} //Main closes HERE
static async Task<string> PacerLogin(string otpCode) //Task is async, which i forget. Change to Task<string> when ready to continue
{
string url = "https://qa-login.uscourts.gov/services/cso-auth";
var payload = new
{
loginId = username,
password = password,
otpCode = otpCode
};
string json = JsonSerializer.Serialize(payload);
HttpClientHandler handler = new HttpClientHandler()
{
UseProxy = false
};
using (HttpClient client = new HttpClient(handler))
{
client.DefaultRequestHeaders.Add("Accept", "application/json");
HttpContent content = new StringContent(json, Encoding.UTF8, "application/json");
try
{
HttpResponseMessage response = await client.PostAsync(url, content);
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine("Raw Response" + responseBody);
using (JsonDocument doc = JsonDocument.Parse(responseBody))
{
JsonElement root = doc.RootElement;
string token = root.GetProperty("nextGenCSO").GetString();
string loginResult = root.GetProperty("loginResult").GetString();
string errorDesc = "";
if (root.TryGetProperty("errorDescription", out JsonElement errorElement))
{
errorDesc = errorElement.GetString();
}
if (loginResult == "0")
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Login successful!");
Console.WriteLine("Token: " + token);
Console.ResetColor();
return token;
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Login failed: " + errorDesc);
Console.ResetColor();
return "1";
}
}
}//try closes here
catch (Exception ex)
{
Console.BackgroundColor = ConsoleColor.DarkRed;
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Error: " + ex.Message);
Console.ResetColor();
return "2";
}
}
} // PacerLogin closes here
static async Task PacerLogout(string token)
{
string url = "https://qa-login.uscourts.gov/services/cso-logout";
var payload = new
{
nextGenCSO = token
};
string json = JsonSerializer.Serialize(payload);
//HttpClientHandler handler = new HttpClientHandler() { UseProxy = false };
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Accept", "application/json");
HttpContent content = new StringContent(json, Encoding.UTF8, "application/json");
try
{
HttpResponseMessage response = await client.PostAsync(url, content);
string responseBody = await response.Content.ReadAsStringAsync();
using (JsonDocument doc = JsonDocument.Parse(responseBody))
{
JsonElement root = doc.RootElement;
string loginResult = root.GetProperty("loginResult").GetString();
if (loginResult == "0")
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Logged out successfully!");
Console.ResetColor();
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Logout failed!");
Console.ResetColor();
}
}
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Logout error: " + ex.Message);
Console.ResetColor();
}
}
}//pacer Logout closes here
} // class closes here
} // namespace closes here