Updated MoonCore dependencies. Switched to asp.net core native authentication scheme abstractions. Updated claim usage in frontend
This commit is contained in:
@@ -34,15 +34,8 @@ public class CoreConfigDiagnoseProvider : IDiagnoseProvider
|
||||
}
|
||||
|
||||
config.Database.Password = CheckForNullOrEmpty(config.Database.Password);
|
||||
|
||||
config.Authentication.OAuth2.ClientSecret = CheckForNullOrEmpty(config.Authentication.OAuth2.ClientSecret);
|
||||
|
||||
config.Authentication.OAuth2.Secret = CheckForNullOrEmpty(config.Authentication.OAuth2.Secret);
|
||||
|
||||
config.Authentication.Secret = CheckForNullOrEmpty(config.Authentication.Secret);
|
||||
|
||||
config.Authentication.OAuth2.ClientId = CheckForNullOrEmpty(config.Authentication.OAuth2.ClientId);
|
||||
|
||||
await archive.AddText(
|
||||
"core/config.txt",
|
||||
JsonSerializer.Serialize(
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Moonlight.ApiServer.Implementations.LocalAuth;
|
||||
|
||||
public static class LocalAuthConstants
|
||||
{
|
||||
public const string AuthenticationScheme = "LocalAuth";
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using System.Text.Encodings.Web;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Moonlight.ApiServer.Implementations.LocalAuth;
|
||||
|
||||
public class LocalAuthHandler : AuthenticationHandler<LocalAuthOptions>
|
||||
{
|
||||
public LocalAuthHandler(
|
||||
IOptionsMonitor<LocalAuthOptions> options,
|
||||
ILoggerFactory logger,
|
||||
UrlEncoder encoder
|
||||
) : base(options, logger, encoder)
|
||||
{
|
||||
}
|
||||
|
||||
protected override Task<AuthenticateResult> HandleAuthenticateAsync()
|
||||
{
|
||||
return Task.FromResult(
|
||||
AuthenticateResult.Fail("Local authentication does not directly support AuthenticateAsync")
|
||||
);
|
||||
}
|
||||
|
||||
protected override async Task HandleChallengeAsync(AuthenticationProperties properties)
|
||||
{
|
||||
await Results
|
||||
.Redirect("/api/localAuth")
|
||||
.ExecuteAsync(Context);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
|
||||
namespace Moonlight.ApiServer.Implementations.LocalAuth;
|
||||
|
||||
public class LocalAuthOptions : AuthenticationSchemeOptions
|
||||
{
|
||||
public string? SignInScheme { get; set; }
|
||||
}
|
||||
@@ -1,106 +0,0 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using MoonCore.Exceptions;
|
||||
using MoonCore.Extended.Abstractions;
|
||||
using MoonCore.Helpers;
|
||||
using Moonlight.ApiServer.Configuration;
|
||||
using Moonlight.ApiServer.Database.Entities;
|
||||
using Moonlight.ApiServer.Interfaces;
|
||||
using Moonlight.Shared.Http.Responses.OAuth2;
|
||||
|
||||
namespace Moonlight.ApiServer.Implementations;
|
||||
|
||||
public class LocalOAuth2Provider : IOAuth2Provider
|
||||
{
|
||||
private readonly AppConfiguration Configuration;
|
||||
private readonly ILogger<LocalOAuth2Provider> Logger;
|
||||
private readonly DatabaseRepository<User> UserRepository;
|
||||
|
||||
public LocalOAuth2Provider(
|
||||
AppConfiguration configuration,
|
||||
ILogger<LocalOAuth2Provider> logger,
|
||||
DatabaseRepository<User> userRepository
|
||||
)
|
||||
{
|
||||
UserRepository = userRepository;
|
||||
Configuration = configuration;
|
||||
Logger = logger;
|
||||
}
|
||||
|
||||
public Task<string> Start()
|
||||
{
|
||||
var redirectUri = string.IsNullOrEmpty(Configuration.Authentication.OAuth2.AuthorizationRedirect)
|
||||
? Configuration.PublicUrl
|
||||
: Configuration.Authentication.OAuth2.AuthorizationRedirect;
|
||||
|
||||
var endpoint = string.IsNullOrEmpty(Configuration.Authentication.OAuth2.AuthorizationEndpoint)
|
||||
? Configuration.PublicUrl + "/oauth2/authorize"
|
||||
: Configuration.Authentication.OAuth2.AuthorizationEndpoint;
|
||||
|
||||
var clientId = Configuration.Authentication.OAuth2.ClientId;
|
||||
|
||||
var url = $"{endpoint}" +
|
||||
$"?client_id={clientId}" +
|
||||
$"&redirect_uri={redirectUri}" +
|
||||
$"&response_type=code";
|
||||
|
||||
return Task.FromResult(url);
|
||||
}
|
||||
|
||||
public async Task<User?> Complete(string code)
|
||||
{
|
||||
// Create http client to call the auth provider
|
||||
var httpClient = new HttpClient();
|
||||
using var httpApiClient = new HttpApiClient(httpClient);
|
||||
|
||||
httpClient.DefaultRequestHeaders.Add("Authorization",
|
||||
$"Basic {Configuration.Authentication.OAuth2.ClientSecret}");
|
||||
|
||||
// Build access endpoint
|
||||
var accessEndpoint = string.IsNullOrEmpty(Configuration.Authentication.OAuth2.AccessEndpoint)
|
||||
? $"{Configuration.PublicUrl}/oauth2/handle"
|
||||
: Configuration.Authentication.OAuth2.AccessEndpoint;
|
||||
|
||||
// Build redirect uri
|
||||
var redirectUri = string.IsNullOrEmpty(Configuration.Authentication.OAuth2.AuthorizationRedirect)
|
||||
? Configuration.PublicUrl
|
||||
: Configuration.Authentication.OAuth2.AuthorizationRedirect;
|
||||
|
||||
// Call the auth provider
|
||||
OAuth2HandleResponse handleData;
|
||||
|
||||
try
|
||||
{
|
||||
handleData = await httpApiClient.PostJson<OAuth2HandleResponse>(accessEndpoint, new FormUrlEncodedContent(
|
||||
[
|
||||
new KeyValuePair<string, string>("grant_type", "authorization_code"),
|
||||
new KeyValuePair<string, string>("code", code),
|
||||
new KeyValuePair<string, string>("redirect_uri", redirectUri),
|
||||
new KeyValuePair<string, string>("client_id", Configuration.Authentication.OAuth2.ClientId)
|
||||
]
|
||||
));
|
||||
}
|
||||
catch (HttpApiException e)
|
||||
{
|
||||
if (e.Status == 400)
|
||||
Logger.LogTrace("The auth server returned an error: {e}", e);
|
||||
else
|
||||
Logger.LogCritical("The auth server returned an error: {e}", e);
|
||||
|
||||
throw new HttpApiException("Unable to request user data", 500);
|
||||
}
|
||||
|
||||
// Notice: We just look up the user id here
|
||||
// which works as our oauth2 provider is using the same db.
|
||||
// a real oauth2 provider would create a user here
|
||||
|
||||
// Handle the returned data
|
||||
var userId = handleData.UserId;
|
||||
|
||||
var user = await UserRepository
|
||||
.Get()
|
||||
.FirstOrDefaultAsync(x => x.Id == userId);
|
||||
|
||||
return user;
|
||||
}
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
using System.Security.Claims;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MoonCore.Extended.Abstractions;
|
||||
using MoonCore.Extended.JwtInvalidation;
|
||||
using Moonlight.ApiServer.Database.Entities;
|
||||
|
||||
namespace Moonlight.ApiServer.Implementations;
|
||||
|
||||
public class UserAuthInvalidation : IJwtInvalidateHandler
|
||||
{
|
||||
private readonly DatabaseRepository<User> UserRepository;
|
||||
private readonly DatabaseRepository<ApiKey> ApiKeyRepository;
|
||||
|
||||
public UserAuthInvalidation(
|
||||
DatabaseRepository<User> userRepository,
|
||||
DatabaseRepository<ApiKey> apiKeyRepository
|
||||
)
|
||||
{
|
||||
UserRepository = userRepository;
|
||||
ApiKeyRepository = apiKeyRepository;
|
||||
}
|
||||
|
||||
public async Task<bool> Handle(ClaimsPrincipal principal)
|
||||
{
|
||||
var userIdClaim = principal.FindFirstValue("userId");
|
||||
|
||||
if (!string.IsNullOrEmpty(userIdClaim))
|
||||
{
|
||||
var userId = int.Parse(userIdClaim);
|
||||
|
||||
var user = await UserRepository
|
||||
.Get()
|
||||
.FirstOrDefaultAsync(x => x.Id == userId);
|
||||
|
||||
if (user == null)
|
||||
return true; // User is deleted, invalidate session
|
||||
|
||||
var iatStr = principal.FindFirstValue("iat")!;
|
||||
var iat = DateTimeOffset.FromUnixTimeSeconds(long.Parse(iatStr));
|
||||
|
||||
// If the token has been issued before the token valid time, its expired, and we want to invalidate it
|
||||
return user.TokenValidTimestamp > iat;
|
||||
}
|
||||
|
||||
var apiKeyIdClaim = principal.FindFirstValue("apiKeyId");
|
||||
|
||||
if (!string.IsNullOrEmpty(apiKeyIdClaim))
|
||||
{
|
||||
var apiKeyId = int.Parse(apiKeyIdClaim);
|
||||
|
||||
var apiKey = await ApiKeyRepository
|
||||
.Get()
|
||||
.FirstOrDefaultAsync(x => x.Id == apiKeyId);
|
||||
|
||||
// If the api key exists, we don't want to invalidate the request.
|
||||
// If it doesn't exist we want to invalidate the request
|
||||
return apiKey == null;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user