Skip to content

Commit ddd6e8a

Browse files
Allow basic http auth for Remote
1 parent 635982c commit ddd6e8a

File tree

6 files changed

+63
-19
lines changed

6 files changed

+63
-19
lines changed

LauncherGamePlugin/Storage.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using LauncherGamePlugin.Enums;
1+
using System.Net.Http.Headers;
2+
using LauncherGamePlugin.Enums;
23
using LauncherGamePlugin.Interfaces;
34
using Newtonsoft.Json;
45

@@ -31,12 +32,13 @@ public static class Storage
3132
}
3233

3334
public static Task<byte[]?> ImageDownload(string? url) => ImageDownload((url == null) ? null : new Uri(url));
34-
public static async Task<byte[]?> ImageDownload(Uri? uri)
35+
public static async Task<byte[]?> ImageDownload(Uri? uri, AuthenticationHeaderValue? auth = null)
3536
{
3637
if (uri == null)
3738
return null;
3839

3940
using HttpClient client = new();
41+
client.DefaultRequestHeaders.Authorization = auth;
4042
try
4143
{
4244
HttpResponseMessage response = await client.GetAsync(uri);

RemoteDownloaderPlugin/Game/GameDownload.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using ICSharpCode.SharpZipLib.Zip;
1+
using System.Net.Http.Headers;
2+
using ICSharpCode.SharpZipLib.Zip;
23
using LauncherGamePlugin;
34
using LauncherGamePlugin.Interfaces;
45
using RemoteDownloaderPlugin.Utils;
@@ -20,11 +21,13 @@ public class GameDownload : ProgressStatus
2021
public ContentTypes InstalledEntries { get; private set; }
2122

2223
private DateTimeOffset _downloadStart = DateTimeOffset.Now;
24+
private AuthenticationHeaderValue? _auth;
2325

24-
public GameDownload(OnlineGameDownload entry)
26+
public GameDownload(OnlineGameDownload entry, AuthenticationHeaderValue? auth)
2527
{
2628
_entry = entry;
2729
InstalledEntries = new();
30+
_auth = auth;
2831
}
2932

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

7376
using HttpClient client = new();
77+
client.DefaultRequestHeaders.Authorization = _auth;
7478

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

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

RemoteDownloaderPlugin/Game/OnlineGame.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public async Task Download()
6868
return;
6969
}
7070

71-
_download = new GameDownload(Game);
71+
_download = new GameDownload(Game, _plugin.Storage.Data.GetAuth());
7272
OnUpdate?.Invoke();
7373

7474
try

RemoteDownloaderPlugin/Gui/SettingsRemoteIndexGui.cs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,64 @@ public class SettingsRemoteIndexGui
88
private IApp _app;
99
private Plugin _instance;
1010

11+
private string _indexUrl;
12+
private string _authUser;
13+
private string _authPass;
14+
1115
public SettingsRemoteIndexGui(IApp app, Plugin instance)
1216
{
1317
_app = app;
1418
_instance = instance;
1519
}
1620

17-
public void ShowGui(string possibleError = "")
21+
public void ShowGui()
1822
{
1923
List<FormEntry> formEntries = new()
2024
{
2125
Form.TextBox("Enter remote index URL", FormAlignment.Left, "Bold"),
22-
Form.TextInput("Index URL:", _instance.Storage.Data.IndexUrl),
26+
Form.TextInput("Index URL:", _instance.Storage.Data.IndexUrl).NotEmpty(),
27+
Form.TextInput("User:", _instance.Storage.Data.IndexUser).WhenNotEmpty(),
28+
Form.TextInput("Pass:", _instance.Storage.Data.IndexPass).WhenNotEmpty(),
2329
Form.Button("Cancel", _ => _app.HideForm(), "Save", Process)
2430
};
2531

26-
if (!string.IsNullOrWhiteSpace(possibleError))
27-
formEntries.Add(Form.TextBox(possibleError, fontWeight: "Bold"));
32+
var errorEntry = Form.TextBox("", FormAlignment.Center);
33+
formEntries.Add(errorEntry);
34+
var form = new Form(formEntries)
35+
{
36+
ValidationFailureField = errorEntry
37+
};
2838

29-
_app.ShowForm(formEntries);
39+
_app.ShowForm(form);
3040
}
3141

3242
private async void Process(Form form)
3343
{
34-
_app.ShowTextPrompt("Loading");
35-
var newUrl = form.GetValue("Index URL:");
36-
var origIndexUrl = _instance.Storage.Data.IndexUrl;
37-
38-
if (string.IsNullOrWhiteSpace(newUrl))
44+
if (!form.Validate(_app))
3945
{
40-
ShowGui("Index URL is empty");
4146
return;
4247
}
4348

49+
_app.ShowTextPrompt("Loading");
50+
var newUrl = form.GetValue("Index URL:")!;
51+
var newUser = form.GetValue("User:")!;
52+
var newPass = form.GetValue("Pass:")!;
53+
var origIndexUrl = _instance.Storage.Data.IndexUrl;
54+
var origUser = _instance.Storage.Data.IndexUser;
55+
var origPass = _instance.Storage.Data.IndexPass;
56+
4457
if (newUrl != origIndexUrl)
4558
{
4659
_instance.Storage.Data.IndexUrl = newUrl;
60+
_instance.Storage.Data.IndexUser = newUser;
61+
_instance.Storage.Data.IndexPass = newPass;
4762
if (!await _instance.FetchRemote())
4863
{
4964
_instance.Storage.Data.IndexUrl = origIndexUrl;
50-
ShowGui("Failed to fetch data from given URL");
65+
_instance.Storage.Data.IndexUser = origUser;
66+
_instance.Storage.Data.IndexPass = origPass;
67+
form.ValidationFailureField!.Name = "Failed to fetch data from given URL";
68+
_app.ShowForm(form);
5169
return;
5270
}
5371

RemoteDownloaderPlugin/Plugin.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using LauncherGamePlugin;
1+
using System.Net.Http.Headers;
2+
using LauncherGamePlugin;
23
using LauncherGamePlugin.Commands;
34
using LauncherGamePlugin.Enums;
45
using LauncherGamePlugin.Forms;
@@ -102,6 +103,8 @@ public async Task<bool> FetchRemote()
102103
{
103104
using HttpClient client = new();
104105
client.Timeout = TimeSpan.FromSeconds(10);
106+
client.DefaultRequestHeaders.Authorization = Storage.Data.GetAuth();
107+
105108
var data = await client.GetStringAsync(Storage.Data.IndexUrl);
106109
_cachedRemote = JsonConvert.DeserializeObject<Remote>(data)!;
107110

RemoteDownloaderPlugin/Store.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Text.Json.Serialization;
1+
using System.Net.Http.Headers;
2+
using System.Text.Json.Serialization;
23
using Newtonsoft.Json;
34

45
namespace RemoteDownloaderPlugin;
@@ -136,6 +137,21 @@ public class Store
136137
public List<EmuProfile> EmuProfiles { get; set; } = new();
137138
public List<string> HiddenRemotePlatforms { get; set; } = new();
138139
public string IndexUrl { get; set; } = "";
140+
public string IndexUser { get; set; } = "";
141+
public string IndexPass { get; set; } = "";
142+
143+
public AuthenticationHeaderValue? GetAuth()
144+
{
145+
if (string.IsNullOrWhiteSpace(IndexUser))
146+
return null;
147+
148+
IndexPass ??= string.Empty;
149+
var authenticationString = $"{IndexUser}:{IndexPass}";
150+
var base64String = Convert.ToBase64String(
151+
System.Text.Encoding.ASCII.GetBytes(authenticationString));
152+
153+
return new AuthenticationHeaderValue("Basic", base64String);
154+
}
139155

140156
public void Migrate()
141157
{

0 commit comments

Comments
 (0)