feat(api): аутентификация и авторизация. Кастомные ParrAuthenticationHandler и BaseSimpleRoleProvider.
This commit is contained in:
87
PARR.API/Services/Implementations/AuthService.cs
Normal file
87
PARR.API/Services/Implementations/AuthService.cs
Normal file
@@ -0,0 +1,87 @@
|
||||
using PARR.API.Authentication.Models;
|
||||
using PARR.API.Services.Interfaces;
|
||||
using PARR.API.Settings;
|
||||
using PARR.Constants;
|
||||
using PARR.DAL.CacheServices;
|
||||
using PARR.DAL.Services.Interfaces;
|
||||
|
||||
namespace PARR.API.Services.Implementations
|
||||
{
|
||||
public class AuthService : IAuthService
|
||||
{
|
||||
private readonly IRedisCacheService cacheService;
|
||||
private readonly IUserService userService;
|
||||
private readonly IHostService hostService;
|
||||
private readonly UserCacheSettings userCacheSettings;
|
||||
|
||||
public AuthService(IRedisCacheService cacheService, IUserService userService, IHostService hostService, UserCacheSettings userCacheSettings)
|
||||
{
|
||||
this.cacheService = cacheService;
|
||||
this.userService = userService;
|
||||
this.hostService = hostService;
|
||||
this.userCacheSettings = userCacheSettings;
|
||||
}
|
||||
|
||||
|
||||
public async Task<bool> UserIsBlockedAsync(string ipAddress)
|
||||
{
|
||||
//пользователь есть в списке блокировки?
|
||||
var existUserInBlockList = await cacheService.GetCachedDataAsync<DateTimeOffset?>(GetBlockKey(ipAddress));
|
||||
|
||||
return existUserInBlockList != null;
|
||||
}
|
||||
|
||||
|
||||
public async Task<UserCache?> GetUserAsync(string ipAddress)
|
||||
{
|
||||
// смотрим, есть ли в кэше
|
||||
var userInCache = await cacheService.GetCachedDataAsync<UserCache>(GetAllowKey(ipAddress));
|
||||
if (userInCache != null)
|
||||
return userInCache;
|
||||
|
||||
// нет в кэше
|
||||
var roles = new List<UserRoleInCache>();
|
||||
|
||||
var user = await userService.GetByIpWithRolesAsync(ipAddress);
|
||||
if (user != null)
|
||||
user.Roles.ToList().ForEach(t =>
|
||||
{
|
||||
if (t.Role != null)
|
||||
roles.Add(new UserRoleInCache { Name = t.Role.Name, Description = t.Role.Description });
|
||||
});
|
||||
|
||||
var host = await hostService.GetByIpAsync(ipAddress);
|
||||
if (host != null)
|
||||
roles.Add(new UserRoleInCache { Name = ParrRoles.Agent.Role, Description = ParrRoles.Agent.Description });
|
||||
|
||||
if (user == null && host == null)
|
||||
{
|
||||
// если это не пользователь и не хост, добавляем в кэш на блокировку
|
||||
await cacheService.SetCachedDataAsync(GetBlockKey(ipAddress), DateTimeOffset.UtcNow, userCacheSettings.UserBlockTtl);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
// пользователь есть, добавляем в КЭШ и возвращаем роли
|
||||
var cacheData = new UserCache { UserIp = ipAddress, Roles = roles };
|
||||
await cacheService.SetCachedDataAsync(GetAllowKey(ipAddress), cacheData, userCacheSettings.UserCacheTtl);
|
||||
|
||||
return cacheData;
|
||||
}
|
||||
|
||||
|
||||
private string GetBlockKey(string ipAddress)
|
||||
{
|
||||
// d - deny
|
||||
return $"d_{ipAddress}";
|
||||
}
|
||||
|
||||
private string GetAllowKey(string ipAddress)
|
||||
{
|
||||
// a - allow
|
||||
return $"a_{ipAddress}";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user