2023-12-01 16:41:37 +10:00
|
|
|
|
using Microsoft.AspNetCore.Authentication;
|
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
|
using PARR.API.Services.Interfaces;
|
|
|
|
|
|
using System.Security.Claims;
|
|
|
|
|
|
using System.Text.Encodings.Web;
|
|
|
|
|
|
|
|
|
|
|
|
namespace PARR.API.Authentication
|
|
|
|
|
|
{
|
|
|
|
|
|
public class ParrAuthenticationHandler : AuthenticationHandler<ParrAuthenticationOptions>
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IServiceProvider serviceProvider;
|
|
|
|
|
|
|
|
|
|
|
|
public ParrAuthenticationHandler(
|
|
|
|
|
|
IOptionsMonitor<ParrAuthenticationOptions> options,
|
|
|
|
|
|
ILoggerFactory logger,
|
|
|
|
|
|
UrlEncoder encoder,
|
|
|
|
|
|
ISystemClock clock,
|
|
|
|
|
|
IServiceProvider serviceProvider) : base(options, logger, encoder, clock)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.serviceProvider = serviceProvider;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
var ipClient = Request.HttpContext.Connection.RemoteIpAddress?.MapToIPv4().ToString();
|
|
|
|
|
|
|
|
|
|
|
|
if (ipClient == null)
|
2023-12-04 10:56:29 +10:00
|
|
|
|
return AuthenticateResult.Fail($"IP address not defined. IP: {ipClient}");
|
2023-12-01 16:41:37 +10:00
|
|
|
|
|
2025-11-21 12:00:54 +10:00
|
|
|
|
|
2023-12-01 16:41:37 +10:00
|
|
|
|
// Аутентификация - просто проверка, есть ли у нас такой пользователь
|
|
|
|
|
|
using (var scope = serviceProvider.CreateScope())
|
|
|
|
|
|
{
|
|
|
|
|
|
var authService = scope.ServiceProvider.GetRequiredService<IAuthService>();
|
|
|
|
|
|
|
|
|
|
|
|
var userIsBlocked = await authService.UserIsBlockedAsync(ipClient);
|
|
|
|
|
|
if (userIsBlocked)
|
2023-12-04 10:56:29 +10:00
|
|
|
|
return AuthenticateResult.Fail($"IP address is on the blocking list. IP: {ipClient}");
|
2023-12-01 16:41:37 +10:00
|
|
|
|
|
|
|
|
|
|
var user = await authService.GetUserAsync(ipClient);
|
|
|
|
|
|
if (user == null)
|
2023-12-04 10:56:29 +10:00
|
|
|
|
return AuthenticateResult.Fail($"IP address not defined. IP: {ipClient}");
|
2023-12-01 16:41:37 +10:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// пользователь найден, аутентификация пройдена
|
|
|
|
|
|
|
|
|
|
|
|
var claims = new List<Claim> {
|
|
|
|
|
|
new Claim(ClaimTypes.Name, user.UserIp)
|
|
|
|
|
|
};
|
2023-12-04 08:48:13 +10:00
|
|
|
|
// добавляем роли
|
|
|
|
|
|
user.Roles.ForEach(r => claims.Add(new Claim(ClaimTypes.Role, r.Name)));
|
2023-12-01 16:41:37 +10:00
|
|
|
|
|
|
|
|
|
|
var claimsIdentity = new ClaimsIdentity(claims, Scheme.Name);
|
|
|
|
|
|
var claimsPrincipal = new ClaimsPrincipal(claimsIdentity);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return AuthenticateResult.Success(new AuthenticationTicket(claimsPrincipal, Scheme.Name));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-11-21 12:00:54 +10:00
|
|
|
|
|
|
|
|
|
|
|
2025-11-21 12:37:46 +10:00
|
|
|
|
//private AuthenticateResult CreateLocalAuth(string ipClient)
|
|
|
|
|
|
//{
|
|
|
|
|
|
// // Когда контейнер запрашивает health статус,
|
2025-11-21 12:00:54 +10:00
|
|
|
|
|
2025-11-21 12:37:46 +10:00
|
|
|
|
// var claims = new List<Claim> {
|
|
|
|
|
|
// new Claim(ClaimTypes.Name, ipClient)
|
|
|
|
|
|
// };
|
2025-11-21 12:00:54 +10:00
|
|
|
|
|
2025-11-21 12:37:46 +10:00
|
|
|
|
// var claimsIdentity = new ClaimsIdentity(claims, Scheme.Name);
|
|
|
|
|
|
// var claimsPrincipal = new ClaimsPrincipal(claimsIdentity);
|
2025-11-21 12:00:54 +10:00
|
|
|
|
|
2025-11-21 12:37:46 +10:00
|
|
|
|
// return AuthenticateResult.Success(new AuthenticationTicket(claimsPrincipal, Scheme.Name));
|
|
|
|
|
|
//}
|
2025-11-21 12:00:54 +10:00
|
|
|
|
|
|
|
|
|
|
|
2023-12-01 16:41:37 +10:00
|
|
|
|
}
|
|
|
|
|
|
}
|