using System.Security.Claims; using BlueWest.Cryptography; using CodeLiturgy.Data.Application.Users; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Identity; using static CodeLiturgy.Data.Auth.Context.Users.AuthConsts; namespace CodeLiturgy.Data.Auth.Context.Users { /// /// Authentication Manager for the Application Users /// public class AuthManager : IAuthManager { private readonly ApplicationUserManager _userManager; private readonly IHasher _hasher; /// /// Auth manager constructor /// /// /// /// /// public AuthManager( ApplicationUserManager userManager, IHasher hasher) { _userManager = userManager; _hasher = hasher; } private string GetHashFromUuid(string uuid) { return _hasher.CreateHash(uuid, BaseCryptoItem.HashAlgorithm.SHA2_512); } /// /// Verify Password /// /// /// /// public async Task VerifyLoginByEmailAsync(string email, string password) { var user = await _userManager.FindByEmailAsync(email); return user != null && await _userManager.CheckPasswordAsync(user, password); } /// /// Create user /// /// /// public async Task CreateUserAsync(RegisterRequest userSignupDto) { userSignupDto.Password = _hasher.CreateHash(userSignupDto.Password, BaseCryptoItem.HashAlgorithm.SHA3_512);; var newUser = userSignupDto.ToUser(); return await _userManager.CreateAsync(newUser); } public async Task<(ClaimsIdentity, bool)> DoLogin(LoginRequest loginRequest) { var user = await _userManager.FindByEmailAsync(loginRequest.Email); if (user == null) return NegativeToken; if (!await _userManager.CheckPasswordAsync(user, loginRequest.Password)) return NegativeToken; var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme); identity.AddClaim(new Claim(ClaimTypes.Email, user.Email)); identity.AddClaim(new Claim(ClaimTypes.MobilePhone, user.PhoneNumber)); identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Id)); return (identity, true); } } }