112 lines
4.5 KiB
C#
112 lines
4.5 KiB
C#
using Manager.Interfaces.DTO;
|
|
using Manager.Services;
|
|
using ManagerService.Service.Services;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Net.Http.Headers;
|
|
using Mqtt.Client.AspNetCore.Services;
|
|
using NSwag.Annotations;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace ManagerService.Service.Controllers
|
|
{
|
|
/// <summary>
|
|
/// Authentication controller
|
|
/// </summary>
|
|
[ApiController, Route("api/[controller]")]
|
|
[Authorize]
|
|
[OpenApiTag("Authentication", Description = "Authentication management")]
|
|
public class AuthenticationController : ControllerBase
|
|
{
|
|
private readonly ILogger<AuthenticationController> _logger;
|
|
private readonly TokensService _tokensService;
|
|
private readonly UserDatabaseService _UserDatabaseService;
|
|
private readonly DeviceDatabaseService _DeviceDatabaseService;
|
|
private readonly ConfigurationDatabaseService _ConfigurationDatabaseService;
|
|
|
|
public AuthenticationController(ILogger<AuthenticationController> logger, TokensService tokensService, UserDatabaseService UserDatabaseService, DeviceDatabaseService DeviceDatabaseService, ConfigurationDatabaseService ConfigurationDatabaseService)
|
|
{
|
|
_logger = logger;
|
|
_tokensService = tokensService;
|
|
_UserDatabaseService = UserDatabaseService;
|
|
_DeviceDatabaseService = DeviceDatabaseService;
|
|
_ConfigurationDatabaseService = ConfigurationDatabaseService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Authenticate (business)
|
|
/// </summary>
|
|
/// <param name="email">user email</param>
|
|
/// <param name="password">user password</param>
|
|
/// <returns>Token descriptor</returns>
|
|
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");
|
|
|
|
MqttClientService.SetServices(_DeviceDatabaseService, _ConfigurationDatabaseService);
|
|
|
|
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}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Authenticate with form parameters (used by Swagger test client)
|
|
/// </summary>
|
|
/// <param name="tokenRequest">Swagger token request</param>
|
|
/// <returns>Token descriptor</returns>
|
|
[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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Authenticate with Json parameters (used by most clients)
|
|
/// </summary>
|
|
/// <param name="login">Login DTO</param>
|
|
/// <returns>Token descriptor</returns>
|
|
[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);
|
|
}
|
|
}
|
|
}
|