From c15f18108d9dc1ad568b5bfde0dd73685bab753a Mon Sep 17 00:00:00 2001 From: Masu Baumgartner <68913099+Masu-Baumgartner@users.noreply.github.com> Date: Sun, 27 Oct 2024 20:49:06 +0100 Subject: [PATCH] Changed auth success ui. Switched to new interface service. Upgraded mooncore versions --- .../Http/Controllers/Auth/AuthController.cs | 23 ++++----- .../Moonlight.ApiServer.csproj | 4 +- Moonlight.ApiServer/Program.cs | 16 +++--- Moonlight.Client/Moonlight.Client.csproj | 4 +- Moonlight.Client/Program.cs | 22 ++++---- Moonlight.Client/UI/Layouts/MainLayout.razor | 9 ++-- Moonlight.Client/UI/Partials/AppSidebar.razor | 4 +- .../UI/Screens/AuthenticationScreen.razor | 50 +++++++++++-------- Moonlight.Client/wwwroot/svg/completed.svg | 1 + 9 files changed, 73 insertions(+), 60 deletions(-) create mode 100644 Moonlight.Client/wwwroot/svg/completed.svg diff --git a/Moonlight.ApiServer/Http/Controllers/Auth/AuthController.cs b/Moonlight.ApiServer/Http/Controllers/Auth/AuthController.cs index c6816159..c1cf59b1 100644 --- a/Moonlight.ApiServer/Http/Controllers/Auth/AuthController.cs +++ b/Moonlight.ApiServer/Http/Controllers/Auth/AuthController.cs @@ -26,23 +26,26 @@ public class AuthController : Controller private readonly TokenHelper TokenHelper; private readonly ConfigService ConfigService; private readonly DatabaseRepository UserRepository; - private readonly ImplementationService ImplementationService; private readonly ILogger Logger; + private readonly IOAuth2Provider[] OAuth2Providers; + private readonly IAuthInterceptor[] AuthInterceptors; public AuthController( OAuth2Service oAuth2Service, TokenHelper tokenHelper, DatabaseRepository userRepository, ConfigService configService, - ImplementationService implementationService, - ILogger logger) + ILogger logger, + IOAuth2Provider[] oAuth2Providers, + IAuthInterceptor[] authInterceptors) { OAuth2Service = oAuth2Service; TokenHelper = tokenHelper; UserRepository = userRepository; ConfigService = configService; - ImplementationService = implementationService; Logger = logger; + OAuth2Providers = oAuth2Providers; + AuthInterceptors = authInterceptors; } [HttpGet] @@ -59,7 +62,7 @@ public class AuthController : Controller var accessData = await OAuth2Service.RequestAccess(request.Code);; // Find oauth2 provider - var provider = ImplementationService.Get().FirstOrDefault(); + var provider = OAuth2Providers.FirstOrDefault(); if (provider == null) throw new HttpApiException("No oauth2 provider has been registered", 500); @@ -71,9 +74,7 @@ public class AuthController : Controller throw new HttpApiException("The oauth2 provider was unable to authenticate you", 401); // Allow plugins to intercept access calls - var interceptors = ImplementationService.Get(); - - if (interceptors.Any(interceptor => !interceptor.AllowAccess(user, HttpContext.RequestServices))) + if (AuthInterceptors.Any(interceptor => !interceptor.AllowAccess(user, HttpContext.RequestServices))) throw new HttpApiException("Unable to get access token", 401); // Save oauth2 refresh and access tokens for later use (re-authentication etc.). @@ -144,7 +145,7 @@ public class AuthController : Controller private bool ProcessRefreshData(Dictionary refreshTokenData, Dictionary newData, IServiceProvider serviceProvider) { // Find oauth2 provider - var provider = ImplementationService.Get().FirstOrDefault(); + var provider = OAuth2Providers.FirstOrDefault(); if (provider == null) throw new HttpApiException("No oauth2 provider has been registered", 500); @@ -162,9 +163,7 @@ public class AuthController : Controller return false; // Allow plugins to intercept the refresh call - var interceptors = ImplementationService.Get(); - - if (interceptors.Any(interceptor => !interceptor.AllowRefresh(user, serviceProvider))) + if (AuthInterceptors.Any(interceptor => !interceptor.AllowRefresh(user, serviceProvider))) return false; // Check if it's time to resync with the oauth2 provider diff --git a/Moonlight.ApiServer/Moonlight.ApiServer.csproj b/Moonlight.ApiServer/Moonlight.ApiServer.csproj index c17595e2..f6d667e8 100644 --- a/Moonlight.ApiServer/Moonlight.ApiServer.csproj +++ b/Moonlight.ApiServer/Moonlight.ApiServer.csproj @@ -12,11 +12,11 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - + - + diff --git a/Moonlight.ApiServer/Program.cs b/Moonlight.ApiServer/Program.cs index 2cd8d78d..918c5cca 100644 --- a/Moonlight.ApiServer/Program.cs +++ b/Moonlight.ApiServer/Program.cs @@ -1,3 +1,4 @@ +using System.Reflection; using System.Text.Json; using Microsoft.OpenApi.Models; using MoonCore.Extended.Abstractions; @@ -7,6 +8,7 @@ using MoonCore.Extended.OAuth2.ApiServer; using MoonCore.Extensions; using MoonCore.Helpers; using MoonCore.Models; +using MoonCore.PluginFramework.Extensions; using MoonCore.PluginFramework.Services; using MoonCore.Services; using Moonlight.ApiServer.Configuration; @@ -16,6 +18,7 @@ using Moonlight.ApiServer.Helpers; using Moonlight.ApiServer.Helpers.Authentication; using Moonlight.ApiServer.Http.Middleware; using Moonlight.ApiServer.Implementations.OAuth2; +using Moonlight.ApiServer.Interfaces.Auth; using Moonlight.ApiServer.Interfaces.OAuth2; // Prepare file system @@ -186,12 +189,13 @@ if (configService.Get().Development.EnableApiDocs) } // Implementation service -var implementationService = new ImplementationService(); - -if(config.Authentication.UseLocalOAuth2) - implementationService.Register(); - -builder.Services.AddSingleton(implementationService); +builder.Services.AddPlugins(configuration => +{ + configuration.AddInterface(); + configuration.AddInterface(); + + configuration.AddAssembly(Assembly.GetEntryAssembly()!); +}, logger); var app = builder.Build(); diff --git a/Moonlight.Client/Moonlight.Client.csproj b/Moonlight.Client/Moonlight.Client.csproj index a9e12ccc..37f43b23 100644 --- a/Moonlight.Client/Moonlight.Client.csproj +++ b/Moonlight.Client/Moonlight.Client.csproj @@ -10,9 +10,9 @@ - + - + diff --git a/Moonlight.Client/Program.cs b/Moonlight.Client/Program.cs index d9de2ab2..60e66cf3 100644 --- a/Moonlight.Client/Program.cs +++ b/Moonlight.Client/Program.cs @@ -1,3 +1,4 @@ +using System.Reflection; using Microsoft.AspNetCore.Components.Web; using Microsoft.AspNetCore.Components.WebAssembly.Hosting; using MoonCore.Blazor.Extensions; @@ -9,6 +10,7 @@ using MoonCore.Exceptions; using MoonCore.Extensions; using MoonCore.Helpers; using MoonCore.Models; +using MoonCore.PluginFramework.Extensions; using MoonCore.PluginFramework.Services; using Moonlight.Client.Implementations; using Moonlight.Client.Interfaces; @@ -103,16 +105,16 @@ builder.Services.AutoAddServices(); FormComponentRepository.Set(); FormComponentRepository.Set(); -// Implementation service -var implementationService = new ImplementationService(); - -implementationService.Register(); - -var authUiHandler = new AuthenticationUiHandler(); -implementationService.Register(authUiHandler); -implementationService.Register(authUiHandler); - -builder.Services.AddSingleton(implementationService); +// Interface service +builder.Services.AddPlugins(configuration => +{ + configuration.AddAssembly(Assembly.GetEntryAssembly()!); + + configuration.AddInterface(); + configuration.AddInterface(); + + configuration.AddInterface(); +}, logger); var app = builder.Build(); diff --git a/Moonlight.Client/UI/Layouts/MainLayout.razor b/Moonlight.Client/UI/Layouts/MainLayout.razor index 26080d7b..6adfe13f 100644 --- a/Moonlight.Client/UI/Layouts/MainLayout.razor +++ b/Moonlight.Client/UI/Layouts/MainLayout.razor @@ -8,10 +8,11 @@ @inherits LayoutComponentBase -@inject ImplementationService ImplementationService @inject IdentityService IdentityService @inject IServiceProvider ServiceProvider @inject ILogger Logger +@inject IAppLoader[] AppLoaders +@inject IAppScreen[] AppScreens @if (IsLoading) { @@ -98,8 +99,7 @@ else private async Task RunLoaders() { - var appLoaders = ImplementationService - .Get() + var appLoaders = AppLoaders .OrderBy(x => x.Priority); foreach (var loader in appLoaders) //TODO: Measure performance of every loader? @@ -120,8 +120,7 @@ else { CurrentScreen = null; - var appScreens = ImplementationService - .Get() + var appScreens = AppScreens .OrderBy(x => x.Priority); foreach (var screen in appScreens) diff --git a/Moonlight.Client/UI/Partials/AppSidebar.razor b/Moonlight.Client/UI/Partials/AppSidebar.razor index a9ad45e5..08ab0197 100644 --- a/Moonlight.Client/UI/Partials/AppSidebar.razor +++ b/Moonlight.Client/UI/Partials/AppSidebar.razor @@ -6,7 +6,7 @@ @inject NavigationManager Navigation @inject IdentityService IdentityService -@inject ImplementationService ImplementationService +@inject ISidebarItemProvider[] SidebarItemProviders @{ var url = new Uri(Navigation.Uri); @@ -168,7 +168,7 @@ protected override void OnInitialized() { - Items = ImplementationService.Get() + Items = SidebarItemProviders .SelectMany(x => x.Get()) .Where(x => x.Permission == null || (x.Permission != null && IdentityService.HasPermission(x.Permission))) .GroupBy(x => x.Group ?? "") diff --git a/Moonlight.Client/UI/Screens/AuthenticationScreen.razor b/Moonlight.Client/UI/Screens/AuthenticationScreen.razor index 8e302685..f4454be2 100644 --- a/Moonlight.Client/UI/Screens/AuthenticationScreen.razor +++ b/Moonlight.Client/UI/Screens/AuthenticationScreen.razor @@ -12,6 +12,8 @@ @inject HttpClient HttpClient @inject LocalStorageService LocalStorageService +@inject ILogger Logger + @if (Code == null) {
@@ -53,14 +55,10 @@ else @if (IsHandlingDone) {
- Moonlight -

Login to access your account

-
- -
-

+ Completed illustration +

Login successful. You can close this window now -

+

} else @@ -91,25 +89,35 @@ else if (Code == null) return; - var authHandleData = await HttpApiClient.PostJson("api/auth", new OAuth2HandleRequest() - { - Code = Code - }); - - await LocalStorageService.SetString("AccessToken", authHandleData.AccessToken); - await LocalStorageService.SetString("RefreshToken", authHandleData.RefreshToken); - await LocalStorageService.Set("ExpiresAt", authHandleData.ExpiresAt); - try { - await WindowService.Close(); - } - finally - { + var authHandleData = await HttpApiClient.PostJson("api/auth", new OAuth2HandleRequest() + { + Code = Code + }); + + // Save the auth handle data + await LocalStorageService.SetString("AccessToken", authHandleData.AccessToken); + await LocalStorageService.SetString("RefreshToken", authHandleData.RefreshToken); + await LocalStorageService.Set("ExpiresAt", authHandleData.ExpiresAt); + + // Update UI IsHandlingDone = true; await InvokeAsync(StateHasChanged); + await Task.Delay(TimeSpan.FromSeconds(2)); - Navigation.NavigateTo("/", true); + try + { + await WindowService.Close(); + } + finally + { + Navigation.NavigateTo("/", true); + } + } + catch (Exception e) + { + Logger.LogError("An unhandled error occured while handling oauth2 code: {e}", e); } } diff --git a/Moonlight.Client/wwwroot/svg/completed.svg b/Moonlight.Client/wwwroot/svg/completed.svg new file mode 100644 index 00000000..6d51f78d --- /dev/null +++ b/Moonlight.Client/wwwroot/svg/completed.svg @@ -0,0 +1 @@ + \ No newline at end of file