117 lines
4.4 KiB
C#
117 lines
4.4 KiB
C#
using System;
|
|
using System.Security.Claims;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using BlueWest.Cryptography;
|
|
using BlueWest.Data.Application;
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
namespace BlueWest.WebApi.Context.Users;
|
|
|
|
internal class AuthManager : IAuthManager
|
|
{
|
|
private readonly ApplicationUserManager _userManager;
|
|
private readonly UserRepository _usersRepo;
|
|
private readonly ISessionManager _sessionManager;
|
|
private readonly IHasher _hasher;
|
|
private readonly IJwtFactory _jwtFactory;
|
|
|
|
/// <summary>
|
|
/// Auth manager constructor
|
|
/// </summary>
|
|
/// <param name="userManager"></param>
|
|
/// <param name="hasher"></param>
|
|
/// <param name="usersRepo"></param>
|
|
/// <param name="jwtFactory"></param>
|
|
public AuthManager(
|
|
ApplicationUserManager userManager,
|
|
IHasher hasher,
|
|
UserRepository usersRepo,
|
|
ISessionManager sessionManager,
|
|
IJwtFactory jwtFactory)
|
|
{
|
|
_userManager = userManager;
|
|
_hasher = hasher;
|
|
_usersRepo = usersRepo;
|
|
_jwtFactory = jwtFactory;
|
|
_sessionManager = sessionManager;
|
|
}
|
|
|
|
public async Task<(bool, ClaimsIdentity, SessionTokenUnique)> DoLogin(LoginRequest loginRequest)
|
|
{
|
|
var user = await _userManager.FindByEmailAsync(loginRequest.Email);
|
|
|
|
if (user != null)
|
|
{
|
|
if(await _userManager.CheckPasswordAsync(user, loginRequest.Password))
|
|
{
|
|
// Identity
|
|
var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
|
|
identity.AddClaim(new Claim(ClaimTypes.Email, user.Email));
|
|
|
|
// Session
|
|
var sessionToken = _sessionManager.GetSessionToken(loginRequest, user);
|
|
var sessionResponse = new SessionTokenUnique(sessionToken);
|
|
return (true, identity, sessionResponse);
|
|
}
|
|
}
|
|
|
|
return (false, null, null);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<(bool, SessionTokenUnique, AccessToken)> GetToken(LoginRequest loginRequest)
|
|
{
|
|
if (!string.IsNullOrEmpty(loginRequest.Email) && !string.IsNullOrEmpty(loginRequest.Password))
|
|
{
|
|
var user = await _userManager.FindByEmailAsync(loginRequest.Email);
|
|
if (user != null)
|
|
{
|
|
if (await VerifyLoginByEmailAsync(loginRequest.Email,loginRequest.Password))
|
|
{
|
|
await _usersRepo.UpdateAsync(user, CancellationToken.None);
|
|
// Session
|
|
var sessionToken = _sessionManager.GetSessionToken(loginRequest, user);
|
|
var sessionResponse = new SessionTokenUnique(sessionToken);
|
|
|
|
var token = await _jwtFactory.GenerateEncodedToken(user.Id, user.UserName);
|
|
var completed = await _userManager.SetAuthenticationTokenAsync(user, "ApiUser", "ApiUser", token.Token);
|
|
|
|
return (completed == IdentityResult.Success, sessionResponse, token);
|
|
}
|
|
}
|
|
}
|
|
|
|
return (false, null, null);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<bool> VerifyLoginByEmailAsync(string email, string password)
|
|
{
|
|
var user = await _userManager.FindByEmailAsync(email);
|
|
|
|
if (user == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return await _userManager.CheckPasswordAsync(user, password);
|
|
}
|
|
|
|
private RegisterViewModel FromSignupToUser(RegisterViewModel signupDto)
|
|
{
|
|
var pwd = signupDto.Password;
|
|
var hash = _hasher.CreateHash(pwd, BaseCryptoItem.HashAlgorithm.SHA3_512);
|
|
signupDto.Password = hash;
|
|
return signupDto;
|
|
}
|
|
|
|
public async Task<IdentityResult> CreateUserAsync(RegisterViewModel userSignupDto)
|
|
{
|
|
RegisterViewModel userToCreate = FromSignupToUser(userSignupDto);
|
|
return await _userManager.CreateAsync(userToCreate.ToUser());
|
|
}
|
|
|
|
} |