128 lines
4.0 KiB
C#
128 lines
4.0 KiB
C#
using System;
|
|
using System.Security.Claims;
|
|
using System.Threading.Tasks;
|
|
using BlueWest.WebApi.Context.Users;
|
|
using Microsoft.AspNetCore.Authentication;
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace BlueWest.WebApi.Controllers;
|
|
|
|
/// <summary>
|
|
/// Auth controller
|
|
/// </summary>
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
[Authorize(Policy = SessionConstants.ApiNamePolicy)]
|
|
|
|
/*[EnableCors(Constants.CorsPolicyName)]*/
|
|
public class AuthController : Controller
|
|
{
|
|
private readonly IAuthManager _authManager;
|
|
private readonly IUserManager _userManager;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="authManager"></param>
|
|
/// <param name="userManager"></param>
|
|
public AuthController( IAuthManager authManager, IUserManager userManager)
|
|
{
|
|
_authManager = authManager;
|
|
_userManager = userManager;
|
|
}
|
|
/// <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("login")]
|
|
public async Task<ActionResult<IdentityResult>> GetSessionToken(LoginRequest loginViewModel)
|
|
{
|
|
var (success, sessionToken, identity) = await _authManager.GetSessionTokenId(loginViewModel);
|
|
|
|
if (success)
|
|
{
|
|
return Ok(new {sessionToken});
|
|
|
|
}
|
|
return Problem();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets a bearer token
|
|
/// </summary>
|
|
/// <param name="loginViewModel"></param>
|
|
/// <returns></returns>
|
|
[AllowAnonymous]
|
|
[HttpPost("bearer")]
|
|
public async Task<ActionResult<IdentityResult>> GetBearerBySessionId(string sessionId)
|
|
{
|
|
var (success, bearer) = await _authManager.GetBearerTokenBySessionTokenId(sessionId);
|
|
|
|
if (success)
|
|
{
|
|
return Ok(new {bearer});
|
|
|
|
}
|
|
return new UnauthorizedObjectResult(new {message = "The provided sessionId didn't return a valid Token."});
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Do Cookie based login.
|
|
/// </summary>
|
|
/// <param name="loginDto"></param>
|
|
/// <returns></returns>
|
|
/*[AllowAnonymous]
|
|
[HttpPost("login")]
|
|
public async Task<ActionResult> DoLoginByCookie(LoginRequest loginDto)
|
|
{
|
|
var (success, sessionToken, identity) = await _authManager.GetSessionTokenId(loginDto);
|
|
|
|
if (success)
|
|
{
|
|
await HttpContext.SignInAsync(
|
|
CookieAuthenticationDefaults.AuthenticationScheme,
|
|
new ClaimsPrincipal(identity),
|
|
new AuthenticationProperties
|
|
{
|
|
IsPersistent = true,
|
|
ExpiresUtc = DateTime.UtcNow.Add(SessionConstants.DefaultValidForSpan)
|
|
});
|
|
|
|
return Ok(new {authenticated = true, sessionToken});
|
|
}
|
|
|
|
return new ForbidResult(CookieAuthenticationDefaults.AuthenticationScheme);
|
|
}*/
|
|
|
|
/// <summary>
|
|
/// Do Cookie based logout
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[AllowAnonymous]
|
|
[HttpPost("logout")]
|
|
public async Task DoCookieLogoutAsync()
|
|
{
|
|
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
|
|
}
|
|
|
|
} |