| | 1 | | using Microsoft.AspNetCore.Mvc; |
| | 2 | | using Microsoft.IdentityModel.Tokens; |
| | 3 | | using MRA.Infrastructure.Settings; |
| | 4 | | using MRA.WebApi.Models.Auth; |
| | 5 | | using MRA.WebApi.Models.Requests.Account; |
| | 6 | | using System.IdentityModel.Tokens.Jwt; |
| | 7 | | using System.Security.Claims; |
| | 8 | | using System.Text; |
| | 9 | |
|
| | 10 | | namespace MRA.WebApi.Controllers; |
| | 11 | |
|
| | 12 | | [Route("api/[controller]")] |
| | 13 | | [ApiController] |
| | 14 | | public class AuthController : ControllerBase |
| | 15 | | { |
| | 16 | | private readonly AppSettings _appConfig; |
| | 17 | |
|
| 0 | 18 | | public AuthController(AppSettings appConfig) |
| | 19 | | { |
| 0 | 20 | | _appConfig = appConfig; |
| 0 | 21 | | } |
| | 22 | |
|
| | 23 | | [HttpPost("login")] |
| | 24 | | public IActionResult Login([FromBody] UserLoginDto loginDto) |
| | 25 | | { |
| 0 | 26 | | if (loginDto.Username != _appConfig.Administrator.User || loginDto.Password != _appConfig.Administrator.Password |
| | 27 | | { |
| 0 | 28 | | return Unauthorized(); |
| | 29 | | } |
| | 30 | |
|
| 0 | 31 | | var claims = new[] |
| 0 | 32 | | { |
| 0 | 33 | | new Claim(JwtRegisteredClaimNames.Sub, loginDto.Username), |
| 0 | 34 | | new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()), |
| 0 | 35 | | new Claim(ClaimTypes.Role, "admin") |
| 0 | 36 | | }; |
| | 37 | |
|
| 0 | 38 | | var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_appConfig.Jwt.Key)); |
| 0 | 39 | | var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); |
| | 40 | |
|
| 0 | 41 | | var token = new JwtSecurityToken( |
| 0 | 42 | | issuer: _appConfig.Jwt.Issuer, |
| 0 | 43 | | audience: _appConfig.Jwt.Audience, |
| 0 | 44 | | claims: claims, |
| 0 | 45 | | expires: DateTime.Now.AddMinutes(1440), |
| 0 | 46 | | signingCredentials: creds); |
| | 47 | |
|
| 0 | 48 | | return Ok(new UserDto() |
| 0 | 49 | | { |
| 0 | 50 | | Username = loginDto.Username, |
| 0 | 51 | | Role = "admin", |
| 0 | 52 | | Token = new JwtSecurityTokenHandler().WriteToken(token) |
| 0 | 53 | | } |
| 0 | 54 | | ); |
| | 55 | |
|
| | 56 | | } |
| | 57 | |
|
| | 58 | | [HttpPost("validate-token")] |
| | 59 | | public IActionResult ValidateToken([FromBody] TokenDto tokenDto) |
| | 60 | | { |
| 0 | 61 | | var tokenHandler = new JwtSecurityTokenHandler(); |
| 0 | 62 | | var key = Encoding.ASCII.GetBytes(_appConfig.Jwt.Key); |
| | 63 | |
|
| | 64 | | try |
| | 65 | | { |
| 0 | 66 | | tokenHandler.ValidateToken(tokenDto.Token, new TokenValidationParameters |
| 0 | 67 | | { |
| 0 | 68 | | ValidateIssuerSigningKey = true, |
| 0 | 69 | | IssuerSigningKey = new SymmetricSecurityKey(key), |
| 0 | 70 | | ValidateIssuer = true, |
| 0 | 71 | | ValidIssuer = _appConfig.Jwt.Issuer, |
| 0 | 72 | | ValidateAudience = true, |
| 0 | 73 | | ValidAudience = _appConfig.Jwt.Audience, |
| 0 | 74 | | ValidateLifetime = true, |
| 0 | 75 | | }, out SecurityToken validatedToken); |
| | 76 | |
|
| 0 | 77 | | return Ok(true); |
| | 78 | | } |
| 0 | 79 | | catch |
| | 80 | | { |
| 0 | 81 | | return Unauthorized(); |
| | 82 | | } |
| 0 | 83 | | } |
| | 84 | | } |