Skip to content

Commit

Permalink
Allow basic http auth for Remote
Browse files Browse the repository at this point in the history
  • Loading branch information
suchmememanyskill committed Jun 23, 2024
1 parent 635982c commit ddd6e8a
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 19 deletions.
6 changes: 4 additions & 2 deletions LauncherGamePlugin/Storage.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using LauncherGamePlugin.Enums;
using System.Net.Http.Headers;
using LauncherGamePlugin.Enums;
using LauncherGamePlugin.Interfaces;
using Newtonsoft.Json;

Expand Down Expand Up @@ -31,12 +32,13 @@ public static class Storage
}

public static Task<byte[]?> ImageDownload(string? url) => ImageDownload((url == null) ? null : new Uri(url));
public static async Task<byte[]?> ImageDownload(Uri? uri)
public static async Task<byte[]?> ImageDownload(Uri? uri, AuthenticationHeaderValue? auth = null)
{
if (uri == null)
return null;

using HttpClient client = new();
client.DefaultRequestHeaders.Authorization = auth;
try
{
HttpResponseMessage response = await client.GetAsync(uri);
Expand Down
9 changes: 7 additions & 2 deletions RemoteDownloaderPlugin/Game/GameDownload.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ICSharpCode.SharpZipLib.Zip;
using System.Net.Http.Headers;
using ICSharpCode.SharpZipLib.Zip;
using LauncherGamePlugin;
using LauncherGamePlugin.Interfaces;
using RemoteDownloaderPlugin.Utils;
Expand All @@ -20,11 +21,13 @@ public class GameDownload : ProgressStatus
public ContentTypes InstalledEntries { get; private set; }

private DateTimeOffset _downloadStart = DateTimeOffset.Now;
private AuthenticationHeaderValue? _auth;

public GameDownload(OnlineGameDownload entry)
public GameDownload(OnlineGameDownload entry, AuthenticationHeaderValue? auth)

Check warning on line 26 in RemoteDownloaderPlugin/Game/GameDownload.cs

View workflow job for this annotation

GitHub Actions / build-linux

Non-nullable property 'Version' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 26 in RemoteDownloaderPlugin/Game/GameDownload.cs

View workflow job for this annotation

GitHub Actions / build-linux

Non-nullable property 'Filename' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 26 in RemoteDownloaderPlugin/Game/GameDownload.cs

View workflow job for this annotation

GitHub Actions / build-linux

Non-nullable property 'BasePath' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
{
_entry = entry;
InstalledEntries = new();
_auth = auth;
}

private void OnProgressUpdate(object? obj, float progress)
Expand Down Expand Up @@ -71,6 +74,7 @@ private async Task DownloadEmu(IApp app)
Directory.CreateDirectory(extraFilesPath);

using HttpClient client = new();
client.DefaultRequestHeaders.Authorization = _auth;

for (int i = 0; i < _entry.Files.Count; i++)
{
Expand Down Expand Up @@ -128,6 +132,7 @@ private async Task DownloadPc(IApp app)
Directory.CreateDirectory(BasePath);

using HttpClient client = new();
client.DefaultRequestHeaders.Authorization = _auth;
Progress<float> progress = new();
progress.ProgressChanged += OnProgressUpdate;

Expand Down
2 changes: 1 addition & 1 deletion RemoteDownloaderPlugin/Game/OnlineGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public async Task Download()
return;
}

_download = new GameDownload(Game);
_download = new GameDownload(Game, _plugin.Storage.Data.GetAuth());
OnUpdate?.Invoke();

try
Expand Down
42 changes: 30 additions & 12 deletions RemoteDownloaderPlugin/Gui/SettingsRemoteIndexGui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,64 @@ public class SettingsRemoteIndexGui
private IApp _app;
private Plugin _instance;

private string _indexUrl;
private string _authUser;
private string _authPass;

public SettingsRemoteIndexGui(IApp app, Plugin instance)

Check warning on line 15 in RemoteDownloaderPlugin/Gui/SettingsRemoteIndexGui.cs

View workflow job for this annotation

GitHub Actions / build-linux

Non-nullable field '_indexUrl' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 15 in RemoteDownloaderPlugin/Gui/SettingsRemoteIndexGui.cs

View workflow job for this annotation

GitHub Actions / build-linux

Non-nullable field '_authUser' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
{
_app = app;
_instance = instance;
}

public void ShowGui(string possibleError = "")
public void ShowGui()
{
List<FormEntry> formEntries = new()
{
Form.TextBox("Enter remote index URL", FormAlignment.Left, "Bold"),
Form.TextInput("Index URL:", _instance.Storage.Data.IndexUrl),
Form.TextInput("Index URL:", _instance.Storage.Data.IndexUrl).NotEmpty(),
Form.TextInput("User:", _instance.Storage.Data.IndexUser).WhenNotEmpty(),
Form.TextInput("Pass:", _instance.Storage.Data.IndexPass).WhenNotEmpty(),
Form.Button("Cancel", _ => _app.HideForm(), "Save", Process)
};

if (!string.IsNullOrWhiteSpace(possibleError))
formEntries.Add(Form.TextBox(possibleError, fontWeight: "Bold"));
var errorEntry = Form.TextBox("", FormAlignment.Center);
formEntries.Add(errorEntry);
var form = new Form(formEntries)
{
ValidationFailureField = errorEntry
};

_app.ShowForm(formEntries);
_app.ShowForm(form);
}

private async void Process(Form form)
{
_app.ShowTextPrompt("Loading");
var newUrl = form.GetValue("Index URL:");
var origIndexUrl = _instance.Storage.Data.IndexUrl;

if (string.IsNullOrWhiteSpace(newUrl))
if (!form.Validate(_app))
{
ShowGui("Index URL is empty");
return;
}

_app.ShowTextPrompt("Loading");
var newUrl = form.GetValue("Index URL:")!;
var newUser = form.GetValue("User:")!;
var newPass = form.GetValue("Pass:")!;
var origIndexUrl = _instance.Storage.Data.IndexUrl;
var origUser = _instance.Storage.Data.IndexUser;
var origPass = _instance.Storage.Data.IndexPass;

if (newUrl != origIndexUrl)
{
_instance.Storage.Data.IndexUrl = newUrl;
_instance.Storage.Data.IndexUser = newUser;
_instance.Storage.Data.IndexPass = newPass;
if (!await _instance.FetchRemote())
{
_instance.Storage.Data.IndexUrl = origIndexUrl;
ShowGui("Failed to fetch data from given URL");
_instance.Storage.Data.IndexUser = origUser;
_instance.Storage.Data.IndexPass = origPass;
form.ValidationFailureField!.Name = "Failed to fetch data from given URL";
_app.ShowForm(form);
return;
}

Expand Down
5 changes: 4 additions & 1 deletion RemoteDownloaderPlugin/Plugin.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using LauncherGamePlugin;
using System.Net.Http.Headers;
using LauncherGamePlugin;
using LauncherGamePlugin.Commands;
using LauncherGamePlugin.Enums;
using LauncherGamePlugin.Forms;
Expand Down Expand Up @@ -102,6 +103,8 @@ public async Task<bool> FetchRemote()
{
using HttpClient client = new();
client.Timeout = TimeSpan.FromSeconds(10);
client.DefaultRequestHeaders.Authorization = Storage.Data.GetAuth();

var data = await client.GetStringAsync(Storage.Data.IndexUrl);
_cachedRemote = JsonConvert.DeserializeObject<Remote>(data)!;

Expand Down
18 changes: 17 additions & 1 deletion RemoteDownloaderPlugin/Store.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.Json.Serialization;
using System.Net.Http.Headers;
using System.Text.Json.Serialization;
using Newtonsoft.Json;

namespace RemoteDownloaderPlugin;
Expand Down Expand Up @@ -136,6 +137,21 @@ public class Store
public List<EmuProfile> EmuProfiles { get; set; } = new();
public List<string> HiddenRemotePlatforms { get; set; } = new();
public string IndexUrl { get; set; } = "";
public string IndexUser { get; set; } = "";
public string IndexPass { get; set; } = "";

public AuthenticationHeaderValue? GetAuth()
{
if (string.IsNullOrWhiteSpace(IndexUser))
return null;

IndexPass ??= string.Empty;
var authenticationString = $"{IndexUser}:{IndexPass}";
var base64String = Convert.ToBase64String(
System.Text.Encoding.ASCII.GetBytes(authenticationString));

return new AuthenticationHeaderValue("Basic", base64String);
}

public void Migrate()
{
Expand Down

0 comments on commit ddd6e8a

Please sign in to comment.