Generate secret keys locally
This commit is contained in:
2
MareAPI
2
MareAPI
Submodule MareAPI updated: 8b77956ec8...b2f4453b79
@@ -108,6 +108,7 @@ public sealed class Plugin : IDalamudPlugin
|
|||||||
collection.AddSingleton<XivDataAnalyzer>();
|
collection.AddSingleton<XivDataAnalyzer>();
|
||||||
collection.AddSingleton<CharacterAnalyzer>();
|
collection.AddSingleton<CharacterAnalyzer>();
|
||||||
collection.AddSingleton<TokenProvider>();
|
collection.AddSingleton<TokenProvider>();
|
||||||
|
collection.AddSingleton<AccountRegistrationService>();
|
||||||
collection.AddSingleton<PluginWarningNotificationService>();
|
collection.AddSingleton<PluginWarningNotificationService>();
|
||||||
collection.AddSingleton<FileCompactor>();
|
collection.AddSingleton<FileCompactor>();
|
||||||
collection.AddSingleton<TagHandler>();
|
collection.AddSingleton<TagHandler>();
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ public partial class IntroUi : WindowMediatorSubscriberBase
|
|||||||
private readonly CacheMonitor _cacheMonitor;
|
private readonly CacheMonitor _cacheMonitor;
|
||||||
private readonly ServerConfigurationManager _serverConfigurationManager;
|
private readonly ServerConfigurationManager _serverConfigurationManager;
|
||||||
private readonly DalamudUtilService _dalamudUtilService;
|
private readonly DalamudUtilService _dalamudUtilService;
|
||||||
|
private readonly AccountRegistrationService _registerService;
|
||||||
private readonly UiSharedService _uiShared;
|
private readonly UiSharedService _uiShared;
|
||||||
private bool _readFirstPage;
|
private bool _readFirstPage;
|
||||||
|
|
||||||
@@ -42,13 +43,14 @@ public partial class IntroUi : WindowMediatorSubscriberBase
|
|||||||
|
|
||||||
public IntroUi(ILogger<IntroUi> logger, UiSharedService uiShared, MareConfigService configService,
|
public IntroUi(ILogger<IntroUi> logger, UiSharedService uiShared, MareConfigService configService,
|
||||||
CacheMonitor fileCacheManager, ServerConfigurationManager serverConfigurationManager, MareMediator mareMediator,
|
CacheMonitor fileCacheManager, ServerConfigurationManager serverConfigurationManager, MareMediator mareMediator,
|
||||||
PerformanceCollectorService performanceCollectorService, DalamudUtilService dalamudUtilService) : base(logger, mareMediator, "Loporrit Setup", performanceCollectorService)
|
PerformanceCollectorService performanceCollectorService, DalamudUtilService dalamudUtilService, AccountRegistrationService registerService) : base(logger, mareMediator, "Loporrit Setup", performanceCollectorService)
|
||||||
{
|
{
|
||||||
_uiShared = uiShared;
|
_uiShared = uiShared;
|
||||||
_configService = configService;
|
_configService = configService;
|
||||||
_cacheMonitor = fileCacheManager;
|
_cacheMonitor = fileCacheManager;
|
||||||
_serverConfigurationManager = serverConfigurationManager;
|
_serverConfigurationManager = serverConfigurationManager;
|
||||||
_dalamudUtilService = dalamudUtilService;
|
_dalamudUtilService = dalamudUtilService;
|
||||||
|
_registerService = registerService;
|
||||||
IsOpen = false;
|
IsOpen = false;
|
||||||
ShowCloseButton = false;
|
ShowCloseButton = false;
|
||||||
RespectCloseHotkey = false;
|
RespectCloseHotkey = false;
|
||||||
@@ -261,16 +263,7 @@ This service is provided as-is.
|
|||||||
_ = Task.Run(async () => {
|
_ = Task.Run(async () => {
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using HttpClient httpClient = new();
|
var reply = await _registerService.RegisterAccount(CancellationToken.None).ConfigureAwait(false);
|
||||||
var ver = Assembly.GetExecutingAssembly().GetName().Version;
|
|
||||||
httpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("MareSynchronos", ver!.Major + "." + ver!.Minor + "." + ver!.Build));
|
|
||||||
var postUri = MareAuth.AuthRegisterFullPath(new Uri(_serverConfigurationManager.CurrentRealApiUrl
|
|
||||||
.Replace("wss://", "https://", StringComparison.OrdinalIgnoreCase)
|
|
||||||
.Replace("ws://", "http://", StringComparison.OrdinalIgnoreCase)));
|
|
||||||
_logger.LogInformation("Registering new account: {uri}", postUri.ToString());
|
|
||||||
var result = await httpClient.PostAsync(postUri, null).ConfigureAwait(false);
|
|
||||||
result.EnsureSuccessStatusCode();
|
|
||||||
var reply = await result.Content.ReadFromJsonAsync<RegisterReplyDto>().ConfigureAwait(false) ?? new();
|
|
||||||
if (!reply.Success)
|
if (!reply.Success)
|
||||||
{
|
{
|
||||||
_logger.LogWarning("Registration failed: {err}", reply.ErrorMessage);
|
_logger.LogWarning("Registration failed: {err}", reply.ErrorMessage);
|
||||||
|
|||||||
@@ -50,9 +50,10 @@ public class SettingsUi : WindowMediatorSubscriberBase
|
|||||||
private readonly ChatService _chatService;
|
private readonly ChatService _chatService;
|
||||||
private readonly GuiHookService _guiHookService;
|
private readonly GuiHookService _guiHookService;
|
||||||
private readonly PerformanceCollectorService _performanceCollector;
|
private readonly PerformanceCollectorService _performanceCollector;
|
||||||
private readonly ServerConfigurationManager _serverConfigurationManager;
|
|
||||||
private readonly PlayerPerformanceConfigService _playerPerformanceConfigService;
|
private readonly PlayerPerformanceConfigService _playerPerformanceConfigService;
|
||||||
private readonly PlayerPerformanceService _playerPerformanceService;
|
private readonly PlayerPerformanceService _playerPerformanceService;
|
||||||
|
private readonly AccountRegistrationService _registerService;
|
||||||
|
private readonly ServerConfigurationManager _serverConfigurationManager;
|
||||||
private readonly UiSharedService _uiShared;
|
private readonly UiSharedService _uiShared;
|
||||||
private bool _deleteAccountPopupModalShown = false;
|
private bool _deleteAccountPopupModalShown = false;
|
||||||
private string _lastTab = string.Empty;
|
private string _lastTab = string.Empty;
|
||||||
@@ -80,7 +81,7 @@ public class SettingsUi : WindowMediatorSubscriberBase
|
|||||||
FileCacheManager fileCacheManager,
|
FileCacheManager fileCacheManager,
|
||||||
FileCompactor fileCompactor, ApiController apiController,
|
FileCompactor fileCompactor, ApiController apiController,
|
||||||
IpcManager ipcManager, IpcProvider ipcProvider, CacheMonitor cacheMonitor,
|
IpcManager ipcManager, IpcProvider ipcProvider, CacheMonitor cacheMonitor,
|
||||||
DalamudUtilService dalamudUtilService) : base(logger, mediator, "Loporrit Settings", performanceCollector)
|
DalamudUtilService dalamudUtilService, AccountRegistrationService registerService) : base(logger, mediator, "Loporrit Settings", performanceCollector)
|
||||||
{
|
{
|
||||||
_configService = configService;
|
_configService = configService;
|
||||||
_pairManager = pairManager;
|
_pairManager = pairManager;
|
||||||
@@ -98,6 +99,7 @@ public class SettingsUi : WindowMediatorSubscriberBase
|
|||||||
_ipcProvider = ipcProvider;
|
_ipcProvider = ipcProvider;
|
||||||
_cacheMonitor = cacheMonitor;
|
_cacheMonitor = cacheMonitor;
|
||||||
_dalamudUtilService = dalamudUtilService;
|
_dalamudUtilService = dalamudUtilService;
|
||||||
|
_registerService = registerService;
|
||||||
_fileCompactor = fileCompactor;
|
_fileCompactor = fileCompactor;
|
||||||
_uiShared = uiShared;
|
_uiShared = uiShared;
|
||||||
AllowClickthrough = false;
|
AllowClickthrough = false;
|
||||||
@@ -1753,16 +1755,7 @@ public class SettingsUi : WindowMediatorSubscriberBase
|
|||||||
_ = Task.Run(async () => {
|
_ = Task.Run(async () => {
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using HttpClient httpClient = new();
|
var reply = await _registerService.RegisterAccount(CancellationToken.None).ConfigureAwait(false);
|
||||||
var ver = Assembly.GetExecutingAssembly().GetName().Version;
|
|
||||||
httpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("MareSynchronos", ver!.Major + "." + ver!.Minor + "." + ver!.Build));
|
|
||||||
var postUri = MareAuth.AuthRegisterFullPath(new Uri(selectedServer.ServerUri
|
|
||||||
.Replace("wss://", "https://", StringComparison.OrdinalIgnoreCase)
|
|
||||||
.Replace("ws://", "http://", StringComparison.OrdinalIgnoreCase)));
|
|
||||||
_logger.LogInformation("Registering new account: {uri}", postUri.ToString());
|
|
||||||
var result = await httpClient.PostAsync(postUri, null).ConfigureAwait(false);
|
|
||||||
result.EnsureSuccessStatusCode();
|
|
||||||
var reply = await result.Content.ReadFromJsonAsync<RegisterReplyDto>().ConfigureAwait(false) ?? new();
|
|
||||||
if (!reply.Success)
|
if (!reply.Success)
|
||||||
{
|
{
|
||||||
_logger.LogWarning("Registration failed: {err}", reply.ErrorMessage);
|
_logger.LogWarning("Registration failed: {err}", reply.ErrorMessage);
|
||||||
|
|||||||
84
MareSynchronos/WebAPI/AccountRegistrationService.cs
Normal file
84
MareSynchronos/WebAPI/AccountRegistrationService.cs
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
using MareSynchronos.API.Dto.Account;
|
||||||
|
using MareSynchronos.API.Routes;
|
||||||
|
using MareSynchronos.Services;
|
||||||
|
using MareSynchronos.Services.ServerConfiguration;
|
||||||
|
using MareSynchronos.Utils;
|
||||||
|
using MareSynchronos.WebAPI.SignalR;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using System.Net.Http.Headers;
|
||||||
|
using System.Net.Http.Json;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Security.Cryptography;
|
||||||
|
|
||||||
|
namespace MareSynchronos.WebAPI;
|
||||||
|
|
||||||
|
public sealed class AccountRegistrationService : IDisposable
|
||||||
|
{
|
||||||
|
private readonly HttpClient _httpClient;
|
||||||
|
private readonly ILogger<AccountRegistrationService> _logger;
|
||||||
|
private readonly ServerConfigurationManager _serverManager;
|
||||||
|
private readonly RemoteConfigurationService _remoteConfig;
|
||||||
|
|
||||||
|
private string GenerateSecretKey()
|
||||||
|
{
|
||||||
|
return Convert.ToHexString(SHA256.HashData(RandomNumberGenerator.GetBytes(64)));
|
||||||
|
}
|
||||||
|
|
||||||
|
public AccountRegistrationService(ILogger<AccountRegistrationService> logger, ServerConfigurationManager serverManager, RemoteConfigurationService remoteConfig)
|
||||||
|
{
|
||||||
|
_logger = logger;
|
||||||
|
_serverManager = serverManager;
|
||||||
|
_remoteConfig = remoteConfig;
|
||||||
|
_httpClient = new(
|
||||||
|
new HttpClientHandler
|
||||||
|
{
|
||||||
|
AllowAutoRedirect = true,
|
||||||
|
MaxAutomaticRedirections = 5
|
||||||
|
}
|
||||||
|
);
|
||||||
|
var ver = Assembly.GetExecutingAssembly().GetName().Version;
|
||||||
|
_httpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("MareSynchronos", ver!.Major + "." + ver!.Minor + "." + ver!.Build));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
_httpClient.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<RegisterReplyDto> RegisterAccount(CancellationToken token)
|
||||||
|
{
|
||||||
|
var authApiUrl = _serverManager.CurrentApiUrl;
|
||||||
|
|
||||||
|
// Override the API URL used for auth from remote config, if one is available
|
||||||
|
if (authApiUrl.Equals(ApiController.LoporritServiceUri, StringComparison.Ordinal))
|
||||||
|
{
|
||||||
|
var config = await _remoteConfig.GetConfigAsync<HubConnectionConfig>("mainServer").ConfigureAwait(false) ?? new();
|
||||||
|
if (!string.IsNullOrEmpty(config.ApiUrl))
|
||||||
|
authApiUrl = config.ApiUrl;
|
||||||
|
else
|
||||||
|
authApiUrl = ApiController.LoporritServiceApiUri;
|
||||||
|
}
|
||||||
|
|
||||||
|
var secretKey = GenerateSecretKey();
|
||||||
|
var hashedSecretKey = secretKey.GetHash256();
|
||||||
|
|
||||||
|
Uri postUri = MareAuth.AuthRegisterV2FullPath(new Uri(authApiUrl
|
||||||
|
.Replace("wss://", "https://", StringComparison.OrdinalIgnoreCase)
|
||||||
|
.Replace("ws://", "http://", StringComparison.OrdinalIgnoreCase)));
|
||||||
|
|
||||||
|
var result = await _httpClient.PostAsync(postUri, new FormUrlEncodedContent([
|
||||||
|
new("hashedSecretKey", hashedSecretKey)
|
||||||
|
]), token).ConfigureAwait(false);
|
||||||
|
result.EnsureSuccessStatusCode();
|
||||||
|
|
||||||
|
var response = await result.Content.ReadFromJsonAsync<RegisterReplyV2Dto>(token).ConfigureAwait(false) ?? new();
|
||||||
|
|
||||||
|
return new RegisterReplyDto()
|
||||||
|
{
|
||||||
|
Success = response.Success,
|
||||||
|
ErrorMessage = response.ErrorMessage,
|
||||||
|
UID = response.UID,
|
||||||
|
SecretKey = secretKey
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user