- tweak: change auth api

This commit is contained in:
2024-12-28 08:29:01 +03:00
parent bdb8915932
commit 87dd1d7c30
10 changed files with 110 additions and 69 deletions

View File

@@ -33,9 +33,9 @@ public partial class AuthService : ObservableObject
var login = authLoginPassword.Login;
var password = authLoginPassword.Password;
_debugService.Debug($"Auth to {authServer}/authenticate {login}");
_debugService.Debug($"Auth to {authServer}api/auth/authenticate {login}");
var authUrl = new Uri($"{authServer}/authenticate");
var authUrl = new Uri($"{authServer}api/auth/authenticate");
var result =
await _restService.PostAsync<AuthenticateResponse, AuthenticateRequest>(
@@ -57,7 +57,7 @@ public partial class AuthService : ObservableObject
{
if (SelectedAuth is null) return false;
var authUrl = new Uri($"{SelectedAuth.AuthLoginPassword.AuthServer}/ping");
var authUrl = new Uri($"{SelectedAuth.AuthLoginPassword.AuthServer}api/auth/ping");
using var requestMessage = new HttpRequestMessage(HttpMethod.Get, authUrl);
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("SS14Auth", SelectedAuth.Token.Token);

View File

@@ -2,8 +2,6 @@ using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
using Nebula.Launcher.FileApis;
namespace Nebula.Launcher.Services;
@@ -12,10 +10,10 @@ public class ConVar<T>
public string Name { get; }
public Type Type => typeof(T);
public T? DefaultValue { get; }
public ConVar(string name, T? defaultValue)
public ConVar(string name, T? defaultValue = default)
{
Name = name;
Name = name ?? throw new ArgumentNullException(nameof(name));
DefaultValue = defaultValue;
}
}
@@ -24,6 +22,9 @@ public static class ConVarBuilder
{
public static ConVar<T> Build<T>(string name, T? defaultValue = default)
{
if (string.IsNullOrWhiteSpace(name))
throw new ArgumentException("ConVar name cannot be null or whitespace.", nameof(name));
return new ConVar<T>(name, defaultValue);
}
}
@@ -36,78 +37,83 @@ public class ConfigurationService
public ConfigurationService(FileService fileService, DebugService debugService)
{
_fileService = fileService;
_debugService = debugService;
_fileService = fileService ?? throw new ArgumentNullException(nameof(fileService));
_debugService = debugService ?? throw new ArgumentNullException(nameof(debugService));
}
public T? GetConfigValue<T>(ConVar<T> conVar)
{
if(!_fileService.ConfigurationApi.TryOpen(GetFileName(conVar), out var stream) ||
!ReadStream<T>(stream, out var obj))
return conVar.DefaultValue;
ArgumentNullException.ThrowIfNull(conVar);
_debugService.Log("Loading config file: " + conVar.Name);
return obj;
}
public void SetConfigValue<T>(ConVar<T> conVar, object value)
{
if(conVar.Type != value.GetType())
{
_debugService.Error("Error config file " + conVar.Name + ": Type is not equals " + value.GetType() + " " + conVar.Type);
return;
}
_debugService.Log("Saving config file: " + conVar.Name);
WriteStream(conVar, value);
}
private bool ReadStream<T>(Stream stream,[NotNullWhen(true)] out T? obj)
{
obj = default;
try
{
obj = JsonSerializer.Deserialize<T>(stream);
return obj != null;
if (_fileService.ConfigurationApi.TryOpen(GetFileName(conVar), out var stream))
{
using (stream)
{
var obj = JsonSerializer.Deserialize<T>(stream);
if (obj != null)
{
_debugService.Log($"Successfully loaded config: {conVar.Name}");
return obj;
}
}
}
}
catch (Exception e)
{
_debugService.Error(e);
return false;
_debugService.Error($"Error loading config for {conVar.Name}: {e.Message}");
}
_debugService.Log($"Using default value for config: {conVar.Name}");
return conVar.DefaultValue;
}
private void WriteStream<T>(ConVar<T> conVar, object value)
public void SetConfigValue<T>(ConVar<T> conVar, T value)
{
using var stream = new MemoryStream();
ArgumentNullException.ThrowIfNull(conVar);
if (value == null) throw new ArgumentNullException(nameof(value));
if (!conVar.Type.IsInstanceOfType(value))
{
_debugService.Error($"Type mismatch for config {conVar.Name}. Expected {conVar.Type}, got {value.GetType()}.");
return;
}
try
{
using var st = new StreamWriter(stream);
var ser = JsonSerializer.Serialize(value);
st.Write(ser);
st.Flush();
stream.Seek(0, SeekOrigin.Begin);
_debugService.Log($"Saving config: {conVar.Name}");
var serializedData = JsonSerializer.Serialize(value);
using var stream = new MemoryStream();
using (var writer = new StreamWriter(stream))
{
writer.Write(serializedData);
writer.Flush();
stream.Seek(0, SeekOrigin.Begin);
}
_fileService.ConfigurationApi.Save(GetFileName(conVar), stream);
}
catch (Exception e)
{
_debugService.Error(e);
_debugService.Error($"Error saving config for {conVar.Name}: {e.Message}");
}
}
private string GetFileName<T>(ConVar<T> conVar)
private static string GetFileName<T>(ConVar<T> conVar)
{
return conVar.Name + ".json";
return $"{conVar.Name}.json";
}
}
public static class ConfigExt
public static class ConfigExtensions
{
public static bool TryGetConfigValue<T>(this ConfigurationService configurationService,ConVar<T> conVar, [NotNullWhen(true)] out T? value)
public static bool TryGetConfigValue<T>(this ConfigurationService configurationService, ConVar<T> conVar, [NotNullWhen(true)] out T? value)
{
ArgumentNullException.ThrowIfNull(configurationService);
ArgumentNullException.ThrowIfNull(conVar);
value = configurationService.GetConfigValue(conVar);
return value != null;
}

View File

@@ -8,6 +8,7 @@ using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Nebula.Launcher.Utils;
namespace Nebula.Launcher.Services;
@@ -151,17 +152,4 @@ public class RawResult
{
return result.Result;
}
}
public static class HttpExt
{
public static readonly JsonSerializerOptions JsonWebOptions = new(JsonSerializerDefaults.Web);
public static async Task<T> AsJson<T>(this HttpContent content) where T : notnull
{
var str = await content.ReadAsStringAsync();
return JsonSerializer.Deserialize<T>(str, JsonWebOptions) ??
throw new JsonException("AsJson: did not expect null response");
}
}

View File

@@ -112,7 +112,7 @@ public class RunnerService: IRedialApi
args.Add("--ss14-address");
args.Add(url.ToString());
_debugService.Debug("Connect to " + url.ToString());
_debugService.Debug("Connect to " + url.ToString() + " " + account.AuthLoginPassword.AuthServer);
await Run(args.ToArray(), buildInfo, this, cancelTokenSource.Token);
}