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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user