Added a way to login and register and a tools project

This commit is contained in:
Giovani
2019-05-06 13:12:35 -04:00
parent 62306c3530
commit 59b3cf87b0
15 changed files with 231 additions and 31 deletions

View File

@@ -5,35 +5,40 @@ using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using BrightGlimmer.Auth.Request;
using BrightGlimmer.Domain.Auth;
using BrightGlimmer.Service.Interfaces;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
namespace BrightGlimmer.Auth.Controllers
{
[Authorize]
[Route("api/[controller]")]
[ApiController]
public class UserController : ControllerBase
{
private readonly IUserService userService;
public UserController(IUserService userService)
{
this.userService = userService;
}
[AllowAnonymous]
[HttpGet]
public ActionResult Login(string username, string password)
{
/* TODO: Move token creation to service */
var tokenHandler = new JwtSecurityTokenHandler();
var key = Encoding.UTF8.GetBytes(Startup.Configuration.GetSection("Keys")["JwtPrivateKey"]);
var tokenDescriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(new Claim[]
{
new Claim(ClaimTypes.NameIdentifier, username)
}),
Expires = DateTime.UtcNow.AddDays(3),
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
};
var tokenSecurity = tokenHandler.CreateToken(tokenDescriptor);
var token = tokenHandler.WriteToken(tokenSecurity);
var token = userService.Login(username, password);
return new JsonResult(token);
}
[AllowAnonymous]
[HttpPost]
public async Task<ActionResult> Register(RegisterUserRequest request)
{
var token = await userService.RegisterAsync(request.User, request.Password);
return new JsonResult(token);
}
}