Cleanud up auth code
This commit is contained in:
@@ -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<string[]>(user.PermissionsJson) ?? [];
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -14,55 +14,7 @@ public class LocalOAuth2Provider : ILocalProviderImplementation<User>
|
||||
{
|
||||
UserRepository = userRepository;
|
||||
}
|
||||
|
||||
/*
|
||||
public async Task<User?> Sync(IServiceProvider provider, string accessToken)
|
||||
{
|
||||
var logger = provider.GetRequiredService<ILogger<LocalOAuth2Provider>>();
|
||||
|
||||
try
|
||||
{
|
||||
var configuration = provider.GetRequiredService<AppConfiguration>();
|
||||
|
||||
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<InfoResponse>();
|
||||
|
||||
var userRepo = provider.GetRequiredService<DatabaseRepository<User>>();
|
||||
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);
|
||||
|
||||
@@ -368,6 +368,8 @@ public static class Startup
|
||||
application.UseOAuth2Authentication<User>();
|
||||
application.UseLocalOAuth2Provider<User>();
|
||||
|
||||
application.UseMiddleware<PermissionLoaderMiddleware>();
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
|
||||
@@ -61,47 +61,7 @@ public class Startup
|
||||
|
||||
builder.AddTokenAuthentication();
|
||||
builder.AddOAuth2();
|
||||
|
||||
/*
|
||||
builder.Services.AddScoped(sp =>
|
||||
{
|
||||
var httpClient = sp.GetRequiredService<HttpClient>();
|
||||
var localStorageService = sp.GetRequiredService<LocalStorageService>();
|
||||
var result = new HttpApiClient(httpClient);
|
||||
|
||||
result.AddLocalStorageTokenAuthentication(localStorageService, async refreshToken =>
|
||||
{
|
||||
try
|
||||
{
|
||||
var httpApiClient = new HttpApiClient(httpClient);
|
||||
|
||||
var response = await httpApiClient.PostJson<RefreshResponse>(
|
||||
"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<WindowService>();
|
||||
builder.Services.AddScoped<LocalStorageService>();
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
@using Moonlight.Client.UI.Layouts
|
||||
@using MoonCore.Blazor.Components
|
||||
@using Moonlight.Client.UI.Components
|
||||
|
||||
<ErrorLogger>
|
||||
<OAuth2AuthenticationHandler>
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
@inject HttpApiClient ApiClient
|
||||
|
||||
<LazyLoader Load="LoadOverview">
|
||||
<div class="gap-5 grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4">
|
||||
<div class="gap-5 grid grid-cols-1 lg:grid-cols-2 xl:grid-cols-4">
|
||||
<StatCard Title="CPU Usage" Text="@(OverviewData.CpuUsage + "%")" Icon="bi bi-cpu"/>
|
||||
<StatCard Title="Memory Usage" Text="@(Formatter.FormatSize(OverviewData.MemoryUsage))" Icon="bi bi-memory"/>
|
||||
<StatCard Title="Host OS" Text="@(OverviewData.OperatingSystem)" Icon="bi bi-motherboard"/>
|
||||
|
||||
Reference in New Issue
Block a user