using MyCore.Interfaces.DTO;
using MyCore.Service.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Net.Http.Headers;
using NSwag.Annotations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Mqtt.Client.AspNetCore.Services;
using MyCore.Services.MyControlPanel;
using MyCore.Services;
using MyCore.Services.Devices;
namespace MyCore.Service.Controllers
{
///
/// Authentication controller
///
[ApiController, Route("api/[controller]")]
[Authorize]
[OpenApiTag("Authentication", Description = "Authentication management")]
public class AuthenticationController : ControllerBase
{
private readonly ILogger _logger;
private readonly TokensService _tokensService;
private readonly DeviceDatabaseService _DeviceDatabaseService;
private readonly GroupDatabaseService _GroupDatabaseService;
private readonly ProviderDatabaseService _ProviderDatabaseService;
private readonly RoomDatabaseService _RoomDatabaseService;
private readonly UserDatabaseService _UserDatabaseService;
private readonly ActionService _ActionService;
private readonly AutomationDatabaseService _AutomationDatabaseService;
private readonly IMqttClientService _mqttClientService;
//private readonly IMqttOnlineClientService _mqttOnlineClientService;
public AuthenticationController(ILogger logger, TokensService tokensService, DeviceDatabaseService DeviceDatabaseService, GroupDatabaseService GroupDatabaseService, ProviderDatabaseService ProviderDatabaseService, RoomDatabaseService RoomDatabaseService, UserDatabaseService UserDatabaseService, ActionService ActionService, AutomationDatabaseService AutomationDatabaseService, MqttClientServiceProvider provider)//, MqttClientOnlineServiceProvider onlineProvider)
{
_logger = logger;
_tokensService = tokensService;
_DeviceDatabaseService = DeviceDatabaseService;
_GroupDatabaseService = GroupDatabaseService;
_ProviderDatabaseService = ProviderDatabaseService;
_RoomDatabaseService = RoomDatabaseService;
_UserDatabaseService = UserDatabaseService;
_ActionService = ActionService;
_AutomationDatabaseService = AutomationDatabaseService;
_mqttClientService = provider.MqttClientService;
//_mqttOnlineClientService = onlineProvider.MqttOnlineClientService;
}
///
/// Authenticate (business)
///
/// user email
/// user password
/// Token descriptor
private ObjectResult Authenticate(string email, string password)
{
try
{
// For TEST
email = "test@email.be";
password = "kljqsdkljqsd";
var token = _tokensService.Authenticate(email.ToLower(), password);
// Set user token ?
var user = _UserDatabaseService.GetByEmail(email.ToLower());
if (user == null)
throw new KeyNotFoundException("User not found");
System.Console.WriteLine($"{user.Email} is connected");
return new OkObjectResult(token);
}
catch (UnauthorizedAccessException ex)
{
_logger?.LogError(ex, $"Authentication error for user '{email}': unauthorized access");
return Unauthorized(ex);
}
catch (Exception ex)
{
_logger?.LogError(ex, $"Authenticate error for user '{email}'");
return Problem($"Authenticate error for user '{email}': {ex.Message}");
}
}
///
/// Authenticate with form parameters (used by Swagger test client)
///
/// Swagger token request
/// Token descriptor
[AllowAnonymous]
[HttpPost("Token")]
[Consumes("application/x-www-form-urlencoded")]
[ProducesResponseType(typeof(TokenDTO), (int) HttpStatusCode.OK)]
[ProducesResponseType(typeof(string), (int) HttpStatusCode.Unauthorized)]
[ProducesResponseType(typeof(string), (int) HttpStatusCode.InternalServerError)]
public ObjectResult AuthenticateWithForm([FromForm] SwaggerTokenRequest tokenRequest)
{
return Authenticate(tokenRequest.username, tokenRequest.password);
}
///
/// Authenticate with Json parameters (used by most clients)
///
/// Login DTO
/// Token descriptor
[AllowAnonymous]
[HttpPost("Authenticate")]
[Consumes("application/json")]
[ProducesResponseType(typeof(TokenDTO), (int)HttpStatusCode.OK)]
[ProducesResponseType(typeof(string), (int)HttpStatusCode.Unauthorized)]
[ProducesResponseType(typeof(string), (int)HttpStatusCode.InternalServerError)]
public ObjectResult AuthenticateWithJson([FromBody] LoginDTO login)
{
return Authenticate(login.Email.ToLower(), login.Password);
}
}
}