logging in with quick connect works now!

This commit is contained in:
random() 2025-05-15 21:05:32 -06:00
parent 70c2372535
commit 6235a69e43
10 changed files with 131 additions and 64 deletions

View file

@ -9,6 +9,7 @@ using System.Reflection;
using System.Security.Cryptography;
using Newtonsoft.Json;
using System.IO;
using System.Threading;
namespace JellyfinRPC
{
@ -179,47 +180,83 @@ namespace JellyfinRPC
}
return "album_cover";
}
public static async Task<string> SendQuickConnectRequest()
public static string quickConnectToken;
static bool authSuccess;
public static bool formClosed;
public static async Task AuthWithQuickConnect()
{
using var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("Authorization", $"MediaBrowser Client=\"JellyfinRPC\", Device=\"{Environment.OSVersion}\", DeviceId=\"{ConfigManager.GetEntry("DeviceID")}\", Version=\"{AssemblyVersion}\"");
httpClient.DefaultRequestHeaders.Add("Authorization", $"MediaBrowser Client=\"JellyfinRPC {AssemblyVersion}, \", Device=\"{System.Net.Dns.GetHostName()}\", DeviceId=\"{ConfigManager.GetEntry("DeviceID")}\", Version=\"running on {Environment.OSVersion}\"");
if (ConfigManager.GetEntry("ServerURL") != "")
{
var response = await httpClient.GetAsync($"{ConfigManager.GetEntry("ServerURL")}/QuickConnect/Initiate");
if (response.StatusCode is (System.Net.HttpStatusCode)401)
var quickConnectInitResponse = await httpClient.GetAsync($"{ConfigManager.GetEntry("ServerURL")}/QuickConnect/Initiate");
authSuccess = false;
if (quickConnectInitResponse.StatusCode is (System.Net.HttpStatusCode)401)
{
System.Windows.Forms.MessageBox.Show("This server does not have Quick Connect enabled.", "Quick Connect Unavailable.", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error, System.Windows.Forms.MessageBoxDefaultButton.Button1);
return "Error: Server does not have Quick Connect Enabled.";
}
else
{
try
{
response.EnsureSuccessStatusCode();
var jsonResponse = await response.Content.ReadAsStringAsync();
quickConnectInitResponse.EnsureSuccessStatusCode();
var jsonResponse = await quickConnectInitResponse.Content.ReadAsStringAsync();
var jsonObject = JObject.Parse(jsonResponse);
if (!jsonObject.HasValues)
{
return "Error: Server did not respond.";
}
else
{
string quickConnectCode = jsonObject.Value<string>("Code");
string quickConnectToken = jsonObject.Value<string>("Secret");
return quickConnectCode;
if (jsonObject.Value<string>("Secret") != null && jsonObject.Value<string>("Secret") != "")
{
quickConnectToken = jsonObject.Value<string>("Secret");
}
else
{
quickConnectToken = null;
}
(System.Windows.Forms.Application.OpenForms["ConfigForm"] as ConfigForm).label9.Text = quickConnectCode;
while (authSuccess == false)
{
var getThings = await CheckQuickConnectStatus();
if (formClosed == true)
{
break;
}
else if (getThings == null)
{
await CheckQuickConnectStatus();
}
else if (getThings == "this isn't gonna work")
{
System.Windows.Forms.MessageBox.Show("Quick Connect doesn't seem to be working. Check your Internet connection and try again later.", "Quick Connect Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
break;
}
else
{
authSuccess = true;
}
}
}
}
catch (Exception ex)
{
return $"{ex.Message.Trim()}";
#if DEBUG
System.Windows.Forms.MessageBox.Show(ex.ToString());
#endif
}
}
}
else
{
return "Error: No Server URL Set.";
System.Windows.Forms.MessageBox.Show("No Server URL Set", "Quick Connect Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation);
}
}
@ -246,16 +283,16 @@ namespace JellyfinRPC
var loginRequest = new Login() { username = username, pw = password };
var loginJson = JsonConvert.SerializeObject(loginRequest);
HttpResponseMessage response = await httpClient.PostAsync($"{ConfigManager.GetEntry("ServerURL")}/Users/AuthenticateByName", new StringContent(loginJson, Encoding.UTF8, "application/json"));
if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
HttpResponseMessage loginWithUsernameandPassResponse = await httpClient.PostAsync($"{ConfigManager.GetEntry("ServerURL")}/Users/AuthenticateByName", new StringContent(loginJson, Encoding.UTF8, "application/json"));
if (loginWithUsernameandPassResponse.StatusCode == System.Net.HttpStatusCode.Unauthorized)
{
System.Windows.Forms.MessageBox.Show("The Username or Password is incorrect.", "Authentication Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error, System.Windows.Forms.MessageBoxDefaultButton.Button1);
}
else
{
response.EnsureSuccessStatusCode();
loginWithUsernameandPassResponse.EnsureSuccessStatusCode();
var jsonResponse = await response.Content.ReadAsStringAsync();
var jsonResponse = await loginWithUsernameandPassResponse.Content.ReadAsStringAsync();
var jsonShit = JObject.Parse(jsonResponse);
@ -269,6 +306,32 @@ namespace JellyfinRPC
}
}
}
public static async Task<string> CheckQuickConnectStatus()
{
using var httpClient = new HttpClient();
if (quickConnectToken != null && quickConnectToken != "")
{
TaskEx.Delay(1000);
string secret = quickConnectToken.Replace('"', ' ').Trim();
HttpResponseMessage QuickConnectStatusResponse = await httpClient.GetAsync($"{ConfigManager.GetEntry("ServerURL")}/QuickConnect/Connect?Secret={quickConnectToken}");
QuickConnectStatusResponse.EnsureSuccessStatusCode();
var responseAsString = await QuickConnectStatusResponse.Content.ReadAsStringAsync();
var responseJson = JObject.Parse(responseAsString);
if (responseJson.ContainsKey("AccessToken"))
{
return responseJson.Value<string>("AccessToken");
}
else
{
return null;
}
}
else
{
return "this isn't gonna work";
}
}
}
public class Login
@ -277,31 +340,5 @@ namespace JellyfinRPC
public string username { get; set; }
public string pw { get; set; }
#pragma warning restore IDE1006 // Naming Styles
}
public class QuickConnect
{
[JsonProperty("Authenticated")]
public bool Authenticated { get; set; }
[JsonProperty("Secret")]
public string Secret { get; set; }
[JsonProperty("Code")]
public string Code { get; set; }
[JsonProperty("DeviceId")]
public string DeviceId { get; set; }
[JsonProperty("DeviceName")]
public string DeviceName { get; set; }
[JsonProperty("AppName")]
public string AppName { get; set; }
[JsonProperty("AppVersion")]
public string AppVersion { get; set; }
[JsonProperty("DateAdded")]
public DateTime DateAdded { get; set; }
}
}
}