diff --git a/Moonlight.ApiServer/Http/Middleware/PermissionLoaderMiddleware.cs b/Moonlight.ApiServer/Http/Middleware/PermissionLoaderMiddleware.cs new file mode 100644 index 00000000..ecacc11b --- /dev/null +++ b/Moonlight.ApiServer/Http/Middleware/PermissionLoaderMiddleware.cs @@ -0,0 +1,33 @@ +using System.Text.Json; +using MoonCore.Authentication; +using Moonlight.ApiServer.Database.Entities; + +namespace Moonlight.ApiServer.Http.Middleware; + +public class PermissionLoaderMiddleware +{ + private readonly RequestDelegate Next; + + public PermissionLoaderMiddleware(RequestDelegate next) + { + Next = next; + } + + public async Task Invoke(HttpContext context) + { + await Load(context); + await Next(context); + } + + private Task Load(HttpContext context) + { + if(context.User is not PermClaimsPrinciple permClaimsPrinciple) + return Task.CompletedTask; + + if(permClaimsPrinciple.IdentityModel is not User user) + return Task.CompletedTask; + + permClaimsPrinciple.Permissions = JsonSerializer.Deserialize(user.PermissionsJson) ?? []; + return Task.CompletedTask; + } +} \ No newline at end of file diff --git a/Moonlight.ApiServer/Implementations/OAuth2/LocalOAuth2Provider.cs b/Moonlight.ApiServer/Implementations/OAuth2/LocalOAuth2Provider.cs index 9bffea3c..d1c82cc0 100644 --- a/Moonlight.ApiServer/Implementations/OAuth2/LocalOAuth2Provider.cs +++ b/Moonlight.ApiServer/Implementations/OAuth2/LocalOAuth2Provider.cs @@ -14,55 +14,7 @@ public class LocalOAuth2Provider : ILocalProviderImplementation { UserRepository = userRepository; } - - /* - public async Task Sync(IServiceProvider provider, string accessToken) - { - var logger = provider.GetRequiredService>(); - - try - { - var configuration = provider.GetRequiredService(); - - using var httpClient = new HttpClient(); - - httpClient.DefaultRequestHeaders.Add("Authorization", accessToken); - - var response = await httpClient.GetAsync($"{configuration.PublicUrl}/oauth2/info"); - await response.HandlePossibleApiError(); - var info = await response.ParseAsJson(); - - var userRepo = provider.GetRequiredService>(); - var user = userRepo.Get().FirstOrDefault(x => x.Email == info.Email); - - if (user == null) // User not found, register a new one - { - user = userRepo.Add(new User() - { - Email = info.Email, - Username = info.Username - }); - } - else if (user.Username != info.Username) // Username updated? - { - // Username not used by another user? - if (!userRepo.Get().Any(x => x.Username == info.Username)) - { - // Update username - user.Username = info.Username; - userRepo.Update(user); - } - } - - return user; - } - catch (Exception e) - { - logger.LogCritical("Unable to sync user: {e}", e); - return null; - } - }*/ - + public Task SaveChanges(User model) { UserRepository.Update(model); diff --git a/Moonlight.ApiServer/Startup.cs b/Moonlight.ApiServer/Startup.cs index 9b11da4e..62e2140a 100644 --- a/Moonlight.ApiServer/Startup.cs +++ b/Moonlight.ApiServer/Startup.cs @@ -368,6 +368,8 @@ public static class Startup application.UseOAuth2Authentication(); application.UseLocalOAuth2Provider(); + application.UseMiddleware(); + return Task.CompletedTask; } diff --git a/Moonlight.Client/Startup.cs b/Moonlight.Client/Startup.cs index cbff2642..ece86d30 100644 --- a/Moonlight.Client/Startup.cs +++ b/Moonlight.Client/Startup.cs @@ -61,47 +61,7 @@ public class Startup builder.AddTokenAuthentication(); builder.AddOAuth2(); - -/* -builder.Services.AddScoped(sp => -{ - var httpClient = sp.GetRequiredService(); - var localStorageService = sp.GetRequiredService(); - var result = new HttpApiClient(httpClient); - - result.AddLocalStorageTokenAuthentication(localStorageService, async refreshToken => - { - try - { - var httpApiClient = new HttpApiClient(httpClient); - - var response = await httpApiClient.PostJson( - "api/auth/refresh", - new RefreshRequest() - { - RefreshToken = refreshToken - } - ); - - return (new TokenPair() - { - AccessToken = response.AccessToken, - RefreshToken = response.RefreshToken - }, response.ExpiresAt); - } - catch (HttpApiException) - { - return (new TokenPair() - { - AccessToken = "unset", - RefreshToken = "unset" - }, DateTime.MinValue); - } - }); - - return result; -});*/ - + builder.Services.AddMoonCoreBlazorTailwind(); builder.Services.AddScoped(); builder.Services.AddScoped(); diff --git a/Moonlight.Client/UI/App.razor b/Moonlight.Client/UI/App.razor index cd153a74..e73c0755 100644 --- a/Moonlight.Client/UI/App.razor +++ b/Moonlight.Client/UI/App.razor @@ -1,6 +1,5 @@ @using Moonlight.Client.UI.Layouts @using MoonCore.Blazor.Components -@using Moonlight.Client.UI.Components diff --git a/Moonlight.Client/UI/Views/Admin/Sys/Index.razor b/Moonlight.Client/UI/Views/Admin/Sys/Index.razor index 6c50da5d..7f9fbcb1 100644 --- a/Moonlight.Client/UI/Views/Admin/Sys/Index.razor +++ b/Moonlight.Client/UI/Views/Admin/Sys/Index.razor @@ -10,7 +10,7 @@ @inject HttpApiClient ApiClient -
+