2018-11-02 21:04:49 +00:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Roadie.Api.Models;
|
|
|
|
|
using Roadie.Api.Services;
|
2018-12-02 15:51:54 +00:00
|
|
|
|
using Roadie.Library.Caching;
|
2018-12-01 03:22:35 +00:00
|
|
|
|
using Roadie.Library.Configuration;
|
2018-11-02 21:04:49 +00:00
|
|
|
|
using Roadie.Library.Identity;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
2018-11-18 16:40:52 +00:00
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Http;
|
2018-11-02 21:04:49 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Roadie.Api.Controllers
|
|
|
|
|
{
|
|
|
|
|
[Produces("application/json")]
|
|
|
|
|
[Route("auth")]
|
|
|
|
|
[ApiController]
|
|
|
|
|
[AllowAnonymous]
|
|
|
|
|
public class AccountController : ControllerBase
|
|
|
|
|
{
|
2018-12-16 23:37:19 +00:00
|
|
|
|
private readonly IConfiguration Configuration;
|
|
|
|
|
private readonly ILogger<AccountController> Logger;
|
|
|
|
|
private readonly SignInManager<ApplicationUser> SignInManager;
|
|
|
|
|
private readonly ITokenService TokenService;
|
|
|
|
|
private readonly UserManager<ApplicationUser> UserManager;
|
2018-12-01 03:22:35 +00:00
|
|
|
|
private IRoadieSettings RoadieSettings { get; }
|
2018-12-02 15:51:54 +00:00
|
|
|
|
private ICacheManager CacheManager { get; }
|
2018-12-15 16:53:14 +00:00
|
|
|
|
private IAdminService AdminService { get; }
|
2018-11-02 21:04:49 +00:00
|
|
|
|
|
|
|
|
|
public AccountController(
|
2018-12-15 16:53:14 +00:00
|
|
|
|
IAdminService adminService,
|
2018-11-02 21:04:49 +00:00
|
|
|
|
UserManager<ApplicationUser> userManager,
|
|
|
|
|
SignInManager<ApplicationUser> signInManager,
|
|
|
|
|
IConfiguration configuration,
|
|
|
|
|
ILogger<AccountController> logger,
|
2018-12-02 15:51:54 +00:00
|
|
|
|
ITokenService tokenService,
|
|
|
|
|
ICacheManager cacheManager)
|
2018-11-02 21:04:49 +00:00
|
|
|
|
{
|
2018-12-16 23:37:19 +00:00
|
|
|
|
this.UserManager = userManager;
|
|
|
|
|
this.SignInManager = signInManager;
|
|
|
|
|
this.Configuration = configuration;
|
|
|
|
|
this.Logger = logger;
|
|
|
|
|
this.TokenService = tokenService;
|
2018-12-02 15:51:54 +00:00
|
|
|
|
this.CacheManager = cacheManager;
|
2018-12-01 03:22:35 +00:00
|
|
|
|
|
|
|
|
|
this.RoadieSettings = new RoadieSettings();
|
|
|
|
|
configuration.GetSection("RoadieSettings").Bind(this.RoadieSettings);
|
2018-12-15 16:53:14 +00:00
|
|
|
|
this.AdminService = adminService;
|
2018-11-02 21:04:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
[Route("token")]
|
|
|
|
|
public async Task<IActionResult> CreateToken([FromBody]LoginModel model)
|
|
|
|
|
{
|
|
|
|
|
if (ModelState.IsValid)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// Login user
|
2018-12-16 23:37:19 +00:00
|
|
|
|
var loginResult = await SignInManager.PasswordSignInAsync(model.Username, model.Password, isPersistent: false, lockoutOnFailure: false);
|
2018-11-02 21:04:49 +00:00
|
|
|
|
if (!loginResult.Succeeded)
|
|
|
|
|
{
|
|
|
|
|
return BadRequest();
|
2018-11-18 16:40:52 +00:00
|
|
|
|
}
|
2018-12-16 23:37:19 +00:00
|
|
|
|
var user = await UserManager.FindByNameAsync(model.Username);
|
2018-11-18 16:40:52 +00:00
|
|
|
|
var now = DateTime.UtcNow;
|
|
|
|
|
user.LastLogin = now;
|
2018-12-01 03:22:35 +00:00
|
|
|
|
user.LastApiAccess = now;
|
2018-11-18 16:40:52 +00:00
|
|
|
|
user.LastUpdated = now;
|
2018-12-16 23:37:19 +00:00
|
|
|
|
await UserManager.UpdateAsync(user);
|
|
|
|
|
var t = await this.TokenService.GenerateToken(user, this.UserManager);
|
|
|
|
|
this.Logger.LogInformation($"Successfully authenticated User [{ model.Username}]");
|
2018-12-02 15:51:54 +00:00
|
|
|
|
this.CacheManager.ClearRegion(EntityControllerBase.ControllerCacheRegionUrn);
|
2018-12-01 03:22:35 +00:00
|
|
|
|
var avatarUrl = $"{this.Request.Scheme}://{this.Request.Host}/images/user/{ user.RoadieId }/{ this.RoadieSettings.ThumbnailImageSize.Width }/{ this.RoadieSettings.ThumbnailImageSize.Height }";
|
2018-11-18 16:40:52 +00:00
|
|
|
|
return Ok(new
|
|
|
|
|
{
|
|
|
|
|
Username = user.UserName,
|
|
|
|
|
user.Email,
|
|
|
|
|
user.LastLogin,
|
2018-12-03 04:12:47 +00:00
|
|
|
|
avatarUrl,
|
|
|
|
|
Token = t,
|
|
|
|
|
user.Timeformat,
|
|
|
|
|
user.Timezone
|
2018-11-18 16:40:52 +00:00
|
|
|
|
});
|
2018-11-02 21:04:49 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2018-12-16 23:37:19 +00:00
|
|
|
|
this.Logger.LogError(ex, "Eror in CreateToken");
|
2018-11-02 21:04:49 +00:00
|
|
|
|
return BadRequest();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return BadRequest(ModelState);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Authorize]
|
|
|
|
|
[HttpPost]
|
|
|
|
|
[Route("refreshtoken")]
|
|
|
|
|
public async Task<IActionResult> RefreshToken()
|
|
|
|
|
{
|
|
|
|
|
var username = User.Identity.Name ??
|
|
|
|
|
User.Claims.Where(c => c.Properties.ContainsKey("unique_name")).Select(c => c.Value).FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
if (!String.IsNullOrWhiteSpace(username))
|
|
|
|
|
{
|
2018-12-16 23:37:19 +00:00
|
|
|
|
var user = await UserManager.FindByNameAsync(username);
|
|
|
|
|
return Ok(await this.TokenService.GenerateToken(user, this.UserManager));
|
2018-11-02 21:04:49 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ModelState.AddModelError("Authentication", "Authentication failed!");
|
|
|
|
|
return BadRequest(ModelState);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
[Route("register")]
|
|
|
|
|
[AllowAnonymous]
|
|
|
|
|
public async Task<IActionResult> Register([FromBody] RegisterModel registerModel)
|
|
|
|
|
{
|
|
|
|
|
if (ModelState.IsValid)
|
|
|
|
|
{
|
|
|
|
|
var user = new ApplicationUser
|
|
|
|
|
{
|
|
|
|
|
UserName = registerModel.Username,
|
2018-12-16 23:37:19 +00:00
|
|
|
|
RegisteredOn = DateTime.UtcNow,
|
2018-12-16 23:45:42 +00:00
|
|
|
|
DoUseHtmlPlayer = true,
|
2018-12-15 16:53:14 +00:00
|
|
|
|
Email = registerModel.Email
|
2018-11-02 21:04:49 +00:00
|
|
|
|
};
|
|
|
|
|
|
2018-12-16 23:37:19 +00:00
|
|
|
|
var identityResult = await this.UserManager.CreateAsync(user, registerModel.Password);
|
2018-11-02 21:04:49 +00:00
|
|
|
|
if (identityResult.Succeeded)
|
|
|
|
|
{
|
2018-12-15 16:53:14 +00:00
|
|
|
|
if(user.Id == 1)
|
|
|
|
|
{
|
2018-12-16 23:37:19 +00:00
|
|
|
|
await this.AdminService.DoInitialSetup(user, this.UserManager);
|
2018-12-15 16:53:14 +00:00
|
|
|
|
}
|
2018-12-16 23:37:19 +00:00
|
|
|
|
await SignInManager.SignInAsync(user, isPersistent: false);
|
|
|
|
|
var t = await this.TokenService.GenerateToken(user, this.UserManager);
|
|
|
|
|
this.Logger.LogInformation($"Successfully created and authenticated User [{ registerModel.Username}]");
|
2018-12-15 16:53:14 +00:00
|
|
|
|
this.CacheManager.ClearRegion(EntityControllerBase.ControllerCacheRegionUrn);
|
|
|
|
|
var avatarUrl = $"{this.Request.Scheme}://{this.Request.Host}/images/user/{ user.RoadieId }/{ this.RoadieSettings.ThumbnailImageSize.Width }/{ this.RoadieSettings.ThumbnailImageSize.Height }";
|
|
|
|
|
return Ok(new
|
|
|
|
|
{
|
|
|
|
|
Username = user.UserName,
|
|
|
|
|
user.Email,
|
|
|
|
|
user.LastLogin,
|
|
|
|
|
avatarUrl,
|
|
|
|
|
Token = t,
|
|
|
|
|
user.Timeformat,
|
|
|
|
|
user.Timezone
|
|
|
|
|
});
|
2018-11-02 21:04:49 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return BadRequest(identityResult.Errors);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return BadRequest(ModelState);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public async Task<IActionResult> ResetPassword([FromBody] ResetPasswordModel resetPasswordModel)
|
|
|
|
|
{
|
|
|
|
|
if (ModelState.IsValid)
|
|
|
|
|
{
|
|
|
|
|
var user = new ApplicationUser
|
|
|
|
|
{
|
|
|
|
|
UserName = resetPasswordModel.Username,
|
|
|
|
|
Email = resetPasswordModel.Email,
|
|
|
|
|
CreatedDate = DateTime.UtcNow
|
|
|
|
|
};
|
|
|
|
|
|
2018-12-16 23:37:19 +00:00
|
|
|
|
var identityResult = await this.UserManager.ResetPasswordAsync(user, resetPasswordModel.Token, resetPasswordModel.Password);
|
2018-11-02 21:04:49 +00:00
|
|
|
|
if (identityResult.Succeeded)
|
|
|
|
|
{
|
2018-12-02 15:51:54 +00:00
|
|
|
|
this.CacheManager.ClearRegion(EntityControllerBase.ControllerCacheRegionUrn);
|
2018-12-16 23:37:19 +00:00
|
|
|
|
await SignInManager.SignInAsync(user, isPersistent: false);
|
|
|
|
|
return Ok(this.TokenService.GenerateToken(user, this.UserManager));
|
2018-11-02 21:04:49 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return BadRequest(identityResult.Errors);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return BadRequest(ModelState);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|