- add: Auth service
This commit is contained in:
65
Nebula.Launcher/Services/AuthService.cs
Normal file
65
Nebula.Launcher/Services/AuthService.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
using System;
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Input;
|
||||
using Nebula.Launcher.Models.Auth;
|
||||
|
||||
namespace Nebula.Launcher.Services;
|
||||
|
||||
[ServiceRegister]
|
||||
public class AuthService
|
||||
{
|
||||
private readonly HttpClient _httpClient = new();
|
||||
private readonly RestService _restService;
|
||||
private readonly DebugService _debugService;
|
||||
|
||||
public CurrentAuthInfo? SelectedAuth;
|
||||
|
||||
public AuthService(RestService restService, DebugService debugService)
|
||||
{
|
||||
_restService = restService;
|
||||
_debugService = debugService;
|
||||
}
|
||||
|
||||
public async Task<bool> Auth(AuthLoginPassword authLoginPassword)
|
||||
{
|
||||
var authServer = authLoginPassword.AuthServer;
|
||||
var login = authLoginPassword.Login;
|
||||
var password = authLoginPassword.Password;
|
||||
|
||||
_debugService.Debug($"Auth to {authServer}/authenticate {login}");
|
||||
|
||||
var authUrl = new Uri($"{authServer}/authenticate");
|
||||
|
||||
var result =
|
||||
await _restService.PostAsync<AuthenticateResponse, AuthenticateRequest>(
|
||||
new AuthenticateRequest(login, password), authUrl, CancellationToken.None);
|
||||
_debugService.Debug("RESULT " + result.Value);
|
||||
if (result.Value is null) return false;
|
||||
|
||||
SelectedAuth = new CurrentAuthInfo(result.Value.UserId, result.Value.Username,
|
||||
new LoginToken(result.Value.Token, result.Value.ExpireTime), authServer);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<bool> EnsureToken()
|
||||
{
|
||||
if (SelectedAuth is null) return false;
|
||||
|
||||
var authUrl = new Uri($"{SelectedAuth.AuthServer}/ping");
|
||||
|
||||
using var requestMessage = new HttpRequestMessage(HttpMethod.Get, authUrl);
|
||||
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("SS14Auth", SelectedAuth.Token.Token);
|
||||
using var resp = await _httpClient.SendAsync(requestMessage);
|
||||
|
||||
if (!resp.IsSuccessStatusCode) SelectedAuth = null;
|
||||
|
||||
return resp.IsSuccessStatusCode;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed record CurrentAuthInfo(Guid UserId, string Username, LoginToken Token, string AuthServer);
|
||||
public record AuthLoginPassword(string Login, string Password, string AuthServer);
|
||||
@@ -54,7 +54,7 @@ public class ConfigurationService
|
||||
return value != null;
|
||||
}
|
||||
|
||||
public void SetValue(ConVar conVar, object value)
|
||||
public void SetConfigValue(ConVar conVar, object value)
|
||||
{
|
||||
if(conVar.Type != value.GetType())
|
||||
return;
|
||||
|
||||
@@ -5,6 +5,7 @@ using System.IO.Compression;
|
||||
using System.Runtime.InteropServices;
|
||||
using Nebula.Launcher.FileApis;
|
||||
using Nebula.Launcher.FileApis.Interfaces;
|
||||
using Nebula.Launcher.Models;
|
||||
using Nebula.Launcher.Utils;
|
||||
using Robust.LoaderApi;
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ public class HubService
|
||||
|
||||
public readonly ObservableCollection<string> HubList = new();
|
||||
|
||||
private readonly Dictionary<string, List<ServerInfo>> _servers = new();
|
||||
private readonly Dictionary<string, List<ServerHubInfo>> _servers = new();
|
||||
|
||||
|
||||
public HubService(ConfigurationService configurationService, RestService restService)
|
||||
@@ -37,7 +37,7 @@ public class HubService
|
||||
foreach (var hubUri in e.NewItems)
|
||||
{
|
||||
var urlStr = (string)hubUri;
|
||||
var servers = await _restService.GetAsyncDefault<List<ServerInfo>>(new Uri(urlStr), [], CancellationToken.None);
|
||||
var servers = await _restService.GetAsyncDefault<List<ServerHubInfo>>(new Uri(urlStr), [], CancellationToken.None);
|
||||
_servers[urlStr] = servers;
|
||||
HubServerChangedEventArgs?.Invoke(new HubServerChangedEventArgs(servers, HubServerChangeAction.Add));
|
||||
}
|
||||
@@ -61,9 +61,9 @@ public class HubService
|
||||
public class HubServerChangedEventArgs : EventArgs
|
||||
{
|
||||
public HubServerChangeAction Action;
|
||||
public List<ServerInfo> Items;
|
||||
public List<ServerHubInfo> Items;
|
||||
|
||||
public HubServerChangedEventArgs(List<ServerInfo> items, HubServerChangeAction action)
|
||||
public HubServerChangedEventArgs(List<ServerHubInfo> items, HubServerChangeAction action)
|
||||
{
|
||||
Items = items;
|
||||
Action = action;
|
||||
|
||||
Reference in New Issue
Block a user