2022-09-17 22:13:35 +03:00
|
|
|
using System;
|
2022-09-10 00:33:17 +03:00
|
|
|
using System.Security.Claims;
|
|
|
|
using System.Threading.Tasks;
|
2022-09-17 22:13:35 +03:00
|
|
|
using BlueWest.Cryptography;
|
|
|
|
using BlueWest.Data.Application;
|
2022-09-10 00:33:17 +03:00
|
|
|
using BlueWest.WebApi.Context.Users;
|
|
|
|
using Microsoft.AspNetCore.Authentication;
|
|
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2022-09-12 17:57:37 +03:00
|
|
|
using Microsoft.AspNetCore.Cors;
|
2022-09-10 00:33:17 +03:00
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
namespace BlueWest.WebApi.Controllers;
|
|
|
|
|
2022-09-10 07:12:03 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Auth controller
|
|
|
|
/// </summary>
|
2022-09-10 00:33:17 +03:00
|
|
|
[ApiController]
|
2022-09-17 22:13:35 +03:00
|
|
|
[Route("api/[controller]")]
|
|
|
|
|
|
|
|
[Authorize(Policy = "ApiUser")]
|
|
|
|
|
|
|
|
/*[EnableCors(Constants.CorsPolicyName)]*/
|
|
|
|
public class AuthController : Controller
|
2022-09-10 00:33:17 +03:00
|
|
|
{
|
|
|
|
private readonly IAuthManager _authManager;
|
|
|
|
private readonly IUserManager _userManager;
|
2022-09-17 22:13:35 +03:00
|
|
|
private readonly ISessionManager _sessionManager;
|
2022-09-10 00:33:17 +03:00
|
|
|
|
2022-09-10 07:12:03 +03:00
|
|
|
/// <summary>
|
|
|
|
///
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="authManager"></param>
|
|
|
|
/// <param name="userManager"></param>
|
2022-09-17 22:13:35 +03:00
|
|
|
public AuthController( IAuthManager authManager, IUserManager userManager, ISessionManager sessionManager)
|
2022-09-10 00:33:17 +03:00
|
|
|
{
|
|
|
|
_authManager = authManager;
|
|
|
|
_userManager = userManager;
|
2022-09-17 22:13:35 +03:00
|
|
|
_sessionManager = sessionManager;
|
2022-09-10 00:33:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-10 07:12:03 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Signup user
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="registerViewModel"></param>
|
|
|
|
/// <returns></returns>
|
2022-09-10 00:33:17 +03:00
|
|
|
[AllowAnonymous]
|
|
|
|
[HttpPost("register")]
|
|
|
|
public async Task<ActionResult<IdentityResult>> SignupUserAsync(RegisterViewModel registerViewModel)
|
|
|
|
{
|
|
|
|
return await _authManager.CreateUserAsync(registerViewModel);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-17 22:13:35 +03:00
|
|
|
|
2022-09-11 01:22:04 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Gets a bearer token
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="loginViewModel"></param>
|
|
|
|
/// <returns></returns>
|
2022-09-10 00:33:17 +03:00
|
|
|
[AllowAnonymous]
|
2022-09-17 22:13:35 +03:00
|
|
|
[HttpPost("token")]
|
|
|
|
public async Task<ActionResult<IdentityResult>> GetTokenAsync(LoginRequest loginViewModel)
|
2022-09-10 00:33:17 +03:00
|
|
|
{
|
2022-09-17 22:13:35 +03:00
|
|
|
var (success, sessionToken, token) = await _authManager.GetToken(loginViewModel);
|
2022-09-10 00:33:17 +03:00
|
|
|
|
2022-09-17 22:13:35 +03:00
|
|
|
if (success)
|
2022-09-10 00:33:17 +03:00
|
|
|
{
|
2022-09-17 22:13:35 +03:00
|
|
|
return Ok(new {sessionToken, token});
|
2022-09-10 00:33:17 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
return Problem();
|
|
|
|
}
|
|
|
|
|
2022-09-17 22:13:35 +03:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Check if user is logged in
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
|
|
|
[HttpGet("isLoggedIn")]
|
|
|
|
|
|
|
|
public ActionResult<bool> IsLoggedIn()
|
|
|
|
{
|
|
|
|
var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
|
|
|
|
|
|
|
|
if (identity.IsAuthenticated)
|
|
|
|
{
|
|
|
|
return Ok(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Ok(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Checks if the session is authorized
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="hash"></param>
|
|
|
|
/// <returns></returns>
|
|
|
|
[HttpGet("isAuthorized")]
|
|
|
|
|
|
|
|
public ActionResult IsAuthorized(string hash)
|
|
|
|
{
|
|
|
|
var isAuthorized = _sessionManager.IsAuthorized(hash);
|
|
|
|
|
|
|
|
return Ok(isAuthorized ? new {authenticated = true} : new {authenticated = false});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-10 00:33:17 +03:00
|
|
|
|
2022-09-11 01:22:04 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Do Cookie based login.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="loginDto"></param>
|
|
|
|
/// <returns></returns>
|
2022-09-10 00:33:17 +03:00
|
|
|
[AllowAnonymous]
|
2022-09-17 22:13:35 +03:00
|
|
|
[HttpPost("login")]
|
|
|
|
public async Task<ActionResult> DoLoginAsync(LoginRequest loginDto)
|
2022-09-10 00:33:17 +03:00
|
|
|
{
|
2022-09-17 22:13:35 +03:00
|
|
|
var (success, identity, sessionToken) = await _authManager.DoLogin(loginDto);
|
|
|
|
|
|
|
|
if (success)
|
2022-09-10 00:33:17 +03:00
|
|
|
{
|
2022-09-17 22:13:35 +03:00
|
|
|
await HttpContext.SignInAsync(
|
|
|
|
CookieAuthenticationDefaults.AuthenticationScheme,
|
|
|
|
new ClaimsPrincipal(identity),
|
|
|
|
new AuthenticationProperties
|
|
|
|
{
|
|
|
|
IsPersistent = true,
|
|
|
|
ExpiresUtc = DateTime.UtcNow.AddDays(1)
|
|
|
|
});
|
|
|
|
|
|
|
|
return Ok(new {authenticated = true, sessionToken});
|
2022-09-10 00:33:17 +03:00
|
|
|
}
|
|
|
|
|
2022-09-17 22:13:35 +03:00
|
|
|
return new ForbidResult(CookieAuthenticationDefaults.AuthenticationScheme);
|
2022-09-10 00:33:17 +03:00
|
|
|
}
|
|
|
|
|
2022-09-11 01:22:04 +03:00
|
|
|
/// <summary>
|
|
|
|
/// Do Cookie based logout
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="loginDto"></param>
|
|
|
|
/// <returns></returns>
|
2022-09-10 00:33:17 +03:00
|
|
|
[AllowAnonymous]
|
|
|
|
[HttpPost("logout")]
|
2022-09-17 22:13:35 +03:00
|
|
|
public async Task DoLogoutAsync()
|
2022-09-10 00:33:17 +03:00
|
|
|
{
|
2022-09-17 22:13:35 +03:00
|
|
|
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
|
2022-09-10 00:33:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|