- fix: auth logic part 1
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Nebula.Shared.Configurations.Migrations;
|
||||
using Nebula.Shared.Models;
|
||||
using Nebula.Shared.Services;
|
||||
|
||||
|
||||
70
Nebula.Shared/Configurations/ComplexConVarBinder.cs
Normal file
70
Nebula.Shared/Configurations/ComplexConVarBinder.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
using System.ComponentModel;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Nebula.Shared.Configurations;
|
||||
|
||||
public abstract class ComplexConVarBinder<T> : INotifyPropertyChanged, INotifyPropertyChanging
|
||||
{
|
||||
private readonly ConVarObserver<T> _baseConVar;
|
||||
private readonly Lock _lock = new();
|
||||
private readonly SemaphoreSlim _valueChangeSemaphore = new(1, 1);
|
||||
|
||||
public T? Value
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
return _baseConVar.Value;
|
||||
}
|
||||
}
|
||||
set
|
||||
{
|
||||
_ = SetValueAsync(value);
|
||||
}
|
||||
}
|
||||
|
||||
protected ComplexConVarBinder(ConVarObserver<T> baseConVar)
|
||||
{
|
||||
_baseConVar = baseConVar ?? throw new ArgumentNullException(nameof(baseConVar));
|
||||
_baseConVar.PropertyChanged += BaseConVarOnPropertyChanged;
|
||||
_baseConVar.PropertyChanging += BaseConVarOnPropertyChanging;
|
||||
}
|
||||
|
||||
|
||||
private async Task SetValueAsync(T? value)
|
||||
{
|
||||
await _valueChangeSemaphore.WaitAsync().ConfigureAwait(false);
|
||||
|
||||
try
|
||||
{
|
||||
var newValue = await OnValueChange(value).ConfigureAwait(false);
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
_baseConVar.Value = newValue;
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
_valueChangeSemaphore.Release();
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract Task<T?> OnValueChange(T? newValue);
|
||||
|
||||
private void BaseConVarOnPropertyChanged(object? sender, PropertyChangedEventArgs e)
|
||||
{
|
||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Value)));
|
||||
}
|
||||
|
||||
private void BaseConVarOnPropertyChanging(object? sender, PropertyChangingEventArgs e)
|
||||
{
|
||||
PropertyChanging?.Invoke(this, new PropertyChangingEventArgs(nameof(Value)));
|
||||
}
|
||||
|
||||
|
||||
public event PropertyChangedEventHandler? PropertyChanged;
|
||||
|
||||
public event PropertyChangingEventHandler? PropertyChanging;
|
||||
}
|
||||
18
Nebula.Shared/Configurations/ConVar.cs
Normal file
18
Nebula.Shared/Configurations/ConVar.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using Nebula.Shared.Services;
|
||||
|
||||
namespace Nebula.Shared.Configurations;
|
||||
|
||||
public class ConVar<T>
|
||||
{
|
||||
internal ConfigurationService.OnConfigurationChangedDelegate<T?>? OnValueChanged;
|
||||
|
||||
public ConVar(string name, T? defaultValue = default)
|
||||
{
|
||||
Name = name ?? throw new ArgumentNullException(nameof(name));
|
||||
DefaultValue = defaultValue;
|
||||
}
|
||||
|
||||
public string Name { get; }
|
||||
public Type Type => typeof(T);
|
||||
public T? DefaultValue { get; }
|
||||
}
|
||||
25
Nebula.Shared/Configurations/ConVarBuilder.cs
Normal file
25
Nebula.Shared/Configurations/ConVarBuilder.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using Nebula.Shared.Configurations.Migrations;
|
||||
using Nebula.Shared.Services;
|
||||
|
||||
namespace Nebula.Shared.Configurations;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public static ConVar<T> BuildWithMigration<T>(string name, IConfigurationMigration migration, T? defaultValue = default)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(name))
|
||||
throw new ArgumentException("ConVar name cannot be null or whitespace.", nameof(name));
|
||||
|
||||
ConfigurationService.AddConfigurationMigration(migration);
|
||||
|
||||
return new ConVar<T>(name, defaultValue);
|
||||
}
|
||||
}
|
||||
68
Nebula.Shared/Configurations/ConVarObserver.cs
Normal file
68
Nebula.Shared/Configurations/ConVarObserver.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
using System.ComponentModel;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Nebula.Shared.Services;
|
||||
|
||||
namespace Nebula.Shared.Configurations;
|
||||
|
||||
public sealed class ConVarObserver<T> : IDisposable, INotifyPropertyChanged, INotifyPropertyChanging
|
||||
{
|
||||
private readonly ConVar<T> _convar;
|
||||
private readonly ConfigurationService _configurationService;
|
||||
|
||||
private T? _value;
|
||||
private ConfigurationService.OnConfigurationChangedDelegate<T> _delegate;
|
||||
|
||||
public T? Value
|
||||
{
|
||||
get => _value;
|
||||
set => _configurationService.SetConfigValue(_convar, value);
|
||||
}
|
||||
|
||||
public ConVarObserver(ConVar<T> convar, ConfigurationService configurationService)
|
||||
{
|
||||
_convar = convar;
|
||||
_convar.OnValueChanged += OnValueChanged;
|
||||
_configurationService = configurationService;
|
||||
_delegate += OnValueChanged;
|
||||
|
||||
OnValueChanged(configurationService.GetConfigValue(_convar));
|
||||
}
|
||||
|
||||
private void OnValueChanged(T? value)
|
||||
{
|
||||
OnPropertyChanging(nameof(Value));
|
||||
|
||||
if(value is null && _value is null)
|
||||
return;
|
||||
if (_value is not null && _value.Equals(value))
|
||||
return;
|
||||
|
||||
_value = value;
|
||||
OnPropertyChanged(nameof(Value));
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_convar.OnValueChanged -= OnValueChanged;
|
||||
}
|
||||
|
||||
public bool HasValue()
|
||||
{
|
||||
return Value != null;
|
||||
}
|
||||
|
||||
public static implicit operator T? (ConVarObserver<T> convar) => convar.Value;
|
||||
|
||||
public event PropertyChangingEventHandler? PropertyChanging;
|
||||
public event PropertyChangedEventHandler? PropertyChanged;
|
||||
|
||||
private void OnPropertyChanged([CallerMemberName] string? propertyName = null)
|
||||
{
|
||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
||||
}
|
||||
private void OnPropertyChanging([CallerMemberName] string? propertyName = null)
|
||||
{
|
||||
PropertyChanging?.Invoke(this, new PropertyChangingEventArgs(propertyName));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using Nebula.Shared.Models;
|
||||
using Nebula.Shared.Services;
|
||||
|
||||
namespace Nebula.Shared.Configurations.Migrations;
|
||||
|
||||
public abstract class BaseConfigurationMigration<T1,T2> : IConfigurationMigration
|
||||
{
|
||||
protected ConVar<T1> OldConVar;
|
||||
protected ConVar<T2> NewConVar;
|
||||
|
||||
public BaseConfigurationMigration(string oldName, string newName)
|
||||
{
|
||||
OldConVar = ConVarBuilder.Build<T1>(oldName);
|
||||
NewConVar = ConVarBuilder.Build<T2>(newName);
|
||||
}
|
||||
|
||||
public async Task DoMigrate(ConfigurationService configurationService, IServiceProvider serviceProvider, ILoadingHandler loadingHandler)
|
||||
{
|
||||
var oldValue = configurationService.GetConfigValue(OldConVar);
|
||||
if(oldValue == null) return;
|
||||
|
||||
var newValue = await Migrate(serviceProvider, oldValue, loadingHandler);
|
||||
configurationService.SetConfigValue(NewConVar, newValue);
|
||||
configurationService.ClearConfigValue(OldConVar);
|
||||
}
|
||||
|
||||
protected abstract Task<T2> Migrate(IServiceProvider serviceProvider, T1 oldValue, ILoadingHandler loadingHandler);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
using Nebula.Shared.Models;
|
||||
using Nebula.Shared.Services;
|
||||
|
||||
namespace Nebula.Shared.Configurations.Migrations;
|
||||
|
||||
public interface IConfigurationMigration
|
||||
{
|
||||
public Task DoMigrate(ConfigurationService configurationService, IServiceProvider serviceProvider, ILoadingHandler loadingHandler);
|
||||
}
|
||||
15
Nebula.Shared/Configurations/Migrations/MigrationQueue.cs
Normal file
15
Nebula.Shared/Configurations/Migrations/MigrationQueue.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using Nebula.Shared.Models;
|
||||
using Nebula.Shared.Services;
|
||||
|
||||
namespace Nebula.Shared.Configurations.Migrations;
|
||||
|
||||
public class MigrationQueue(List<IConfigurationMigration> migrations) : IConfigurationMigration
|
||||
{
|
||||
public async Task DoMigrate(ConfigurationService configurationService, IServiceProvider serviceProvider , ILoadingHandler loadingHandler)
|
||||
{
|
||||
foreach (var migration in migrations)
|
||||
{
|
||||
await migration.DoMigrate(configurationService, serviceProvider, loadingHandler);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
namespace Nebula.Shared.Configurations.Migrations;
|
||||
|
||||
public class MigrationQueueBuilder
|
||||
{
|
||||
public static MigrationQueueBuilder Instance => new();
|
||||
|
||||
private readonly List<IConfigurationMigration> _migrations = [];
|
||||
|
||||
public MigrationQueueBuilder With(IConfigurationMigration migration)
|
||||
{
|
||||
_migrations.Add(migration);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MigrationQueue Build()
|
||||
{
|
||||
return new MigrationQueue(_migrations);
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
using Nebula.Shared.Configurations;
|
||||
using Nebula.Shared.Models;
|
||||
using Nebula.Shared.Services;
|
||||
|
||||
|
||||
@@ -50,6 +50,21 @@ public class AuthService(
|
||||
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("SS14Auth", tokenCredentials.Token.Token);
|
||||
using var resp = await _httpClient.SendAsync(requestMessage, cancellationService.Token);
|
||||
}
|
||||
|
||||
public async Task Logout(AuthTokenCredentials tokenCredentials)
|
||||
{
|
||||
var authUrl = new Uri($"{tokenCredentials.AuthServer}api/auth/logout");
|
||||
await restService.PostAsync<NullResponse, TokenRequest>(TokenRequest.From(tokenCredentials), authUrl, cancellationService.Token);
|
||||
}
|
||||
|
||||
public async Task<AuthTokenCredentials> Refresh(AuthTokenCredentials tokenCredentials)
|
||||
{
|
||||
var authUrl = new Uri($"{tokenCredentials.AuthServer}api/auth/refresh");
|
||||
var newToken = await restService.PostAsync<LoginToken, TokenRequest>(
|
||||
TokenRequest.From(tokenCredentials), authUrl, cancellationService.Token);
|
||||
|
||||
return tokenCredentials with { Token = newToken };
|
||||
}
|
||||
}
|
||||
|
||||
public sealed record AuthTokenCredentials(Guid UserId, LoginToken Token, string Login, string AuthServer);
|
||||
@@ -71,3 +86,14 @@ public enum AuthenticateDenyCode
|
||||
TfaInvalid = 4,
|
||||
AccountLocked = 5,
|
||||
}
|
||||
|
||||
public sealed record TokenRequest(string Token)
|
||||
{
|
||||
public static TokenRequest From(AuthTokenCredentials authTokenCredentials)
|
||||
{
|
||||
return new TokenRequest(authTokenCredentials.Token.Token);
|
||||
}
|
||||
|
||||
public static TokenRequest Empty { get; } = new TokenRequest("");
|
||||
|
||||
}
|
||||
@@ -1,107 +1,13 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Text.Json;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Nebula.Shared.Configurations;
|
||||
using Nebula.Shared.Configurations.Migrations;
|
||||
using Nebula.Shared.FileApis.Interfaces;
|
||||
using Nebula.Shared.Models;
|
||||
using Nebula.Shared.Services.Logging;
|
||||
using Robust.LoaderApi;
|
||||
|
||||
namespace Nebula.Shared.Services;
|
||||
|
||||
public class ConVar<T>
|
||||
{
|
||||
internal ConfigurationService.OnConfigurationChangedDelegate<T?>? OnValueChanged;
|
||||
|
||||
public ConVar(string name, T? defaultValue = default)
|
||||
{
|
||||
Name = name ?? throw new ArgumentNullException(nameof(name));
|
||||
DefaultValue = defaultValue;
|
||||
}
|
||||
|
||||
public string Name { get; }
|
||||
public Type Type => typeof(T);
|
||||
public T? DefaultValue { get; }
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public static ConVar<T> BuildWithMigration<T>(string name, IConfigurationMigration migration, T? defaultValue = default)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(name))
|
||||
throw new ArgumentException("ConVar name cannot be null or whitespace.", nameof(name));
|
||||
|
||||
ConfigurationService.AddConfigurationMigration(migration);
|
||||
|
||||
return new ConVar<T>(name, defaultValue);
|
||||
}
|
||||
}
|
||||
|
||||
public interface IConfigurationMigration
|
||||
{
|
||||
public Task DoMigrate(ConfigurationService configurationService, IServiceProvider serviceProvider, ILoadingHandler loadingHandler);
|
||||
}
|
||||
|
||||
public abstract class BaseConfigurationMigration<T1,T2> : IConfigurationMigration
|
||||
{
|
||||
protected ConVar<T1> OldConVar;
|
||||
protected ConVar<T2> NewConVar;
|
||||
|
||||
public BaseConfigurationMigration(string oldName, string newName)
|
||||
{
|
||||
OldConVar = ConVarBuilder.Build<T1>(oldName);
|
||||
NewConVar = ConVarBuilder.Build<T2>(newName);
|
||||
}
|
||||
|
||||
public async Task DoMigrate(ConfigurationService configurationService, IServiceProvider serviceProvider, ILoadingHandler loadingHandler)
|
||||
{
|
||||
var oldValue = configurationService.GetConfigValue(OldConVar);
|
||||
if(oldValue == null) return;
|
||||
|
||||
var newValue = await Migrate(serviceProvider, oldValue, loadingHandler);
|
||||
configurationService.SetConfigValue(NewConVar, newValue);
|
||||
configurationService.ClearConfigValue(OldConVar);
|
||||
}
|
||||
|
||||
protected abstract Task<T2> Migrate(IServiceProvider serviceProvider, T1 oldValue, ILoadingHandler loadingHandler);
|
||||
}
|
||||
|
||||
public class MigrationQueue(List<IConfigurationMigration> migrations) : IConfigurationMigration
|
||||
{
|
||||
public async Task DoMigrate(ConfigurationService configurationService, IServiceProvider serviceProvider , ILoadingHandler loadingHandler)
|
||||
{
|
||||
foreach (var migration in migrations)
|
||||
{
|
||||
await migration.DoMigrate(configurationService, serviceProvider, loadingHandler);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class MigrationQueueBuilder
|
||||
{
|
||||
public static MigrationQueueBuilder Instance => new();
|
||||
|
||||
private readonly List<IConfigurationMigration> _migrations = [];
|
||||
|
||||
public MigrationQueueBuilder With(IConfigurationMigration migration)
|
||||
{
|
||||
_migrations.Add(migration);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MigrationQueue Build()
|
||||
{
|
||||
return new MigrationQueue(_migrations);
|
||||
}
|
||||
}
|
||||
|
||||
[ServiceRegister]
|
||||
public class ConfigurationService
|
||||
{
|
||||
@@ -142,15 +48,22 @@ public class ConfigurationService
|
||||
});
|
||||
}
|
||||
|
||||
public ConfigChangeSubscriberDisposable<T> SubscribeVarChanged<T>(ConVar<T> convar, OnConfigurationChangedDelegate<T?> @delegate, bool invokeNow = false)
|
||||
public ConVarObserver<T> SubscribeVarChanged<T>(ConVar<T> convar, OnConfigurationChangedDelegate<T?> @delegate, bool invokeNow = false)
|
||||
{
|
||||
convar.OnValueChanged += @delegate;
|
||||
if (invokeNow)
|
||||
{
|
||||
@delegate(GetConfigValue(convar));
|
||||
}
|
||||
|
||||
return new ConfigChangeSubscriberDisposable<T>(convar, @delegate);
|
||||
|
||||
var delegation = SubscribeVarChanged<T>(convar);
|
||||
delegation.PropertyChanged += (_, _) => @delegate(delegation.Value);
|
||||
return delegation;
|
||||
}
|
||||
|
||||
public ConVarObserver<T> SubscribeVarChanged<T>(ConVar<T> convar)
|
||||
{
|
||||
return new ConVarObserver<T>(convar, this);
|
||||
}
|
||||
|
||||
public T? GetConfigValue<T>(ConVar<T> conVar)
|
||||
@@ -252,20 +165,4 @@ public class ConfigurationService
|
||||
{
|
||||
return $"{conVar.Name}.json";
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ConfigChangeSubscriberDisposable<T> : IDisposable
|
||||
{
|
||||
private readonly ConVar<T> _convar;
|
||||
private readonly ConfigurationService.OnConfigurationChangedDelegate<T> _delegate;
|
||||
|
||||
public ConfigChangeSubscriberDisposable(ConVar<T> convar, ConfigurationService.OnConfigurationChangedDelegate<T> @delegate)
|
||||
{
|
||||
_convar = convar;
|
||||
_delegate = @delegate;
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
_convar.OnValueChanged -= _delegate;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Nebula.Shared.Configurations;
|
||||
using Nebula.Shared.FileApis;
|
||||
using Nebula.Shared.FileApis.Interfaces;
|
||||
using Nebula.Shared.Models;
|
||||
|
||||
@@ -46,8 +46,7 @@ public class RestService
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
[Pure]
|
||||
|
||||
public async Task<K> PostAsync<K, T>(T information, Uri uri, CancellationToken cancellationToken) where K : notnull
|
||||
{
|
||||
var json = JsonSerializer.Serialize(information, _serializerOptions);
|
||||
@@ -57,11 +56,11 @@ public class RestService
|
||||
}
|
||||
|
||||
[Pure]
|
||||
public async Task<T> PostAsync<T>(Stream stream, Uri uri, CancellationToken cancellationToken) where T : notnull
|
||||
public async Task<T> PostAsync<T>(Stream stream, string fileName, Uri uri, CancellationToken cancellationToken) where T : notnull
|
||||
{
|
||||
using var multipartFormContent =
|
||||
new MultipartFormDataContent("Upload----" + DateTime.Now.ToString(CultureInfo.InvariantCulture));
|
||||
multipartFormContent.Add(new StreamContent(stream), "formFile", "image.png");
|
||||
multipartFormContent.Add(new StreamContent(stream), "formFile", fileName);
|
||||
var response = await _client.PostAsync(uri, multipartFormContent, cancellationToken);
|
||||
return await ReadResult<T>(response, cancellationToken, uri);
|
||||
}
|
||||
@@ -76,9 +75,12 @@ public class RestService
|
||||
[Pure]
|
||||
private async Task<T> ReadResult<T>(HttpResponseMessage response, CancellationToken cancellationToken, Uri uri) where T : notnull
|
||||
{
|
||||
var content = await response.Content.ReadAsStringAsync(cancellationToken);
|
||||
if (typeof(T) == typeof(NullResponse) && new NullResponse() is T nullResponse)
|
||||
{
|
||||
return nullResponse;
|
||||
}
|
||||
|
||||
if (typeof(T) == typeof(string) && content is T t)
|
||||
if (typeof(T) == typeof(string) && await response.Content.ReadAsStringAsync(cancellationToken) is T t)
|
||||
return t;
|
||||
|
||||
if (response.IsSuccessStatusCode)
|
||||
@@ -90,6 +92,10 @@ public class RestService
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class NullResponse
|
||||
{
|
||||
}
|
||||
|
||||
public sealed class RestRequestException(HttpContent content, HttpStatusCode statusCode, string message) : Exception(message)
|
||||
{
|
||||
public HttpStatusCode StatusCode { get; } = statusCode;
|
||||
|
||||
Reference in New Issue
Block a user