Started testing oauth2 handler from mooncore

This commit is contained in:
Masu Baumgartner
2024-11-05 22:46:26 +01:00
parent 69e5e1c75b
commit 288b0c8d97
11 changed files with 337 additions and 45 deletions

View File

@@ -12,6 +12,7 @@ public class AuthenticationUiHandler : IAppLoader, IAppScreen
public Task<bool> ShouldRender(IServiceProvider serviceProvider)
{
return Task.FromResult<bool>(false);
var identityService = serviceProvider.GetRequiredService<IdentityService>();
return Task.FromResult(!identityService.IsLoggedIn); // Only show the screen when we are not logged in
@@ -21,6 +22,7 @@ public class AuthenticationUiHandler : IAppLoader, IAppScreen
public async Task Load(IServiceProvider serviceProvider)
{
return;
var identityService = serviceProvider.GetRequiredService<IdentityService>();
await identityService.Check();
}

View File

@@ -10,12 +10,12 @@
<ItemGroup>
<PackageReference Include="Blazor-ApexCharts" Version="3.5.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.6"/>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.6" PrivateAssets="all"/>
<PackageReference Include="MoonCore" Version="1.7.1" />
<PackageReference Include="MoonCore.Blazor" Version="1.2.5" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.10"/>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.10" PrivateAssets="all"/>
<PackageReference Include="MoonCore" Version="1.7.3" />
<PackageReference Include="MoonCore.Blazor" Version="1.2.6" />
<PackageReference Include="MoonCore.PluginFramework" Version="1.0.4" />
<PackageReference Include="MoonCore.Blazor.Tailwind" Version="1.1.0" />
<PackageReference Include="MoonCore.Blazor.Tailwind" Version="1.1.1" />
</ItemGroup>
<ItemGroup>

View File

@@ -56,6 +56,11 @@ builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");
builder.Services.AddScoped(_ => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
builder.AddTokenAuthentication();
builder.AddOAuth2();
/*
builder.Services.AddScoped(sp =>
{
var httpClient = sp.GetRequiredService<HttpClient>();
@@ -93,7 +98,7 @@ builder.Services.AddScoped(sp =>
});
return result;
});
});*/
builder.Services.AddMoonCoreBlazorTailwind();
builder.Services.AddScoped<WindowService>();

View File

@@ -0,0 +1,78 @@
@using Microsoft.AspNetCore.WebUtilities
@using MoonCore.Blazor.Services
@using MoonCore.Exceptions
@using MoonCore.Helpers
@inherits ErrorBoundaryBase
@inject NavigationManager Navigation
@inject OAuth2FrontendService OAuth2FrontendService
@if (IsCompleting)
{
}
else
{
@ChildContent
}
@code
{
private bool IsCompleting = false;
private string Code;
protected override void OnInitialized()
{
var uri = new Uri(Navigation.Uri);
if (!QueryHelpers.ParseQuery(uri.Query).TryGetValue("code", out var codeSv))
return;
try
{
if (codeSv.Count == 0 || string.IsNullOrEmpty(codeSv.First()))
return;
var code = codeSv.First();
Code = code!;
IsCompleting = true;
}
catch (Exception e)
{
Console.WriteLine("FUUCK");
Console.WriteLine(e);
throw;
}
}
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (!firstRender)
return;
if(!IsCompleting)
return;
if (!await OAuth2FrontendService.Complete(Code))
await RedirectToAuth();
IsCompleting = false;
await InvokeAsync(StateHasChanged);
}
protected override async Task OnErrorAsync(Exception exception)
{
if (exception is not HttpApiException httpApiException || httpApiException.Status != 401)
throw exception;
// If we reach this point, we got a 401 unauthenticated, so we need to log in
await RedirectToAuth();
}
private async Task RedirectToAuth()
{
var url = await OAuth2FrontendService.Start();
Navigation.NavigateTo(url, true);
}
}

View File

@@ -2,6 +2,8 @@
@using Moonlight.Client.Interfaces
@using Moonlight.Client.Services
@using Moonlight.Client.UI.Partials
@using MoonCore.Blazor.Components
@using Moonlight.Client.UI.Components
@inherits LayoutComponentBase
@@ -35,17 +37,19 @@ else
<div class="px-4 sm:px-6 lg:px-8">
<ErrorHandler CustomHandler="HandleException">
<PermissionHandler CheckFunction="CheckPermission">
<CascadingValue Value="this" IsFixed="true">
@Body
</CascadingValue>
</PermissionHandler>
<TestyHmm>
<PermissionHandler CheckFunction="CheckPermission">
<CascadingValue Value="this" IsFixed="true">
@Body
</CascadingValue>
</PermissionHandler>
</TestyHmm>
</ErrorHandler>
</div>
</main>
</div>
</div>
}
@@ -72,9 +76,9 @@ else
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if(!firstRender)
if (!firstRender)
return;
await Load();
}
@@ -82,7 +86,7 @@ else
{
IsLoading = true;
await InvokeAsync(StateHasChanged);
//
await RunLoaders();
@@ -126,7 +130,7 @@ else
continue;
CurrentScreen = screen.Render();
await InvokeAsync(StateHasChanged);
return;
}
@@ -143,7 +147,7 @@ else
Task.Run(Load);
return Task.FromResult(true);
}
return Task.FromResult(false);
}
}