CodeLiturgy.Dashboard/BlueWest.Api/Users/Auth/AuthManager.cs

117 lines
4.4 KiB
C#
Raw Normal View History

2022-09-17 22:13:35 +03:00
using System;
using System.Security.Claims;
2022-09-10 00:33:17 +03:00
using System.Threading;
using System.Threading.Tasks;
using BlueWest.Cryptography;
2022-09-17 22:13:35 +03:00
using BlueWest.Data.Application;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Http;
2022-09-10 00:33:17 +03:00
using Microsoft.AspNetCore.Identity;
namespace BlueWest.WebApi.Context.Users;
2022-09-11 01:22:04 +03:00
internal class AuthManager : IAuthManager
2022-09-10 00:33:17 +03:00
{
2022-09-17 22:13:35 +03:00
private readonly ApplicationUserManager _userManager;
2022-09-10 07:12:03 +03:00
private readonly UserRepository _usersRepo;
2022-09-17 22:13:35 +03:00
private readonly ISessionManager _sessionManager;
2022-09-10 00:33:17 +03:00
private readonly IHasher _hasher;
private readonly IJwtFactory _jwtFactory;
2022-09-10 07:12:03 +03:00
/// <summary>
/// Auth manager constructor
/// </summary>
/// <param name="userManager"></param>
/// <param name="hasher"></param>
/// <param name="usersRepo"></param>
/// <param name="jwtFactory"></param>
2022-09-17 22:13:35 +03:00
public AuthManager(
ApplicationUserManager userManager,
IHasher hasher,
UserRepository usersRepo,
ISessionManager sessionManager,
IJwtFactory jwtFactory)
2022-09-10 00:33:17 +03:00
{
_userManager = userManager;
_hasher = hasher;
_usersRepo = usersRepo;
_jwtFactory = jwtFactory;
2022-09-17 22:13:35 +03:00
_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);
2022-09-10 00:33:17 +03:00
}
2022-09-10 07:12:03 +03:00
/// <inheritdoc />
2022-09-17 22:13:35 +03:00
public async Task<(bool, SessionTokenUnique, AccessToken)> GetToken(LoginRequest loginRequest)
2022-09-10 00:33:17 +03:00
{
2022-09-17 22:13:35 +03:00
if (!string.IsNullOrEmpty(loginRequest.Email) && !string.IsNullOrEmpty(loginRequest.Password))
2022-09-10 00:33:17 +03:00
{
2022-09-17 22:13:35 +03:00
var user = await _userManager.FindByEmailAsync(loginRequest.Email);
2022-09-10 00:33:17 +03:00
if (user != null)
{
2022-09-17 22:13:35 +03:00
if (await VerifyLoginByEmailAsync(loginRequest.Email,loginRequest.Password))
2022-09-10 00:33:17 +03:00
{
await _usersRepo.UpdateAsync(user, CancellationToken.None);
2022-09-17 22:13:35 +03:00
// 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);
2022-09-10 00:33:17 +03:00
2022-09-17 22:13:35 +03:00
return (completed == IdentityResult.Success, sessionResponse, token);
2022-09-10 00:33:17 +03:00
}
}
}
2022-09-17 22:13:35 +03:00
return (false, null, null);
2022-09-10 00:33:17 +03:00
}
2022-09-10 07:12:03 +03:00
/// <inheritdoc />
2022-09-17 22:13:35 +03:00
public async Task<bool> VerifyLoginByEmailAsync(string email, string password)
2022-09-10 00:33:17 +03:00
{
var user = await _userManager.FindByEmailAsync(email);
if (user == null)
{
2022-09-17 22:13:35 +03:00
return false;
2022-09-10 00:33:17 +03:00
}
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;
}
2022-09-10 07:12:03 +03:00
2022-09-10 00:33:17 +03:00
public async Task<IdentityResult> CreateUserAsync(RegisterViewModel userSignupDto)
{
2022-09-10 07:12:03 +03:00
RegisterViewModel userToCreate = FromSignupToUser(userSignupDto);
return await _userManager.CreateAsync(userToCreate.ToUser());
2022-09-10 00:33:17 +03:00
}
2022-09-10 07:12:03 +03:00
2022-09-10 00:33:17 +03:00
}