using System.Reflection.PortableExecutable;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using Dapr.Client;
using FrameworkBLL.User;
using FrameworkDAL.DTO.User;
using GB5Shared.DaprCache;
using GB5Shared.DTO.Framework.Authentication;
using GB5Shared.DTO.Framework.Enum;
using GB5Shared.DTO.Framework.Logging;
using GB5Shared.DTO.Framework.Login;
using GB5Shared.DTO.Framework.ResponseStandard;
using GB5Shared.DTO.Framework.ServerConfig;
using GB5Shared.DTO.Framework.SuccessORError;
using GB5Shared.EventLogPublish;
using GB5Shared.GB5Constant;
using GB5Shared.ResponseStandard;
using GoodBooks.FrameworkBLL.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Hybrid;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using OpenQA.Selenium.DevTools.V130.Browser;
using static Dapr.Client.Autogen.Grpc.v1.Dapr;
using static GB5Shared.GB5Constant.Constant;
namespace FrameworkSL.Controllers.User
{
///
/// User service controller to handle user-related operations.
///
[ApiController]
[Route("[controller]")]
public class UserService : ControllerBase
{
private readonly Dapr.Client.DaprClient _daprClient;
private readonly IAuthenticationBLL _AuthenticationBLL;
private readonly IUserBLL _UserBLL;
private readonly EventLogPublish EventLogPublish;
private readonly ILogger _logger;
///
/// Initializes a new instance of the class.
///
/// The authentication BLL.
/// The user BLL.
/// The Dapr client.
///
///
public UserService(IAuthenticationBLL IAuthenticationBLL, IUserBLL userBLL,
Dapr.Client.DaprClient daprClient, EventLogPublish eventLogPublish, ILogger logger)
{
_AuthenticationBLL = IAuthenticationBLL;
_UserBLL = userBLL;
_daprClient = daprClient;
EventLogPublish = eventLogPublish;
_logger = logger;
}
///
/// Authenticates the user.
///
/// The user code.
/// The authentication DTO.
/// The connection name supplied via request header.
/// A response indicating the result of the authentication.
[HttpPost("AuthenticateUser")]
public async Task> AuthenticateUser(string UserCode, AuthenticationDTO AuthenticationDTO,[FromHeader(Name = "ConnectionName")] string ConnectionName)
{
SuccessDTO successDTO = new();
try
{
AuthenticationDTO.BaseUri = $"{Request.Scheme}://{Request.Host}";
AuthenticationDTO.MachineIP = GetMachineIP(HttpContext);
AuthenticationDTO LoginUserDTO = await _AuthenticationBLL.AuthenticateUser(ConnectionName, UserCode, AuthenticationDTO);
LoginUserDTO.SourceType = 5;
LoginUserDTO.PushNotificationToken = AuthenticationDTO.PushNotificationToken;
LoginDTO loginDTO = LoginDTO(LoginUserDTO);
await EventLogPublish.PublishPushNotification(loginDTO, "GBUSER");
successDTO.Body = JsonConvert.SerializeObject(LoginUserDTO);
if (successDTO.Body != null)
{
return await GB5Shared.ResponseStandard.Response.CreateSuccessResponse(
successDTO.Body, CacheKeyLevel.NOT_REQUIRED,null!
);
}
return await GB5Shared.ResponseStandard.Response.CreateErrorResponseGZipCompress(
"Invalid User or Password",
401
);
}
catch (Exception ex)
{
return await GB5Shared.ResponseStandard.Response.CreateExceptionErrorGZipCompress(ex, CacheKeyLevel.NOT_REQUIRED,
null!, ex.Message,
500
);
}
}
[NonAction]
private string GetMachineIP(HttpContext httpContext)
{
if (httpContext == null)
return null!;
string ip = httpContext.Request.Headers["X-Forwarded-For"].FirstOrDefault()!;
if (string.IsNullOrEmpty(ip))
ip = httpContext.Connection.RemoteIpAddress?.ToString()!;
return ip;
}
[NonAction]
private LoginDTO LoginDTO(AuthenticationDTO AuthenticationDTO)
{
string Authentication = JsonConvert.SerializeObject(AuthenticationDTO);
LoginDTO LoginDTO = JsonConvert.DeserializeObject(Authentication)!;
return LoginDTO;
}
[HttpGet("GetAuthorizeUser")]
public async Task> GetAuthorizeUser(string UserCode, string ConnectionName)
{
AuthenticationDTO AuthenticationDTO = new();
SuccessDTO successDTO = new();
try
{
AuthenticationDTO.BaseUri = $"{Request.Scheme}://{Request.Host}";
AuthenticationDTO.MachineIP = GetMachineIP(HttpContext);
AuthenticationDTO LoginUserDTO = await _AuthenticationBLL.GetAuthorizeUser(UserCode,ConnectionName);
successDTO.Body = JsonConvert.SerializeObject(LoginUserDTO);
return await GB5Shared.ResponseStandard.Response.CreateSuccessResponse(
successDTO.Body, CacheKeyLevel.NOT_REQUIRED, null!
);
}
catch (Exception ex)
{
return await GB5Shared.ResponseStandard.Response.CreateExceptionError(ex, CacheKeyLevel.NOT_REQUIRED,
null!, ex.Message,
500
);
}
}
[HttpPost("pubsub")]
public async Task PublishMessage([FromBody] string message)
{
await _daprClient.PublishEventAsync("pubsub-redis", "GB5Framework", message);
_logger.LogInformation("Published message {Message}", message);
return Ok();
}
}
}