2020-10-24 23:23:22 +02:00
|
|
|
|
using AutoMapper;
|
|
|
|
|
using Birdmap.API.DTOs;
|
|
|
|
|
using Birdmap.BLL.Interfaces;
|
|
|
|
|
using Birdmap.Models;
|
2020-10-21 17:04:54 +02:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
2020-10-25 16:15:06 +01:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2020-10-21 17:04:54 +02:00
|
|
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
|
|
using System;
|
|
|
|
|
using System.IdentityModel.Tokens.Jwt;
|
|
|
|
|
using System.Security.Claims;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Birdmap.Controllers
|
|
|
|
|
{
|
|
|
|
|
[Authorize]
|
|
|
|
|
[ApiController]
|
|
|
|
|
[Route("api/[controller]")]
|
|
|
|
|
public class AuthController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
private readonly IAuthService _service;
|
|
|
|
|
private readonly IConfiguration _configuration;
|
2020-10-24 23:23:22 +02:00
|
|
|
|
private readonly IMapper _mapper;
|
2020-10-25 16:15:06 +01:00
|
|
|
|
private readonly ILogger<AuthController> _logger;
|
2020-10-21 17:04:54 +02:00
|
|
|
|
|
2020-10-25 16:15:06 +01:00
|
|
|
|
public AuthController(IAuthService service, IConfiguration configuration, IMapper mapper, ILogger<AuthController> logger)
|
2020-10-21 17:04:54 +02:00
|
|
|
|
{
|
|
|
|
|
_service = service;
|
|
|
|
|
_configuration = configuration;
|
2020-10-24 23:23:22 +02:00
|
|
|
|
_mapper = mapper;
|
2020-10-25 16:15:06 +01:00
|
|
|
|
_logger = logger;
|
2020-10-21 17:04:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[AllowAnonymous]
|
2020-11-07 14:19:29 +01:00
|
|
|
|
[HttpPost("authenticate"), ProducesResponseType(typeof(object), StatusCodes.Status200OK)]
|
2020-10-21 17:04:54 +02:00
|
|
|
|
public async Task<IActionResult> AuthenticateAsync([FromBody] AuthenticateRequest model)
|
|
|
|
|
{
|
2020-10-25 16:15:06 +01:00
|
|
|
|
_logger.LogInformation($"Authenticating user [{model.Username}] with password [*******]...");
|
|
|
|
|
|
2020-10-21 17:04:54 +02:00
|
|
|
|
var user = await _service.AuthenticateUserAsync(model.Username, model.Password);
|
2020-10-25 16:15:06 +01:00
|
|
|
|
|
|
|
|
|
_logger.LogInformation($"Authenticated user [{user.Name}]. Returning token...");
|
|
|
|
|
|
2020-10-23 15:05:20 +02:00
|
|
|
|
var expiresInSeconds = TimeSpan.FromHours(2).TotalSeconds;
|
2020-10-21 17:04:54 +02:00
|
|
|
|
var tokenHandler = new JwtSecurityTokenHandler();
|
2020-10-24 23:23:22 +02:00
|
|
|
|
var key = Encoding.ASCII.GetBytes(_configuration["Secret"]);
|
2020-10-21 17:04:54 +02:00
|
|
|
|
var tokenDescriptor = new SecurityTokenDescriptor
|
|
|
|
|
{
|
|
|
|
|
Subject = new ClaimsIdentity(new Claim[]
|
|
|
|
|
{
|
2020-10-24 23:23:22 +02:00
|
|
|
|
new Claim(ClaimTypes.Name, user.Name),
|
|
|
|
|
new Claim(ClaimTypes.Role, user.Role.ToString()),
|
2020-10-21 17:04:54 +02:00
|
|
|
|
}),
|
2020-10-23 15:05:20 +02:00
|
|
|
|
Expires = DateTime.UtcNow.AddHours(expiresInSeconds),
|
2020-10-21 17:04:54 +02:00
|
|
|
|
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
|
|
|
|
|
};
|
|
|
|
|
var token = tokenHandler.CreateToken(tokenDescriptor);
|
|
|
|
|
var tokenString = tokenHandler.WriteToken(token);
|
|
|
|
|
|
2020-10-24 23:23:22 +02:00
|
|
|
|
var response = _mapper.Map<AuthenticateResponse>(user);
|
|
|
|
|
response.AccessToken = tokenString;
|
|
|
|
|
response.TokenType = "Bearer";
|
|
|
|
|
response.ExpiresIn = expiresInSeconds;
|
|
|
|
|
|
|
|
|
|
return Ok(response);
|
2020-10-21 17:04:54 +02:00
|
|
|
|
}
|
2020-10-25 16:15:06 +01:00
|
|
|
|
|
|
|
|
|
[AllowAnonymous]
|
2020-11-07 14:19:29 +01:00
|
|
|
|
[HttpPost("register"), ProducesResponseType(StatusCodes.Status204NoContent)]
|
2020-10-25 16:15:06 +01:00
|
|
|
|
public async Task<IActionResult> RegisterAsync([FromBody] RegisterRequest model)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation($"Registering user [{model.Username}]...");
|
|
|
|
|
var created = await _service.RegisterUserAsync(model.Username, model.Password);
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation($"Registered user [{created.Id}.");
|
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
2020-10-21 17:04:54 +02:00
|
|
|
|
}
|
|
|
|
|
}
|