62 lines
2.2 KiB
C#
62 lines
2.2 KiB
C#
using Birdmap.Models;
|
|
using Birdmap.Services.Interfaces;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Configuration;
|
|
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;
|
|
|
|
public AuthController(IAuthService service, IConfiguration configuration)
|
|
{
|
|
_service = service;
|
|
_configuration = configuration;
|
|
}
|
|
|
|
[AllowAnonymous]
|
|
[HttpPost("authenticate")]
|
|
[ProducesResponseType(typeof(object), StatusCodes.Status200OK)]
|
|
public async Task<IActionResult> AuthenticateAsync([FromBody] AuthenticateRequest model)
|
|
{
|
|
var user = await _service.AuthenticateUserAsync(model.Username, model.Password);
|
|
var expiresInSeconds = TimeSpan.FromHours(2).TotalSeconds;
|
|
var tokenHandler = new JwtSecurityTokenHandler();
|
|
var key = Encoding.ASCII.GetBytes(_configuration["BasicAuth:Secret"]);
|
|
var tokenDescriptor = new SecurityTokenDescriptor
|
|
{
|
|
Subject = new ClaimsIdentity(new Claim[]
|
|
{
|
|
new Claim(ClaimTypes.Name, user.Name)
|
|
}),
|
|
Expires = DateTime.UtcNow.AddHours(expiresInSeconds),
|
|
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
|
|
};
|
|
var token = tokenHandler.CreateToken(tokenDescriptor);
|
|
var tokenString = tokenHandler.WriteToken(token);
|
|
|
|
return Ok(
|
|
new
|
|
{
|
|
user_name = user.Name,
|
|
access_token = tokenString,
|
|
token_type = "Bearer",
|
|
expires_in = expiresInSeconds,
|
|
});
|
|
}
|
|
}
|
|
}
|