CodeLiturgy.Dashboard/BlueWest.Api/Controllers/AuthController.cs

153 lines
4.5 KiB
C#

using System;
using System.Security.Claims;
using System.Threading.Tasks;
using BlueWest.Cryptography;
using BlueWest.Data.Application;
using BlueWest.WebApi.Context.Users;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
namespace BlueWest.WebApi.Controllers;
/// <summary>
/// Auth controller
/// </summary>
[ApiController]
[Route("api/[controller]")]
[Authorize(Policy = "ApiUser")]
/*[EnableCors(Constants.CorsPolicyName)]*/
public class AuthController : Controller
{
private readonly IAuthManager _authManager;
private readonly IUserManager _userManager;
private readonly ISessionManager _sessionManager;
/// <summary>
///
/// </summary>
/// <param name="authManager"></param>
/// <param name="userManager"></param>
public AuthController( IAuthManager authManager, IUserManager userManager, ISessionManager sessionManager)
{
_authManager = authManager;
_userManager = userManager;
_sessionManager = sessionManager;
}
/// <summary>
/// Signup user
/// </summary>
/// <param name="registerViewModel"></param>
/// <returns></returns>
[AllowAnonymous]
[HttpPost("register")]
public async Task<ActionResult<IdentityResult>> SignupUserAsync(RegisterViewModel registerViewModel)
{
return await _authManager.CreateUserAsync(registerViewModel);
}
/// <summary>
/// Gets a bearer token
/// </summary>
/// <param name="loginViewModel"></param>
/// <returns></returns>
[AllowAnonymous]
[HttpPost("token")]
public async Task<ActionResult<IdentityResult>> GetTokenAsync(LoginRequest loginViewModel)
{
var (success, sessionToken, token) = await _authManager.GetToken(loginViewModel);
if (success)
{
return Ok(new {sessionToken, token});
}
return Problem();
}
/// <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});
}
/// <summary>
/// Do Cookie based login.
/// </summary>
/// <param name="loginDto"></param>
/// <returns></returns>
[AllowAnonymous]
[HttpPost("login")]
public async Task<ActionResult> DoLoginAsync(LoginRequest loginDto)
{
var (success, identity, sessionToken) = await _authManager.DoLogin(loginDto);
if (success)
{
await HttpContext.SignInAsync(
CookieAuthenticationDefaults.AuthenticationScheme,
new ClaimsPrincipal(identity),
new AuthenticationProperties
{
IsPersistent = true,
ExpiresUtc = DateTime.UtcNow.AddDays(1)
});
return Ok(new {authenticated = true, sessionToken});
}
return new ForbidResult(CookieAuthenticationDefaults.AuthenticationScheme);
}
/// <summary>
/// Do Cookie based logout
/// </summary>
/// <param name="loginDto"></param>
/// <returns></returns>
[AllowAnonymous]
[HttpPost("logout")]
public async Task DoLogoutAsync()
{
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
}
}