Skip to content

Commit

Permalink
Token logic for auth
Browse files Browse the repository at this point in the history
+ solve #26
  • Loading branch information
LucHeart committed May 9, 2024
1 parent f8a6df3 commit 1dbc7a1
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 15 deletions.
4 changes: 2 additions & 2 deletions ShockOsc/Platforms/Windows/WindowsEntryPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ private static void Start(CliOptions config)

if (PipeHelper.EnumeratePipes().Any(x => x.Equals(pipeName, StringComparison.InvariantCultureIgnoreCase)))
{
// TODO: Refactor this
if (!string.IsNullOrEmpty(config.Uri))
{
using var pipeClientStream = new NamedPipeClientStream(".", "OpenShock.ShockOsc", PipeDirection.Out);
Expand All @@ -72,7 +73,7 @@ private static void Start(CliOptions config)
writer.WriteLine(JsonSerializer.Serialize(new PipeMessage
{
Type = PipeMessageType.Token,
Data = parsedUri.Arguments
Data = string.Join('/', parsedUri.Arguments)
}));
}

Expand All @@ -84,7 +85,6 @@ private static void Start(CliOptions config)
return;
}


if (config.Headless)
{
Console.WriteLine("Running in headless mode.");
Expand Down
13 changes: 11 additions & 2 deletions ShockOsc/Services/Pipes/PipeServerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public PipeServerService(ILogger<PipeServerService> logger)
_logger = logger;
}

public ConcurrentQueue<PipeMessage> MessageQueue { get; } = new();
public string? Token { get; set; }
public event Func<Task>? OnMessageReceived;

public void StartServer()
Expand Down Expand Up @@ -61,7 +61,16 @@ private async Task ServerLoop()
continue;
}

MessageQueue.Enqueue(jsonObj);
switch (jsonObj.Type)
{
case PipeMessageType.Token:
Token = jsonObj.Data?.ToString();
break;
}
{

}

await OnMessageReceived.Raise();
_logger.LogInformation("[{Id}], Received pipe message of type: {Type}", id, jsonObj.Type);
}
Expand Down
96 changes: 88 additions & 8 deletions ShockOsc/Services/UnderscoreConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,41 @@ private void HandleGlobalConfigCommand(string action, object? value)
{
switch (action)
{
case "ModeIntensity":
if (value is bool modeIntensity)
{
_configManager.Config.Behaviour.RandomIntensity = modeIntensity;
_configManager.SaveFnf();
OnConfigUpdate?.Invoke(); // update Ui
}
break;

case "ModeDuration":
if (value is bool modeDuration)
{
_configManager.Config.Behaviour.RandomDuration = modeDuration;
_configManager.SaveFnf();
OnConfigUpdate?.Invoke(); // update Ui
}
break;

case "Intensity":
// 0..10sec
if (value is float intensityFloat)
{
var currentIntensity =
MathUtils.ClampFloat(_configManager.Config.Behaviour.FixedIntensity / 100f);
if (Math.Abs(intensityFloat - currentIntensity) < 0.001) return;

_configManager.Config.Behaviour.FixedIntensity =
Math.Clamp((byte)Math.Round(intensityFloat * 100), (byte)0, (byte)100);
ValidateSettings();
_configManager.SaveFnf();
OnConfigUpdate?.Invoke(); // update Ui
}

break;

case "MinIntensity":
// 0..100%
if (value is float minIntensityFloat)
Expand All @@ -91,7 +126,7 @@ private void HandleGlobalConfigCommand(string action, object? value)
_configManager.Config.Behaviour.IntensityRange.Min =
MathUtils.ClampUint((uint)Math.Round(minIntensityFloat * 100), 0, 100);
ValidateSettings();
_configManager.Save();
_configManager.SaveFnf();
OnConfigUpdate?.Invoke(); // update Ui
}

Expand All @@ -108,7 +143,39 @@ private void HandleGlobalConfigCommand(string action, object? value)
_configManager.Config.Behaviour.IntensityRange.Max =
MathUtils.ClampUint((uint)Math.Round(maxIntensityFloat * 100), 0, 100);
ValidateSettings();
_configManager.Save();
_configManager.SaveFnf();
OnConfigUpdate?.Invoke(); // update Ui
}

break;

case "MinDuration":
// 0..10sec
if (value is float minDurationFloat)
{
var currentMinDuration = _configManager.Config.Behaviour.DurationRange.Min / 10_000f;
if (Math.Abs(minDurationFloat - currentMinDuration) < 0.001) return;

_configManager.Config.Behaviour.DurationRange.Min =
MathUtils.ClampUint((uint)Math.Round(minDurationFloat * 100), 300, 30_000);
ValidateSettings();
_configManager.SaveFnf();
OnConfigUpdate?.Invoke(); // update Ui
}

break;

case "MaxDuration":
// 0..10sec
if (value is float maxDurationFloat)
{
var currentMaxDuration = _configManager.Config.Behaviour.DurationRange.Max / 10_000f;
if (Math.Abs(maxDurationFloat - currentMaxDuration) < 0.001) return;

_configManager.Config.Behaviour.DurationRange.Max =
MathUtils.ClampUint((uint)Math.Round(maxDurationFloat * 10_000), 300, 30_000);
ValidateSettings();
_configManager.SaveFnf();
OnConfigUpdate?.Invoke(); // update Ui
}

Expand All @@ -118,14 +185,13 @@ private void HandleGlobalConfigCommand(string action, object? value)
// 0..10sec
if (value is float durationFloat)
{
var currentDuration =
MathUtils.ClampFloat(_configManager.Config.Behaviour.FixedDuration / 10000f);
var currentDuration = _configManager.Config.Behaviour.FixedDuration / 10000f;
if (Math.Abs(durationFloat - currentDuration) < 0.001) return;

_configManager.Config.Behaviour.FixedDuration =
MathUtils.ClampUint((uint)Math.Round(durationFloat * 10000), 0, 10000);
MathUtils.ClampUint((uint)Math.Round(durationFloat * 10_000), 300, 10_000);
ValidateSettings();
_configManager.Save();
_configManager.SaveFnf();
OnConfigUpdate?.Invoke(); // update Ui
}

Expand All @@ -142,7 +208,7 @@ private void HandleGlobalConfigCommand(string action, object? value)
_configManager.Config.Behaviour.CooldownTime =
MathUtils.ClampUint((uint)Math.Round(cooldownTimeFloat * 100000), 0, 100000);
ValidateSettings();
_configManager.Save();
_configManager.SaveFnf();
OnConfigUpdate?.Invoke(); // update Ui
}

Expand All @@ -158,7 +224,7 @@ private void HandleGlobalConfigCommand(string action, object? value)
_configManager.Config.Behaviour.HoldTime =
MathUtils.ClampUint((uint)Math.Round(holdTimeFloat * 1000), 0, 1000);
ValidateSettings();
_configManager.Save();
_configManager.SaveFnf();
OnConfigUpdate?.Invoke(); // update Ui
}

Expand Down Expand Up @@ -188,6 +254,10 @@ private void ValidateSettings()
var intensityRange = _configManager.Config.Behaviour.IntensityRange;
if (intensityRange.Min > intensityRange.Max) intensityRange.Max = intensityRange.Min;
if (intensityRange.Max < intensityRange.Min) intensityRange.Min = intensityRange.Max;

var durationRange = _configManager.Config.Behaviour.DurationRange;
if (durationRange.Min > durationRange.Max) durationRange.Max = durationRange.Min;
if (durationRange.Max < durationRange.Min) durationRange.Min = durationRange.Max;
}

public async Task SendUpdateForAll()
Expand All @@ -204,5 +274,15 @@ await _oscClient.SendGameMessage("/avatar/parameters/ShockOsc/_Config/_All/Coold
MathUtils.ClampFloat(_configManager.Config.Behaviour.CooldownTime / 100000f));
await _oscClient.SendGameMessage("/avatar/parameters/ShockOsc/_Config/_All/HoldTime",
MathUtils.ClampFloat(_configManager.Config.Behaviour.HoldTime / 1000f));
await _oscClient.SendGameMessage("/avatar/parameters/ShockOsc/_Config/_All/ModeIntensity",
_configManager.Config.Behaviour.RandomIntensity);
await _oscClient.SendGameMessage("/avatar/parameters/ShockOsc/_Config/_All/ModeDuration",
_configManager.Config.Behaviour.RandomDuration);
await _oscClient.SendGameMessage("/avatar/parameters/ShockOsc/_Config/_All/Intensity",
MathUtils.ClampFloat(_configManager.Config.Behaviour.FixedIntensity / 100f));
await _oscClient.SendGameMessage("/avatar/parameters/ShockOsc/_Config/_All/MinDuration",
MathUtils.ClampFloat(_configManager.Config.Behaviour.DurationRange.Min / 10_000f));
await _oscClient.SendGameMessage("/avatar/parameters/ShockOsc/_Config/_All/MaxDuration",
MathUtils.ClampFloat(_configManager.Config.Behaviour.DurationRange.Max / 10_000f));
}
}
19 changes: 16 additions & 3 deletions ShockOsc/Ui/Pages/Authentication/Authenticate.razor
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
@using OpenShock.ShockOsc.Config
@using OpenShock.ShockOsc.Services
@using OpenShock.ShockOsc.Services.Pipes
@inject ConfigManager ConfigManager
@inject NavigationManager NavigationManager
@inject ISnackbar Snackbar
@inject AuthService AuthService
@inject PipeServerService PipeService
@layout NotAuthedLayout
@inherits LayoutComponentBase


@page "/"

<MudContainer Class="d-flex align-content-center" Style="padding: 0 0 15px 0;">
Expand Down Expand Up @@ -54,13 +57,24 @@

private State _currentState = State.Login;

private bool Loading { get; set; }

protected override async Task OnInitializedAsync()
{
if (await CheckTokenReceived()) return;

PipeService.OnMessageReceived += CheckTokenReceived;

if (string.IsNullOrEmpty(ConfigManager.Config.OpenShock.Token)) return;
await ProceedAuthenticated();
}

private async Task<bool> CheckTokenReceived()
{
if (string.IsNullOrEmpty(PipeService.Token) || _currentState != State.Login) return false;
ConfigManager.Config.OpenShock.Token = PipeService.Token;
await ConfigManager.SaveAsync();
PipeService.Token = null;
await ProceedAuthenticated();
return true;
}

public void ReLogin()
Expand All @@ -70,7 +84,6 @@

private async Task ProceedAuthenticated()
{
Loading = true;
_currentState = State.Loading;
await InvokeAsync(StateHasChanged);

Expand Down
7 changes: 7 additions & 0 deletions ShockOsc/Ui/Pages/Authentication/LoginPart.razor
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
@using OneOf.Types
@using OpenShock.ShockOsc.Backend
@using OpenShock.ShockOsc.Config
@using OpenShock.ShockOsc.Utils

@inject ConfigManager ConfigManager
@inject OpenShockApi ApiClient

<MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="GetToken">Get Token</MudButton>
<MudTextField @bind-Value="ConfigManager.Config.OpenShock.Token" Label="API Token" Variant="Variant.Outlined"></MudTextField>
<br/>
<MudButton OnClick="Login" Variant="Variant.Filled" Color="Color.Primary">Continue</MudButton>
Expand Down Expand Up @@ -146,4 +148,9 @@
if (Server == BackendServer.Custom) _customServerUri = ConfigManager.Config.OpenShock.Backend;
}

private async Task GetToken()
{
UiUtils.OpenUrl("https://shockl.ink/t/?redirect_uri=shockosc:token/%&permissions=shockers.use");
}

}

0 comments on commit 1dbc7a1

Please sign in to comment.